Merge pull request #39022 from AarDG10/fix-limit-offset

fix(query): use default limit when offset is used in MariaDB and SQLite w/o limit
This commit is contained in:
Aarol D'Souza 2026-04-30 11:24:05 +05:30 committed by GitHub
commit b7657442de
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View file

@ -172,6 +172,9 @@ BACKTICK_FIELD_PARSE_REGEX = re.compile(r"^`tab([\w\s-]+)`\.(`?)(\w+)\2$")
# Group 3: Fieldname # Group 3: Fieldname
CHILD_TABLE_FIELD_PATTERN = re.compile(r'^[`"]?tab([\w\s]+)[`"]?\.([`"]?)(\w+)\2$') CHILD_TABLE_FIELD_PATTERN = re.compile(r'^[`"]?tab([\w\s]+)[`"]?\.([`"]?)(\w+)\2$')
# Maximum value of an unsigned 64-bit integer
MAX_LIMIT = 18446744073709551615
# Direct mapping from uppercase function names to pypika function classes # Direct mapping from uppercase function names to pypika function classes
FUNCTION_MAPPING = { FUNCTION_MAPPING = {
"COUNT": functions.Count, "COUNT": functions.Count,
@ -299,6 +302,11 @@ class Engine:
if offset: if offset:
if not isinstance(offset, int) or offset < 0: if not isinstance(offset, int) or offset < 0:
frappe.throw(_("Offset must be a non-negative integer"), TypeError) frappe.throw(_("Offset must be a non-negative integer"), TypeError)
# In MariaDB and SQLite, offset requires limit
if not self.is_postgres and not limit:
self.query = self.query.limit(MAX_LIMIT)
self.query = self.query.offset(offset) self.query = self.query.offset(offset)
if distinct: if distinct:

View file

@ -2625,6 +2625,19 @@ class TestQuery(IntegrationTestCase):
) )
self.assertFalse(index_exists) self.assertFalse(index_exists)
def test_limit_offset_query(self):
"""Test if query builder correctly uses limit with offset in MariaDB and SQLite when limit is omitted."""
from frappe.database.query import MAX_LIMIT
query = frappe.qb.get_query("Doctype", offset=10).get_sql()
if frappe.db.db_type != "postgres":
self.assertIn(f"LIMIT {MAX_LIMIT} OFFSET 10", query)
query = frappe.qb.get_query("Doctype", limit=10, offset=10).get_sql()
self.assertIn("LIMIT 10 OFFSET 10", query)
else:
self.assertNotIn("LIMIT", query)
self.assertIn("OFFSET 10", query)
# This function is used as a permission query condition hook # This function is used as a permission query condition hook
def test_permission_hook_condition(user): def test_permission_hook_condition(user):