Skip to content

Commit b8f4677

Browse files
authored
Replace build with scope parameter (#118643)
This change replaces the build with scope parameter in order to make it cleaner when parameter is build and how it is used.
1 parent 51eb386 commit b8f4677

File tree

13 files changed

+61
-29
lines changed

13 files changed

+61
-29
lines changed

x-pack/plugin/esql/compute/ann/src/main/java/org/elasticsearch/compute/ann/Fixed.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.lang.annotation.Retention;
1212
import java.lang.annotation.RetentionPolicy;
1313
import java.lang.annotation.Target;
14-
import java.util.function.Function;
1514

1615
/**
1716
* Used on parameters on methods annotated with {@link Evaluator} to indicate
@@ -27,12 +26,23 @@
2726
boolean includeInToString() default true;
2827

2928
/**
30-
* Should the Evaluator's factory build this per evaluator with a
31-
* {@code Function<DriverContext, T>} or just take fixed implementation?
32-
* This is typically set to {@code true} to use the {@link Function}
33-
* to make "scratch" objects which have to be isolated in a single thread.
34-
* This is typically set to {@code false} when the parameter is simply
35-
* immutable and can be shared.
29+
* Defines the scope of the parameter.
30+
* - SINGLETON (default) will build a single instance and share it across all evaluators
31+
* - THREAD_LOCAL will build a new instance for each evaluator thread
3632
*/
37-
boolean build() default false;
33+
Scope scope() default Scope.SINGLETON;
34+
35+
/**
36+
* Defines the parameter scope
37+
*/
38+
enum Scope {
39+
/**
40+
* Should be used for immutable parameters that can be shared across different threads
41+
*/
42+
SINGLETON,
43+
/**
44+
* Should be used for mutable or not thread safe parameters
45+
*/
46+
THREAD_LOCAL,
47+
}
3848
}

x-pack/plugin/esql/compute/gen/src/main/java/org/elasticsearch/compute/gen/EvaluatorImplementer.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.squareup.javapoet.TypeSpec;
1717

1818
import org.elasticsearch.compute.ann.Fixed;
19+
import org.elasticsearch.compute.ann.Fixed.Scope;
1920

2021
import java.util.ArrayList;
2122
import java.util.Arrays;
@@ -725,7 +726,7 @@ public String closeInvocation() {
725726
}
726727
}
727728

728-
private record FixedProcessFunctionArg(TypeName type, String name, boolean includeInToString, boolean build, boolean releasable)
729+
private record FixedProcessFunctionArg(TypeName type, String name, boolean includeInToString, Scope scope, boolean releasable)
729730
implements
730731
ProcessFunctionArg {
731732
@Override
@@ -762,12 +763,18 @@ public void implementFactoryCtor(MethodSpec.Builder builder) {
762763
}
763764

764765
private TypeName factoryFieldType() {
765-
return build ? ParameterizedTypeName.get(ClassName.get(Function.class), DRIVER_CONTEXT, type.box()) : type;
766+
return switch (scope) {
767+
case SINGLETON -> type;
768+
case THREAD_LOCAL -> ParameterizedTypeName.get(ClassName.get(Function.class), DRIVER_CONTEXT, type.box());
769+
};
766770
}
767771

768772
@Override
769773
public String factoryInvocation(MethodSpec.Builder factoryMethodBuilder) {
770-
return build ? name + ".apply(context)" : name;
774+
return switch (scope) {
775+
case SINGLETON -> name;
776+
case THREAD_LOCAL -> name + ".apply(context)";
777+
};
771778
}
772779

773780
@Override
@@ -1020,7 +1027,7 @@ private ProcessFunction(
10201027
type,
10211028
name,
10221029
fixed.includeInToString(),
1023-
fixed.build(),
1030+
fixed.scope(),
10241031
Types.extendsSuper(types, v.asType(), "org.elasticsearch.core.Releasable")
10251032
)
10261033
);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Base64;
3131
import java.util.List;
3232

33+
import static org.elasticsearch.compute.ann.Fixed.Scope.THREAD_LOCAL;
3334
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString;
3435
import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD;
3536

@@ -85,7 +86,7 @@ protected NodeInfo<? extends Expression> info() {
8586
}
8687

8788
@Evaluator()
88-
static BytesRef process(BytesRef field, @Fixed(includeInToString = false, build = true) BytesRefBuilder oScratch) {
89+
static BytesRef process(BytesRef field, @Fixed(includeInToString = false, scope = THREAD_LOCAL) BytesRefBuilder oScratch) {
8990
byte[] bytes = new byte[field.length];
9091
System.arraycopy(field.bytes, field.offset, bytes, 0, field.length);
9192
oScratch.grow(field.length);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Base64;
3131
import java.util.List;
3232

33+
import static org.elasticsearch.compute.ann.Fixed.Scope.THREAD_LOCAL;
3334
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString;
3435
import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD;
3536

@@ -78,7 +79,7 @@ protected NodeInfo<? extends Expression> info() {
7879
}
7980

8081
@Evaluator(warnExceptions = { ArithmeticException.class })
81-
static BytesRef process(BytesRef field, @Fixed(includeInToString = false, build = true) BytesRefBuilder oScratch) {
82+
static BytesRef process(BytesRef field, @Fixed(includeInToString = false, scope = THREAD_LOCAL) BytesRefBuilder oScratch) {
8283
int outLength = Math.multiplyExact(4, (Math.addExact(field.length, 2) / 3));
8384
byte[] bytes = new byte[field.length];
8485
System.arraycopy(field.bytes, field.offset, bytes, 0, field.length);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Arrays;
3131
import java.util.List;
3232

33+
import static org.elasticsearch.compute.ann.Fixed.Scope.THREAD_LOCAL;
3334
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
3435
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
3536
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.THIRD;
@@ -138,7 +139,7 @@ static BytesRef process(
138139
BytesRef ip,
139140
int prefixLengthV4,
140141
int prefixLengthV6,
141-
@Fixed(includeInToString = false, build = true) BytesRef scratch
142+
@Fixed(includeInToString = false, scope = THREAD_LOCAL) BytesRef scratch
142143
) {
143144
if (prefixLengthV4 < 0 || prefixLengthV4 > 32) {
144145
throw new IllegalArgumentException("Prefix length v4 must be in range [0, 32], found " + prefixLengthV4);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.Arrays;
3434
import java.util.List;
3535

36+
import static org.elasticsearch.compute.ann.Fixed.Scope.THREAD_LOCAL;
3637
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
3738
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
3839
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isFoldable;
@@ -144,7 +145,7 @@ static void process(
144145
DoubleBlock.Builder builder,
145146
int position,
146147
DoubleBlock block,
147-
@Fixed(includeInToString = false, build = true) CompensatedSum sum,
148+
@Fixed(includeInToString = false, scope = THREAD_LOCAL) CompensatedSum sum,
148149
@Fixed double p
149150
) {
150151
sum.reset(0, 0);

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.Arrays;
3636
import java.util.List;
3737

38+
import static org.elasticsearch.compute.ann.Fixed.Scope.THREAD_LOCAL;
3839
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
3940
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
4041
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType;
@@ -167,7 +168,7 @@ static void process(
167168
int position,
168169
DoubleBlock values,
169170
double percentile,
170-
@Fixed(includeInToString = false, build = true) DoubleSortingScratch scratch
171+
@Fixed(includeInToString = false, scope = THREAD_LOCAL) DoubleSortingScratch scratch
171172
) {
172173
int valueCount = values.getValueCount(position);
173174
int firstValueIndex = values.getFirstValueIndex(position);
@@ -190,7 +191,7 @@ static void process(
190191
int position,
191192
IntBlock values,
192193
double percentile,
193-
@Fixed(includeInToString = false, build = true) IntSortingScratch scratch
194+
@Fixed(includeInToString = false, scope = THREAD_LOCAL) IntSortingScratch scratch
194195
) {
195196
int valueCount = values.getValueCount(position);
196197
int firstValueIndex = values.getFirstValueIndex(position);
@@ -213,7 +214,7 @@ static void process(
213214
int position,
214215
LongBlock values,
215216
double percentile,
216-
@Fixed(includeInToString = false, build = true) LongSortingScratch scratch
217+
@Fixed(includeInToString = false, scope = THREAD_LOCAL) LongSortingScratch scratch
217218
) {
218219
int valueCount = values.getValueCount(position);
219220
int firstValueIndex = values.getFirstValueIndex(position);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.stream.Stream;
3333

3434
import static org.elasticsearch.common.unit.ByteSizeUnit.MB;
35+
import static org.elasticsearch.compute.ann.Fixed.Scope.THREAD_LOCAL;
3536
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.DEFAULT;
3637
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString;
3738

@@ -111,7 +112,7 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
111112
}
112113

113114
@Evaluator
114-
static BytesRef process(@Fixed(includeInToString = false, build = true) BreakingBytesRefBuilder scratch, BytesRef[] values) {
115+
static BytesRef process(@Fixed(includeInToString = false, scope = THREAD_LOCAL) BreakingBytesRefBuilder scratch, BytesRef[] values) {
115116
scratch.grow(checkedTotalLength(values));
116117
scratch.clear();
117118
for (int i = 0; i < values.length; i++) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Arrays;
3131
import java.util.List;
3232

33+
import static org.elasticsearch.compute.ann.Fixed.Scope.THREAD_LOCAL;
3334
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
3435
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
3536
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString;
@@ -77,8 +78,8 @@ public String getWriteableName() {
7778

7879
@Evaluator
7980
static BytesRef process(
80-
@Fixed(includeInToString = false, build = true) BytesRef out,
81-
@Fixed(includeInToString = false, build = true) UnicodeUtil.UTF8CodePoint cp,
81+
@Fixed(includeInToString = false, scope = THREAD_LOCAL) BytesRef out,
82+
@Fixed(includeInToString = false, scope = THREAD_LOCAL) UnicodeUtil.UTF8CodePoint cp,
8283
BytesRef str,
8384
int length
8485
) {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.List;
3232

3333
import static org.elasticsearch.common.unit.ByteSizeUnit.MB;
34+
import static org.elasticsearch.compute.ann.Fixed.Scope.THREAD_LOCAL;
3435
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
3536
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
3637
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString;
@@ -101,15 +102,19 @@ public boolean foldable() {
101102

102103
@Evaluator(extraName = "Constant", warnExceptions = { IllegalArgumentException.class })
103104
static BytesRef processConstantNumber(
104-
@Fixed(includeInToString = false, build = true) BreakingBytesRefBuilder scratch,
105+
@Fixed(includeInToString = false, scope = THREAD_LOCAL) BreakingBytesRefBuilder scratch,
105106
BytesRef str,
106107
@Fixed int number
107108
) {
108109
return processInner(scratch, str, number);
109110
}
110111

111112
@Evaluator(warnExceptions = { IllegalArgumentException.class })
112-
static BytesRef process(@Fixed(includeInToString = false, build = true) BreakingBytesRefBuilder scratch, BytesRef str, int number) {
113+
static BytesRef process(
114+
@Fixed(includeInToString = false, scope = THREAD_LOCAL) BreakingBytesRefBuilder scratch,
115+
BytesRef str,
116+
int number
117+
) {
113118
if (number < 0) {
114119
throw new IllegalArgumentException("Number parameter cannot be negative, found [" + number + "]");
115120
}

0 commit comments

Comments
 (0)