Skip to content

Commit ddcdf29

Browse files
authored
Fix alias id when drop all aggregates (#135247)
When dropping all aggregates, we create a synthetic aggregate referring to the first grouping. However, we use the ID of the first aggregate instead of the first grouping, which can lead to a single attribute referring to multiple channels when building the layout.
1 parent e529fa7 commit ddcdf29

File tree

5 files changed

+26
-1
lines changed

5 files changed

+26
-1
lines changed

docs/changelog/135247.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 135247
2+
summary: Fix alias id when drop all aggregates
3+
area: ES|QL
4+
type: bug
5+
issues: []

x-pack/plugin/esql/qa/testFixtures/src/main/resources/eval.csv-spec

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,3 +788,20 @@ from firewall_logs,threat_list,airports
788788
dsjbDWegyVkg:boolean | name:integer | nhIWDevxEtU:null | iPIbTFddDK:integer | rUvqtgSl:integer
789789
false | 1 | null | 1 | 1
790790
;
791+
792+
evalAfterGroupingUsingSameName6
793+
required_capability: fix_alias_id_when_drop_all_aggregates
794+
from employees
795+
| stats sum=sum(salary) by last_name, year = bucket(birth_date, 1 year)
796+
| DROP sum
797+
| DROP last_name
798+
| eval year = 12
799+
| LIMIT 4
800+
;
801+
802+
year:integer
803+
12
804+
12
805+
12
806+
12
807+
;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/EsqlCapabilities.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1538,6 +1538,8 @@ public enum Cap {
15381538
*/
15391539
TS_COMMAND_V0(),
15401540

1541+
FIX_ALIAS_ID_WHEN_DROP_ALL_AGGREGATES
1542+
15411543
;
15421544

15431545
private final boolean enabled;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical/PruneColumns.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ private static LogicalPlan pruneColumnsInAggregate(Aggregate aggregate, Attribut
115115
// Aggs cannot produce pages with 0 columns, so retain one grouping.
116116
Attribute attribute = Expressions.attribute(aggregate.groupings().getFirst());
117117
NamedExpression firstAggregate = aggregate.aggregates().getFirst();
118-
remaining = List.of(new Alias(firstAggregate.source(), firstAggregate.name(), attribute, firstAggregate.id()));
118+
remaining = List.of(new Alias(firstAggregate.source(), firstAggregate.name(), attribute, attribute.id()));
119119
p = aggregate.with(aggregate.groupings(), remaining);
120120
}
121121
} else {

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/optimizer/LogicalPlanOptimizerTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ public void testEmptyProjectInStatWithGroupAndEval() {
291291

292292
assertThat(Expressions.names(agg.groupings()), contains("emp_no"));
293293
assertThat(Expressions.names(agg.aggregates()), contains("c"));
294+
assertThat(agg.aggregates().get(0).id(), equalTo(Expressions.attribute(agg.groupings().get(0)).id()));
294295

295296
var exprs = eval.fields();
296297
assertThat(exprs.size(), equalTo(1));

0 commit comments

Comments
 (0)