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

Commit

Permalink
Overwrite limit_clause in TrinoSQLCompiler (#37)
Browse files Browse the repository at this point in the history
Overwrite limit_clause in TrinoSQLCompiler
  • Loading branch information
long2ice authored Nov 26, 2021
1 parent 72eed3d commit ee500a2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
13 changes: 12 additions & 1 deletion sqlalchemy_trino/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,18 @@


class TrinoSQLCompiler(compiler.SQLCompiler):
pass

def limit_clause(self, select, **kw):
"""
Trino support only OFFSET...LIMIT but not LIMIT...OFFSET syntax.
See https://github.com/trinodb/trino/issues/4335.
"""
text = ""
if select._offset_clause is not None:
text += " OFFSET " + self.process(select._offset_clause, **kw)
if select._limit_clause is not None:
text += "\n LIMIT " + self.process(select._limit_clause, **kw)
return text


class TrinoDDLCompiler(compiler.DDLCompiler):
Expand Down
29 changes: 29 additions & 0 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from sqlalchemy import Table, MetaData, Column, Integer, String, select

from sqlalchemy_trino.dialect import TrinoDialect

metadata = MetaData()
table = Table(
'table',
metadata,
Column('id', Integer, primary_key=True),
Column('name', String),
)


def test_limit_offset():
statement = select(table).limit(10).offset(0)
query = statement.compile(dialect=TrinoDialect())
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table" OFFSET :param_1\n LIMIT :param_2'


def test_limit():
statement = select(table).limit(10)
query = statement.compile(dialect=TrinoDialect())
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table"\n LIMIT :param_1'


def test_offset():
statement = select(table).offset(0)
query = statement.compile(dialect=TrinoDialect())
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table" OFFSET :param_1'

0 comments on commit ee500a2

Please sign in to comment.