|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.esql.optimizer.rules.logical; |
| 9 | + |
| 10 | +import org.elasticsearch.xpack.esql.plan.logical.Aggregate; |
| 11 | +import org.elasticsearch.xpack.esql.plan.logical.Drop; |
| 12 | +import org.elasticsearch.xpack.esql.plan.logical.Enrich; |
| 13 | +import org.elasticsearch.xpack.esql.plan.logical.Eval; |
| 14 | +import org.elasticsearch.xpack.esql.plan.logical.Filter; |
| 15 | +import org.elasticsearch.xpack.esql.plan.logical.InlineStats; |
| 16 | +import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan; |
| 17 | +import org.elasticsearch.xpack.esql.plan.logical.Lookup; |
| 18 | +import org.elasticsearch.xpack.esql.plan.logical.MvExpand; |
| 19 | +import org.elasticsearch.xpack.esql.plan.logical.OrderBy; |
| 20 | +import org.elasticsearch.xpack.esql.plan.logical.Project; |
| 21 | +import org.elasticsearch.xpack.esql.plan.logical.RegexExtract; |
| 22 | +import org.elasticsearch.xpack.esql.plan.logical.Rename; |
| 23 | +import org.elasticsearch.xpack.esql.plan.logical.TopN; |
| 24 | +import org.elasticsearch.xpack.esql.plan.logical.UnaryPlan; |
| 25 | +import org.elasticsearch.xpack.esql.plan.logical.join.Join; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.IdentityHashMap; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +/** |
| 32 | + * SORT cannot be executed without a LIMIT, as ES|QL doesn't support unbounded sort (yet). |
| 33 | + * <p> |
| 34 | + * The planner tries to push down LIMIT and transform all the unbounded sorts into a TopN. |
| 35 | + * In some cases it's not possible though, eg. |
| 36 | + * <p> |
| 37 | + * from test | sort x | lookup join lookup on x | sort y |
| 38 | + * <p> |
| 39 | + * from test | sort x | mv_expand x | sort y |
| 40 | + * <p> |
| 41 | + * "sort y" will become a TopN, but "sort x" will remain unbounded, so the query could not be executed. |
| 42 | + * <p> |
| 43 | + * In most cases though, following commands can make the previous SORTs redundant, |
| 44 | + * because it will re-sort previously sorted results (eg. if there is another SORT) |
| 45 | + * or because the order will be scrambled by another command (eg. a STATS) |
| 46 | + * <p> |
| 47 | + * This rule finds and prunes redundant SORTs, attempting to make the plan executable. |
| 48 | + */ |
| 49 | +public class PruneRedundantOrderBy extends OptimizerRules.OptimizerRule<LogicalPlan> { |
| 50 | + |
| 51 | + @Override |
| 52 | + protected LogicalPlan rule(LogicalPlan plan) { |
| 53 | + if (plan instanceof OrderBy || plan instanceof TopN || plan instanceof Aggregate) { |
| 54 | + IdentityHashMap<OrderBy, Void> redundant = findRedundantSort(((UnaryPlan) plan).child()); |
| 55 | + if (redundant.isEmpty()) { |
| 56 | + return plan; |
| 57 | + } |
| 58 | + return plan.transformUp(p -> { |
| 59 | + if (redundant.containsKey(p)) { |
| 60 | + return ((OrderBy) p).child(); |
| 61 | + } |
| 62 | + return p; |
| 63 | + }); |
| 64 | + } else { |
| 65 | + return plan; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private IdentityHashMap<OrderBy, Void> findRedundantSort(LogicalPlan plan) { |
| 70 | + List<LogicalPlan> toCheck = new ArrayList<>(); |
| 71 | + toCheck.add(plan); |
| 72 | + |
| 73 | + IdentityHashMap<OrderBy, Void> result = new IdentityHashMap<>(); |
| 74 | + LogicalPlan p = null; |
| 75 | + while (true) { |
| 76 | + if (p == null) { |
| 77 | + if (toCheck.isEmpty()) { |
| 78 | + return result; |
| 79 | + } else { |
| 80 | + p = toCheck.remove(0); |
| 81 | + } |
| 82 | + } else if (p instanceof OrderBy ob) { |
| 83 | + result.put(ob, null); |
| 84 | + p = ob.child(); |
| 85 | + } else if (p instanceof UnaryPlan unary) { |
| 86 | + if (unary instanceof Project |
| 87 | + || unary instanceof Drop |
| 88 | + || unary instanceof Rename |
| 89 | + || unary instanceof MvExpand |
| 90 | + || unary instanceof Enrich |
| 91 | + || unary instanceof RegexExtract |
| 92 | + || unary instanceof InlineStats |
| 93 | + || unary instanceof Lookup |
| 94 | + // IMPORTANT |
| 95 | + // If we introduce window functions or order-sensitive aggs (eg. STREAMSTATS), |
| 96 | + // the previous sort could actually become relevant |
| 97 | + // so we have to be careful with plans that could use them, ie. the following |
| 98 | + || unary instanceof Filter |
| 99 | + || unary instanceof Eval |
| 100 | + || unary instanceof Aggregate) { |
| 101 | + p = unary.child(); |
| 102 | + } else { |
| 103 | + // stop here, other unary plans could be sensitive to SORT |
| 104 | + p = null; |
| 105 | + } |
| 106 | + } else if (p instanceof Join lj) { |
| 107 | + toCheck.add(lj.left()); |
| 108 | + toCheck.add(lj.right()); |
| 109 | + p = null; |
| 110 | + } else { |
| 111 | + // stop here, other unary plans could be sensitive to SORT |
| 112 | + p = null; |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments