Skip to content

Commit 85f957e

Browse files
ioanatiageorgewallace
authored andcommitted
Better DataType string checks (elastic#114863)
* Use DataType.isString * Add DataType.stringTypes() * Fix shouldHideSignature check
1 parent 508f65e commit 85f957e

File tree

21 files changed

+32
-59
lines changed

21 files changed

+32
-59
lines changed

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/DataType.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,8 @@ public enum DataType {
266266
.sorted(Comparator.comparing(DataType::typeName))
267267
.toList();
268268

269+
private static final Collection<DataType> STRING_TYPES = DataType.types().stream().filter(DataType::isString).toList();
270+
269271
private static final Map<String, DataType> NAME_TO_TYPE = TYPES.stream().collect(toUnmodifiableMap(DataType::typeName, t -> t));
270272

271273
private static final Map<String, DataType> ES_TO_TYPE;
@@ -292,6 +294,10 @@ public static Collection<DataType> types() {
292294
return TYPES;
293295
}
294296

297+
public static Collection<DataType> stringTypes() {
298+
return STRING_TYPES;
299+
}
300+
295301
/**
296302
* Resolve a type from a name. This name is sometimes user supplied,
297303
* like in the case of {@code ::<typename>} and is sometimes the name

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/type/DataTypeConverter.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@
3030
import static org.elasticsearch.xpack.esql.core.type.DataType.FLOAT;
3131
import static org.elasticsearch.xpack.esql.core.type.DataType.INTEGER;
3232
import static org.elasticsearch.xpack.esql.core.type.DataType.IP;
33-
import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD;
3433
import static org.elasticsearch.xpack.esql.core.type.DataType.LONG;
3534
import static org.elasticsearch.xpack.esql.core.type.DataType.NULL;
3635
import static org.elasticsearch.xpack.esql.core.type.DataType.SHORT;
37-
import static org.elasticsearch.xpack.esql.core.type.DataType.TEXT;
3836
import static org.elasticsearch.xpack.esql.core.type.DataType.UNSIGNED_LONG;
3937
import static org.elasticsearch.xpack.esql.core.type.DataType.VERSION;
4038
import static org.elasticsearch.xpack.esql.core.type.DataType.isDateTime;
@@ -62,7 +60,7 @@ public static Converter converterFor(DataType from, DataType to) {
6260
return DefaultConverter.TO_NULL;
6361
}
6462
// proper converters
65-
if (to == KEYWORD || to == TEXT) {
63+
if (isString(to)) {
6664
return conversionToString(from);
6765
}
6866
if (to == LONG) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public AggregatorFunctionSupplier supplier(List<Integer> inputChannels) {
209209
if (type == DataType.DOUBLE) {
210210
return new CountDistinctDoubleAggregatorFunctionSupplier(inputChannels, precision);
211211
}
212-
if (type == DataType.KEYWORD || type == DataType.IP || type == DataType.VERSION || type == DataType.TEXT) {
212+
if (DataType.isString(type) || type == DataType.IP || type == DataType.VERSION) {
213213
return new CountDistinctBytesRefAggregatorFunctionSupplier(inputChannels, precision);
214214
}
215215
throw EsqlIllegalArgumentException.illegalDataType(type);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public final AggregatorFunctionSupplier supplier(List<Integer> inputChannels) {
128128
if (type == DataType.IP) {
129129
return new MaxIpAggregatorFunctionSupplier(inputChannels);
130130
}
131-
if (type == DataType.VERSION || type == DataType.KEYWORD || type == DataType.TEXT) {
131+
if (type == DataType.VERSION || DataType.isString(type)) {
132132
return new MaxBytesRefAggregatorFunctionSupplier(inputChannels);
133133
}
134134
throw EsqlIllegalArgumentException.illegalDataType(type);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public final AggregatorFunctionSupplier supplier(List<Integer> inputChannels) {
128128
if (type == DataType.IP) {
129129
return new MinIpAggregatorFunctionSupplier(inputChannels);
130130
}
131-
if (type == DataType.VERSION || type == DataType.KEYWORD || type == DataType.TEXT) {
131+
if (type == DataType.VERSION || DataType.isString(type)) {
132132
return new MinBytesRefAggregatorFunctionSupplier(inputChannels);
133133
}
134134
throw EsqlIllegalArgumentException.illegalDataType(type);

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,7 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
155155
if (dataType == DataType.LONG || dataType == DataType.DATETIME) {
156156
return new GreatestLongEvaluator.Factory(source(), factories);
157157
}
158-
if (dataType == DataType.KEYWORD
159-
|| dataType == DataType.TEXT
160-
|| dataType == DataType.IP
161-
|| dataType == DataType.VERSION
162-
|| dataType == DataType.UNSUPPORTED) {
158+
if (DataType.isString(dataType) || dataType == DataType.IP || dataType == DataType.VERSION || dataType == DataType.UNSUPPORTED) {
163159

164160
return new GreatestBytesRefEvaluator.Factory(source(), factories);
165161
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,7 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
154154
if (dataType == DataType.LONG || dataType == DataType.DATETIME) {
155155
return new LeastLongEvaluator.Factory(source(), factories);
156156
}
157-
if (dataType == DataType.KEYWORD
158-
|| dataType == DataType.TEXT
159-
|| dataType == DataType.IP
160-
|| dataType == DataType.VERSION
161-
|| dataType == DataType.UNSUPPORTED) {
157+
if (DataType.isString(dataType) || dataType == DataType.IP || dataType == DataType.VERSION || dataType == DataType.UNSUPPORTED) {
162158

163159
return new LeastBytesRefEvaluator.Factory(source(), factories);
164160
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public abstract class AbstractConvertFunction extends UnaryScalarFunction {
4848

4949
// the numeric types convert functions need to handle; the other numeric types are converted upstream to one of these
5050
private static final List<DataType> NUMERIC_TYPES = List.of(DataType.INTEGER, DataType.LONG, DataType.UNSIGNED_LONG, DataType.DOUBLE);
51-
public static final List<DataType> STRING_TYPES = DataType.types().stream().filter(DataType::isString).toList();
5251

5352
protected AbstractConvertFunction(Source source, Expression field) {
5453
super(source, field);
@@ -90,9 +89,9 @@ private static String supportedTypesNames(Set<DataType> types) {
9089
NUMERIC_TYPES.forEach(supportTypes::remove);
9190
}
9291

93-
if (types.containsAll(STRING_TYPES)) {
92+
if (types.containsAll(DataType.stringTypes())) {
9493
supportedTypesNames.add("string");
95-
STRING_TYPES.forEach(supportTypes::remove);
94+
DataType.stringTypes().forEach(supportTypes::remove);
9695
}
9796

9897
supportTypes.forEach(t -> supportedTypesNames.add(t.nameUpper().toLowerCase(Locale.ROOT)));

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/predicate/operator/comparison/InsensitiveEqualsMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public final ExpressionEvaluator.Factory map(InsensitiveEquals bc, Layout layout
3434

3535
var leftEval = toEvaluator(bc.left(), layout);
3636
var rightEval = toEvaluator(bc.right(), layout);
37-
if (leftType == DataType.KEYWORD || leftType == DataType.TEXT) {
37+
if (DataType.isString(leftType)) {
3838
if (bc.right().foldable() && DataType.isString(rightType)) {
3939
BytesRef rightVal = BytesRefs.toBytesRef(bc.right().fold());
4040
Automaton automaton = InsensitiveEquals.automaton(rightVal);

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,13 +1283,6 @@ public void allMemoryReleased() {
12831283
}
12841284
}
12851285

1286-
/**
1287-
* All string types (keyword, text, match_only_text, etc).
1288-
*/
1289-
protected static DataType[] strings() {
1290-
return DataType.types().stream().filter(DataType::isString).toArray(DataType[]::new);
1291-
}
1292-
12931286
/**
12941287
* Validate that we know the types for all the test cases already created
12951288
* @param suppliers - list of suppliers before adding in the illegal type combinations
@@ -1316,10 +1309,9 @@ private static boolean isAggregation() {
13161309
*/
13171310
private static boolean shouldHideSignature(List<DataType> argTypes, DataType returnType) {
13181311
for (DataType dt : DataType.UNDER_CONSTRUCTION.keySet()) {
1319-
if (returnType == dt) {
1312+
if (returnType == dt || argTypes.contains(dt)) {
13201313
return true;
13211314
}
1322-
return argTypes.contains(dt);
13231315
}
13241316
return false;
13251317
}

0 commit comments

Comments
 (0)