The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix: read a set-operated insert's row source term by term
mateo-berria46f919e8d74
diff --git a/tests/code_coverage_tests/check_migrations_no_data_rewrites.py b/tests/code_coverage_tests/check_migrations_no_data_rewrites.pyindex b52289b4fe2..1cb61586060 100644--- a/tests/code_coverage_tests/check_migrations_no_data_rewrites.py+++ b/tests/code_coverage_tests/check_migrations_no_data_rewrites.py@@ -115,6 +115,8 @@ REWRITES_ROWS = frozenset({"UPDATE", "DELETE", "MERGE"})JOINS_QUERIES = ("UNION", "INTERSECT", "EXCEPT")+SET_OPERATION = re.compile(rf"\b(?:{'|'.join(JOINS_QUERIES)})\b", re.IGNORECASE)+STATEMENT_KEYWORDS = REWRITES_ROWS | frozenset({"INSERT",@@ -378,20 +380,64 @@ def offending_keyword(statement: str) -> str | None:def row_source_keyword(statement: str) -> str | None:"""Which keyword supplies an `INSERT` its rows, or `None` when a literal `VALUES` listdoes. A query outside every parenthesis is the row source outright. Failing that, a- `VALUES` outside every parenthesis is itself the row source, so the scalar subqueries- and helper CTEs nested within that list do not make the insert a rewrite, though only- while no set operation sits beside it at that same level: one that does joins the list- to a second query term, and that term is the row source however deeply it is- parenthesised. Failing both, the rows come from a parenthesised query, which Postgres+ set operation at that same level joins several terms, and the insert is a rewrite when+ any one of them is a query, so each term is read on its own rather than the statement+ read whole. Failing that, a `VALUES` outside every parenthesis is itself the row source,+ so the scalar subqueries and helper CTEs nested within that list do not make the insert+ a rewrite. Failing all three, the rows come from a parenthesised group, which Postgresaccepts and which reading only the unparenthesised text would let through:`INSERT INTO "t" ("a") (SELECT ...)` copies a whole table."""outer = strip_parens(statement)joined = row_source_in(outer)if joined is not None:return joined- if contains(outer, "VALUES") and not any(contains(outer, word) for word in JOINS_QUERIES):+ if SET_OPERATION.search(outer):+ sources = (row_source_keyword(term) for term in set_operation_terms(statement, outer))+ return next((source for source in sources if source is not None), None)+ if contains(outer, "VALUES"):return None- return row_source_in(statement)+ wrapped = parenthesised_row_source(statement)+ return row_source_in(statement) if wrapped is None else row_source_keyword(wrapped)+++def set_operation_terms(statement: str, outer: str) -> Iterator[str]:+ """The terms a top-level set operation joins. The operators are read from the text outside+ every parenthesis, which `strip_parens` blanks in place rather than removing, so their+ offsets are offsets into the statement itself and each term comes back from the original+ text with its own parentheses intact. Reading them at that level is what keeps a set+ operation written inside a `VALUES` list from cutting the list in half. An `ALL` or a+ `DISTINCT` stays at the head of the term that follows, where it names no row source and+ so reads as nothing."""+ edges = [0]+ for operation in SET_OPERATION.finditer(outer):+ edges += [operation.start(), operation.end()]+ edges.append(len(statement))++ for opens, closes in zip(edges[::2], edges[1::2]):+ yield statement[opens:closes]+++def parenthesised_row_source(statement: str) -> str | None:+ """What the last group of parentheses closed at the statement's outermost level holds,+ which is where an `INSERT` keeps a row source it has wrapped, the column list before it+ being a group of its own. Postgres takes `INSERT INTO "t" ("a") (SELECT ...)` and+ `... (VALUES (1))` alike, so reading the wrapped text on its own terms is what stops a+ scalar subquery nested inside a wrapped `VALUES` list standing in for the rows."""+ depth = 0+ opens = None+ wrapped = None++ for index, character in enumerate(statement):+ if character == "(":+ if depth == 0:+ opens = index+ depth += 1+ elif character == ")":+ depth = max(depth - 1, 0)+ if depth == 0 and opens is not None:+ wrapped = statement[opens + 1 : index]++ return wrappeddef row_source_in(text: str) -> str | None:diff --git a/tests/test_litellm/test_check_migrations_no_data_rewrites.py b/tests/test_litellm/test_check_migrations_no_data_rewrites.pyindex 866a0d6bfe6..c6c41a40c1a 100644--- a/tests/test_litellm/test_check_migrations_no_data_rewrites.py+++ b/tests/test_litellm/test_check_migrations_no_data_rewrites.py@@ -161,6 +161,39 @@ class TestInsert:sql = 'INSERT INTO "Foo" ("id") VALUES ((SELECT 1 UNION SELECT 2 LIMIT 1));'assert _keywords(tmp_path, sql) == ()+ def test_a_scalar_subquery_in_a_set_operated_values_list_stays_bounded(self, tmp_path):+ sql = (+ 'INSERT INTO "Foo" ("id") VALUES ((SELECT max("id") FROM "Bar"))'+ " UNION ALL VALUES (2);"+ )+ assert _keywords(tmp_path, sql) == ()++ def test_a_scalar_subquery_in_a_parenthesised_values_list_stays_bounded(self, tmp_path):+ sql = 'INSERT INTO "Foo" ("id") (VALUES ((SELECT max("id") FROM "Bar")));'+ assert _keywords(tmp_path, sql) == ()++ def test_a_scalar_subquery_in_set_operated_parenthesised_values_lists_stays_bounded(self, tmp_path):+ sql = (+ 'INSERT INTO "Foo" ("id") (VALUES ((SELECT max("id") FROM "Bar")))'+ " UNION ALL (VALUES (2));"+ )+ assert _keywords(tmp_path, sql) == ()++ def test_a_set_operation_inside_a_values_list_does_not_split_the_terms(self, tmp_path):+ sql = (+ 'INSERT INTO "Foo" ("id") VALUES ((SELECT max("id") FROM "Bar"'+ ' UNION SELECT max("id") FROM "Bar")) UNION ALL VALUES (2);'+ )+ assert _keywords(tmp_path, sql) == ()++ def test_a_query_term_written_before_a_values_term_is_still_the_row_source(self, tmp_path):+ sql = 'INSERT INTO "Foo" ("id") (SELECT "id" FROM "Bar") UNION ALL (VALUES (2));'+ assert _keywords(tmp_path, sql) == ("INSERT ... SELECT",)++ def test_a_table_term_beside_parenthesised_values_is_still_the_row_source(self, tmp_path):+ sql = 'INSERT INTO "Foo" ("id") (VALUES (1)) UNION ALL (TABLE "Bar");'+ assert _keywords(tmp_path, sql) == ("INSERT ... TABLE",)+def test_a_table_row_source_is_flagged(self, tmp_path):assert _keywords(tmp_path, 'INSERT INTO "Foo" TABLE "Bar";') == ("INSERT ... TABLE",)