Skip to content

Aggregates of x and CAST(x AS ...) in one query fail with "duplicate unqualified field name", even with aliases #26221

Open

Description

@cblp

Describe the bug

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:

SELECT sum(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)"

SELECT sum(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.

SELECT count(x) AS a, count(CAST(x AS BIGINT)) 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:

SELECT sum(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).

Willingness to contribute

None

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions