Skip to content

Commit 0cabf52

Browse files
laserkaplanebyhr
authored andcommitted
Add cte_follows_insert flag to support proper statement creation
1 parent fb92e93 commit 0cabf52

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

tests/unit/sqlalchemy/test_compiler.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,15 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212
import pytest
13-
from sqlalchemy import Table, MetaData, Column, Integer, String, select
13+
from sqlalchemy import (
14+
Column,
15+
insert,
16+
Integer,
17+
MetaData,
18+
select,
19+
String,
20+
Table,
21+
)
1422

1523
from trino.sqlalchemy.dialect import TrinoDialect
1624

@@ -44,3 +52,15 @@ def test_offset(dialect):
4452
statement = select(table).offset(0)
4553
query = statement.compile(dialect=dialect)
4654
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table"\nOFFSET :param_1'
55+
56+
57+
def test_cte_insert_order(dialect):
58+
cte = select(table).cte('cte')
59+
statement = insert(table).from_select(table.columns, cte)
60+
query = statement.compile(dialect=dialect)
61+
assert str(query) == \
62+
'INSERT INTO "table" (id, name) WITH cte AS \n'\
63+
'(SELECT "table".id AS id, "table".name AS name \n'\
64+
'FROM "table")\n'\
65+
' SELECT cte.id, cte.name \n'\
66+
'FROM cte'

trino/sqlalchemy/dialect.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class TrinoDialect(DefaultDialect):
6262
# and tests are needed before being enabled
6363
supports_statement_cache = False
6464

65+
# Support proper ordering of CTEs in regard to an INSERT statement
66+
cte_follows_insert = True
67+
6568
@classmethod
6669
def dbapi(cls):
6770
"""

0 commit comments

Comments
 (0)