You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ossie.apache.org by jb...@apache.org on 2026/10/14 14:57:16 UTC

(ossie) branch main updated: fix(validation): report an expression that holds no statement instead of passing it (#583)

This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ossie.git


The following commit(s) were added to refs/heads/main by this push:
     new c5cb931d fix(validation): report an expression that holds no statement instead of passing it (#583)
c5cb931d is described below

commit c5cb931d72029dc523a5dd18d267f083403786f2
Author: Kyoung Min Kim <ka...@gmail.com>
AuthorDate: Wed Oct 14 23:57:10 2026 +0900

    fix(validation): report an expression that holds no statement instead of passing it (#583)
    
    An empty string, whitespace, a lone comment or stray semicolons parse to
    no statement at all. `parse_one` raises on them, and the `SELECT {expr}`
    retry then parsed a bare `SELECT`, so a field or metric with such an
    expression validated as correct. `validate_sql` also skipped an empty
    string entirely, rather than only a missing key.
    
    `validate_sql_expression` now reports `expression is empty` for those
    inputs, and `validate_sql` only skips an expression that is absent. The
    schema requires the key, so after schema validation every declared
    expression is checked.
    
    Generated-by: Claude Code
---
 validation/tests/test_validate.py | 49 +++++++++++++++++++++++++++++++++++++++
 validation/validate.py            | 27 +++++++++++++++++----
 2 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/validation/tests/test_validate.py b/validation/tests/test_validate.py
index b85d339a..44b570ad 100644
--- a/validation/tests/test_validate.py
+++ b/validation/tests/test_validate.py
@@ -416,6 +416,27 @@ def test_accepts_a_single_expression_containing_a_semicolon(expression: str) ->
     assert _VALIDATE.validate_sql_expression(expression, "ANSI_SQL", "ctx") is None
 
 
+@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed")
+@pytest.mark.parametrize(
+    "expression",
+    ["", "   ", "\n", ";", ";;", "-- comment only", "/* comment only */"],
+    ids=[
+        "empty",
+        "whitespace",
+        "newline",
+        "semicolon",
+        "semicolons",
+        "line-comment",
+        "block-comment",
+    ],
+)
+def test_rejects_an_expression_holding_no_statement(expression: str) -> None:
+    """Nothing to validate is an error, not a pass through `SELECT {expr}`."""
+    error = _VALIDATE.validate_sql_expression(expression, "ANSI_SQL", "ctx")
+
+    assert error == "[SQL] ctx: expression is empty"
+
+
 def _relationship(to_columns: list[str], to: str = "customers") -> dict:
     return {
         "name": "orders_to_customers",
@@ -637,6 +658,34 @@ def test_sql_error_in_metric_with_warning_in_name_is_an_error(run_validator):
     assert "[SQL] Metric 'Warning: broken_metric'" in output
 
 
+@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed")
+def test_empty_field_expression_fails_validation(run_validator):
+    dataset = {**_ORDERS, "fields": [{
+        "name": "blank",
+        "expression": {"dialects": [{"dialect": "ANSI_SQL", "expression": ""}]},
+    }]}
+    document = _document([dataset], [])
+
+    exit_code, output = run_validator(document)
+
+    assert exit_code == 1
+    assert "[SQL] Field 'orders.blank' in model 'm' (ANSI_SQL): expression is empty" in output
+
+
+@pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed")
+def test_comment_only_metric_expression_fails_validation(run_validator):
+    document = _document([_ORDERS], [])
+    document["metrics"] = [{
+        "name": "blank_total",
+        "expression": {"dialects": [{"dialect": "ANSI_SQL", "expression": "-- TODO"}]},
+    }]
+
+    exit_code, output = run_validator(document)
+
+    assert exit_code == 1
+    assert "[SQL] Metric 'blank_total' in model 'm' (ANSI_SQL): expression is empty" in output
+
+
 @pytest.mark.skipif(not _VALIDATE.SQLGLOT_AVAILABLE, reason="sqlglot is not installed")
 def test_field_expressed_only_in_ossie_sql_2026_validates(run_validator):
     dataset = {**_ORDERS, "fields": [{
diff --git a/validation/validate.py b/validation/validate.py
index b2543123..548bf127 100644
--- a/validation/validate.py
+++ b/validation/validate.py
@@ -391,6 +391,21 @@ def validate_sql_expression(expr: str, dialect: str, context: str) -> str | None
             )
         return None
 
+    def holds_no_statement(sql: str) -> bool:
+        """True when there is nothing to validate: an empty string, whitespace,
+        a lone comment or only semicolons.
+
+        Those parse to a list with no statement in it. `parse_one` raises on
+        them, and the `SELECT {expr}` retry below would then parse a bare
+        `SELECT` and report the expression as valid, so they are caught before
+        the retry. Parse failures return False; the retry reports those.
+        """
+        try:
+            statements = sqlglot.parse(sql, dialect=sqlglot_dialect)
+        except (ParseError, TokenError, RecursionError):
+            return False
+        return all(statement is None for statement in statements)
+
     try:
         # Try parsing as expression first (for field expressions like "column_name")
         sqlglot.parse_one(expr, dialect=sqlglot_dialect)
@@ -399,7 +414,8 @@ def validate_sql_expression(expr: str, dialect: str, context: str) -> str | None
         # SELECT below. RecursionError (deeply nested input) is included so the
         # retry reports it instead of crashing, while genuine errors such as a
         # non-string expr raising TypeError still surface.
-        pass
+        if holds_no_statement(expr):
+            return f"[SQL] {context}: expression is empty"
     else:
         return statement_count_error(expr)
 
@@ -439,8 +455,9 @@ def validate_sql(data: dict) -> list[str]:
             expression = field.get("expression", {})
             for dialect_expr in expression.get("dialects", []):
                 dialect = dialect_expr.get("dialect", "ANSI_SQL")
-                expr = dialect_expr.get("expression", "")
-                if expr:
+                # Only a missing expression is skipped; an empty one is reported.
+                expr = dialect_expr.get("expression")
+                if expr is not None:
                     context = f"Field '{dataset_name}.{field_name}' in model '{model_name}' ({dialect})"
                     error = validate_sql_expression(expr, dialect, context)
                     if error:
@@ -452,8 +469,8 @@ def validate_sql(data: dict) -> list[str]:
         expression = metric.get("expression", {})
         for dialect_expr in expression.get("dialects", []):
             dialect = dialect_expr.get("dialect", "ANSI_SQL")
-            expr = dialect_expr.get("expression", "")
-            if expr:
+            expr = dialect_expr.get("expression")
+            if expr is not None:
                 context = f"Metric '{metric_name}' in model '{model_name}' ({dialect})"
                 error = validate_sql_expression(expr, dialect, context)
                 if error: