Skip to content

Commit 89d8907

Browse files
committed
LIMITs should not pass through STATS
1 parent 81f84a2 commit 89d8907

File tree

1 file changed

+8
-0
lines changed
  • x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/logical

1 file changed

+8
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.xpack.esql.core.util.Holder;
1212
import org.elasticsearch.xpack.esql.expression.function.vector.Knn;
1313
import org.elasticsearch.xpack.esql.optimizer.LogicalOptimizerContext;
14+
import org.elasticsearch.xpack.esql.plan.logical.Aggregate;
1415
import org.elasticsearch.xpack.esql.plan.logical.Filter;
1516
import org.elasticsearch.xpack.esql.plan.logical.Limit;
1617
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
@@ -25,7 +26,12 @@ public PushLimitToKnn() {
2526
public LogicalPlan rule(Limit limit, LogicalOptimizerContext ctx) {
2627
var currentLimit = (int) limit.limit().fold(ctx.foldCtx());
2728
Holder<Integer> currentLimitHolder = new Holder<>(currentLimit);
29+
Holder<Boolean> breakerReached = new Holder<>(false);
2830
return limit.transformDown(plan -> {
31+
if (breakerReached.get()) {
32+
// We reached a breaker and don't want to continue processing
33+
return plan;
34+
}
2935
if (plan instanceof Filter filter) {
3036
Expression limitAppliedExpression = limitFilterExpressions(filter.condition(), limit, ctx);
3137
if (limitAppliedExpression.equals(filter.condition()) == false) {
@@ -37,6 +43,8 @@ public LogicalPlan rule(Limit limit, LogicalOptimizerContext ctx) {
3743
currentLimitHolder.set(newLimit);
3844
}
3945
return descendantLimit;
46+
} else if (plan instanceof Aggregate) {
47+
breakerReached.set(true);
4048
}
4149

4250
return plan;

0 commit comments

Comments
 (0)