fix(sql): catch sqlglot ParseError when parsing RLS predicates (#43651)

This commit is contained in:
Elizabeth Thompson
2026-09-01 15:04:18 -07:00
committed by GitHub
parent c9f625627d
commit 531587a4b2
2 changed files with 54 additions and 1 deletions
+35
View File
@@ -5578,6 +5578,41 @@ def test_parse_predicate_length_check() -> None:
stmt.parse_predicate("x" * 101)
def test_parse_predicate_invalid_sql_raises_superset_parse_error() -> None:
"""
A syntactically invalid RLS predicate raises ``SupersetParseError``.
``parse_predicate`` is reachable via ``apply_rls`` for any RLS clause
configured on a queried table; an invalid clause must surface as the
typed 422 parse error rather than leaking a raw ``sqlglot`` exception.
"""
stmt = SQLStatement("SELECT 1", "postgresql")
with pytest.raises(SupersetParseError) as excinfo:
stmt.parse_predicate("a >")
assert excinfo.value.status == 422
def test_parse_predicate_sqlglot_error_raises_superset_parse_error(
mocker: MockerFixture,
) -> None:
"""
A non-``ParseError`` ``sqlglot`` failure also surfaces as a typed error.
``parse_predicate`` catches the generic ``SqlglotError`` base class as a
fallback so any sqlglot failure (e.g. tokenize errors) is converted into a
``SupersetParseError`` rather than leaking a raw sqlglot exception.
"""
# Build the statement before patching, since the constructor also parses.
stmt = SQLStatement("SELECT 1", "postgresql")
mocker.patch(
"sqlglot.parse_one",
side_effect=sqlglot.errors.SqlglotError("boom"),
)
with pytest.raises(SupersetParseError) as excinfo:
stmt.parse_predicate("a > 1")
assert excinfo.value.status == 422
@pytest.mark.usefixtures("_small_parse_cap")
def test_transpile_to_dialect_length_check() -> None:
"""