Skip to content

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]

fix: don't read a quoted table's column list as a routine call site

mateo-berrie018b9ce74b2
diff --git a/tests/code_coverage_tests/check_migrations_no_data_rewrites.py b/tests/code_coverage_tests/check_migrations_no_data_rewrites.py
index eca9094ef93..3ef290b40c9 100644
--- a/tests/code_coverage_tests/check_migrations_no_data_rewrites.py
+++ b/tests/code_coverage_tests/check_migrations_no_data_rewrites.py
@@ -128,6 +128,8 @@ DEFINES_A_ROUTINE = re.compile(
QUALIFIED_NAME = r"(?:\"[^\"]*\"|[A-Za-z_][A-Za-z0-9_$]*)"
ROUTINE_NAME = re.compile(rf"\s*(?:{QUALIFIED_NAME}\s*\.\s*)?({QUALIFIED_NAME})")
OPENS_A_CALL = re.compile(r"\s*\(")
+NAMES_AN_INDEX = re.compile(r"\bCREATE\b.+\bINDEX\b", re.IGNORECASE | re.DOTALL)
+INTRODUCES_A_RELATION = frozenset({"TABLE", "INTO", "REFERENCES", "EXISTS", "COPY"})
REWRITES_ROWS = frozenset({"UPDATE", "DELETE", "MERGE"})
@@ -270,8 +272,10 @@ def mask(
of text is. Whether an identifier opens a call is read from the masked text rather than the
raw SQL, so a comment sitting between the name and its parenthesis, blanked to spaces here, is
skipped exactly as whitespace is. A double-quoted identifier that opens no call, a column,
- table, index, or constraint name, is left out, so it never masquerades as a call to a
- like-named routine."""
+ index, or constraint name, is left out, so it never masquerades as a call to a like-named
+ routine, as is one whose parenthesis is a column list rather than an argument list, the table
+ of a `CREATE TABLE`, `INSERT INTO`, `REFERENCES`, `COPY`, or `CREATE INDEX`, which
+ `names_a_relation` reads from the word before the name."""
chunks: list[str] = []
bodies: list[tuple[int, int]] = []
literals: list[tuple[int, int]] = []
@@ -323,7 +327,11 @@ def mask(
index += 1
masked = "".join(chunks)
- calls = tuple((start, end) for start, end in identifiers if OPENS_A_CALL.match(masked, end))
+ calls = tuple(
+ (start, end)
+ for start, end in identifiers
+ if OPENS_A_CALL.match(masked, end) and not names_a_relation(masked[:start])
+ )
return masked, tuple(bodies), tuple(literals), calls
@@ -567,6 +575,27 @@ def names_a_table(before: str) -> bool:
return word is not None and word.group(1).upper() == "INSERT"
+def names_a_relation(before: str) -> bool:
+ """Whether the parenthesised quoted identifier this text runs up to names a table with a
+ column list rather than opening a routine call. The two look alike, a name then a `(`, so
+ an uncalled routine sharing a name with a table would otherwise read as called. The word
+ immediately before tells most of them apart: `CREATE TABLE`, `INSERT INTO`, a foreign key's
+ `REFERENCES`, `CREATE TABLE IF NOT EXISTS`, and `COPY` each put a table there, and none can
+ precede a call. `ON` is the ambiguous one, since it introduces the table of a `CREATE INDEX`
+ but also a join condition that may itself be a call, so it counts only inside a statement
+ that creates an index, leaving `JOIN ... ON f()` and an index predicate's `WHERE f()` as
+ calls."""
+ word = PRECEDING_WORD.search(before)
+ if word is None:
+ return False
+ keyword = word.group(1).upper()
+ if keyword in INTRODUCES_A_RELATION:
+ return True
+ if keyword == "ON":
+ return NAMES_AN_INDEX.search(before[before.rfind(";") + 1 :]) is not None
+ return False
+
+
def assignment_reach(statement: str) -> tuple[str, ...]:
"""The words the statement's assignment is reached through, empty where it holds none.
PL/pgSQL spells the operator `:=` and takes a bare `=` as the same thing, so both count,
diff --git a/tests/test_litellm/test_check_migrations_no_data_rewrites.py b/tests/test_litellm/test_check_migrations_no_data_rewrites.py
index b111a486d5c..f3b35fd3448 100644
--- a/tests/test_litellm/test_check_migrations_no_data_rewrites.py
+++ b/tests/test_litellm/test_check_migrations_no_data_rewrites.py
@@ -449,6 +449,50 @@ class TestStoredRoutines:
sql = self.DEFINITION + 'ALTER TABLE "Foo" ADD COLUMN "backfill" /* note */ int;\n'
assert _keywords(tmp_path, sql) == ()
+ def test_a_like_named_table_with_a_column_list_is_not_a_call(self, tmp_path):
+ sql = self.DEFINITION + 'CREATE TABLE "backfill" (id int);\n'
+ assert _keywords(tmp_path, sql) == ()
+
+ def test_an_insert_into_a_like_named_table_is_not_a_call(self, tmp_path):
+ sql = self.DEFINITION + 'INSERT INTO "backfill" ("id") VALUES (1);\n'
+ assert _keywords(tmp_path, sql) == ()
+
+ def test_an_insert_into_a_like_named_table_with_a_commented_column_list_is_not_a_call(self, tmp_path):
+ sql = self.DEFINITION + 'INSERT INTO "backfill" /* cols */ ("id") VALUES (1);\n'
+ assert _keywords(tmp_path, sql) == ()
+
+ def test_a_foreign_key_referencing_a_like_named_table_is_not_a_call(self, tmp_path):
+ sql = self.DEFINITION + 'CREATE TABLE "Bar" (id int REFERENCES "backfill" ("id"));\n'
+ assert _keywords(tmp_path, sql) == ()
+
+ def test_an_index_on_a_like_named_table_is_not_a_call(self, tmp_path):
+ sql = self.DEFINITION + 'CREATE INDEX ON "backfill" ("id");\n'
+ assert _keywords(tmp_path, sql) == ()
+
+ def test_an_if_not_exists_table_named_after_the_routine_is_not_a_call(self, tmp_path):
+ sql = self.DEFINITION + 'CREATE TABLE IF NOT EXISTS "backfill" (id int);\n'
+ assert _keywords(tmp_path, sql) == ()
+
+ def test_a_copy_into_a_like_named_table_is_not_a_call(self, tmp_path):
+ sql = self.DEFINITION + 'COPY "backfill" ("id") FROM stdin;\n'
+ assert _keywords(tmp_path, sql) == ()
+
+ def test_a_set_returning_call_in_from_still_counts(self, tmp_path):
+ sql = self.DEFINITION + 'SELECT * FROM "backfill"();\n'
+ assert _keywords(tmp_path, sql) == ("UPDATE",)
+
+ def test_a_call_in_a_join_condition_still_counts(self, tmp_path):
+ sql = self.DEFINITION + 'SELECT 1 FROM "Bar" b JOIN "Baz" z ON "backfill"();\n'
+ assert _keywords(tmp_path, sql) == ("UPDATE",)
+
+ def test_a_call_in_an_index_predicate_still_counts(self, tmp_path):
+ sql = self.DEFINITION + 'CREATE INDEX ON "Foo" ("a") WHERE "backfill"();\n'
+ assert _keywords(tmp_path, sql) == ("UPDATE",)
+
+ def test_a_join_condition_call_after_an_earlier_index_still_counts(self, tmp_path):
+ sql = self.DEFINITION + 'CREATE INDEX ON "Foo" ("a");\nSELECT 1 FROM "Bar" b JOIN "Baz" z ON "backfill"();\n'
+ assert _keywords(tmp_path, sql) == ("UPDATE",)
+
def test_a_trigger_wiring_the_function_up_counts_as_a_call(self, tmp_path):
sql = self.DEFINITION + 'CREATE TRIGGER t AFTER INSERT ON "Foo" EXECUTE FUNCTION backfill();\n'
assert _keywords(tmp_path, sql) == ("UPDATE",)