Skip to content

Commit 0ec1539

Browse files
Verify OrderBy after optimization
1 parent 4d623d7 commit 0ec1539

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

muted-tests.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,6 @@ tests:
314314
- class: org.elasticsearch.xpack.security.profile.ProfileIntegTests
315315
method: testSetEnabled
316316
issue: https://github.com/elastic/elasticsearch/issues/121183
317-
- class: org.elasticsearch.xpack.esql.optimizer.LogicalPlanOptimizerTests
318-
issue: https://github.com/elastic/elasticsearch/issues/121185
319317
- class: org.elasticsearch.xpack.security.CoreWithSecurityClientYamlTestSuiteIT
320318
method: test {yaml=cat.aliases/10_basic/Simple alias}
321319
issue: https://github.com/elastic/elasticsearch/issues/121186

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public Failures verify(LogicalPlan plan) {
2727
PlanConsistencyChecker.checkPlan(p, dependencyFailures);
2828

2929
if (failures.hasFailures() == false) {
30+
if (p instanceof PostOptimizationVerificationAware pova) {
31+
pova.postOptimizationVerification(failures);
32+
}
3033
p.forEachExpression(ex -> {
3134
if (ex instanceof PostOptimizationVerificationAware va) {
3235
va.postOptimizationVerification(failures);

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/plan/logical/OrderBy.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.elasticsearch.common.io.stream.StreamInput;
1111
import org.elasticsearch.common.io.stream.StreamOutput;
1212
import org.elasticsearch.xpack.esql.capabilities.PostAnalysisVerificationAware;
13+
import org.elasticsearch.xpack.esql.capabilities.PostOptimizationVerificationAware;
1314
import org.elasticsearch.xpack.esql.capabilities.TelemetryAware;
1415
import org.elasticsearch.xpack.esql.common.Failures;
1516
import org.elasticsearch.xpack.esql.core.capabilities.Resolvables;
@@ -25,7 +26,7 @@
2526

2627
import static org.elasticsearch.xpack.esql.common.Failure.fail;
2728

28-
public class OrderBy extends UnaryPlan implements PostAnalysisVerificationAware, TelemetryAware {
29+
public class OrderBy extends UnaryPlan implements PostAnalysisVerificationAware, PostOptimizationVerificationAware, TelemetryAware {
2930
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(LogicalPlan.class, "OrderBy", OrderBy::new);
3031

3132
private final List<Order> order;
@@ -109,4 +110,9 @@ public void postAnalysisVerification(Failures failures) {
109110
}
110111
});
111112
}
113+
114+
@Override
115+
public void postOptimizationVerification(Failures failures) {
116+
failures.add(fail(this, "The query cannot be executed because it would require unbounded sort"));
117+
}
112118
}

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7279,7 +7279,7 @@ public void testFunctionNamedParamsAsFunctionArgument() {
72797279
* | \_EsRelation[test][_meta_field{f}#17, emp_no{f}#11, first_name{f}#12, ..]
72807280
* \_EsRelation[languages_lookup][LOOKUP][language_code{f}#22, language_name{f}#23]
72817281
*/
7282-
public void testRedundantSortOnJoin() throws Exception {
7282+
public void testRedundantSortOnJoin() {
72837283
assumeTrue("Requires LOOKUP JOIN", EsqlCapabilities.Cap.JOIN_LOOKUP_V12.isEnabled());
72847284

72857285
var plan = optimizedPlan("""
@@ -7307,7 +7307,7 @@ public void testRedundantSortOnJoin() throws Exception {
73077307
* \_Eval[[[62 61 72][KEYWORD] AS foo]]
73087308
* \_EsRelation[test][_meta_field{f}#15, emp_no{f}#9, first_name{f}#10, g..]
73097309
*/
7310-
public void testRedundantSortOnMvExpand() throws Exception {
7310+
public void testRedundantSortOnMvExpand() {
73117311
var plan = optimizedPlan("""
73127312
FROM test
73137313
| SORT languages
@@ -7333,7 +7333,7 @@ public void testRedundantSortOnMvExpand() throws Exception {
73337333
* | \_EsRelation[test][_meta_field{f}#17, emp_no{f}#11, first_name{f}#12, ..]
73347334
* \_EsRelation[languages_lookup][LOOKUP][language_code{f}#22, language_name{f}#23]
73357335
*/
7336-
public void testRedundantSortOnMvExpandAndJoin() throws Exception {
7336+
public void testRedundantSortOnMvExpandAndJoin() {
73377337
var plan = optimizedPlan("""
73387338
FROM test
73397339
| SORT languages
@@ -7361,7 +7361,7 @@ public void testRedundantSortOnMvExpandAndJoin() throws Exception {
73617361
* | \_EsRelation[test][_meta_field{f}#18, emp_no{f}#12, first_name{f}#13, ..]
73627362
* \_EsRelation[languages_lookup][LOOKUP][language_code{f}#23, language_name{f}#24]
73637363
*/
7364-
public void testMultlipleRedundantSortOnMvExpandAndJoin() throws Exception {
7364+
public void testMultlipleRedundantSortOnMvExpandAndJoin() {
73657365
var plan = optimizedPlan("""
73667366
FROM test
73677367
| SORT first_name
@@ -7393,7 +7393,7 @@ public void testMultlipleRedundantSortOnMvExpandAndJoin() throws Exception {
73937393
* \_Eval[[TOSTRING(languages{f}#19) AS foo]]
73947394
* \_EsRelation[test][_meta_field{f}#22, emp_no{f}#16, first_name{f}#17, ..]
73957395
*/
7396-
public void testRedundantSortOnMvExpandEnrichGrokDissect() throws Exception {
7396+
public void testRedundantSortOnMvExpandEnrichGrokDissect() {
73977397
var plan = optimizedPlan("""
73987398
FROM test
73997399
| SORT languages
@@ -7430,7 +7430,7 @@ public void testRedundantSortOnMvExpandEnrichGrokDissect() throws Exception {
74307430
* | \_EsRelation[test][_meta_field{f}#26, emp_no{f}#20, first_name{f}#21, ..]
74317431
* \_EsRelation[languages_lookup][LOOKUP][language_code{f}#31]
74327432
*/
7433-
public void testRedundantSortOnMvExpandJoinEnrichGrokDissect() throws Exception {
7433+
public void testRedundantSortOnMvExpandJoinEnrichGrokDissect() {
74347434
var plan = optimizedPlan("""
74357435
FROM test
74367436
| SORT languages
@@ -7454,4 +7454,21 @@ public void testRedundantSortOnMvExpandJoinEnrichGrokDissect() throws Exception
74547454
var eval = as(join.left(), Eval.class);
74557455
as(eval.child(), EsRelation.class);
74567456
}
7457+
7458+
public void testUnboundedSort() throws Exception {
7459+
String query = """
7460+
FROM test
7461+
| EVAL language_code = 1
7462+
| LOOKUP JOIN languages_lookup ON language_code
7463+
| SORT language_name
7464+
| MV_EXPAND language_name
7465+
| EVAL foo = concat(language_name, "foo")
7466+
| MV_EXPAND foo
7467+
| WHERE emp_no > 1
7468+
| SORT emp_no
7469+
""";
7470+
7471+
var e = expectThrows(VerificationException.class, () -> plan(query));
7472+
assertThat(e.getMessage(), is("Found 1 problem\nline 4:3: The query cannot be executed because it would require unbounded sort"));
7473+
}
74577474
}

0 commit comments

Comments
 (0)