diff --git a/validation/tests/test_validate.py b/validation/tests/test_validate.py index 94dd20c7..644b4b79 100644 --- a/validation/tests/test_validate.py +++ b/validation/tests/test_validate.py @@ -1255,6 +1255,50 @@ def test_rejects_a_sql_statement_in_ossie_sql_2026() -> None: assert error == "[SQL] ctx: expected an expression, found a DROP statement" +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize("dialect", ["ANSI_SQL", "OSSIE_SQL_2026", "SNOWFLAKE"]) +@pytest.mark.parametrize( + ("expression", "kind"), + [ + ("DROP TABLE orders; -- cleanup", "DROP"), + ("DELETE FROM orders; -- x", "DELETE"), + ("DROP TABLE orders; /* cleanup */", "DROP"), + ("TRUNCATE TABLE orders;\n-- cleanup", "TRUNCATE TABLE"), + ("GRANT SELECT ON orders TO public;; -- x", "GRANT"), + ], + ids=["drop", "delete", "block-comment", "comment-on-next-line", "two-semicolons"], +) +def test_rejects_a_statement_followed_by_a_comment( + expression: str, kind: str, dialect: str +) -> None: + """`parse_one` returns the statement and the comment's Semicolon in a Block. + + That is one statement to the count check, so the kind check has to read the + statement inside the Block. + """ + error = _VALIDATE.validate_sql_expression(expression, dialect, "ctx") + + assert error == f"[SQL] ctx: expected an expression, found a {kind} statement" + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize("dialect", ["ANSI_SQL", "OSSIE_SQL_2026"]) +@pytest.mark.parametrize( + "expression", + [ + "amount; -- note", + "SUM(amount); /* total */", + "commit; -- a column named commit", + "drop * 2; -- c", + "comment IS NOT NULL; -- c", + ], + ids=["column", "aggregate", "keyword-column", "first-token-quoted", "select-wrap"], +) +def test_accepts_an_expression_followed_by_a_comment(expression: str, dialect: str) -> None: + """A comment after the semicolon leaves one expression, keyword-named columns included.""" + assert _VALIDATE.validate_sql_expression(expression, dialect, "ctx") is None + + @pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") @pytest.mark.parametrize( ("expression", "dialect", "kind"), @@ -4300,3 +4344,250 @@ def test_invalid_sql_is_reported_under_postgres() -> None: @pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") def test_invalid_sql_is_reported_under_redshift() -> None: assert _is_error_not_warning(_VALIDATE.validate_sql(_unparseable_document("REDSHIFT"))) + + +# ── Portable expressions are expressions, not queries ────────────────────────── + + +def _metric_document(expression: str, dialect: str = "OSSIE_SQL_2026") -> dict: + document = _document([_ORDERS], []) + document["metrics"] = [_metric("m1", expression, dialect)] + return document + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize( + "expression, reported", + [ + ("(SELECT SUM(orders.customer_id) FROM orders)", "contains a subquery"), + ("SUM(orders.customer_id) + (SELECT 1)", "contains a subquery"), + ("SELECT SUM(orders.customer_id) FROM orders", "contains a FROM clause"), + ("SELECT SUM(orders.customer_id) WHERE orders.product_id = 1", "contains a WHERE clause"), + ("SELECT SUM(orders.customer_id) FROM orders GROUP BY orders.product_id", "contains a FROM clause"), + # Every clause on the root SELECT is named from the sqlglot argument that holds it (review of #698). + ("WITH t AS (SELECT 1) SELECT 1", "contains a WITH clause"), + ("SELECT SUM(orders.customer_id) OVER w WINDOW w AS (PARTITION BY orders.product_id)", "contains a WINDOW clause"), + ("SELECT SUM(orders.customer_id) LATERAL VIEW explode(orders.product_id) t AS b", "contains a LATERAL VIEW clause"), + ("SELECT SUM(orders.customer_id) INTO t", "contains an INTO clause"), + ("SELECT SUM(orders.customer_id) CLUSTER BY orders.product_id", "contains a CLUSTER BY clause"), + ("SELECT SUM(orders.customer_id) SORT BY orders.product_id", "contains a SORT BY clause"), + ("SELECT SUM(orders.customer_id) DISTRIBUTE BY orders.product_id", "contains a DISTRIBUTE BY clause"), + ("SELECT DISTINCT orders.customer_id", "contains a DISTINCT clause"), + ("SUM(orders.customer_id) UNION SELECT 1", "is a set operation (UNION)"), + ("SUM(orders.customer_id) INTERSECT SELECT 1", "is a set operation (INTERSECT)"), + ("SUM(orders.customer_id) EXCEPT SELECT 1", "is a set operation (EXCEPT)"), + ("orders.product_id IN (SELECT 1)", "contains a subquery"), + ("SELECT SUM(orders.customer_id) FOR UPDATE", "contains a locking clause"), + # One expression, so a second projection is a projection list. + ("SELECT SUM(orders.customer_id), SUM(orders.product_id)", "holds 2 expressions"), + ("SELECT SUM(orders.customer_id), SUM(orders.product_id) FROM orders", "contains a FROM clause"), + # A VALUES list is a query with no SELECT under it. + ("SUM(orders.customer_id) * (VALUES (2))", "contains a subquery"), + ("orders.product_id = ANY (VALUES 1)", "contains a subquery"), + ("EXISTS (VALUES (1))", "contains a subquery"), + ("VALUES (1)", "is a VALUES list"), + ], +) +def test_a_portable_expression_that_is_a_query_is_an_error(expression: str, reported: str) -> None: + result = _VALIDATE.validate_sql(_metric_document(expression)) + assert _is_error_not_warning(result) + assert len(result) == 1 + assert reported in result[0] + + +_EXPRESSIONS_BEHIND_A_SELECT = [ + "SELECT SUM(orders.customer_id)", + "SELECT orders.customer_id", + "SELECT orders.customer_id AS total", + "select sum(orders.customer_id)", + "SELECT SUM(orders.customer_id);", + "SELECT SUM(orders.customer_id); -- total", + "-- total\nSELECT SUM(orders.customer_id)", +] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize("expression", _EXPRESSIONS_BEHIND_A_SELECT) +def test_a_portable_expression_written_behind_a_select_is_rejected(expression: str) -> None: + """A SELECT with one projection and no clause is the tree the `SELECT {expr}` + wrapper produces, so the tree cannot say who wrote the SELECT. The parse can: + text that parses alone was not wrapped. expression_language.md lists SELECT + under Not Supported in Expressions.""" + result = _VALIDATE.validate_sql(_metric_document(expression, "OSSIE_SQL_2026")) + assert _is_error_not_warning(result) + assert len(result) == 1 + assert "is written as a SELECT" in result[0] + assert "Metric 'm1'" in result[0] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize("dialect", ["SNOWFLAKE", "ANSI_SQL"]) +@pytest.mark.parametrize("expression", _EXPRESSIONS_BEHIND_A_SELECT) +def test_an_expression_behind_a_select_in_another_dialect_is_not_shape_checked( + expression: str, dialect: str +) -> None: + assert _VALIDATE.validate_sql(_metric_document(expression, dialect)) == [] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize( + "expression", + ["SUM(orders.customer_id)", "comment IS NOT NULL", "drop * 2", "commit"], + ids=["parses-alone", "select-wrap", "first-token-quoted", "keyword-column"], +) +def test_a_portable_expression_the_validator_wraps_itself_still_passes(expression: str) -> None: + """The SELECT the validator adds to parse an expression is not the author's.""" + assert _VALIDATE.validate_sql_expression(expression, "OSSIE_SQL_2026", "ctx") is None + + +_QUERIES_WITHOUT_SELECT = [ + ("SUM(orders.customer_id) FROM orders", "contains a FROM clause"), + ("SUM(orders.customer_id) WHERE orders.product_id = 1", "contains a WHERE clause"), + ("SUM(orders.customer_id) FROM orders GROUP BY orders.product_id", "contains a FROM clause"), + ("SUM(orders.customer_id) OVER w WINDOW w AS (PARTITION BY orders.product_id)", "contains a WINDOW clause"), + ("SUM(orders.customer_id) LATERAL VIEW explode(orders.product_id) t AS b", "contains a LATERAL VIEW clause"), + ("SUM(orders.customer_id) INTO t", "contains an INTO clause"), + ("SUM(orders.customer_id) CLUSTER BY orders.product_id", "contains a CLUSTER BY clause"), + ("SUM(orders.customer_id) SORT BY orders.product_id", "contains a SORT BY clause"), + ("SUM(orders.customer_id) DISTRIBUTE BY orders.product_id", "contains a DISTRIBUTE BY clause"), + ("SUM(orders.customer_id) FOR UPDATE", "contains a locking clause"), + ("SUM(orders.customer_id), SUM(orders.product_id)", "holds 2 expressions"), + ("SUM(orders.customer_id), SUM(orders.product_id) FROM orders", "contains a FROM clause"), +] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize(("fragment", "reported"), _QUERIES_WITHOUT_SELECT) +def test_a_portable_query_written_without_its_select_is_named_too(fragment: str, reported: str) -> None: + """The text parses only once it is wrapped in SELECT and the result is more + than one bare projection, which the statement check from #647 rejects. In + the portable dialect that error says what made the text a query, as it does + for the same query written in full in the test above.""" + result = _VALIDATE.validate_sql(_metric_document(fragment, "OSSIE_SQL_2026")) + assert _is_error_not_warning(result) + assert len(result) == 1 + assert reported in result[0] + assert "found a statement" not in result[0] + assert "Metric 'm1'" in result[0] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize("fragment", [fragment for fragment, _ in _QUERIES_WITHOUT_SELECT]) +def test_a_query_written_without_its_select_stays_a_statement_in_another_dialect(fragment: str) -> None: + """The specification defines no expression language for ANSI_SQL, so #647's + statement error is left as it is there.""" + result = _VALIDATE.validate_sql(_metric_document(fragment, "ANSI_SQL")) + assert _is_error_not_warning(result) + assert len(result) == 1 + assert "expected an expression, found a statement" in result[0] + assert "Metric 'm1'" in result[0] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize( + "expression", + [ + "SUM(orders.customer_id) FILTER (WHERE orders.product_id = 1)", + "COUNT(*) FILTER (WHERE orders.product_id IN (1, 2))", + "COUNT(DISTINCT orders.customer_id) FILTER (WHERE orders.product_id IS NOT NULL)", + "SUM(orders.customer_id) OVER (PARTITION BY orders.product_id ORDER BY orders.variant_id)", + "CASE WHEN orders.product_id = 1 THEN orders.customer_id ELSE 0 END", + "CAST(SUM(orders.customer_id) AS DECIMAL) / NULLIF(COUNT(orders.product_id), 0)", + "orders.customer_id", + "SUM(orders.customer_id) OVER (ORDER BY orders.variant_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)", + "PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY orders.customer_id)", + "STRING_AGG(CAST(orders.customer_id AS VARCHAR), ',' ORDER BY orders.customer_id)", + "ARRAY_AGG(orders.customer_id ORDER BY orders.variant_id)", + "orders.product_id IN (1, 2)", + "COALESCE(orders.customer_id, orders.product_id)", + # A comment after the last semicolon is not a second statement. + "SUM(orders.customer_id); -- the order count", + "SUM(orders.customer_id) /* orders */;", + ], +) +def test_filters_windows_and_scalars_are_expressions(expression: str) -> None: + assert _VALIDATE.validate_sql(_metric_document(expression)) == [] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize("dialect", ["SNOWFLAKE", "ANSI_SQL"]) +def test_a_query_in_another_dialect_is_not_shape_checked(dialect: str) -> None: + # expression_language.md governs OSSIE_SQL_2026 alone; every other dialect is opaque + # to Ossie (extensions/dialects.md), ANSI_SQL included. + assert _VALIDATE.validate_sql(_metric_document("(SELECT SUM(customer_id) FROM orders)", dialect)) == [] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +def test_only_semicolons_and_a_comment_is_an_empty_expression() -> None: + result = _VALIDATE.validate_sql(_metric_document("; -- nothing here")) + assert _is_error_not_warning(result) + assert "expression is empty" in result[0] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +def test_a_field_expression_is_shape_checked_too() -> None: + document = _document([_ORDERS], []) + document["datasets"][0]["fields"].append(_field("total", "(SELECT 1)", "OSSIE_SQL_2026")) + result = _VALIDATE.validate_sql(document) + assert _is_error_not_warning(result) + assert "Field 'orders.total'" in result[0] + + +# ── A dataset source written as a query parses ───────────────────────────────── + + +def _source_document(source: str) -> dict: + return _document([{**_ORDERS, "source": source}], []) + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +@pytest.mark.parametrize( + "source", + [ + "db.s.orders", + "SELECT customer_id, product_id, variant_id FROM db.s.orders WHERE product_id IS NOT NULL", + "(SELECT customer_id, product_id, variant_id FROM db.s.orders)", + "WITH o AS (SELECT * FROM db.s.orders) SELECT * FROM o", + "-- the orders with a product\nSELECT customer_id FROM db.s.orders WHERE product_id IS NOT NULL", + "/* orders */ SELECT customer_id FROM db.s.orders", + "((SELECT customer_id FROM db.s.orders))", + # A comment after the last semicolon is not a second statement. + "SELECT customer_id FROM db.s.orders;\n-- end of query", + "SELECT customer_id FROM db.s.orders; /* end */", + # Not read as queries, so not parsed. + "VALUES (1), (2)", + "TABLE db.s.orders", + ], +) +def test_a_table_reference_or_a_query_that_parses_is_accepted(source: str) -> None: + assert _VALIDATE.validate_dataset_sources(_source_document(source)) == [] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +def test_a_query_source_that_does_not_parse_is_a_warning() -> None: + result = _VALIDATE.validate_dataset_sources(_source_document("SELECT customer_id FROM WHERE (((")) + assert len(result) == 1 + assert isinstance(result[0], _VALIDATE.ValidationWarning) + assert "Dataset 'orders'" in result[0] + assert "source query does not parse" in result[0] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +def test_a_query_source_behind_a_leading_comment_is_still_checked() -> None: + result = _VALIDATE.validate_dataset_sources(_source_document("-- note\nSELECT customer_id FROM WHERE (((")) + assert len(result) == 1 + assert "source query does not parse" in result[0] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +def test_a_source_holding_two_statements_is_a_warning() -> None: + result = _VALIDATE.validate_dataset_sources(_source_document("SELECT 1; SELECT 2")) + assert len(result) == 1 + assert "more than one statement" in result[0] + + +@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed") +def test_dataset_sources_are_checked_by_validate_document(core_schema: dict) -> None: + document = _source_document("SELECT customer_id FROM WHERE (((") + result = _VALIDATE.validate_document(document, core_schema) + assert any("source query does not parse" in item for item in result) diff --git a/validation/validate.py b/validation/validate.py index a6f5b41a..7b43824b 100644 --- a/validation/validate.py +++ b/validation/validate.py @@ -39,6 +39,12 @@ 4. Relationship column arity (from_columns and to_columns lengths match) 5. SQL syntax (using sqlglot) 6. Metric dialect agreement and expression purity (warnings; GitHub issue #279) +7. Portable expressions are expressions: an OSSIE_SQL_2026 field or metric + expression that is a query (a subquery, a set operation, a VALUES list, a + projection list, or a FROM, WHERE or other clause) is an error + (expression_language.md, Not Supported in Expressions) +8. A dataset `source` written as a query parses as SQL (a warning when it does not, + since a source declares no dialect) Ontology documents (--schema ontology/ontology.json) are checked for unique concept and relationship names, valid namespace-prefix IRIs, and for extends, @@ -325,11 +331,7 @@ def _expression_with_first_token_quoted(sql: str, dialect: object) -> object | N quoted = exp.to_identifier(text, quoted=True).sql(dialect=dialect) rewritten = sql[: first.start] + quoted + sql[first.end + 1 :] try: - statements = [ - statement - for statement in sqlglot.parse(rewritten, dialect=dialect) - if statement is not None - ] + statements = _statements(sqlglot.parse(rewritten, dialect=dialect)) except (ParseError, TokenError, RecursionError): return None if len(statements) != 1: @@ -1242,6 +1244,35 @@ def passes_through(tree, name: str) -> bool: return warnings +def _statements(parsed: list) -> list: + """The statements in a `sqlglot.parse` result. + + `sqlglot.parse` returns one entry per statement, None for an empty one (a + stray semicolon) and a `Semicolon` node for a comment that follows the last + semicolon. Neither is a statement, so `amount;`, `amount;;` and + `amount; -- note` each hold one, and `; -- note` holds none. A semicolon + inside a string literal or a comment does not split. This is the one copy of + the rule: the expression check and the dataset source check both count with + it. + """ + return [s for s in parsed if s is not None and not isinstance(s, exp.Semicolon)] + + +def _sole_statement(tree: object) -> object: + """The statement a `parse_one` result holds when a comment follows it. + + `parse_one` returns `DROP TABLE t; -- note` as a `Block` of the `Drop` and + the comment's `Semicolon`. That is one statement to `_statements`, so the + count check passes it, and the checks after it have to read the `Drop`, not + the `Block` around it. Any other tree is returned as it is. + """ + if isinstance(tree, exp.Block): + statements = _statements(tree.expressions) + if len(statements) == 1: + return statements[0] + return tree + + def _parse_sql_expression( expr: str, dialect: str, context: str ) -> tuple[object | None, str | None]: @@ -1271,17 +1302,13 @@ def statement_count_error(sql: str) -> str | None: `parse_one` accepts a statement list and returns one `Block` spanning it, so `amount; DROP TABLE t` parses and would otherwise validate as an - expression. A semicolon inside a string literal or comment does not - split, so this only rejects a real statement list. Stray semicolons parse - as empty statements and are not counted, so `amount;` and `amount;;` are - both a single expression. Parse failures return None; the checks below - report those. + expression. `_statements` says what counts as a statement. Parse failures + return None; the checks below report those. """ try: - statements = sqlglot.parse(sql, dialect=sqlglot_dialect) + count = len(_statements(sqlglot.parse(sql, dialect=sqlglot_dialect))) except (ParseError, TokenError, RecursionError): return None - count = len([statement for statement in statements if statement is not None]) if count > 1: return ( f"[SQL] {context}: expected a single expression but found " @@ -1299,10 +1326,9 @@ def holds_no_statement(sql: str) -> bool: the retry. Parse failures return False; the retry reports those. """ try: - statements = sqlglot.parse(sql, dialect=sqlglot_dialect) + return not _statements(sqlglot.parse(sql, dialect=sqlglot_dialect)) except (ParseError, TokenError, RecursionError): return False - return all(statement is None for statement in statements) def statement_kind_error(node: object) -> str | None: """Reject a single parseable unit that is a statement, not an expression. @@ -1335,7 +1361,7 @@ def wrapped_select() -> tuple[object | None, str | None]: count_error = statement_count_error(f"SELECT {expr}") if count_error: return None, count_error - return wrapped, None + return _sole_statement(wrapped), None try: # Try parsing as expression first (for field expressions like "column_name") @@ -1351,6 +1377,8 @@ def wrapped_select() -> tuple[object | None, str | None]: count_error = statement_count_error(expr) if count_error: return None, count_error + # One statement followed by a comment is a Block; check the statement. + tree = _sole_statement(tree) kind_error = statement_kind_error(tree) if kind_error: ident = _single_identifier_text(expr, sqlglot_dialect) @@ -1365,6 +1393,16 @@ def wrapped_select() -> tuple[object | None, str | None]: if quoted is not None: return quoted, None return None, kind_error + if dialect == PORTABLE_DIALECT and isinstance(tree, exp.Select): + # The text parsed alone, so the author wrote this SELECT: the + # wrapper below was not used. Name a clause if it has one; a + # SELECT with a single projection and no clause is still a + # SELECT (expression_language.md, Not Supported in Expressions). + return None, query_shape_error(tree, context) or ( + f"[SQL] {context}: is written as a SELECT; an expression is an " + f"aggregate or scalar expression over fields, not a query " + f"(expression_language.md, Not Supported in Expressions)" + ) return tree, None wrapped, wrap_error = wrapped_select() @@ -1372,6 +1410,12 @@ def wrapped_select() -> tuple[object | None, str | None]: return None, wrap_error if wrapped is not None and _is_bare_projection(wrapped): return wrapped, None + if wrapped is not None and dialect == PORTABLE_DIALECT: + # The portable dialect has a defined expression language, so say what + # made the text a query (`SUM(x) FROM t` contains a FROM clause). + shape_error = query_shape_error(wrapped, context) + if shape_error: + return None, shape_error return None, f"[SQL] {context}: expected an expression, found a statement" @@ -1380,12 +1424,153 @@ def validate_sql_expression(expr: str, dialect: str, context: str) -> str | None A field or metric holds one expression (spec.md Fields / Metrics), not a statement. Multi-statement strings and DDL/DML/session statements are - rejected. Top-level SELECT is not rejected here. + rejected for every dialect sqlglot parses. A query (a subquery, a set + operation, VALUES, a clause on the root SELECT or a projection list) is + rejected in OSSIE_SQL_2026 only, with or without its SELECT, and the error + names what made it one. So is an expression written behind its own SELECT + (`SELECT SUM(x)`). The other dialects are opaque to the specification, + where only a query written without its SELECT is reported, as a statement. """ - _tree, error = _parse_sql_expression(expr, dialect, context) + tree, error = _parse_sql_expression(expr, dialect, context) + if error is None and tree is not None and dialect == PORTABLE_DIALECT: + return query_shape_error(tree, context) return error +# How a clause is named in the error, where the name of the sqlglot argument +# that holds it does not read as the clause. sqlglot renamed `from` to `from_` +# and `with` to `with_` from version 28 on; both spellings are listed. Any other +# argument is named from the argument itself (`into` is INTO, `having` is HAVING). +_CLAUSE_NAMES = { + "with": "WITH", "with_": "WITH", "from": "FROM", "from_": "FROM", "joins": "JOIN", + "group": "GROUP BY", "order": "ORDER BY", "distinct": "DISTINCT", "windows": "WINDOW", + "laterals": "LATERAL VIEW", "cluster": "CLUSTER BY", "sort": "SORT BY", + "distribute": "DISTRIBUTE BY", "hint": "hint", "locks": "locking", +} + + +def _clause_name(arg: str) -> str: + return _CLAUSE_NAMES.get(arg, arg.rstrip("_").replace("_", " ").upper()) + + +def query_shape_error(tree, context: str) -> str | None: + """The reason a parsed portable expression is a query rather than an + expression, or None when it is an expression. + + expression_language.md lists subqueries and the standalone WHERE clause under + Not Supported in Expressions: a field or metric expression is one aggregate or + scalar expression over fields, and the consuming query supplies the FROM, + WHERE and GROUP BY. A root SELECT reaches this check two ways. One is the + wrapper _parse_sql_expression puts around an expression that does not parse + alone: a wrapped result that is one bare projection is an expression, and any + other (`SUM(x) FROM t`, `SUM(x), SUM(y)`) is passed here by the statement + check so the error names what made it a query. The other is a query the text + spells out, `SELECT SUM(x) FROM t` or `WITH t AS (...) SELECT 1`, which parses + alone and is not a statement to that check. Either way an argument on the root + SELECT other than the projections (`expressions`) is a clause of a query, and + more than one projection is a projection list. Naming every argument rather + than listing clauses keeps the check independent of sqlglot's argument names. + A root that is a set operation (`SUM(x) UNION SELECT 1`) or a VALUES list is a + query too, and any query below the root is a subquery: a SELECT, or a VALUES + list, which sqlglot parses with no SELECT under it (`x = ANY (VALUES 1)`). + `FILTER (WHERE ...)` on an aggregate is a modifier the language requires + (exp.Filter), not a WHERE clause, and a window frame (exp.Window) is likewise + part of the expression; neither sets an argument on the root. + + A bare `SELECT SUM(x)` parses to the same tree as the wrapper around an + expression that needed one, so it is not told apart here. The parse knows + which of the two it was: _parse_sql_expression reports a SELECT the text + spells out and hands this check a wrapped tree only. + """ + set_operation = getattr(exp, "SetOperation", exp.Union) + if isinstance(tree, set_operation): + return ( + f"[SQL] {context}: is a set operation ({tree.key.upper()}); an expression is an " + f"aggregate or scalar expression over fields, not a query " + f"(expression_language.md, Not Supported in Expressions)" + ) + if isinstance(tree, exp.Values): + return ( + f"[SQL] {context}: is a VALUES list; an expression is an aggregate or scalar " + f"expression over fields, not a query (expression_language.md, Not Supported " + f"in Expressions)" + ) + if isinstance(tree, exp.Select): + for arg, value in tree.args.items(): + if value and arg != "expressions": + clause = _clause_name(arg) + article = "an" if clause[0] in "AEIOU" else "a" + return ( + f"[SQL] {context}: contains {article} {clause} clause; an expression is an " + f"aggregate or scalar expression over fields, not a query " + f"(expression_language.md, Not Supported in Expressions)" + ) + if len(tree.expressions) != 1: + return ( + f"[SQL] {context}: holds {len(tree.expressions)} expressions; an expression is " + f"one aggregate or scalar expression over fields, not a projection list " + f"(expression_language.md, Not Supported in Expressions)" + ) + inner = list(tree.expressions) + else: + inner = [tree] + for node in inner: + if node is not None and node.find(exp.Query, exp.Values) is not None: + return ( + f"[SQL] {context}: contains a subquery; the expression language does " + f"not support subqueries (expression_language.md, Not Supported in " + f"Expressions)" + ) + return None + + +# A source that is a query rather than a table reference: after any leading +# comments it starts with SELECT or WITH, possibly in parentheses. +_LEADING_COMMENTS = re.compile(r"^(?:\s*(?:--[^\n]*(?:\n|$)|/\*.*?\*/))*", re.DOTALL) +_QUERY_SOURCE = re.compile(r"^\s*(?:\(\s*)*(SELECT|WITH)\b", re.IGNORECASE) + + +def _is_query_source(source: str) -> bool: + return _QUERY_SOURCE.match(_LEADING_COMMENTS.sub("", source, count=1)) is not None + + +def validate_dataset_sources(data: dict) -> list[str]: + """Warn when a dataset `source` written as a query does not parse as SQL. + + `Dataset.source` is a table reference (database.schema.table) or a query + (ossie-schema.json). A source is read as a query when, after any leading + comments and parentheses, it starts with SELECT or WITH; anything else, + a table reference and a `VALUES (...)` or `TABLE t` source alike, is not + parsed. A query is read with sqlglot's default reader because a source + declares no dialect, so a query in a vendor dialect the default reader cannot + read is reported as a warning rather than an error. + """ + if not SQLGLOT_AVAILABLE or not isinstance(data, dict) or "datasets" not in data: + return [] + model_name = data.get("name", "") + warnings = [] + for dataset in as_list(data.get("datasets")): + if not isinstance(dataset, dict): + continue + source = dataset.get("source") + if not isinstance(source, str) or not _is_query_source(source): + continue + context = f"Dataset '{dataset.get('name', '')}' in model '{model_name}'" + try: + statements = sqlglot.parse(source) + except (ParseError, TokenError, RecursionError) as e: + warnings.append(ValidationWarning( + f"[SQL] Warning: {context}: source query does not parse: " + f"{str(e).split(chr(10))[0]}" + )) + continue + if len(_statements(statements)) != 1: + warnings.append(ValidationWarning( + f"[SQL] Warning: {context}: source query holds more than one statement" + )) + return warnings + + def parse_tree(expr: str, dialect: str, context: str): """The parsed tree of one expression, or None when it does not parse: for the checks that read the columns and leave the error to the SQL check.""" @@ -2069,6 +2254,7 @@ def validate_semantic_model(data: dict) -> list[str]: errors.extend(validate_agg_placement(data)) errors.extend(validate_sql(data)) errors.extend(validate_metric_agreement(data)) + errors.extend(validate_dataset_sources(data)) return errors