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

Overwrite limit_clause in TrinoSQLCompiler #37

Merged
merged 4 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
"""
Trino support only OFFSET...LIMIT but not LIMIT...OFFSET syntax.
Copy link
Owner

@dungdm93 dungdm93 Nov 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be docstring for method limit_clause, not for class TrinoSQLCompiler

See https://github.com/trinodb/trino/issues/4335.
"""

def limit_clause(self, select, **kw):
dungdm93 marked this conversation as resolved.
Show resolved Hide resolved
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
17 changes: 17 additions & 0 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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():
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 more test cases:

  • select(table).limit(10)
  • select(table).offset(3)

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'