Skip to content

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

Closed as duplicate

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

Activity

  1. added theissue type on Nov 28, 2025
  2. changed the title [-]Aggregate plan schema build error if aggr expr is cast[/-] [+]Aggregate plan schema build error if aggr exprs contain cast and column[/+] on Nov 28, 2025
  3. Jefffrey commented on Nov 28, 2025

    @Jefffrey
    Contributor

    Seems similar to #3353

    I looked into #3353 a bit before, haven't gotten around to typing up my investigation/thoughts yet (hopefully soon)

  4. Jefffrey commented on Dec 2, 2025

    @Jefffrey
    Contributor

    Seems similar to #3353

    I looked into #3353 a bit before, haven't gotten around to typing up my investigation/thoughts yet (hopefully soon)

    Put my notes here: #3353 (comment)

    However looking at it from the cast expr name would be valid too, since if we remove the aliases from the query, I think we'd still run into this problem.

  5. lewiszlw commented on Dec 3, 2025

    @lewiszlw
    MemberAuthor

    Thanks for looking into it. As this issue is duplicate, feel free to close.

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

    bugSomething isn't working

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions