Skip to content

Commit ebd00dd

Browse files
committed
Added a ConfigurationFUnction interface and fixed tests with static configurations
1 parent a11d4b8 commit ebd00dd

File tree

8 files changed

+58
-29
lines changed

8 files changed

+58
-29
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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.expression.function;
9+
10+
/**
11+
* Marker interface indicating that a function needs a Configuration object as its last parameter.
12+
* <p>
13+
* Extend {@link org.elasticsearch.xpack.esql.expression.function.scalar.EsqlConfigurationFunction} instead if possible.
14+
* It automatically takes care of:
15+
* </p>
16+
* <ul>
17+
* <li>The Configuration field</li>
18+
* <li>HashCode</li>
19+
* <li>Equals</li>
20+
* </ul>
21+
* <p>
22+
* If overridden directly, take a look at {@link org.elasticsearch.xpack.esql.expression.function.scalar.EsqlConfigurationFunction}
23+
* and add or update the required methods.
24+
* </p>
25+
*/
26+
public interface ConfigurationFunction {
27+
28+
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
import org.elasticsearch.xpack.esql.expression.function.grouping.TBucket;
7171
import org.elasticsearch.xpack.esql.expression.function.inference.TextEmbedding;
7272
import org.elasticsearch.xpack.esql.expression.function.scalar.Clamp;
73-
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlConfigurationFunction;
7473
import org.elasticsearch.xpack.esql.expression.function.scalar.conditional.Case;
7574
import org.elasticsearch.xpack.esql.expression.function.scalar.conditional.ClampMax;
7675
import org.elasticsearch.xpack.esql.expression.function.scalar.conditional.ClampMin;
@@ -773,7 +772,7 @@ public static FunctionDescription description(FunctionDefinition def) {
773772
if (TimestampAware.class.isAssignableFrom(def.clazz())) {
774773
countOfParamsToDescribe--; // skip the implicit @timestamp parameter (last or last before Configuration)
775774
}
776-
if (EsqlConfigurationFunction.class.isAssignableFrom(def.clazz())) {
775+
if (ConfigurationFunction.class.isAssignableFrom(def.clazz())) {
777776
// this isn't enforced by the contract, but the convention is: func(..., Expression timestamp, Configuration config)
778777
assert Configuration.class.isAssignableFrom(params[params.length - 1].getType())
779778
: "The configuration parameter must be the last argument of an EsqlConfigurationFunction definition";

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/grouping/Bucket.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.elasticsearch.xpack.esql.core.tree.Source;
2626
import org.elasticsearch.xpack.esql.core.type.DataType;
2727
import org.elasticsearch.xpack.esql.expression.Foldables;
28+
import org.elasticsearch.xpack.esql.expression.function.ConfigurationFunction;
2829
import org.elasticsearch.xpack.esql.expression.function.Example;
2930
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
3031
import org.elasticsearch.xpack.esql.expression.function.FunctionType;
@@ -62,7 +63,8 @@
6263
public class Bucket extends GroupingFunction.EvaluatableGroupingFunction
6364
implements
6465
PostOptimizationVerificationAware,
65-
TwoOptionalArguments {
66+
TwoOptionalArguments,
67+
ConfigurationFunction {
6668
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(Expression.class, "Bucket", Bucket::new);
6769

6870
// TODO maybe we should just cover the whole of representable dates here - like ten years, 100 years, 1000 years, all the way up.
@@ -88,12 +90,6 @@ public class Bucket extends GroupingFunction.EvaluatableGroupingFunction
8890
Rounding.builder(TimeValue.timeValueMillis(10)),
8991
Rounding.builder(TimeValue.timeValueMillis(1)), };
9092

91-
/*
92-
* As Bucket already extends GroupingFunction, it can't extend EsqlConfigurationFunction, so we had to replicate here:
93-
* - The Configuration field
94-
* - HashCode
95-
* - Equals
96-
*/
9793
private final Configuration configuration;
9894
private final Expression field;
9995
private final Expression buckets;

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/grouping/TBucket.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import org.elasticsearch.xpack.esql.core.tree.Source;
1515
import org.elasticsearch.xpack.esql.core.type.DataType;
1616
import org.elasticsearch.xpack.esql.expression.SurrogateExpression;
17+
import org.elasticsearch.xpack.esql.expression.function.ConfigurationFunction;
1718
import org.elasticsearch.xpack.esql.expression.function.Example;
1819
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
1920
import org.elasticsearch.xpack.esql.expression.function.FunctionType;
@@ -32,15 +33,13 @@
3233
/**
3334
* Splits dates into a given number of buckets. The span is derived from a time range provided.
3435
*/
35-
public class TBucket extends GroupingFunction.EvaluatableGroupingFunction implements SurrogateExpression, TimestampAware {
36+
public class TBucket extends GroupingFunction.EvaluatableGroupingFunction
37+
implements
38+
SurrogateExpression,
39+
TimestampAware,
40+
ConfigurationFunction {
3641
public static final String NAME = "TBucket";
3742

38-
/*
39-
* As Bucket already extends GroupingFunction, it can't extend EsqlConfigurationFunction, so we had to replicate here:
40-
* - The Configuration field
41-
* - HashCode
42-
* - Equals
43-
*/
4443
private final Configuration configuration;
4544
private final Expression buckets;
4645
private final Expression timestamp;

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@
99

1010
import org.elasticsearch.xpack.esql.core.expression.Expression;
1111
import org.elasticsearch.xpack.esql.core.tree.Source;
12+
import org.elasticsearch.xpack.esql.expression.function.ConfigurationFunction;
1213
import org.elasticsearch.xpack.esql.session.Configuration;
1314

1415
import java.util.List;
1516
import java.util.Objects;
1617

17-
public abstract class EsqlConfigurationFunction extends EsqlScalarFunction {
18+
/**
19+
* Implementation of {@link ConfigurationFunction}.
20+
* <p>
21+
* Extend this class if possible instead of the base interface.
22+
* </p>
23+
*/
24+
public abstract class EsqlConfigurationFunction extends EsqlScalarFunction implements ConfigurationFunction {
1825

1926
private final Configuration configuration;
2027

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/grouping/BucketTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static void dateCases(List<TestCaseSupplier> suppliers, String name, Lon
114114
+ "rounding=Rounding[DAY_OF_MONTH in Z][fixed to midnight]]",
115115
DataType.DATETIME,
116116
resultsMatcher(args)
117-
);
117+
).withStaticConfiguration();
118118
}));
119119
// same as above, but a low bucket count and datetime bounds that match it (at hour span)
120120
suppliers.add(new TestCaseSupplier(name, List.of(DataType.DATETIME, DataType.INTEGER, fromType, toType), () -> {
@@ -128,7 +128,7 @@ private static void dateCases(List<TestCaseSupplier> suppliers, String name, Lon
128128
"DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding[3600000 in Z][fixed]]",
129129
DataType.DATETIME,
130130
equalTo(Rounding.builder(Rounding.DateTimeUnit.HOUR_OF_DAY).build().prepareForUnknown().round(date.getAsLong()))
131-
);
131+
).withStaticConfiguration();
132132
}));
133133
}
134134
}
@@ -161,7 +161,7 @@ private static void dateCasesWithSpan(
161161
"DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding" + spanStr + "]",
162162
DataType.DATETIME,
163163
resultsMatcher(args)
164-
);
164+
).withStaticConfiguration();
165165
}));
166166
}
167167

@@ -182,7 +182,7 @@ private static void dateNanosCasesWithSpan(
182182
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
183183
DataType.DATE_NANOS,
184184
resultsMatcher(args)
185-
);
185+
).withStaticConfiguration();
186186
}));
187187
}
188188

@@ -201,7 +201,7 @@ private static void dateNanosCases(List<TestCaseSupplier> suppliers, String name
201201
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
202202
DataType.DATE_NANOS,
203203
resultsMatcher(args)
204-
);
204+
).withStaticConfiguration();
205205
}));
206206
// same as above, but a low bucket count and datetime bounds that match it (at hour span)
207207
suppliers.add(new TestCaseSupplier(name, List.of(DataType.DATE_NANOS, DataType.INTEGER, fromType, toType), () -> {
@@ -215,7 +215,7 @@ private static void dateNanosCases(List<TestCaseSupplier> suppliers, String name
215215
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
216216
DataType.DATE_NANOS,
217217
equalTo(Rounding.builder(Rounding.DateTimeUnit.HOUR_OF_DAY).build().prepareForUnknown().round(date.getAsLong()))
218-
);
218+
).withStaticConfiguration();
219219
}));
220220
}
221221
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/grouping/TBucketTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private static void dateCasesWithSpan(
9595
"DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding" + spanStr + "]",
9696
DataType.DATETIME,
9797
resultsMatcher(args)
98-
);
98+
).withStaticConfiguration();
9999
}));
100100
}
101101

@@ -115,7 +115,7 @@ private static void dateNanosCasesWithSpan(
115115
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
116116
DataType.DATE_NANOS,
117117
resultsMatcher(args)
118-
);
118+
).withStaticConfiguration();
119119
}));
120120
}
121121

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/date/DateTruncTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private static List<TestCaseSupplier> ofDatePeriod(Period period, long value, St
7676
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
7777
DataType.DATETIME,
7878
equalTo(toMillis(expectedDate))
79-
)
79+
).withStaticConfiguration()
8080
),
8181
new TestCaseSupplier(
8282
List.of(DataType.DATE_PERIOD, DataType.DATE_NANOS),
@@ -88,7 +88,7 @@ private static List<TestCaseSupplier> ofDatePeriod(Period period, long value, St
8888
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
8989
DataType.DATE_NANOS,
9090
equalTo(toNanos(expectedDate))
91-
)
91+
).withStaticConfiguration()
9292
)
9393
);
9494
}
@@ -105,7 +105,7 @@ private static List<TestCaseSupplier> ofDuration(Duration duration, long value,
105105
Matchers.startsWith("DateTruncDatetimeEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
106106
DataType.DATETIME,
107107
equalTo(toMillis(expectedDate))
108-
)
108+
).withStaticConfiguration()
109109
),
110110
new TestCaseSupplier(
111111
List.of(DataType.TIME_DURATION, DataType.DATE_NANOS),
@@ -117,7 +117,7 @@ private static List<TestCaseSupplier> ofDuration(Duration duration, long value,
117117
Matchers.startsWith("DateTruncDateNanosEvaluator[fieldVal=Attribute[channel=0], rounding=Rounding["),
118118
DataType.DATE_NANOS,
119119
equalTo(toNanos(expectedDate))
120-
)
120+
).withStaticConfiguration()
121121
)
122122
);
123123
}

0 commit comments

Comments
 (0)