Skip to content

Commit 787143d

Browse files
committed
iter
1 parent 94a16b6 commit 787143d

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/aggregate/Count.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public Nullability nullable() {
146146
public boolean isWildcard() {
147147
Expression field = field();
148148
if (field instanceof Literal lit) {
149-
return StringUtils.WILDCARD.equals(lit.value());
149+
return StringUtils.WILDCARD.equals(lit.toString());
150150
}
151151
return false;
152152
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.esql.optimizer.rules.logical;
99

1010
import org.elasticsearch.xpack.esql.core.expression.AttributeSet;
11+
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
1112
import org.elasticsearch.xpack.esql.core.util.Holder;
1213
import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
1314
import org.elasticsearch.xpack.esql.plan.logical.EsRelation;
@@ -24,13 +25,13 @@
2425

2526
import java.util.ArrayList;
2627
import java.util.List;
28+
import java.util.Set;
2729
import java.util.stream.Collectors;
2830

2931
import static org.elasticsearch.xpack.esql.optimizer.rules.logical.PruneColumns.pruneColumnsInAggregate;
3032
import static org.elasticsearch.xpack.esql.optimizer.rules.logical.PruneColumns.pruneColumnsInEsRelation;
3133
import static org.elasticsearch.xpack.esql.optimizer.rules.logical.PruneColumns.pruneColumnsInEval;
3234
import static org.elasticsearch.xpack.esql.optimizer.rules.logical.PruneColumns.pruneColumnsInInlineJoinRight;
33-
import static org.elasticsearch.xpack.esql.optimizer.rules.logical.PruneColumns.pruneColumnsInProject;
3435

3536
/**
3637
* This is used to prune unused columns and expressions in each branch of a Fork.
@@ -65,12 +66,12 @@ public LogicalPlan apply(LogicalPlan plan) {
6566

6667
used.addAll(p.output());
6768
forkFound.set(true);
68-
69+
var forkOutputNames = p.output().stream().map(NamedExpression::name).collect(Collectors.toSet());
6970
boolean changed = false;
7071
List<LogicalPlan> newChildren = new ArrayList<>();
7172
for (var child : p.children()) {
7273
var clonedUsed = AttributeSet.forkBuilder().addAll(used);
73-
var newChild = pruneSubPlan(child, clonedUsed);
74+
var newChild = pruneSubPlan(child, clonedUsed, forkOutputNames);
7475
newChildren.add(newChild);
7576
if (false == newChild.equals(child)) {
7677
changed = true;
@@ -84,7 +85,7 @@ public LogicalPlan apply(LogicalPlan plan) {
8485
});
8586
}
8687

87-
private static LogicalPlan pruneSubPlan(LogicalPlan plan, AttributeSet.Builder usedAttrs) {
88+
private static LogicalPlan pruneSubPlan(LogicalPlan plan, AttributeSet.Builder usedAttrs, Set<String> forkOutput) {
8889
if (plan instanceof LocalRelation localRelation) {
8990
var outputAttrs = localRelation.output().stream().filter(usedAttrs::contains).collect(Collectors.toList());
9091
return new LocalRelation(localRelation.source(), outputAttrs, localRelation.supplier());
@@ -111,7 +112,8 @@ private static LogicalPlan pruneSubPlan(LogicalPlan plan, AttributeSet.Builder u
111112
yield p;
112113
} else {
113114
projectHolder.set(true);
114-
yield pruneColumnsInProject(project, usedAttrs);
115+
var prunedAttrs = project.projections().stream().filter(x -> forkOutput.contains(x.name())).toList();
116+
yield new Project(project.source(), project.child(), prunedAttrs);
115117
}
116118
}
117119
case EsRelation esr -> pruneColumnsInEsRelation(esr, usedAttrs);

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.ToString;
7171
import org.elasticsearch.xpack.esql.expression.function.scalar.internal.PackDimension;
7272
import org.elasticsearch.xpack.esql.expression.function.scalar.internal.UnpackDimension;
73-
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Exp;
7473
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Round;
7574
import org.elasticsearch.xpack.esql.expression.function.scalar.multivalue.MvAvg;
7675
import org.elasticsearch.xpack.esql.expression.function.scalar.multivalue.MvCount;
@@ -9643,14 +9642,18 @@ public void testPruneColumnsInForkBranchesDropNestedEvalsIfNotNeeded() {
96439642
var filter = as(limitInBranch.child(), Filter.class);
96449643
assertThat(filter.toString(), containsString("IN(10048[INTEGER],10081[INTEGER],emp_no"));
96459644
var relation = as(filter.child(), EsRelation.class);
9646-
assertThat(relation.output().stream().map(Attribute::name).collect(Collectors.toSet()), hasItems("emp_no", "first_name", "last_name"));
9645+
assertThat(
9646+
relation.output().stream().map(Attribute::name).collect(Collectors.toSet()),
9647+
hasItems("emp_no", "first_name", "last_name")
9648+
);
96479649
}
96489650
}
96499651

96509652
/**
96519653
* Limit[10000[INTEGER],false,false]
96529654
* \_Aggregate[[],[COUNT(*[KEYWORD],true[BOOLEAN],PT0S[TIME_DURATION]) AS count(*)#36, COUNT(emp_no{r}#109,true[BOOLEAN],PT0S[
9653-
* TIME_DURATION]) AS d#39, MAX(_fork{r}#124,true[BOOLEAN],PT0S[TIME_DURATION]) AS m#42, COUNT(s{r}#119,true[BOOLEAN],PT0S[TIME_DURATION]) AS ls#45]]
9655+
* TIME_DURATION]) AS d#39, MAX(_fork{r}#124,true[BOOLEAN],PT0S[TIME_DURATION]) AS m#42,
9656+
* COUNT(s{r}#119,true[BOOLEAN],PT0S[TIME_DURATION]) AS ls#45]]
96549657
* \_Fork[[emp_no{r}#109, s{r}#119, _fork{r}#124]]
96559658
* |_Project[[emp_no{f}#46, s{r}#6, _fork{r}#7]]
96569659
* | \_Eval[[fork1[KEYWORD] AS _fork#7]]
@@ -9722,7 +9725,10 @@ public void testPruneColumnsInForkBranchesDoNotPruneIfWildcardAggregation() {
97229725
var evalInSecondBranch = as(secondBranchProject.child(), Eval.class);
97239726
var limitInSecondBranch = as(evalInSecondBranch.child(), Limit.class);
97249727
var localRelation = as(limitInSecondBranch.child(), LocalRelation.class);
9725-
assertThat(localRelation.output().stream().map(Attribute::name).collect(Collectors.toSet()), hasItems("$$COUNT$COUNT(*)::keywo>$0"));
9728+
assertThat(
9729+
localRelation.output().stream().map(Attribute::name).collect(Collectors.toSet()),
9730+
hasItems("$$COUNT$COUNT(*)::keywo>$0")
9731+
);
97269732

97279733
var thirdBranch = fork.children().get(2);
97289734
var thirdBranchProject = as(thirdBranch, Project.class);

0 commit comments

Comments
 (0)