Skip to content

Custom SQL in Metric Gets Modified Automatically After Save (Whitespace Removed in String Literal) #37626

@ramana-pb

Description

@ramana-pb

Bug description

Description

We are experiencing an issue in Apache Superset where custom SQL defined inside a metric is automatically modified after saving, causing incorrect results.

Specifically, whitespace inside string literals is removed, which changes the logic of the condition and leads to filters becoming inactive or returning incorrect values.

Example

Original metric SQL:

COUNT(
DISTINCT CASE
WHEN type = 'Internal'
AND "Days B/W A and S" = '1 Day (Same day)'
AND sourcetype = 'Cheque'
THEN CONCAT("ID", '-', title)
END
)

After saving the chart (or sometimes after editing/reopening), Superset automatically changes the string literal to:

'1 Day(Same day)'

Notice the space between Day and (Same day) is removed.

Actual Behavior

Superset alters the SQL inside the metric without user input.

Whitespace inside string values is removed.

As a result:

The condition no longer matches the actual column value.

Filters become inactive.

Metric values change unexpectedly.

Expected Behavior

Superset should preserve the custom SQL exactly as written, especially string literals.

No automatic formatting or modification should occur that changes query semantics.

Impact

Incorrect metric calculations

Inconsistent dashboard results

Hard-to-detect data issues after saving or reloading charts

Frequency

Occurs intermittently after saving or editing charts/metrics.

Workarounds Tried

Re-editing and re-saving the metric manually

Replacing the condition with alternative logic
(Neither provides a reliable long-term fix.)

Screenshots/recordings

No response

Superset version

master / latest-dev

Python version

3.9

Node version

16

Browser

Chrome

Additional context

Using Superset Version 4.1.1

Checklist

  • I have searched Superset docs and Slack and didn't find a solution to my problem.
  • I have searched the GitHub issue tracker and didn't find a similar bug report.
  • I have checked Superset's logs for errors and if I found a relevant Python stacktrace, I included it here as text in the "additional context" section.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions