Skip to content

Commit f4cc63d

Browse files
committed
Enable LIMIT pushdown on LEFT joins in general
1 parent 561fca0 commit f4cc63d

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/PhysicalVerifier.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.xpack.esql.core.expression.Attribute;
1313
import org.elasticsearch.xpack.esql.core.expression.Expressions;
1414
import org.elasticsearch.xpack.esql.optimizer.rules.PlanConsistencyChecker;
15-
import org.elasticsearch.xpack.esql.plan.physical.AggregateExec;
1615
import org.elasticsearch.xpack.esql.plan.physical.FieldExtractExec;
1716
import org.elasticsearch.xpack.esql.plan.physical.PhysicalPlan;
1817

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.elasticsearch.xpack.esql.plan.logical.UnaryPlan;
2020
import org.elasticsearch.xpack.esql.plan.logical.join.Join;
2121
import org.elasticsearch.xpack.esql.plan.logical.join.JoinTypes;
22-
import org.elasticsearch.xpack.esql.plan.logical.local.LocalRelation;
2322

2423
public final class PushDownAndCombineLimits extends OptimizerRules.OptimizerRule<Limit> {
2524

@@ -63,8 +62,10 @@ public LogicalPlan rule(Limit limit) {
6362
}
6463
}
6564
} else if (limit.child() instanceof Join join) {
66-
if (join.config().type() == JoinTypes.LEFT && join.right() instanceof LocalRelation) {
67-
// This is a hash join from something like a lookup.
65+
if (join.config().type() == JoinTypes.LEFT) {
66+
// NOTE! This is only correct because our LEFT JOINs preserve the number of rows from the left hand side.
67+
// This deviates from SQL semantics. In SQL, multiple matches on the right hand side lead to multiple rows in the output.
68+
// For us, multiple matches on the right hand side lead are collected into multi-values.
6869
return join.replaceChildren(limit.replaceChild(join.left()), join.right());
6970
}
7071
}

0 commit comments

Comments
 (0)