You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Aggregates of x and CAST(x AS ...) in one query fail with "duplicate unqualified field name", even with aliases #26221
A query that aggregates both a column and a cast of the same column fails to plan:
Schema error: Schema contains duplicate unqualified field name "sum(t.x)"
Aliases on the aggregates do not help. It happens with any aggregate function and any target type, e.g. sum(x) and sum(CAST(x AS FLOAT)), or count(x) and count(CAST(x AS BIGINT)).
The schema name of CAST(expr AS type) is the schema name of expr (Expr::Cast and Expr::TryCast in SchemaDisplay, datafusion/expr/src/expr.rs, introduced in #3222 for consistency with Postgres and Spark). So sum(CAST(x AS FLOAT)) gets the same name sum(t.x) as sum(x) in the Aggregate node, before the user's aliases are applied in the projection above it.
To Reproduce
In datafusion-cli:
SELECTsum(x) AS a, sum(CAST(x AS FLOAT)) AS b FROM (VALUES (1.0), (2.0)) AS t(x);
-- Schema error: Schema contains duplicate unqualified field name "sum(t.x)"SELECTsum(x), sum(CAST(x AS FLOAT)) FROM (VALUES (1.0), (2.0)) AS t(x);
-- Error during planning: Projections require unique expression names but the expression "sum(t.x)" at position 0-- and "sum(CAST(t.x AS Float32))" at position 1 have the same name. Consider aliasing ("AS") one of them.SELECTcount(x) AS a, count(CAST(x ASBIGINT)) AS b FROM (VALUES (1.0), (2.0)) AS t(x);
-- Schema error: Schema contains duplicate unqualified field name "count(t.x)"
The second error message suggests aliasing, but aliasing leads to the first error.
Computing the cast in a subquery works:
SELECTsum(x) AS a, sum(y) AS b
FROM (SELECT x, CAST(x AS FLOAT) AS y FROM (VALUES (1.0), (2.0)) AS t(x));
-- 3.0 | 3.0
Expected behavior
Both aggregates are computed, since they are different expressions: 3.0 and 3.0 in the first query.
Additional context
Reproduced on main at commit 6663e43 (debug build of datafusion-cli).
Activity
fix(expr): differentiate aggregate functions with and without explici…
fix(expr): differentiate aggregate functions with and without explici…
fix(expr): differentiate aggregate functions with and without explici…
fix(expr): differentiate aggregate functions with and without explici…