Skip to content

Commit f03d2cc

Browse files
committed
push down through where and sort
1 parent 5ef59ff commit f03d2cc

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
import org.elasticsearch.xpack.esql.optimizer.LogicalOptimizerContext;
1414
import org.elasticsearch.xpack.esql.plan.logical.Enrich;
1515
import org.elasticsearch.xpack.esql.plan.logical.Eval;
16+
import org.elasticsearch.xpack.esql.plan.logical.Filter;
1617
import org.elasticsearch.xpack.esql.plan.logical.Insist;
1718
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
19+
import org.elasticsearch.xpack.esql.plan.logical.OrderBy;
1820
import org.elasticsearch.xpack.esql.plan.logical.Project;
1921
import org.elasticsearch.xpack.esql.plan.logical.RandomSample;
2022
import org.elasticsearch.xpack.esql.plan.logical.RegexExtract;
@@ -36,7 +38,9 @@ protected LogicalPlan rule(RandomSample randomSample, LogicalOptimizerContext co
3638
plan = new RandomSample(randomSample.source(), probability, seed, rsChild.child());
3739
} else if (child instanceof Enrich
3840
|| child instanceof Eval
41+
|| child instanceof Filter
3942
|| child instanceof Insist
43+
|| child instanceof OrderBy
4044
|| child instanceof Project
4145
|| child instanceof RegexExtract) {
4246
var unaryChild = (UnaryPlan) child;

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7672,14 +7672,33 @@ public void testRandomSamplePushDown() {
76727672
}
76737673
}
76747674

7675+
public void testRandomSamplePushDown_sort() {
7676+
var query = "FROM TEST | WHERE emp_no > 0 | RANDOM_SAMPLE 0.5 | LIMIT 100";
7677+
var optimized = optimizedPlan(query);
7678+
7679+
var limit = as(optimized, Limit.class);
7680+
var filter = as(limit.child(), Filter.class);
7681+
var randomSample = as(filter.child(), RandomSample.class);
7682+
var source = as(randomSample.child(), EsRelation.class);
7683+
7684+
assertThat(randomSample.probability().fold(FoldContext.small()), equalTo(0.5));
7685+
assertNull(randomSample.seed());
7686+
}
7687+
7688+
public void testRandomSamplePushDown_where() {
7689+
var query = "FROM TEST | SORT emp_no | RANDOM_SAMPLE 0.5 | LIMIT 100";
7690+
var optimized = optimizedPlan(query);
7691+
7692+
var topN = as(optimized, TopN.class);
7693+
var randomSample = as(topN.child(), RandomSample.class);
7694+
var source = as(randomSample.child(), EsRelation.class);
7695+
7696+
assertThat(randomSample.probability().fold(FoldContext.small()), equalTo(0.5));
7697+
assertNull(randomSample.seed());
7698+
}
7699+
76757700
public void testRandomSampleNoPushDown() {
7676-
for (var command : List.of(
7677-
"LIMIT 100",
7678-
"MV_EXPAND languages",
7679-
"SORT emp_no | LIMIT 100", // the limit avoids an unbounded sort
7680-
"STATS COUNT()",
7681-
"WHERE emp_no > 1"
7682-
)) {
7701+
for (var command : List.of("LIMIT 100", "MV_EXPAND languages", "STATS COUNT()")) {
76837702
var query = "FROM TEST | " + command + " | RANDOM_SAMPLE .5";
76847703
var optimized = optimizedPlan(query);
76857704

0 commit comments

Comments
 (0)