-
Notifications
You must be signed in to change notification settings - Fork 25.6k
ES|QL: Remove redundant sorts from execution plan #121156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 19 commits
4910db5
a0b463a
78c86de
10117dc
4d623d7
0ec1539
7ec3534
937e0dc
3569a16
df50502
3af3c11
16bfba0
5c1ed70
c73246d
1c260e1
a5d30ef
f881d2d
319cc69
7a9c8ab
2d34e50
f2af414
2303d8b
9b22923
9083477
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| pr: 121156 | ||
| summary: Remove redundant sorts from execution plan | ||
| area: ES|QL | ||
| type: bug | ||
| issues: [] |
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.esql.optimizer.rules.logical; | ||
|
|
||
| import org.elasticsearch.xpack.esql.plan.logical.Aggregate; | ||
| import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; | ||
| import org.elasticsearch.xpack.esql.plan.logical.OrderBy; | ||
| import org.elasticsearch.xpack.esql.plan.logical.SortAware; | ||
| import org.elasticsearch.xpack.esql.plan.logical.TopN; | ||
| import org.elasticsearch.xpack.esql.plan.logical.UnaryPlan; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.IdentityHashMap; | ||
|
|
||
| /** | ||
| * SORT cannot be executed without a LIMIT, as ES|QL doesn't support unbounded sort (yet). | ||
| * <p> | ||
| * The planner tries to push down LIMIT and transform all the unbounded sorts into a TopN. | ||
| * In some cases it's not possible though, eg. | ||
| * <p> | ||
| * from test | sort x | lookup join lookup on x | sort y | ||
| * <p> | ||
| * from test | sort x | mv_expand x | sort y | ||
| * <p> | ||
| * "sort y" will become a TopN due to the addition of the default Limit, but "sort x" will remain unbounded, | ||
| * so the query could not be executed. | ||
| * <p> | ||
| * In most cases though, following commands can make the previous SORTs redundant, | ||
| * because it will re-sort previously sorted results (eg. if there is another SORT) | ||
| * or because the order will be scrambled by another command (eg. a STATS) | ||
| * <p> | ||
| * This rule finds and prunes redundant SORTs, attempting to make the plan executable. | ||
| */ | ||
| public class PruneRedundantOrderBy extends OptimizerRules.OptimizerRule<LogicalPlan> { | ||
|
|
||
| @Override | ||
| protected LogicalPlan rule(LogicalPlan plan) { | ||
| if (plan instanceof OrderBy || plan instanceof TopN || plan instanceof Aggregate) { | ||
| IdentityHashMap<OrderBy, Void> redundant = findRedundantSort(((UnaryPlan) plan).child()); | ||
| if (redundant.isEmpty()) { | ||
| return plan; | ||
| } | ||
| return plan.transformDown(p -> { | ||
| if (redundant.containsKey(p)) { | ||
| return ((OrderBy) p).child(); | ||
| } | ||
| return p; | ||
|
||
| }); | ||
| } else { | ||
| return plan; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * breadth-first recursion to find redundant SORTs in the children tree. | ||
| */ | ||
| private IdentityHashMap<OrderBy, Void> findRedundantSort(LogicalPlan plan) { | ||
|
||
| IdentityHashMap<OrderBy, Void> result = new IdentityHashMap<>(); | ||
|
|
||
| Deque<LogicalPlan> toCheck = new ArrayDeque<>(); | ||
| toCheck.push(plan); | ||
|
|
||
| while (true) { | ||
| if (toCheck.isEmpty()) { | ||
| return result; | ||
| } | ||
| LogicalPlan p = toCheck.pop(); | ||
| if (p instanceof OrderBy ob) { | ||
| result.put(ob, null); | ||
| toCheck.push(ob.child()); | ||
| } else if (p instanceof SortAware sa && sa.dependsOnInputOrder() == false) { | ||
| for (LogicalPlan child : p.children()) { | ||
| toCheck.push(child); | ||
| } | ||
alex-spies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
++ to the deletion, the rule was unfortunately incorrect.