Skip to content

Commit 0fb9d8f

Browse files
long2iceebyhr
authored andcommitted
Overwrite limit_clause in TrinoSQLCompiler
1 parent 62f53c9 commit 0fb9d8f

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License");
2+
# you may not use this file except in compliance with the License.
3+
# You may obtain a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS,
9+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
# See the License for the specific language governing permissions and
11+
# limitations under the License.
12+
import pytest
13+
from sqlalchemy import Table, MetaData, Column, Integer, String, select
14+
15+
from trino.sqlalchemy.dialect import TrinoDialect
16+
17+
metadata = MetaData()
18+
table = Table(
19+
'table',
20+
metadata,
21+
Column('id', Integer, primary_key=True),
22+
Column('name', String),
23+
)
24+
25+
26+
@pytest.fixture
27+
def dialect():
28+
return TrinoDialect()
29+
30+
31+
def test_limit_offset(dialect):
32+
statement = select(table).limit(10).offset(0)
33+
query = statement.compile(dialect=dialect)
34+
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table"\nOFFSET :param_1\nLIMIT :param_2'
35+
36+
37+
def test_limit(dialect):
38+
statement = select(table).limit(10)
39+
query = statement.compile(dialect=dialect)
40+
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table"\nLIMIT :param_1'
41+
42+
43+
def test_offset(dialect):
44+
statement = select(table).offset(0)
45+
query = statement.compile(dialect=dialect)
46+
assert str(query) == 'SELECT "table".id, "table".name \nFROM "table"\nOFFSET :param_1'

trino/sqlalchemy/compiler.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,16 @@
9191

9292

9393
class TrinoSQLCompiler(compiler.SQLCompiler):
94-
pass
94+
def limit_clause(self, select, **kw):
95+
"""
96+
Trino support only OFFSET...LIMIT but not LIMIT...OFFSET syntax.
97+
"""
98+
text = ""
99+
if select._offset_clause is not None:
100+
text += "\nOFFSET " + self.process(select._offset_clause, **kw)
101+
if select._limit_clause is not None:
102+
text += "\nLIMIT " + self.process(select._limit_clause, **kw)
103+
return text
95104

96105

97106
class TrinoDDLCompiler(compiler.DDLCompiler):

0 commit comments

Comments
 (0)