Skip to content

fix(py): quote column names in SQLAlchemySource statistics queries - #332

Open
pcbeingused333 wants to merge 2 commits into
posit-dev:mainfrom
pcbeingused333:fixer3/fix/sqlalchemy-stats-quote
Open

pcbeingused333 wants to merge 2 commits into
posit-dev:mainfrom
pcbeingused333:fixer3/fix/sqlalchemy-stats-quote

Conversation

@pcbeingused333

Copy link
Copy Markdown

SQLAlchemySource builds its per-column statistics queries by interpolating column names unquoted: MIN(unit price) as unit price__min, COUNT(DISTINCT Group) ..., and the categorical-values UNION ALL query. For a column whose name has a space, capital letters, a reserved word or an apostrophe the query is invalid, and the broad except Exception then drops the statistics for the whole table: get_schema() lists the columns but no ranges and no categorical values, so the LLM gets less context with no warning. The DuckDB-backed sources already quote these names (quote_identifier).

What changed: the column names and the __min/__max/__nunique aliases are quoted with the engine's own identifier_preparer, which only quotes where the dialect needs it and so leaves the SQL for plain names unchanged. The column name used as a string literal in the categorical query has its single quotes doubled. table_name is left as it was.

Tests. A SQLite table with columns unit price, Group and it's is checked for its range and categorical values in get_schema(). The test fails on main and passes here, as do the other tests in test_datasource.py and test_deferred_datasource.py (45 passed). No LLM or database service was used.

This change was made with Claude Code.

🤖 Generated with Claude Code

pcbeingused333 and others added 2 commits October 21, 2026 02:57
The min/max/nunique and categorical-value queries interpolated column names
unquoted, so a name with a space, capitals, a reserved word or an apostrophe
made the query fail; the broad except then dropped the statistics for the
whole table. Quote with the engine's identifier preparer, as the DuckDB path
does with quote_identifier.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant