From f7a0f9028adc2ea432ce3434b9b1eaf0fa545c0c Mon Sep 17 00:00:00 2001 From: Luegg Date: Wed, 4 Aug 2021 11:18:57 +0200 Subject: [PATCH] QL: Make UnaryPlan.replaceChild public and use it where appropriate --- .../org/elasticsearch/xpack/eql/optimizer/Optimizer.java | 5 ++--- .../org/elasticsearch/xpack/eql/plan/logical/Head.java | 2 +- .../elasticsearch/xpack/eql/plan/logical/KeyedFilter.java | 2 +- .../xpack/eql/plan/logical/LimitWithOffset.java | 2 +- .../org/elasticsearch/xpack/eql/plan/logical/Tail.java | 2 +- .../elasticsearch/xpack/ql/optimizer/OptimizerRules.java | 7 ++----- .../org/elasticsearch/xpack/ql/plan/logical/Aggregate.java | 2 +- .../org/elasticsearch/xpack/ql/plan/logical/Filter.java | 2 +- .../org/elasticsearch/xpack/ql/plan/logical/Limit.java | 2 +- .../org/elasticsearch/xpack/ql/plan/logical/OrderBy.java | 2 +- .../org/elasticsearch/xpack/ql/plan/logical/Project.java | 2 +- .../org/elasticsearch/xpack/ql/plan/logical/UnaryPlan.java | 2 +- .../xpack/sql/analysis/analyzer/Analyzer.java | 3 ++- .../org/elasticsearch/xpack/sql/optimizer/Optimizer.java | 2 +- .../org/elasticsearch/xpack/sql/plan/logical/Distinct.java | 2 +- .../org/elasticsearch/xpack/sql/plan/logical/Having.java | 2 +- .../org/elasticsearch/xpack/sql/plan/logical/Pivot.java | 2 +- .../xpack/sql/plan/logical/SubQueryAlias.java | 2 +- .../org/elasticsearch/xpack/sql/plan/logical/With.java | 2 +- 19 files changed, 22 insertions(+), 25 deletions(-) diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/optimizer/Optimizer.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/optimizer/Optimizer.java index d70ba343afb0c..787e745c0354d 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/optimizer/Optimizer.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/optimizer/Optimizer.java @@ -65,7 +65,6 @@ import java.util.Objects; import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static java.util.stream.Collectors.toList; import static org.elasticsearch.xpack.ql.optimizer.OptimizerRules.PropagateNullable; @@ -448,13 +447,13 @@ protected LogicalPlan rule(OrderBy orderBy) { // preserve the order for the base query, everything else needs to be ascending List pushedOrder = baseFilter ? orderBy.order() : ascendingOrders; OrderBy order = new OrderBy(filter.source(), filter.child(), pushedOrder); - orderedQueries.add((KeyedFilter) filter.replaceChildrenSameSize(singletonList(order))); + orderedQueries.add(filter.replaceChild(order)); baseFilter = false; } KeyedFilter until = join.until(); OrderBy order = new OrderBy(until.source(), until.child(), ascendingOrders); - until = (KeyedFilter) until.replaceChildrenSameSize(singletonList(order)); + until = until.replaceChild(order); OrderDirection direction = orderBy.order().get(0).direction(); plan = join.with(orderedQueries, until, direction); diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Head.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Head.java index 400fc1c929d06..fc332d971cc63 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Head.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Head.java @@ -25,7 +25,7 @@ protected NodeInfo info() { } @Override - protected Head replaceChild(LogicalPlan newChild) { + public Head replaceChild(LogicalPlan newChild) { return new Head(source(), limit(), newChild); } } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/KeyedFilter.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/KeyedFilter.java index cd431ed620cd3..2ab922606e635 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/KeyedFilter.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/KeyedFilter.java @@ -43,7 +43,7 @@ protected NodeInfo info() { } @Override - protected KeyedFilter replaceChild(LogicalPlan newChild) { + public KeyedFilter replaceChild(LogicalPlan newChild) { return new KeyedFilter(source(), newChild, keys, timestamp, tiebreaker); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/LimitWithOffset.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/LimitWithOffset.java index 582e565149877..70238b85616bd 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/LimitWithOffset.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/LimitWithOffset.java @@ -34,7 +34,7 @@ protected NodeInfo info() { } @Override - protected LimitWithOffset replaceChild(LogicalPlan newChild) { + public LimitWithOffset replaceChild(LogicalPlan newChild) { return new LimitWithOffset(source(), limit(), offset, newChild); } diff --git a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Tail.java b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Tail.java index 57d12d5823eb2..0dc2e888bbcc8 100644 --- a/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Tail.java +++ b/x-pack/plugin/eql/src/main/java/org/elasticsearch/xpack/eql/plan/logical/Tail.java @@ -33,7 +33,7 @@ protected NodeInfo info() { } @Override - protected Tail replaceChild(LogicalPlan newChild) { + public Tail replaceChild(LogicalPlan newChild) { return new Tail(source(), newChild, limit()); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java index 7229a3348782c..00762ad497ee4 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/optimizer/OptimizerRules.java @@ -68,7 +68,6 @@ import static java.lang.Math.signum; import static java.util.Arrays.asList; -import static java.util.Collections.singletonList; import static org.elasticsearch.xpack.ql.expression.Literal.FALSE; import static org.elasticsearch.xpack.ql.expression.Literal.TRUE; import static org.elasticsearch.xpack.ql.expression.predicate.Predicates.combineAnd; @@ -1207,14 +1206,12 @@ else if (child instanceof UnaryPlan) { } // if at least one expression can be pushed down, update the tree if (conjunctions.size() > 0) { - child = child.replaceChildrenSameSize( - singletonList(filter.with(unary.child(), Predicates.combineAnd(conjunctions))) - ); + child = unary.replaceChild(filter.with(unary.child(), Predicates.combineAnd(conjunctions))); plan = filter.with(child, Predicates.combineAnd(inPlace)); } } else { // push down filter - plan = child.replaceChildrenSameSize(singletonList(filter.with(unary.child(), condition))); + plan = unary.replaceChild(filter.with(unary.child(), condition)); } } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Aggregate.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Aggregate.java index b5f35405eccd7..ad0c608b08687 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Aggregate.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Aggregate.java @@ -34,7 +34,7 @@ protected NodeInfo info() { } @Override - protected Aggregate replaceChild(LogicalPlan newChild) { + public Aggregate replaceChild(LogicalPlan newChild) { return new Aggregate(source(), newChild, groupings, aggregates); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Filter.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Filter.java index d8afc24d6a1ca..ad1a0deb50125 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Filter.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Filter.java @@ -32,7 +32,7 @@ protected NodeInfo info() { } @Override - protected Filter replaceChild(LogicalPlan newChild) { + public Filter replaceChild(LogicalPlan newChild) { return new Filter(source(), newChild, condition); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Limit.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Limit.java index 65aeae5bdd442..0a12cb78e4f07 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Limit.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Limit.java @@ -27,7 +27,7 @@ protected NodeInfo info() { } @Override - protected Limit replaceChild(LogicalPlan newChild) { + public Limit replaceChild(LogicalPlan newChild) { return new Limit(source(), limit, newChild); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/OrderBy.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/OrderBy.java index 0d74bba3f8821..62e7ee940ff2c 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/OrderBy.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/OrderBy.java @@ -29,7 +29,7 @@ protected NodeInfo info() { } @Override - protected OrderBy replaceChild(LogicalPlan newChild) { + public OrderBy replaceChild(LogicalPlan newChild) { return new OrderBy(source(), newChild, order); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Project.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Project.java index 6d8552faa9b99..1f250905fda0f 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Project.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/Project.java @@ -35,7 +35,7 @@ protected NodeInfo info() { } @Override - protected Project replaceChild(LogicalPlan newChild) { + public Project replaceChild(LogicalPlan newChild) { return new Project(source(), newChild, projections); } diff --git a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/UnaryPlan.java b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/UnaryPlan.java index 869e4f03916c4..a63cad8586fee 100644 --- a/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/UnaryPlan.java +++ b/x-pack/plugin/ql/src/main/java/org/elasticsearch/xpack/ql/plan/logical/UnaryPlan.java @@ -31,7 +31,7 @@ public final UnaryPlan replaceChildren(List newChildren) { return replaceChild(newChildren.get(0)); } - protected abstract UnaryPlan replaceChild(LogicalPlan newChild); + public abstract UnaryPlan replaceChild(LogicalPlan newChild); public LogicalPlan child() { return child; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java index 606ad5adb7bb6..77ecaab060e4e 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/analysis/analyzer/Analyzer.java @@ -784,7 +784,8 @@ private static LogicalPlan propagateMissing(LogicalPlan plan, AttributeSet missi // LeafPlans are tables and BinaryPlans are joins so pushing can only happen on unary if (plan instanceof UnaryPlan) { - return plan.replaceChildrenSameSize(singletonList(propagateMissing(((UnaryPlan) plan).child(), missing, failed))); + UnaryPlan unary = (UnaryPlan) plan; + return unary.replaceChild(propagateMissing(unary.child(), missing, failed)); } failed.addAll(missing); diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java index fdc4ffea3efb3..29fbaaca0c9d9 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/optimizer/Optimizer.java @@ -1240,7 +1240,7 @@ static class SkipQueryForLiteralAggregations extends OptimizerRule { @Override protected LogicalPlan rule(Aggregate plan) { if (plan.groupings().isEmpty() && plan.child() instanceof EsRelation && plan.aggregates().stream().allMatch(this::foldable)) { - return plan.replaceChildrenSameSize(singletonList(new LocalRelation(plan.source(), new SingletonExecutable()))); + return plan.replaceChild(new LocalRelation(plan.source(), new SingletonExecutable())); } return plan; diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Distinct.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Distinct.java index d7a3510366247..93bfcfa0dbb7c 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Distinct.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Distinct.java @@ -23,7 +23,7 @@ protected NodeInfo info() { } @Override - protected Distinct replaceChild(LogicalPlan newChild) { + public Distinct replaceChild(LogicalPlan newChild) { return new Distinct(source(), newChild); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Having.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Having.java index ed5d08a4955db..94f146c8b55c8 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Having.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Having.java @@ -29,7 +29,7 @@ protected NodeInfo info() { } @Override - protected Having replaceChild(LogicalPlan newChild) { + public Having replaceChild(LogicalPlan newChild) { return new Having(source(), newChild, condition()); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Pivot.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Pivot.java index 2a0561fbf9b51..b10d3197bbf44 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Pivot.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/Pivot.java @@ -74,7 +74,7 @@ protected NodeInfo info() { } @Override - protected Pivot replaceChild(LogicalPlan newChild) { + public Pivot replaceChild(LogicalPlan newChild) { return new Pivot(source(), newChild, column, values, aggregates, grouping); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/SubQueryAlias.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/SubQueryAlias.java index a15539221e6f8..7492686ac7b18 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/SubQueryAlias.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/SubQueryAlias.java @@ -33,7 +33,7 @@ protected NodeInfo info() { } @Override - protected SubQueryAlias replaceChild(LogicalPlan newChild) { + public SubQueryAlias replaceChild(LogicalPlan newChild) { return new SubQueryAlias(source(), newChild, alias); } diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/With.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/With.java index 44c7f5823c719..0dab55f7d0f1f 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/With.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/plan/logical/With.java @@ -28,7 +28,7 @@ protected NodeInfo info() { } @Override - protected With replaceChild(LogicalPlan newChild) { + public With replaceChild(LogicalPlan newChild) { return new With(source(), newChild, subQueries); }