Skip to content
This repository was archived by the owner on May 5, 2022. It is now read-only.

Commit ee500a2

Browse files
authored
Overwrite limit_clause in TrinoSQLCompiler (#37)
Overwrite limit_clause in TrinoSQLCompiler
1 parent 72eed3d commit ee500a2

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

sqlalchemy_trino/compiler.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,18 @@
8080

8181

8282
class TrinoSQLCompiler(compiler.SQLCompiler):
83-
pass
83+
84+
def limit_clause(self, select, **kw):
85+
"""
86+
Trino support only OFFSET...LIMIT but not LIMIT...OFFSET syntax.
87+
See https://github.com/trinodb/trino/issues/4335.
88+
"""
89+
text = ""
90+
if select._offset_clause is not None:
91+
text += " OFFSET " + self.process(select._offset_clause, **kw)
92+
if select._limit_clause is not None:
93+
text += "\n LIMIT " + self.process(select._limit_clause, **kw)
94+
return text
8495

8596

8697
class TrinoDDLCompiler(compiler.DDLCompiler):

tests/test_compiler.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from sqlalchemy import Table, MetaData, Column, Integer, String, select
2+
3+
from sqlalchemy_trino.dialect import TrinoDialect
4+
5+
metadata = MetaData()
6+
table = Table(
7+
'table',
8+
metadata,
9+
Column('id', Integer, primary_key=True),
10+
Column('name', String),
11+
)
12+
13+
14+
def test_limit_offset():
15+
statement = select(table).limit(10).offset(0)
16+
query = statement.compile(dialect=TrinoDialect())
17+
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table" OFFSET :param_1\n LIMIT :param_2'
18+
19+
20+
def test_limit():
21+
statement = select(table).limit(10)
22+
query = statement.compile(dialect=TrinoDialect())
23+
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table"\n LIMIT :param_1'
24+
25+
26+
def test_offset():
27+
statement = select(table).offset(0)
28+
query = statement.compile(dialect=TrinoDialect())
29+
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table" OFFSET :param_1'

0 commit comments

Comments
 (0)