Skip to content

Quote pushdown identifiers and stabilize MSSQL projection aliases - #18

Merged
timkpaine merged 1 commit into
mainfrom
feat/mssql-identifier-quoting
Aug 20, 2026
Merged

Quote pushdown identifiers and stabilize MSSQL projection aliases#18
timkpaine merged 1 commit into
mainfrom
feat/mssql-identifier-quoting

Conversation

@ptomecek

Copy link
Copy Markdown
Collaborator

When apply_polars_io_source_exprs wraps a source query in a subquery and adds the outer projection/predicate for column and predicate pushdown, unquoted identifiers could fail to resolve or bind to the wrong column — reserved words (e.g. group), mixed-case names, or names with special characters, depending on the target dialect.

Changes

  • Quote outer projection identifiers for dialects SQLGlot recognizes.
  • Quote the leaf identifiers of pushed-down predicate columns.
  • For MSSQL, stabilize the output-defining SELECT: quote explicit aliases and give simple columns an explicit quoted alias — following only the leftmost output branch of set operations / parenthesized queries, and leaving CTE bodies untouched.

New helpers: _is_known_dialect, _quote_column_leaf_identifier, _leftmost_output_select, _stabilize_mssql_projection_aliases.

apply_polars_io_source_exprs is shared by scan_db and scan_clickhouse, so both benefit.

Tests: quoting matrix across dialects (postgres/oracle/snowflake), metadata-name round-trips through duckdb, predicate-leaf quoting scope, and MSSQL stabilization edge cases (idempotency, CTE bodies, parenthesized set operations, projection order). Full test_lazy_sql_reader (108) and test_lazy_clickhouse_reader (10) pass; ruff clean.

apply_polars_io_source_exprs wraps the source query in a subquery and adds the
outer projection/predicate for column and predicate pushdown. When the source
projection used unquoted reserved words, mixed-case names, or names with special
characters, the generated outer references could fail to resolve or bind to the
wrong column depending on the target dialect.

- Quote outer projection identifiers for dialects SQLGlot recognizes.
- Quote the leaf identifiers of pushed-down predicate columns.
- For MSSQL, stabilize the output-defining SELECT: quote explicit aliases and
  give simple columns an explicit quoted alias, following only the leftmost
  output branch of set operations / parenthesized queries and leaving CTE
  bodies untouched.

Adds _is_known_dialect, _quote_column_leaf_identifier, _leftmost_output_select,
and _stabilize_mssql_projection_aliases, plus tests covering the quoting matrix
across dialects and the MSSQL stabilization edge cases.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: Pascal Tomecek <40371786+ptomecek@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

1 390 tests  +12   1 342 ✅ +12   39s ⏱️ -1s
    2 suites ± 0      48 💤 ± 0 
    2 files   ± 0       0 ❌ ± 0 

Results for commit 75409e6. ± Comparison against base commit 5071d08.

@timkpaine
timkpaine merged commit a4a16e5 into main Aug 20, 2026
5 checks passed
@timkpaine
timkpaine deleted the feat/mssql-identifier-quoting branch August 20, 2026 00:37
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.

2 participants