fix(core): preserve CTE names during schema prefixing in SQLDatabase.run_sql - #21028
Merged
Merged
Conversation
…run_sql
The naive str.replace("FROM ", ...) in run_sql() incorrectly schema-prefixes
CTE (Common Table Expression) names, breaking queries like:
WITH my_cte AS (SELECT * FROM t) SELECT * FROM my_cte
Replace with regex-based _add_schema_prefix() that extracts CTE names from
WITH clauses and skips them (and already-qualified identifiers) when prefixing.
Fixes run-llama#19889
…run_sql
The naive str.replace("FROM ", ...) in run_sql() incorrectly schema-prefixes
CTE (Common Table Expression) names, breaking queries like:
WITH my_cte AS (SELECT * FROM t) SELECT * FROM my_cte
Replace with regex-based _add_schema_prefix() that extracts CTE names from
WITH clauses and skips them (and already-qualified identifiers) when prefixing.
Fixes run-llama#19889
Contributor
Author
|
Hi @AstraBert thanks for your time on the review |
Contributor
|
Please do not ping maintainers as soon as you open a PR. We go through all PRs at our own pace |
AstraBert
approved these changes
Mar 17, 2026
claytonlin1110
pushed a commit
to claytonlin1110/llama_index
that referenced
this pull request
Mar 25, 2026
…run_sql (run-llama#21028) * fix(core): preserve CTE names during schema prefixing in SQLDatabase.run_sql The naive str.replace("FROM ", ...) in run_sql() incorrectly schema-prefixes CTE (Common Table Expression) names, breaking queries like: WITH my_cte AS (SELECT * FROM t) SELECT * FROM my_cte Replace with regex-based _add_schema_prefix() that extracts CTE names from WITH clauses and skips them (and already-qualified identifiers) when prefixing. Fixes run-llama#19889 * fix(core): preserve CTE names during schema prefixing in SQLDatabase.run_sql The naive str.replace("FROM ", ...) in run_sql() incorrectly schema-prefixes CTE (Common Table Expression) names, breaking queries like: WITH my_cte AS (SELECT * FROM t) SELECT * FROM my_cte Replace with regex-based _add_schema_prefix() that extracts CTE names from WITH clauses and skips them (and already-qualified identifiers) when prefixing. Fixes run-llama#19889
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fix naive
str.replace("FROM ", ...)inSQLDatabase.run_sql()that incorrectly schema-prefixes CTE (Common Table Expression) names, breaking queries like:Replace with regex-based
_add_schema_prefix()method that:WITH [RECURSIVE] name AS (clauses (including comma-separated CTEs)FROM/JOINtable referencesFixes #19889
Version Bump?
Type of Change
How Has This Been Tested?
10 new tests covering:
WITH a AS (...), b AS (...))WITH RECURSIVE)other_schema.table)LEFT JOIN)FROM (SELECT ...))All 16 tests pass (6 existing + 10 new). Pre-commit hooks, ruff, mypy all pass.
Suggested Checklist:
uv run make format; uv run make lintto appease the lint gods