Skip to content
Closed
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
30 changes: 27 additions & 3 deletions validation/tests/test_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1457,7 +1457,7 @@ def test_rejects_a_sql_statement_in_ossie_sql_2026() -> None:
("SHOW TABLES", "SNOWFLAKE", "SHOW"),
("UNDROP TABLE t", "SNOWFLAKE", "UNDROP"),
("PUT file://x @stage", "SNOWFLAKE", "PUT"),
("GET file://x @stage", "SNOWFLAKE", "GET"),
("GET @stage 'file:///tmp/x'", "SNOWFLAKE", "GET"),
("LIST @stage", "SNOWFLAKE", "LIST STAGE"),
("REFRESH TABLE t", "DATABRICKS", "REFRESH"),
("UNCACHE TABLE t", "DATABRICKS", "UNCACHE"),
Expand Down Expand Up @@ -4430,14 +4430,38 @@ def test_metric_check_reads_a_date_part_as_a_unit_not_a_column(expression) -> No


def test_metric_check_reads_the_redshift_pgdate_part_unit_as_a_unit() -> None:
# The redshift reader leaves PGDATE_PART untyped, so without an entry in
# DATE_PART_FUNCTIONS `day` would be read as an unqualified column.
# Up to sqlglot 30.33.0 the redshift reader leaves PGDATE_PART untyped, so
# without an entry in DATE_PART_FUNCTIONS `day` would be read as an
# unqualified column. From 30.34.0 it reads PGDATE_PART into an Extract
# and leaves `day` as a column in the unit position.
document = _renamed_model(metrics=[_metric("latest_day", "MAX(PGDATE_PART(day, orders.ordered))", "REDSHIFT")])
document["datasets"][0]["fields"].append(_field("ordered"))

assert _VALIDATE.validate_metric_references(document) == []


def test_metric_check_reads_the_postgres_date_part_unit_as_a_unit() -> None:
# From sqlglot 30.34.0 the postgres reader parses the unit of DATE_PART as
# an expression, so `day` arrives as a column in the unit position of an
# Extract. Earlier versions read it as a Var.
document = _renamed_model(metrics=[_metric("latest_day", "MAX(DATE_PART(day, orders.ordered))", "POSTGRES")])
document["datasets"][0]["fields"].append(_field("ordered"))

assert _VALIDATE.validate_metric_references(document) == []


def test_metric_check_reads_a_quoted_name_in_the_unit_position_of_extract_as_a_column() -> None:
document = _renamed_model(
metrics=[_metric("latest_day", 'MAX(DATE_PART("day", orders.ordered))', "SNOWFLAKE")]
)
document["datasets"][0]["fields"].append(_field("ordered"))

findings = _VALIDATE.validate_metric_references(document)

assert len(findings) == 1
assert "'day' is not qualified" in findings[0]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged with #784 this test fails on 30.29.0 and 30.34.0. #784 prints an identifier as written, so the finding reads '"day"' is not qualified. assert "is not qualified" in findings[0] passes with #784 and without it.



def test_metric_check_compares_names_without_regard_to_case() -> None:
document = _renamed_model(metrics=[
_metric("Revenue", "SUM(ORDERS.TOTAL_USD)"),
Expand Down
9 changes: 9 additions & 0 deletions validation/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,10 +1068,19 @@ def is_date_part(column, dialect: str) -> bool:
anonymous carry the unit as their first argument. DATE_TRUNC and its kin
never parse the unit as a column. Under a dialect sqlglot parses with its
own parser, a column in ``this`` is a column.

EXTRACT holds its unit in ``this`` and nothing else. From sqlglot 30.34.0
the Postgres and Redshift parsers read DATE_PART and PGDATE_PART into
EXTRACT and parse the unit as an expression, so a unit written as a bare
name stays a column there. Earlier versions turn it into a Var, or leave
PGDATE_PART anonymous. A quoted name there is an identifier and stays a
column.
"""
parent = column.parent
if column.table or not is_date_part_name(column.name):
return False
if isinstance(parent, exp.Extract):
return column.arg_key == "this" and not column.this.args.get("quoted")
if isinstance(parent, UNIT_SLOT_FUNCTIONS) and uses_default_parser(dialect):
unit = parent.args.get("unit")
unit_consumed = isinstance(unit, exp.Var) and is_date_part_name(unit.name)
Expand Down