Skip to content

[Superset 7.0 rc] SQL metric check blocks previously valid queries using groupby #46641

Backlog

Description

@MikolajBalcerek

SUMMARY

#42199 rejects a Custom SQL metric using a GROUP BY when has_aggregate() is False.

That is stricter than SQL and 6.1.0, and breaks charts that worked before, using legitimate SQL.

Examples

This blocks legit SQLs from being charted that worked on 6.1.0, like:

SELECT channel, COUNT(*) AS orders,
       'WIP' AS sessions          
FROM orders
GROUP BY channel

using constants as temporary values

SELECT franchise, this_week, last_week,
       (this_week - last_week) / last_week AS wow    -- metric
FROM top_10_weekly_franchise
GROUP BY franchise, this_week, last_week

or expressions over grouped columns

Charts like these worked in 6.x and break after upgrading.
There's no setting to turn the check off, and there's no automatic migration for the issue

Severity

Specific charts will be not working when migrating from previous releases.
On my instance, out of 3417 real users' charts, 26 were broken

Screenshots/recordings

image

Working on Superset 6.1.0

image

Broken on Superset 7.0 rc3

SELECT COALESCE(marketing_channel, 'Not Attributed')    AS marketing_channel,
       COALESCE(marketing_subchannel, 'Not Attributed') AS marketing_subchannel,
       'WIP'                                            AS "Sessions",      -- metric: 'WIP'
       'WIP'                                            AS "Session CVR",   -- metric: 'WIP'
       COUNT(DISTINCT user_id)                          AS "Users",
       'WIP'                                            AS "User CVR",      -- metric: 'WIP'
       COUNT(DISTINCT iid)                              AS "Units",
       SUM(revenue_ex_tax) / 100                        AS "Revenue",
       SUM(margin) / 100                                AS "Margin",
       CASE WHEN SUM(margin) <> 0 AND SUM(revenue_ex_tax) <> 0
            THEN SUM(margin) / SUM(revenue_ex_tax) ELSE 0 END           AS "Margin %",
       COUNT(DISTINCT order_id)                         AS "Orders",
       CASE WHEN COUNT(DISTINCT order_id) > 0
            THEN (SUM(revenue_ex_tax) / 100) / COUNT(DISTINCT order_id)
            ELSE 0 END                                  AS "AOV",
       COUNT(DISTINCT CASE WHEN is_new_customer IS TRUE THEN user_id ELSE NULL END) AS "New Customers",
       COUNT(DISTINCT iid)                              AS "Units %"
FROM marts.daily_orders
WHERE (NOT is_gift IS TRUE) AND (NOT is_redeem IS TRUE)
GROUP BY COALESCE(marketing_channel, 'Not Attributed'),
         COALESCE(marketing_subchannel, 'Not Attributed')
ORDER BY "Revenue" DESC
LIMIT 10000

example query used

Chart types:

  • table, aggregate mode
  • bubble chart (bubble_v2)

Stacktrace

Traceback (most recent call last):
  File "/app/superset/models/helpers.py", line 2333, in get_query_str_extended
    sqlaq = self.get_sqla_query(**cast(Any, filtered_query_obj))
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/superset/models/helpers.py", line 5267, in get_sqla_query
    raise QueryObjectValidationError(
superset.exceptions.QueryObjectValidationError: The Custom SQL metric "Sessions" is not an aggregate and can't be combined with a GROUP BY. Wrap it in an aggregate function, e.g. MAX('WIP').

Superset version

Superset 7.0 rc3 release candidate

Python version

3.11

Node version

16

Browser

Chrome

Additional context

No response

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    #bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions