Skip to content

fix(table_diff): resolve on/skip_columns case-insensitively against schema - #6077

Draft
naikvaib wants to merge 1 commit into
SQLMesh:mainfrom
naikvaib:fix/table-diff-case-insensitive-key-columns
Draft

naikvaib wants to merge 1 commit into
SQLMesh:mainfrom
naikvaib:fix/table-diff-case-insensitive-key-columns

Conversation

@naikvaib

Copy link
Copy Markdown

Fixes #6067.

Note: I originally looked at #5970 (Fabric CI failing) but found it's already fully addressed by two open, unreviewed PRs from the issue's own reporter/assignee (#6041, #6042), so I didn't duplicate that work and picked a different open bug instead.

#6067: table_diff raises KeyError when -o/skip_columns names aren't lowercase on BigQuery/DuckDB. Root cause: key_columns normalizes -o names via sqlglot's dialect (lowercasing on BigQuery/DuckDB) then does an exact-match lookup against adapter.columns(), which preserves the table's real casing — skip_columns has the identical bug.

Added TableDiff._resolve_column_name(): returns the exact schema key if present, else falls back to a case-insensitive match. Applied to both the single- and multi-column key_columns paths, and converted skip_columns to a cached_property resolved the same way against both source and target schemas.

Added test_data_diff_on_columns_with_non_lowercase_names and test_data_diff_skip_columns_with_non_lowercase_names (DuckDB repro matching the issue).

Verified: pytest tests/core/test_table_diff.py — 18/18 passed (16 pre-existing + 2 new). ruff check/ruff format clean. mypy sqlmesh/core/table_diff.py — 0 errors in this file.

…chema

On engines whose sqlglot dialect lowercases unquoted identifiers (e.g.
BigQuery, DuckDB), TableDiff.key_columns normalized `on` column names via
normalize_identifiers() and then looked them up verbatim in the schema
returned by adapter.columns(), which preserves the table's actual column
casing. When the two disagreed, this raised a bare KeyError before any
query ran (table_diff.py:326), and in the single-key path caused a
similar KeyError deeper in _fetch_sample. `skip_columns` had the same
normalize-then-exact-match mismatch, so columns intended to be excluded
silently were not.

Add TableDiff._resolve_column_name() to fall back to a case-insensitive
match against the schema when there's no exact match, and use it when
building key_columns (for both the single- and multi-column paths) and
when resolving skip_columns. Add regression tests reproducing the
reported KeyError for both single- and multi-column `on` lists, plus a
case-insensitive skip_columns test.

Fixes SQLMesh#6067

Signed-off-by: Vaibhav Naik <naikvaib@amazon.com>
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.

table_diff: KeyError when -o key columns are not lower case on Bigquery

1 participant