From 1b35dff29c2372c7b5ec620a842f3322250b5064 Mon Sep 17 00:00:00 2001 From: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:15:06 -0600 Subject: [PATCH] fix: look up time column case-insensitively when dialect requires it With normalization_strategy=case_insensitive the model's time column identifier is normalized, while engine_adapter.columns() returns the engine's native casing, so the exact-match lookup in convert_to_time_column() could not find the column and sqlmesh plan failed with 'Time column not found'. Fall back to a case-insensitive match for dialects whose normalization strategy compares identifiers case-insensitively. Fixes #5710 Signed-off-by: draarivpatel-ui <287273626+draarivpatel-ui@users.noreply.github.com> --- sqlmesh/core/model/definition.py | 23 ++++++++++++++-- tests/core/test_model.py | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/sqlmesh/core/model/definition.py b/sqlmesh/core/model/definition.py index e8e122dece..896c0cd429 100644 --- a/sqlmesh/core/model/definition.py +++ b/sqlmesh/core/model/definition.py @@ -10,6 +10,7 @@ from pydantic import Field from sqlglot import exp +from sqlglot.dialects.dialect import NormalizationStrategy from sqlglot.helper import seq_get from sqlglot.optimizer.qualify_columns import quote_identifiers from sqlglot.optimizer.simplify import gen @@ -850,12 +851,30 @@ def convert_to_time_column( if columns_to_types is None: columns_to_types = self.columns_to_types_or_raise - if self.time_column.column.name not in columns_to_types: + time_column_name = self.time_column.column.name + if time_column_name not in columns_to_types: + # columns_to_types may come from engine_adapter.columns(), which returns + # the engine's native casing rather than normalized column names + dialect = d.Dialect.get_or_raise(self.dialect) + if dialect.normalization_strategy in ( + NormalizationStrategy.CASE_INSENSITIVE, + NormalizationStrategy.CASE_INSENSITIVE_UPPERCASE, + ): + time_column_name = next( + ( + column + for column in columns_to_types + if column.lower() == time_column_name.lower() + ), + time_column_name, + ) + + if time_column_name not in columns_to_types: raise ConfigError( f"Time column '{self.time_column.column.sql(dialect=self.dialect)}' not found in model '{self.name}'." ) - time_column_type = columns_to_types[self.time_column.column.name] + time_column_type = columns_to_types[time_column_name] return to_time_column( time, diff --git a/tests/core/test_model.py b/tests/core/test_model.py index 34bb5d1d1d..bb1ec0774f 100644 --- a/tests/core/test_model.py +++ b/tests/core/test_model.py @@ -2971,6 +2971,51 @@ def test_convert_to_time_column(): ) +def test_convert_to_time_column_case_insensitive_normalization(): + # engine_adapter.columns() returns the engine's native casing, e.g. UPPERCASE + # for Snowflake, while normalization_strategy=case_insensitive lowercases the + # model's time column identifier + expressions = d.parse( + """ + MODEL ( + name db.table, + kind INCREMENTAL_BY_TIME_RANGE( + time_column CreatedAtUtc + ), + dialect 'tsql,normalization_strategy=case_insensitive' + ); + + SELECT CreatedAtUtc::TIMESTAMP_NTZ + """ + ) + model = load_sql_based_model(expressions) + columns_to_types = {"CREATEDATUTC": exp.DataType.build("TIMESTAMP_NTZ")} + assert model.convert_to_time_column( + "2022-01-01", columns_to_types + ) == model.convert_to_time_column("2022-01-01") + + +def test_convert_to_time_column_case_sensitive_mismatch(): + # for dialects that compare identifiers case-sensitively, a differently cased + # column name must still fail + expressions = d.parse( + """ + MODEL ( + name db.table, + kind INCREMENTAL_BY_TIME_RANGE( + time_column ds + ) + ); + + SELECT ds::timestamp + """ + ) + model = load_sql_based_model(expressions) + columns_to_types = {"DS": exp.DataType.build("TIMESTAMP")} + with pytest.raises(ConfigError, match="not found in model"): + model.convert_to_time_column("2022-01-01", columns_to_types) + + def test_parse(assert_exp_eq): expressions = d.parse( """