Skip to content

Commit 98fa981

Browse files
committed
surrogate
1 parent 33ef895 commit 98fa981

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/parser/LogicalPlanBuilder.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.antlr.v4.runtime.tree.ParseTree;
1313
import org.elasticsearch.Build;
1414
import org.elasticsearch.common.Strings;
15-
import org.elasticsearch.compute.operator.ChangePointOperator;
1615
import org.elasticsearch.core.Tuple;
1716
import org.elasticsearch.dissect.DissectException;
1817
import org.elasticsearch.dissect.DissectParser;
@@ -467,21 +466,7 @@ public PlanFactory visitChangePointCommand(EsqlBaseParser.ChangePointCommandCont
467466
ctx.targetPvalue == null ? "pvalue" : visitQualifiedName(ctx.targetPvalue).name(),
468467
DataType.DOUBLE
469468
);
470-
return child -> {
471-
// ChangePoint should always run on the coordinating node after the data is collected
472-
// in sorted order. This is enforced by adding OrderBy here.
473-
// Furthermore, ChangePoint should be called with at most 1000 data points. That's
474-
// enforced by the Limits here. The first Limit of 1001 data points is necessary to
475-
// generate a possible warning, the second Limit of 1000 is to truncate the output.
476-
OrderBy orderBy = new OrderBy(src, child, List.of(new Order(src, key, Order.OrderDirection.ASC, Order.NullsPosition.ANY)));
477-
Limit limit = new Limit(
478-
src,
479-
new Literal(Source.EMPTY, ChangePointOperator.INPUT_VALUE_COUNT_LIMIT + 1, DataType.INTEGER),
480-
orderBy
481-
);
482-
ChangePoint changePoint = new ChangePoint(src, limit, value, key, targetType, targetPvalue);
483-
return new Limit(src, new Literal(Source.EMPTY, ChangePointOperator.INPUT_VALUE_COUNT_LIMIT, DataType.INTEGER), changePoint);
484-
};
469+
return child -> new ChangePoint(src, child, value, key, targetType, targetPvalue);
485470
}
486471

487472
private static Tuple<Mode, String> parsePolicyName(Token policyToken) {

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,27 @@
99
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1010
import org.elasticsearch.common.io.stream.StreamInput;
1111
import org.elasticsearch.common.io.stream.StreamOutput;
12+
import org.elasticsearch.compute.operator.ChangePointOperator;
1213
import org.elasticsearch.xpack.esql.core.expression.Attribute;
1314
import org.elasticsearch.xpack.esql.core.expression.AttributeSet;
1415
import org.elasticsearch.xpack.esql.core.expression.Expressions;
16+
import org.elasticsearch.xpack.esql.core.expression.Literal;
1517
import org.elasticsearch.xpack.esql.core.expression.NameId;
1618
import org.elasticsearch.xpack.esql.core.expression.NamedExpression;
1719
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1820
import org.elasticsearch.xpack.esql.core.tree.Source;
21+
import org.elasticsearch.xpack.esql.core.type.DataType;
1922
import org.elasticsearch.xpack.esql.expression.NamedExpressions;
23+
import org.elasticsearch.xpack.esql.expression.Order;
2024
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
2125
import org.elasticsearch.xpack.esql.plan.GeneratingPlan;
2226

2327
import java.io.IOException;
2428
import java.util.List;
2529
import java.util.Objects;
2630

27-
public class ChangePoint extends UnaryPlan implements GeneratingPlan<ChangePoint> {
31+
public class ChangePoint extends UnaryPlan implements GeneratingPlan<ChangePoint>, SurrogateLogicalPlan {
32+
2833
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
2934
LogicalPlan.class,
3035
"ChangePoint",
@@ -161,4 +166,22 @@ public boolean equals(Object obj) {
161166
&& Objects.equals(targetType, other.targetType)
162167
&& Objects.equals(targetPvalue, other.targetPvalue);
163168
}
169+
170+
@Override
171+
public LogicalPlan surrogate() {
172+
// ChangePoint should always run on the coordinating node after the data is collected
173+
// in sorted order. This is enforced by adding OrderBy here.
174+
// Furthermore, ChangePoint should be called with at most 1000 data points. That's
175+
// enforced by the Limits here. The first Limit of N+1 data points is necessary to
176+
// generate a possible warning, the second Limit of N is to truncate the output.
177+
Order order = new Order(source(), key, Order.OrderDirection.ASC, Order.NullsPosition.ANY);
178+
OrderBy orderBy = new OrderBy(source(), child(), List.of(order));
179+
Limit limit = new Limit(
180+
source(),
181+
new Literal(Source.EMPTY, ChangePointOperator.INPUT_VALUE_COUNT_LIMIT + 1, DataType.INTEGER),
182+
orderBy
183+
);
184+
ChangePoint changePoint = new ChangePoint(source(), limit, value, key, targetType, targetPvalue);
185+
return new Limit(source(), new Literal(Source.EMPTY, ChangePointOperator.INPUT_VALUE_COUNT_LIMIT, DataType.INTEGER), changePoint);
186+
}
164187
}

0 commit comments

Comments
 (0)