fix(sql): guard FORCE_LIMIT against SHOW statements (#36939) (#42588)

Co-authored-by: Claude Code <noreply@anthropic.com>
This commit is contained in:
Evan Rusackas
2026-08-11 23:33:15 -07:00
committed by GitHub
co-authored by Claude Code
parent 8e455034d0
commit b4f3fae288
2 changed files with 55 additions and 0 deletions
+46
View File
@@ -2387,6 +2387,52 @@ def test_set_limit_value(
assert statement.format() == expected
@pytest.mark.parametrize(
"engine",
[
# Engines whose sqlglot dialect parses `SHOW` into a real `exp.Show`
# node (as opposed to falling back to an opaque `exp.Command`, which
# doesn't expose a `limit` arg and so was never affected by this bug).
"starrocks",
"mysql",
"snowflake",
],
)
@pytest.mark.parametrize(
"sql",
[
"SHOW TABLES",
"SHOW DATABASES",
"SHOW CREATE TABLE test.will_test1",
],
)
def test_set_limit_value_leaves_show_statements_unchanged(
sql: str, engine: str
) -> None:
"""
Regression for #36939: FORCE_LIMIT must not touch ``SHOW`` statements.
``SHOW`` statements have no `LIMIT` clause in sqlglot's expression tree,
so forcing one via ``args["limit"]`` doesn't reject cleanly, it produces
a malformed statement with two ``LIMIT`` keywords (one from a stray
rendering of the bare ``Limit`` expression, one from the forced value).
StarRocks (and presumably other engines) reject that outright: "Getting
syntax error ... Unexpected input 'LIMIT'". The statement should be
left untouched instead, matching how ``SELECT`` statements without a
scannable row source aren't force-limited either.
Covers multiple engines, not just StarRocks: the fix guards on the AST
node type (``exp.Show``), not the dialect, so any engine whose sqlglot
dialect parses ``SHOW`` into a real ``Show`` node (e.g. MySQL, Snowflake)
is equally exposed and must be equally protected.
"""
statement = SQLStatement(sql, engine)
original = statement.format()
statement.set_limit_value(1000, LimitMethod.FORCE_LIMIT)
assert statement.format() == original
assert "LIMIT" not in statement.format()
@pytest.mark.parametrize(
"kql, limit, expected",
[