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: read bind values from the USING the command expression has closed

mateo-berri12c4652fc424
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 c82d52167b4..b52289b4fe2 100644
--- a/tests/code_coverage_tests/check_migrations_no_data_rewrites.py
+++ b/tests/code_coverage_tests/check_migrations_no_data_rewrites.py
@@ -566,9 +566,14 @@ def bind_values_start(statement: str) -> int:
"""Where a statement stops handing commands to the server and starts listing bind values.
The expressions after `USING` are values substituted into the command, never commands in
their own right, so one that merely spells out a rewrite is not running it. Read off the
- masked text, so a `USING` written inside the command string is not mistaken for this one."""
- keyword = BIND_VALUES.search(statement)
- return len(statement) if keyword is None else keyword.start()
+ masked text, so a `USING` written inside the command string is not mistaken for this one,
+ and only once the parentheses have closed, so that the `USING` of a `JOIN` in a subquery
+ that helps build the command does not cut the command short and hide the rest of it."""
+ for keyword in BIND_VALUES.finditer(statement):
+ preceding = statement[: keyword.start()]
+ if preceding.count("(") == preceding.count(")"):
+ return keyword.start()
+ return len(statement)
def statement_start(statement: re.Match[str]) -> int:
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 970bc9f5264..866a0d6bfe6 100644
--- a/tests/test_litellm/test_check_migrations_no_data_rewrites.py
+++ b/tests/test_litellm/test_check_migrations_no_data_rewrites.py
@@ -635,6 +635,27 @@ class TestDynamicSql:
)
assert _keywords(tmp_path, sql) == ("DELETE",)
+ def test_a_join_using_in_a_subquery_building_the_command_does_not_end_it(self, tmp_path):
+ sql = (
+ "DO $$\nDECLARE v text;\nBEGIN\n EXECUTE (SELECT v FROM \"A\" x JOIN \"A\" y"
+ " USING (\"id\")) || 'UPDATE \"Foo\" SET \"a\" = 1';\nEND $$;"
+ )
+ assert _keywords(tmp_path, sql) == ("UPDATE",)
+
+ def test_a_join_using_does_not_take_the_place_of_the_real_bind_values(self, tmp_path):
+ sql = (
+ "DO $$\nDECLARE v text;\nBEGIN\n EXECUTE (SELECT v FROM \"A\" x JOIN \"A\" y"
+ " USING (\"id\")) || 'UPDATE \"Foo\" SET \"a\" = $1' USING 2;\nEND $$;"
+ )
+ assert _keywords(tmp_path, sql) == ("UPDATE",)
+
+ def test_a_bind_value_naming_a_rewrite_after_a_subquery_join_is_not_run(self, tmp_path):
+ sql = (
+ "DO $$\nDECLARE v text;\nBEGIN\n EXECUTE (SELECT v FROM \"A\" x JOIN \"A\" y"
+ " USING (\"id\")) USING 'UPDATE \"Foo\" SET \"a\" = 1';\nEND $$;"
+ )
+ assert _keywords(tmp_path, sql) == ()
+
def test_execute_of_ddl_passes(self, tmp_path):
sql = "DO $$\nBEGIN\n EXECUTE 'ALTER TABLE \"Foo\" ADD COLUMN \"b\" TEXT';\nEND $$;"
assert _keywords(tmp_path, sql) == ()