Skip to content

Commit e82722b

Browse files
committed
fix: generate correct max limit when unspecified with offset
1 parent e879aad commit e82722b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

google/cloud/sqlalchemy_spanner/sqlalchemy_spanner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ def limit_clause(self, select, **kw):
259259
text += "\n LIMIT " + self.process(select._limit_clause, **kw)
260260
if select._offset_clause is not None:
261261
if select._limit_clause is None:
262-
text += "\n LIMIT 9223372036854775805"
262+
offset = int(self.process(select._offset_clause, **kw))
263+
text += f"\n LIMIT {9223372036854775807-offset}"
263264
text += " OFFSET " + self.process(select._offset_clause, **kw)
264265
return text
265266

test/test_suite.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,31 @@ def test_staleness(self):
16161616
assert connection.connection.staleness is None
16171617

16181618

1619+
class LimitOffsetTest(fixtures.TestBase):
1620+
"""
1621+
Check that SQL with an offset and no limit is being generated correctly.
1622+
"""
1623+
1624+
def setUp(self):
1625+
self._engine = create_engine(get_db_url(), pool_size=1)
1626+
self._metadata = MetaData(bind=self._engine)
1627+
1628+
self._table = Table(
1629+
"users",
1630+
self._metadata,
1631+
Column("user_id", Integer, primary_key=True),
1632+
Column("user_name", String(16), nullable=False),
1633+
)
1634+
1635+
self._metadata.create_all(self._engine)
1636+
1637+
def test_offset_only(self):
1638+
for offset in [1, 7, 10, 100, 1000, 10000]:
1639+
1640+
with self._engine.connect().execution_options(read_only=True) as connection:
1641+
list(connection.execute(self._table.select().offset(offset)).fetchall())
1642+
1643+
16191644
class ComputedReflectionFixtureTest(_ComputedReflectionFixtureTest):
16201645
@classmethod
16211646
def define_tables(cls, metadata):

0 commit comments

Comments
 (0)