Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``<table_name>.`` qualifier within a
SQL expression, ignoring case just like SQL identifier resolution does.
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down