Skip to content

Commit 9a1a57e

Browse files
committed
Fix 'TableClause' object has no attribute 'dialect_options'
1 parent cd614ff commit 9a1a57e

File tree

2 files changed

+16
-21
lines changed

2 files changed

+16
-21
lines changed

tests/unit/sqlalchemy/test_compiler.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020
Table,
2121
)
2222
from sqlalchemy.schema import CreateTable
23+
from sqlalchemy.sql import column, table
2324

2425
from trino.sqlalchemy.dialect import TrinoDialect
2526

2627
metadata = MetaData()
27-
table = Table(
28+
table_without_catalog = Table(
2829
'table',
2930
metadata,
3031
Column('id', Integer),
@@ -45,26 +46,26 @@ def dialect():
4546

4647

4748
def test_limit_offset(dialect):
48-
statement = select(table).limit(10).offset(0)
49+
statement = select(table_without_catalog).limit(10).offset(0)
4950
query = statement.compile(dialect=dialect)
5051
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table"\nOFFSET :param_1\nLIMIT :param_2'
5152

5253

5354
def test_limit(dialect):
54-
statement = select(table).limit(10)
55+
statement = select(table_without_catalog).limit(10)
5556
query = statement.compile(dialect=dialect)
5657
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table"\nLIMIT :param_1'
5758

5859

5960
def test_offset(dialect):
60-
statement = select(table).offset(0)
61+
statement = select(table_without_catalog).offset(0)
6162
query = statement.compile(dialect=dialect)
6263
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table"\nOFFSET :param_1'
6364

6465

6566
def test_cte_insert_order(dialect):
66-
cte = select(table).cte('cte')
67-
statement = insert(table).from_select(table.columns, cte)
67+
cte = select(table_without_catalog).cte('cte')
68+
statement = insert(table_without_catalog).from_select(table_without_catalog.columns, cte)
6869
query = statement.compile(dialect=dialect)
6970
assert str(query) == \
7071
'INSERT INTO "table" (id, name) WITH cte AS \n'\
@@ -89,3 +90,9 @@ def test_catalogs_create_table(dialect):
8990
'\tid INTEGER\n'\
9091
')\n'\
9192
'\n'
93+
94+
95+
def test_table_clause(dialect):
96+
statement = select(table("user", column("id"), column("name"), column("description")))
97+
query = statement.compile(dialect=dialect)
98+
assert str(query) == 'SELECT user.id, user.name, user.description \nFROM user'

trino/sqlalchemy/compiler.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,8 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212
from sqlalchemy.sql import compiler
13-
try:
14-
from sqlalchemy.sql.expression import (
15-
Alias,
16-
CTE,
17-
Subquery,
18-
)
19-
except ImportError:
20-
# For SQLAlchemy versions < 1.4, the CTE and Subquery classes did not explicitly exist
21-
from sqlalchemy.sql.expression import Alias
22-
CTE = type(None)
23-
Subquery = type(None)
13+
from sqlalchemy.sql.base import DialectKWArgs
14+
2415

2516
# https://trino.io/docs/current/language/reserved.html
2617
RESERVED_WORDS = {
@@ -122,10 +113,7 @@ def visit_table(self, table, asfrom=False, iscrud=False, ashint=False,
122113

123114
@staticmethod
124115
def add_catalog(sql, table):
125-
if table is None:
126-
return sql
127-
128-
if isinstance(table, (Alias, CTE, Subquery)):
116+
if table is None or not isinstance(table, DialectKWArgs):
129117
return sql
130118

131119
if (

0 commit comments

Comments
 (0)