Bug description
Affected Version: Apache Superset 6.1.0
Database: ClickHouse 25.3.1.2703 (official build)
Component: Chart SQL generation (virtual table queries)
Description
When rendering charts in Superset that use a virtual table as a data source, the generated SQL query causes a ClickHouse error (code 215). The issue arises due to a mismatch in how the GROUP BY clause references a column that is defined in the SELECT clause via a function.
Steps to Reproduce
- Create or edit a chart in Superset using a dataset based on a virtual table (SQL query).
- Configure the chart to group data by a date field truncated to the day level (e.g., using
dateTrunc('DAY', ...)).
- Execute the query to render the chart.
Actual Behaviour
Superset generates a SQL query where the GROUP BY clause repeats the full expression from the SELECT clause:
SELECT
dateTrunc('DAY', toDateTime("date")) AS "date",
...
FROM (
...
) AS "virtual_table"
WHERE
...
GROUP BY
dateTrunc('DAY', toDateTime("date"))
ORDER BY
...
LIMIT 10000
This query fails on ClickHouse with the following error:
HTTPDriver for http://ch-s21.cgorod.pw:8123/ received ClickHouse error code 215. Code: 215. DB::Exception: Column virtual_table.date is not under aggregate function and not in GROUP BY keys.
Expected Behaviour
ClickHouse expects the GROUP BY clause to reference the aliased column name ("date") instead of the full expression. The query should be generated as follows:
SELECT
dateTrunc('DAY', toDateTime("date")) AS "date",
...
FROM (
...
) AS "virtual_table"
WHERE
...
GROUP BY
"date"
ORDER BY
...
LIMIT 10000
With this syntax, the query executes successfully and the chart renders correctly.
Root Cause Analysis
The SQL query generator in Superset (version 6.1.0) does not properly handle column aliases in the GROUP BY clause when the data source is a virtual table. Instead of using the alias defined in the SELECT statement, it duplicates the original expression. While this approach might work in some databases, ClickHouse strictly requires that non‑aggregated columns in the SELECT list must be explicitly listed in the GROUP BY clause by their output name (alias).
Proposed Solution
Modify the SQL generation logic for virtual tables to:
- detect when a column in the
SELECT clause is an expression with an alias;
- ensure that the
GROUP BY clause uses the column alias (e.g., "date") rather than the full expression (dateTrunc('DAY', toDateTime("date"))).
This change would align the generated SQL with ClickHouse’s requirements and prevent error 215.
Additional Information
- Error Code: 215 (DB::Exception)
- ClickHouse Documentation Reference: The behaviour is consistent with ClickHouse documentation regarding
GROUP BY requirements.
- Workaround: Manually edit the SQL query in the chart’s datasource to replace the expression in
GROUP BY with the column alias.
Screenshots/recordings
No response
Superset version
master / latest-dev
Python version
I don't know
Node version
I don't know
Browser
Not applicable
Additional context
No response
Checklist
Bug description
Affected Version: Apache Superset 6.1.0
Database: ClickHouse 25.3.1.2703 (official build)
Component: Chart SQL generation (virtual table queries)
Description
When rendering charts in Superset that use a virtual table as a data source, the generated SQL query causes a ClickHouse error (code 215). The issue arises due to a mismatch in how the
GROUP BYclause references a column that is defined in theSELECTclause via a function.Steps to Reproduce
dateTrunc('DAY', ...)).Actual Behaviour
Superset generates a SQL query where the
GROUP BYclause repeats the full expression from theSELECTclause:This query fails on ClickHouse with the following error:
Expected Behaviour
ClickHouse expects the
GROUP BYclause to reference the aliased column name ("date") instead of the full expression. The query should be generated as follows:With this syntax, the query executes successfully and the chart renders correctly.
Root Cause Analysis
The SQL query generator in Superset (version 6.1.0) does not properly handle column aliases in the
GROUP BYclause when the data source is a virtual table. Instead of using the alias defined in theSELECTstatement, it duplicates the original expression. While this approach might work in some databases, ClickHouse strictly requires that non‑aggregated columns in theSELECTlist must be explicitly listed in theGROUP BYclause by their output name (alias).Proposed Solution
Modify the SQL generation logic for virtual tables to:
SELECTclause is an expression with an alias;GROUP BYclause uses the column alias (e.g.,"date") rather than the full expression (dateTrunc('DAY', toDateTime("date"))).This change would align the generated SQL with ClickHouse’s requirements and prevent error 215.
Additional Information
GROUP BYrequirements.GROUP BYwith the column alias.Screenshots/recordings
No response
Superset version
master / latest-dev
Python version
I don't know
Node version
I don't know
Browser
Not applicable
Additional context
No response
Checklist