From 7457531f88b3052894176ee9eb26faa37903fc77 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Mon, 9 Nov 2026 06:23:41 -0400 Subject: [PATCH] fix(rfm): unquote base table names read from a semantic view SYSTEM$READ_YAML_FROM_SEMANTIC_VIEW echoes base_table parts as written in the view DDL, so a quoted identifier arrived with its quotes and was quoted again by SnowTable, giving TableNotFoundError. Signed-off-by: Alex Castillo --- CHANGELOG.md | 3 ++ .../src/sdm_relational_engine/rfm/graph.py | 24 ++++++++++++-- .../tests/backend/snow/test_snow_graph.py | 32 +++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 14af03209..eabe84a65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -97,6 +97,9 @@ these packages. All five distributions share one version and one entry. ### Fixed +- `Graph.from_snowflake_semantic_view` reads a semantic view whose base table + is a quoted identifier. The view reports such a name with its quotes, which + were quoted a second time, so the table was reported as not found. - A predictive query longer than 1,024 characters is answered. A relational request carries the query in `metadata.source_query` for logging, which the contract caps at 1,024 characters, so a NIM refused a longer one with diff --git a/packages/structured-data-models-relational-engine/src/sdm_relational_engine/rfm/graph.py b/packages/structured-data-models-relational-engine/src/sdm_relational_engine/rfm/graph.py index 6a5fa7ed0..0a6613242 100644 --- a/packages/structured-data-models-relational-engine/src/sdm_relational_engine/rfm/graph.py +++ b/packages/structured-data-models-relational-engine/src/sdm_relational_engine/rfm/graph.py @@ -141,6 +141,22 @@ def _sub_outside_literals(pattern: re.Pattern[str], expr: str) -> str: return ''.join(out) +def _unquote_identifier(name: str) -> str: + r"""Return the name that a Snowflake identifier spells. + + ``SYSTEM$READ_YAML_FROM_SEMANTIC_VIEW`` echoes the parts of a base table as + they were written in the view's DDL, so a quoted identifier arrives with + its quotes and with any embedded quote doubled. Anything else is returned + unchanged. + + Args: + name: The identifier, as echoed. + """ + if len(name) >= 2 and name[0] == '"' and name[-1] == '"': + return name[1:-1].replace('""', '"') + return name + + def _qualifier_pattern(table_name: str) -> re.Pattern[str]: r"""Returns a pattern that matches a ``.`` qualifier within a SQL expression, ignoring case just like SQL identifier resolution does. @@ -2073,9 +2089,11 @@ def from_snowflake_semantic_view( table_names = {table_cfg['name'] for table_cfg in cfg['tables']} for table_cfg in cfg['tables']: table_name = table_cfg['name'] - source_table_name = table_cfg['base_table']['table'] - database = table_cfg['base_table']['database'] - schema = table_cfg['base_table']['schema'] + source_table_name = _unquote_identifier( + table_cfg['base_table']['table'] + ) + database = _unquote_identifier(table_cfg['base_table']['database']) + schema = _unquote_identifier(table_cfg['base_table']['schema']) primary_key: str | None = None if 'primary_key' in table_cfg: diff --git a/packages/structured-data-models-relational-engine/tests/backend/snow/test_snow_graph.py b/packages/structured-data-models-relational-engine/tests/backend/snow/test_snow_graph.py index 6d911dca0..7cee3b9d9 100644 --- a/packages/structured-data-models-relational-engine/tests/backend/snow/test_snow_graph.py +++ b/packages/structured-data-models-relational-engine/tests/backend/snow/test_snow_graph.py @@ -291,6 +291,38 @@ def test_from_snowflake_semantic_view_keeps_self_qualified_columns() -> None: assert "Failed to add column 'DC_REGION' of table 'RACKS'" in message +def test_from_snowflake_semantic_view_unquotes_base_table_names( + monkeypatch: pytest.MonkeyPatch, +) -> None: + # Regression test: `SYSTEM$READ_YAML_FROM_SEMANTIC_VIEW` echoes the parts + # of `base_table` as they were written in the view DDL, so a quoted + # identifier arrives with its quotes. They must be stripped before the + # name is quoted again, or the lookup is for a table named `""DB""`. + quoted = ( + _SEMANTIC_VIEW_YAML.replace('database: DB', 'database: \'"DB"\'') + .replace('schema: SCH', 'schema: \'"SCH"\'') + .replace(' table: DATACENTERS', ' table: \'"DATACENTERS"\'') + .replace(' table: RACKS', ' table: \'"RACKS"\'') + ) + monkeypatch.setattr(sys.modules[__name__], '_SEMANTIC_VIEW_YAML', quoted) + + graph = Graph.from_snowflake_semantic_view( + 'GPU_FLEET_SV', + connection=_FakeConnection(), + verbose=False, + warn=False, + ) + + assert graph['RACKS'].source_name == 'DB.SCH.RACKS' + assert [(e.src_table, e.fkey, e.dst_table) for e in graph.edges] == [ + ('RACKS', 'DATACENTER_ID', 'DATACENTERS') + ] + assert {column.name for column in graph['RACKS'].columns} >= { + 'RACK_ID', + 'HALL', + } + + def test_from_snowflake_case_insensitive_identifiers() -> None: # Regression test: # Snowflake folds unquoted identifiers to upper case, so a name typed in