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

rusackas pushed a commit to branch tdd/issue-30637-custom-sql-metric-quoting
in repository https://gitbox.apache.org/repos/asf/superset.git

commit 654cbb3952b892acd08ea46fc36ef9861fbb8524
Author: Claude Code <[email protected]>
AuthorDate: Sat Jul 11 11:12:17 2026 -0700

    test(sqla): assert simple metric quotes columns requiring quoting (#30637)
    
    Regression coverage for #30637: a SIMPLE adhoc metric that aggregates a
    column whose name requires identifier quoting must render the column with
    the dialect's quote characters. Exercises the metric compilation path
    (adhoc_metric_to_sqla -> TableColumn.get_sqla_col -> column()).
    
    Closes #30637
    
    Co-Authored-By: Claude Opus 4.8 <[email protected]>
---
 tests/unit_tests/models/helpers_test.py | 49 +++++++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)

diff --git a/tests/unit_tests/models/helpers_test.py 
b/tests/unit_tests/models/helpers_test.py
index 9603e4cd0f5..3c6f5f9cf2b 100644
--- a/tests/unit_tests/models/helpers_test.py
+++ b/tests/unit_tests/models/helpers_test.py
@@ -3410,3 +3410,52 @@ def test_get_sqla_query_dotted_struct_column_bigquery(
     # ```forecasts.original`.`total_cost``` (the regression), so this negative
     # assertion catches the actual failure mode, not just an exact-string 
match.
     assert "`forecasts.original`" not in sql
+
+
+def test_simple_metric_quotes_column_requiring_quoting(database: Database) -> 
None:
+    """
+    Regression for #30637: a SIMPLE adhoc metric that aggregates a column whose
+    name requires identifier quoting (e.g. it contains a space) must render the
+    column with the dialect's quote characters, otherwise the emitted SQL is
+    invalid for dialects that need quoting.
+
+    This exercises the metric compilation path named in the issue
+    (``adhoc_metric_to_sqla`` -> ``TableColumn.get_sqla_col`` -> ``column()``),
+    asserting the identifier is quoted rather than emitted bare.
+    """
+    from superset.connectors.sqla.models import SqlaTable, TableColumn
+    from superset.utils.core import AdhocMetricExpressionType
+
+    column_name = "Amount HT"
+    table = SqlaTable(
+        database=database,
+        schema=None,
+        table_name="test_table",
+        columns=[TableColumn(column_name=column_name, type="INTEGER")],
+    )
+    columns_by_name = {col.column_name: col for col in table.columns}
+
+    metric: AdhocMetric = {
+        "expressionType": AdhocMetricExpressionType.SIMPLE,
+        "aggregate": "SUM",
+        "column": {"column_name": column_name},
+        "label": "total",
+    }
+
+    sqla_metric = table.adhoc_metric_to_sqla(metric, columns_by_name)
+
+    with database.get_sqla_engine() as engine:
+        dialect = engine.dialect
+
+    rendered = str(
+        sqla_metric.compile(dialect=dialect, compile_kwargs={"literal_binds": 
True})
+    )
+
+    # The spaced identifier must be quoted (double quotes for the SQLite/ANSI
+    # dialect used here) and must never appear bare.
+    assert f'"{column_name}"' in rendered, (
+        f"Expected quoted identifier in rendered metric SQL, got: {rendered}"
+    )
+    assert f"SUM({column_name})" not in rendered, (
+        f"Column requiring quoting was emitted unquoted: {rendered}"
+    )

Reply via email to