fix(sql): normalize comment clauses with base dialect to keep #36113 fix

The comment-handling branch of sanitize_clause re-rendered through the
engine dialect, which re-applied the Postgres ROUND/CAST rewrite for any
clause containing a comment, reintroducing #36113. Re-render with the
base dialect instead so comments are normalized without engine-specific
semantic rewrites, and add a parametrized regression test for the
comment path.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Evan
2026-06-23 18:02:36 -07:00
parent 401d218992
commit 0228ff4f33
2 changed files with 31 additions and 4 deletions

View File

@@ -3238,6 +3238,29 @@ def test_sanitize_clause_preserves_aggregation_semantics(engine: str) -> None:
assert sanitized == clause
@pytest.mark.parametrize(
"engine",
["postgresql", "redshift", "cockroachdb", "netezza", "hana", "base", "mysql"],
)
def test_sanitize_clause_preserves_aggregation_semantics_with_comment(
engine: str,
) -> None:
"""
Regression test for https://github.com/apache/superset/issues/36113.
A clause that contains a comment takes the re-rendering branch of
``sanitize_clause``. That branch must normalize comments using the *base*
dialect rather than the engine dialect, so it must not re-apply the Postgres
``ROUND(AVG(x), n)`` -> ``ROUND(CAST(AVG(x) AS DECIMAL), n)`` rewrite that
truncates results on engines where ``DECIMAL`` defaults to scale 0.
"""
clause = "ROUND(AVG(col), 4) /* precise_count_distinct=true */"
sanitized = sanitize_clause(clause, engine)
assert "CAST" not in sanitized.upper(), (
f"sanitize_clause injected a cast for engine {engine!r}: {sanitized!r}"
)
@pytest.mark.parametrize(
"engine",
[