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 every assignment operator, not only the statement's first
mateo-berri813d3a991fab
diff --git a/tests/code_coverage_tests/check_migrations_no_data_rewrites.py b/tests/code_coverage_tests/check_migrations_no_data_rewrites.pyindex cbc24fe3660..9e6df46899b 100644--- a/tests/code_coverage_tests/check_migrations_no_data_rewrites.py+++ b/tests/code_coverage_tests/check_migrations_no_data_rewrites.py@@ -98,12 +98,13 @@ MARKER = re.compile(r"--[ \t]*data-migration-ok:[ \t]*(\S.*?)[ \t]*$", re.MULTILDOLLAR_TAG = re.compile(r"\$(?:[A-Za-z_][A-Za-z0-9_]*)?\$")FIRST_WORD = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")STATEMENT = re.compile(r"[^;]+")-RUN_BY_NAME = re.compile(r"\bEXECUTE[ \t]+([A-Za-z_][A-Za-z0-9_]*)", re.IGNORECASE)+RUN_BY_NAME = re.compile(r"\bEXECUTE\s+([A-Za-z_][A-Za-z0-9_]*)", re.IGNORECASE)INTO_TARGETS = re.compile(- r"\bINTO[ \t]+(?:STRICT[ \t]+)?"- r"([A-Za-z_][A-Za-z0-9_]*(?:[ \t]*,[ \t]*[A-Za-z_][A-Za-z0-9_]*)*)",+ r"\bINTO\s+(?:STRICT\s+)?"+ r"([A-Za-z_][A-Za-z0-9_]*(?:\s*,\s*[A-Za-z_][A-Za-z0-9_]*)*)",re.IGNORECASE,)+ASSIGNS = re.compile(r":=|(?<![<>!:=])=(?!=)")PRECEDING_WORD = re.compile(r"([A-Za-z_][A-Za-z0-9_]*)[^A-Za-z0-9_]*$")EXPLAIN_OPTIONS = re.compile(r"\bEXPLAIN\b(?:\s+(?:ANALYZE|ANALYSE|VERBOSE)\b)+", re.IGNORECASE)@@ -368,17 +369,17 @@ def assigned_names(statement: str) -> frozenset[str]:alongside the name, and none of that is worth parsing when the only question is whichname is executed. PL/pgSQL spells that operator `:=` and takes a bare `=` as the samething, so both count, the second only where `assigns_rather_than_compares` reads it as- an assignment. A query assigns through the target list after its `INTO` instead, which- is how a rewrite reaches a variable with neither operator appearing at all."""+ an assignment. Every operator in the statement is read rather than only the first, since+ a comparison earlier on the line would otherwise claim the one slot and hide the+ assignment after it: `IF n = 1 THEN stmt = '...'` writes `stmt` at its second `=`. A+ query assigns through the target list after its `INTO` instead, which is how a rewrite+ reaches a variable with neither operator appearing at all."""names: set[str] = set()- head, operator, _ = statement.partition(":=")- assigns = bool(operator)- if not assigns:- head, operator, _ = statement.partition("=")- assigns = bool(operator) and assigns_rather_than_compares(head)- if assigns:- names.update(word.group().lower() for word in FIRST_WORD.finditer(head))+ for operator in ASSIGNS.finditer(statement):+ reached = reached_words(statement[: operator.start()])+ if operator.group() == ":=" or assigns_rather_than_compares(reached):+ names.update(word.lower() for word in reached)for targets in INTO_TARGETS.finditer(statement):if names_a_table(statement[: targets.start()]):@@ -398,22 +399,32 @@ def names_a_table(before: str) -> bool:return word is not None and word.group(1).upper() == "INSERT"-def assigns_rather_than_compares(head: str) -> bool:- """Whether the bare `=` this text runs up to writes a variable or tests one. Only the- words ahead of it tell the two apart: an assignment is reached with a name and perhaps a- type, while a comparison is reached either through a statement carrying its own keyword- or through a word that guards a condition. Those words stop counting once something- opens a block after them, since a `THEN` ends the condition its `IF` began and the- assignment that follows on the same line is an assignment like any other."""+def reached_words(head: str) -> tuple[str, ...]:+ """The words an assignment operator is reached through, which is everything since the last+ word to open a block. A `THEN` ends the condition its `IF` began, so nothing ahead of it+ describes what follows, and neither the name being written nor the keywords that would+ mark a comparison ever sit further back than that."""words = [word.group().upper() for word in FIRST_WORD.finditer(head)]opened = max((index for index, word in enumerate(words) if word in OPENS_A_BLOCK), default=-1)- reached = set(words[opened + 1 :])- return not (reached & STATEMENT_KEYWORDS) and not (reached & GUARDS_A_CONDITION)+ return tuple(words[opened + 1 :])+++def assigns_rather_than_compares(reached: tuple[str, ...]) -> bool:+ """Whether a bare `=` reached through these words writes a variable or tests one. They are+ all that tells the two apart: an assignment is reached with a name and perhaps a type,+ while a comparison is reached either through a statement carrying its own keyword or+ through a word that guards a condition."""+ words = set(reached)+ return not (words & STATEMENT_KEYWORDS) and not (words & GUARDS_A_CONDITION)def executed_names(masked: str) -> frozenset[str]:"""The variables handed to an `EXECUTE` by name. Reading these off the masked text keeps- an `EXECUTE` written inside a comment or a string from counting."""+ an `EXECUTE` written inside a comment or a string from counting. Masking blanks a literal+ in place rather than removing it, so `EXECUTE '...'` can leave the word after it looking+ like the name being run. Reaching that word means crossing no semicolon, which leaves only+ the syntax `INTO`, `USING` and `END`, and no assignment ever writes one of those, so the+ stray name has nothing to match."""return frozenset(match.group(1).lower() for match in RUN_BY_NAME.finditer(masked))diff --git a/tests/test_litellm/test_check_migrations_no_data_rewrites.py b/tests/test_litellm/test_check_migrations_no_data_rewrites.pyindex 7ed8b9978a3..fe53e1ac823 100644--- a/tests/test_litellm/test_check_migrations_no_data_rewrites.py+++ b/tests/test_litellm/test_check_migrations_no_data_rewrites.py@@ -572,6 +572,97 @@ class TestDynamicSql:)assert _keywords(tmp_path, sql) == ("UPDATE",)+ def test_a_rewrite_assigned_past_an_earlier_comparison_is_flagged(self, tmp_path):+ sql = (+ "DO $$\n"+ "DECLARE\n"+ " stmt text;\n"+ " total int := 1;\n"+ "BEGIN\n"+ " IF total = 1 THEN stmt = 'DELETE FROM \"Foo\" WHERE true'; END IF;\n"+ " EXECUTE stmt;\n"+ "END $$;"+ )+ assert _keywords(tmp_path, sql) == ("DELETE",)+ assert _scan(tmp_path, sql)[0].line == 6++ def test_a_rewrite_assigned_past_a_loop_comparison_is_flagged(self, tmp_path):+ sql = (+ "DO $$\n"+ "DECLARE\n"+ " stmt text;\n"+ " total int := 3;\n"+ "BEGIN\n"+ " WHILE total >= 1 LOOP stmt = 'UPDATE \"Foo\" SET \"a\" = 1'; END LOOP;\n"+ " EXECUTE stmt;\n"+ "END $$;"+ )+ assert _keywords(tmp_path, sql) == ("UPDATE",)++ def test_a_rewrite_selected_into_a_target_a_line_down_is_flagged(self, tmp_path):+ sql = (+ "DO $$\n"+ "DECLARE\n"+ " stmt text;\n"+ "BEGIN\n"+ " SELECT 'UPDATE \"Foo\" SET \"a\" = 1'\n"+ " INTO\n"+ " stmt;\n"+ " EXECUTE stmt;\n"+ "END $$;"+ )+ assert _keywords(tmp_path, sql) == ("UPDATE",)++ def test_a_rewrite_selected_into_a_strict_target_a_line_down_is_flagged(self, tmp_path):+ sql = (+ "DO $$\n"+ "DECLARE\n"+ " stmt text;\n"+ "BEGIN\n"+ " SELECT 'DELETE FROM \"Foo\"' INTO STRICT\n"+ " stmt;\n"+ " EXECUTE stmt;\n"+ "END $$;"+ )+ assert _keywords(tmp_path, sql) == ("DELETE",)++ def test_a_rewrite_executed_by_a_name_a_line_down_is_flagged(self, tmp_path):+ sql = (+ "DO $$\n"+ "DECLARE\n"+ " stmt text := 'UPDATE \"Foo\" SET \"a\" = 1';\n"+ "BEGIN\n"+ " EXECUTE\n"+ " stmt;\n"+ "END $$;"+ )+ assert _keywords(tmp_path, sql) == ("UPDATE",)++ def test_a_rewrite_compared_against_is_not_an_assignment(self, tmp_path):+ sql = (+ "DO $$\n"+ "DECLARE\n"+ " stmt text := 'ALTER TABLE \"Foo\" ADD COLUMN \"b\" TEXT';\n"+ "BEGIN\n"+ " IF stmt = 'UPDATE \"Foo\" SET \"a\" = 1' THEN\n"+ " RAISE NOTICE 'the application owns that one';\n"+ " END IF;\n"+ " EXECUTE stmt;\n"+ "END $$;"+ )+ assert _keywords(tmp_path, sql) == ()++ def test_a_rewrite_passed_to_execute_as_a_parameter_is_not_run(self, tmp_path):+ sql = (+ "DO $$\n"+ "DECLARE\n"+ " stmt text := 'UPDATE \"Foo\" SET \"a\" = 1';\n"+ "BEGIN\n"+ " EXECUTE 'INSERT INTO \"Log\" (\"sql\") VALUES ($1)' USING stmt;\n"+ "END $$;"+ )+ assert _keywords(tmp_path, sql) == ()+def test_a_literal_selected_into_a_variable_nothing_runs_is_inert(self, tmp_path):sql = ("DO $$\n"