Skip to content

Aggregate plan schema build error if aggr exprs contain cast and column #18975

@lewiszlw

Description

@lewiszlw

Describe the bug

Execute below sqls in datafusion-cli

create table t (a int);
select max(a) as a1, max(cast(a, bigint)) as a2 from t;

will get error: Schema error: Schema contains duplicate unqualified field name "max(t.a)". I tried in postgres and it won't fail.

I've found root cause. it's due to aggregate plan output schema building.

qualified_fields.extend(exprlist_to_fields(aggr_expr.as_slice(), &input)?);
let schema = DFSchema::new_with_metadata(
qualified_fields,
input.schema().metadata().clone(),
)?;
It will call SchemaDisplay code.
// Cast expr is not shown to be consistent with Postgres and Spark <https://github.com/apache/datafusion/pull/3222>
Expr::Cast(Cast { expr, .. }) | Expr::TryCast(TryCast { expr, .. }) => {
write!(f, "{}", SchemaDisplay(expr))
}
The schema name of cast expr is same with column expr.
But the comment says Cast expr is not shown to be consistent with Postgres and Spark. So I don't know what is the right fix.

To Reproduce

No response

Expected behavior

No response

Additional context

No response

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