Skip to content

Conversation

@GaneshPatil7517
Copy link
Contributor

Which issue does this PR close?

Closes #19685

Rationale for this change

When an aggregate expression has been aliased, the logical plan EXPLAIN shows both the alias and the original expression. However, the physical plan EXPLAIN only showed the alias name, making plans hard to interpret.

Before:
AggregateExec: mode=Single, gby=[], aggr=[agg]

AggregateExec: mode=Single, gby=[], aggr=[sum(column1@0) FILTER (WHERE column2@1 <= Int64(0)) as agg]

What changes are included in this PR?

  1. datafusion/core/src/physical_planner.rs: Modified create_aggregate_expr_and_maybe_filter() to use the unaliased expression for human_display, so it captures the actual aggregate expression instead of just the alias name.

  2. datafusion/physical-plan/src/aggregates/mod.rs: Modified DisplayAs impl for AggregateExec to show both the expression (human_display) and alias (name()) when they differ, in the format {expression} as {alias}.

  3. Test file updates: Updated test expectations in explain.slt, aggregate.slt, and agg_func_substitute.slt to reflect the new output format.

Are these changes tested?

Yes. Added new test cases in explain.slt that specifically verify aliased aggregate expressions are visible in physical EXPLAIN output. Updated existing test expectations in aggregate.slt and agg_func_substitute.slt.

Are there any user-facing changes?

Yes. The physical plan EXPLAIN output now shows the full aggregate expression along with its alias when they differ, improving plan readability and debuggability.

@github-actions github-actions bot added documentation Improvements or additions to documentation logical-expr Logical plan and expressions core Core DataFusion crate sqllogictest SQL Logic Tests (.slt) functions Changes to functions implementation physical-plan Changes to the physical-plan crate labels Jan 7, 2026
Copy link
Contributor

@crepererum crepererum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either I am misunderstanding the PR, or it is misnamed, or it accidentally includes a bunch of unrelated changes

let (name, human_display, e) = match e {
Expr::Alias(Alias { name, .. }) => {
let unaliased = e.clone().unalias_nested().data;
(Some(name.clone()), e.human_display().to_string(), unaliased)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems to be the core change that also matches the PR title

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are these changes included in this PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same: these changes don't seem to belong to this PR or I am missing the connection of the PR title to the reasoning.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

let a: Vec<String> = self
.aggr_expr
.iter()
.map(|agg| agg.name().to_string())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change makes sense

4 tag2 90 75 80 95
5 tag2 100 80 80 100

###########
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doesn't belong to this PR?

select percentile_cont(null, 0.5);
----
NULL

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also doesn't belong here?

[`create_udaf`]: https://docs.rs/datafusion/latest/datafusion/logical_expr/fn.create_udaf.html
[`advanced_udaf.rs`]: https://github.com/apache/datafusion/blob/main/datafusion-examples/examples/udf/advanced_udaf.rs

### Nullability of Aggregate Functions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Copy link
Contributor Author

@GaneshPatil7517 GaneshPatil7517 Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies for the confusion! I accidentally created the branch from the wrong base.

…pache#19685)

When an aggregate expression has been aliased, the logical plan EXPLAIN
shows both the alias and the original expression. However, the physical
plan EXPLAIN only showed the alias, making plans hard to interpret.

This fix updates the physical EXPLAIN output to show both the underlying
aggregate expression and its alias in the format:

  AggregateExec: mode=Single, gby=[], aggr=[sum(column1@0) as my_alias]

instead of:

  AggregateExec: mode=Single, gby=[], aggr=[my_alias]

Changes:
- Modified create_aggregate_expr_and_maybe_filter() in physical_planner.rs
  to use the unaliased expression for human_display, so it captures the
  actual aggregate expression instead of just the alias name.
- Modified DisplayAs impl for AggregateExec to show both expression and
  alias when they differ.
- Updated test expectations in explain.slt, aggregate.slt, and
  agg_func_substitute.slt to reflect the new output format.
@GaneshPatil7517 GaneshPatil7517 force-pushed the fix-explain-aggregate-alias-visibility branch from eeb1ede to 4ef18bd Compare January 9, 2026 16:21
@github-actions github-actions bot removed documentation Improvements or additions to documentation logical-expr Logical plan and expressions functions Changes to functions implementation labels Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Core DataFusion crate physical-plan Changes to the physical-plan crate sqllogictest SQL Logic Tests (.slt)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aliased aggregation expressions not visible in physical explain output

2 participants