Skip to content

Commit 7b81e0c

Browse files
authored
ESQL: Move more function serialization (#109950)
This moves yet more of the function serialization code from `PlanNamedTypes` into `NamedWriteable` to line up better with the rest of Elasticsearch. This moves 13 more functions including all "configuration" functions and all date functions and all varags functions. There are 38 left.
1 parent 4a0b026 commit 7b81e0c

39 files changed

+1143
-156
lines changed

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/function/scalar/UnaryScalarFunction.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@
66
*/
77
package org.elasticsearch.xpack.esql.core.expression.function.scalar;
88

9+
import org.elasticsearch.common.io.stream.StreamInput;
10+
import org.elasticsearch.common.io.stream.StreamOutput;
911
import org.elasticsearch.xpack.esql.core.expression.Expression;
1012
import org.elasticsearch.xpack.esql.core.expression.gen.processor.Processor;
1113
import org.elasticsearch.xpack.esql.core.tree.Source;
14+
import org.elasticsearch.xpack.esql.core.util.PlanStreamInput;
15+
import org.elasticsearch.xpack.esql.core.util.PlanStreamOutput;
1216

17+
import java.io.IOException;
1318
import java.util.List;
1419

1520
import static java.util.Collections.singletonList;
@@ -18,16 +23,21 @@ public abstract class UnaryScalarFunction extends ScalarFunction {
1823

1924
private final Expression field;
2025

21-
protected UnaryScalarFunction(Source source) {
22-
super(source);
23-
this.field = null;
24-
}
25-
2626
protected UnaryScalarFunction(Source source, Expression field) {
2727
super(source, singletonList(field));
2828
this.field = field;
2929
}
3030

31+
protected UnaryScalarFunction(StreamInput in) throws IOException {
32+
this(Source.readFrom((StreamInput & PlanStreamInput) in), ((PlanStreamInput) in).readExpression());
33+
}
34+
35+
@Override
36+
public final void writeTo(StreamOutput out) throws IOException {
37+
source().writeTo(out);
38+
((PlanStreamOutput) out).writeExpression(field);
39+
}
40+
3141
@Override
3242
public final UnaryScalarFunction replaceChildren(List<Expression> newChildren) {
3343
return replaceChild(newChildren.get(0));

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/predicate/logical/Not.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.elasticsearch.xpack.esql.core.expression.predicate.logical;
88

9+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
10+
import org.elasticsearch.common.io.stream.StreamInput;
911
import org.elasticsearch.xpack.esql.core.expression.Expression;
1012
import org.elasticsearch.xpack.esql.core.expression.function.scalar.UnaryScalarFunction;
1113
import org.elasticsearch.xpack.esql.core.expression.gen.processor.Processor;
@@ -14,15 +16,27 @@
1416
import org.elasticsearch.xpack.esql.core.tree.Source;
1517
import org.elasticsearch.xpack.esql.core.type.DataType;
1618

19+
import java.io.IOException;
20+
1721
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.DEFAULT;
1822
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isBoolean;
1923

2024
public class Not extends UnaryScalarFunction implements Negatable<Expression> {
25+
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Not", Not::new);
2126

2227
public Not(Source source, Expression child) {
2328
super(source, child);
2429
}
2530

31+
private Not(StreamInput in) throws IOException {
32+
super(in);
33+
}
34+
35+
@Override
36+
public String getWriteableName() {
37+
return ENTRY.name;
38+
}
39+
2640
@Override
2741
protected NodeInfo<Not> info() {
2842
return NodeInfo.create(this, Not::new, field());

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/predicate/nulls/IsNotNull.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.elasticsearch.xpack.esql.core.expression.predicate.nulls;
88

9+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
10+
import org.elasticsearch.common.io.stream.StreamInput;
911
import org.elasticsearch.xpack.esql.core.expression.Expression;
1012
import org.elasticsearch.xpack.esql.core.expression.Nullability;
1113
import org.elasticsearch.xpack.esql.core.expression.function.scalar.UnaryScalarFunction;
@@ -16,12 +18,28 @@
1618
import org.elasticsearch.xpack.esql.core.tree.Source;
1719
import org.elasticsearch.xpack.esql.core.type.DataType;
1820

21+
import java.io.IOException;
22+
1923
public class IsNotNull extends UnaryScalarFunction implements Negatable<UnaryScalarFunction> {
24+
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
25+
Expression.class,
26+
"IsNotNull",
27+
IsNotNull::new
28+
);
2029

2130
public IsNotNull(Source source, Expression field) {
2231
super(source, field);
2332
}
2433

34+
private IsNotNull(StreamInput in) throws IOException {
35+
super(in);
36+
}
37+
38+
@Override
39+
public String getWriteableName() {
40+
return ENTRY.name;
41+
}
42+
2543
@Override
2644
protected NodeInfo<IsNotNull> info() {
2745
return NodeInfo.create(this, IsNotNull::new, field());

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/expression/predicate/nulls/IsNull.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
*/
77
package org.elasticsearch.xpack.esql.core.expression.predicate.nulls;
88

9+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
10+
import org.elasticsearch.common.io.stream.StreamInput;
911
import org.elasticsearch.xpack.esql.core.expression.Expression;
1012
import org.elasticsearch.xpack.esql.core.expression.Nullability;
1113
import org.elasticsearch.xpack.esql.core.expression.function.scalar.UnaryScalarFunction;
@@ -16,12 +18,24 @@
1618
import org.elasticsearch.xpack.esql.core.tree.Source;
1719
import org.elasticsearch.xpack.esql.core.type.DataType;
1820

21+
import java.io.IOException;
22+
1923
public class IsNull extends UnaryScalarFunction implements Negatable<UnaryScalarFunction> {
24+
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "IsNull", IsNull::new);
2025

2126
public IsNull(Source source, Expression field) {
2227
super(source, field);
2328
}
2429

30+
private IsNull(StreamInput in) throws IOException {
31+
super(in);
32+
}
33+
34+
@Override
35+
public String getWriteableName() {
36+
return ENTRY.name;
37+
}
38+
2539
@Override
2640
protected NodeInfo<IsNull> info() {
2741
return NodeInfo.create(this, IsNull::new, field());

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/EsqlScalarFunction.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,25 @@
77

88
package org.elasticsearch.xpack.esql.expression.function.scalar;
99

10+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1011
import org.elasticsearch.xpack.esql.core.expression.Expression;
1112
import org.elasticsearch.xpack.esql.core.expression.function.scalar.ScalarFunction;
1213
import org.elasticsearch.xpack.esql.core.tree.Source;
1314
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
15+
import org.elasticsearch.xpack.esql.expression.function.scalar.conditional.Case;
16+
import org.elasticsearch.xpack.esql.expression.function.scalar.conditional.Greatest;
17+
import org.elasticsearch.xpack.esql.expression.function.scalar.conditional.Least;
18+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.DateDiff;
19+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.DateExtract;
20+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.DateFormat;
21+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.DateParse;
22+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.DateTrunc;
23+
import org.elasticsearch.xpack.esql.expression.function.scalar.date.Now;
24+
import org.elasticsearch.xpack.esql.expression.function.scalar.nulls.Coalesce;
25+
import org.elasticsearch.xpack.esql.expression.function.scalar.string.Concat;
26+
import org.elasticsearch.xpack.esql.expression.function.scalar.string.ToLower;
27+
import org.elasticsearch.xpack.esql.expression.function.scalar.string.ToUpper;
28+
import org.elasticsearch.xpack.esql.expression.predicate.operator.comparison.InsensitiveEquals;
1429

1530
import java.util.List;
1631

@@ -25,6 +40,24 @@
2540
* </p>
2641
*/
2742
public abstract class EsqlScalarFunction extends ScalarFunction implements EvaluatorMapper {
43+
public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
44+
return List.of(
45+
Case.ENTRY,
46+
Coalesce.ENTRY,
47+
Concat.ENTRY,
48+
Greatest.ENTRY,
49+
InsensitiveEquals.ENTRY,
50+
DateExtract.ENTRY,
51+
DateDiff.ENTRY,
52+
DateFormat.ENTRY,
53+
DateParse.ENTRY,
54+
DateTrunc.ENTRY,
55+
Least.ENTRY,
56+
Now.ENTRY,
57+
ToLower.ENTRY,
58+
ToUpper.ENTRY
59+
);
60+
}
2861

2962
protected EsqlScalarFunction(Source source) {
3063
super(source);
@@ -38,5 +71,4 @@ protected EsqlScalarFunction(Source source, List<Expression> fields) {
3871
public Object fold() {
3972
return EvaluatorMapper.super.fold();
4073
}
41-
4274
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/UnaryScalarFunction.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import org.elasticsearch.common.io.stream.StreamOutput;
1313
import org.elasticsearch.xpack.esql.core.expression.Expression;
1414
import org.elasticsearch.xpack.esql.core.expression.TypeResolutions;
15+
import org.elasticsearch.xpack.esql.core.expression.predicate.logical.Not;
16+
import org.elasticsearch.xpack.esql.core.expression.predicate.nulls.IsNotNull;
17+
import org.elasticsearch.xpack.esql.core.expression.predicate.nulls.IsNull;
1518
import org.elasticsearch.xpack.esql.core.tree.Source;
1619
import org.elasticsearch.xpack.esql.core.type.DataType;
1720
import org.elasticsearch.xpack.esql.expression.function.scalar.convert.FromBase64;
@@ -76,10 +79,13 @@ public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
7679
Cosh.ENTRY,
7780
Floor.ENTRY,
7881
FromBase64.ENTRY,
82+
IsNotNull.ENTRY,
83+
IsNull.ENTRY,
7984
Length.ENTRY,
8085
Log10.ENTRY,
8186
LTrim.ENTRY,
8287
Neg.ENTRY,
88+
Not.ENTRY,
8389
RTrim.ENTRY,
8490
Signum.ENTRY,
8591
Sin.ENTRY,

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/conditional/Case.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
package org.elasticsearch.xpack.esql.expression.function.scalar.conditional;
99

10+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
11+
import org.elasticsearch.common.io.stream.StreamInput;
12+
import org.elasticsearch.common.io.stream.StreamOutput;
1013
import org.elasticsearch.compute.data.Block;
1114
import org.elasticsearch.compute.data.BooleanBlock;
1215
import org.elasticsearch.compute.data.ElementType;
@@ -27,8 +30,11 @@
2730
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
2831
import org.elasticsearch.xpack.esql.expression.function.Param;
2932
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
33+
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
34+
import org.elasticsearch.xpack.esql.io.stream.PlanStreamOutput;
3035
import org.elasticsearch.xpack.esql.planner.PlannerUtils;
3136

37+
import java.io.IOException;
3238
import java.util.ArrayList;
3339
import java.util.List;
3440
import java.util.function.Function;
@@ -37,8 +43,12 @@
3743

3844
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
3945
import static org.elasticsearch.xpack.esql.core.type.DataType.NULL;
46+
import static org.elasticsearch.xpack.esql.io.stream.PlanNameRegistry.PlanReader.readerFromPlanReader;
47+
import static org.elasticsearch.xpack.esql.io.stream.PlanNameRegistry.PlanWriter.writerFromPlanWriter;
4048

4149
public final class Case extends EsqlScalarFunction {
50+
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Case", Case::new);
51+
4252
record Condition(Expression condition, Expression value) {}
4353

4454
private final List<Condition> conditions;
@@ -110,6 +120,26 @@ public Case(
110120
elseValue = elseValueIsExplicit() ? children().get(children().size() - 1) : new Literal(source, null, NULL);
111121
}
112122

123+
private Case(StreamInput in) throws IOException {
124+
this(
125+
Source.readFrom((PlanStreamInput) in),
126+
((PlanStreamInput) in).readExpression(),
127+
in.readCollectionAsList(readerFromPlanReader(PlanStreamInput::readExpression))
128+
);
129+
}
130+
131+
@Override
132+
public void writeTo(StreamOutput out) throws IOException {
133+
source().writeTo(out);
134+
((PlanStreamOutput) out).writeExpression(children().get(0));
135+
out.writeCollection(children().subList(1, children().size()), writerFromPlanWriter(PlanStreamOutput::writeExpression));
136+
}
137+
138+
@Override
139+
public String getWriteableName() {
140+
return ENTRY.name;
141+
}
142+
113143
private boolean elseValueIsExplicit() {
114144
return children().size() % 2 == 1;
115145
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/conditional/Greatest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
package org.elasticsearch.xpack.esql.expression.function.scalar.conditional;
99

1010
import org.apache.lucene.util.BytesRef;
11+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
12+
import org.elasticsearch.common.io.stream.StreamInput;
13+
import org.elasticsearch.common.io.stream.StreamOutput;
1114
import org.elasticsearch.compute.ann.Evaluator;
1215
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
1316
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
@@ -23,17 +26,24 @@
2326
import org.elasticsearch.xpack.esql.expression.function.Param;
2427
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
2528
import org.elasticsearch.xpack.esql.expression.function.scalar.multivalue.MvMax;
29+
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
30+
import org.elasticsearch.xpack.esql.io.stream.PlanStreamOutput;
2631

32+
import java.io.IOException;
2733
import java.util.List;
2834
import java.util.function.Function;
2935
import java.util.stream.Stream;
3036

3137
import static org.elasticsearch.xpack.esql.core.type.DataType.NULL;
38+
import static org.elasticsearch.xpack.esql.io.stream.PlanNameRegistry.PlanReader.readerFromPlanReader;
39+
import static org.elasticsearch.xpack.esql.io.stream.PlanNameRegistry.PlanWriter.writerFromPlanWriter;
3240

3341
/**
3442
* Returns the maximum value of multiple columns.
3543
*/
3644
public class Greatest extends EsqlScalarFunction implements OptionalArgument {
45+
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Greatest", Greatest::new);
46+
3747
private DataType dataType;
3848

3949
@FunctionInfo(
@@ -61,6 +71,26 @@ public Greatest(
6171
super(source, Stream.concat(Stream.of(first), rest.stream()).toList());
6272
}
6373

74+
private Greatest(StreamInput in) throws IOException {
75+
this(
76+
Source.readFrom((PlanStreamInput) in),
77+
((PlanStreamInput) in).readExpression(),
78+
in.readCollectionAsList(readerFromPlanReader(PlanStreamInput::readExpression))
79+
);
80+
}
81+
82+
@Override
83+
public void writeTo(StreamOutput out) throws IOException {
84+
source().writeTo(out);
85+
((PlanStreamOutput) out).writeExpression(children().get(0));
86+
out.writeCollection(children().subList(1, children().size()), writerFromPlanWriter(PlanStreamOutput::writeExpression));
87+
}
88+
89+
@Override
90+
public String getWriteableName() {
91+
return ENTRY.name;
92+
}
93+
6494
@Override
6595
public DataType dataType() {
6696
if (dataType == null) {

0 commit comments

Comments
 (0)