Skip to content

Commit 5928582

Browse files
[ES|QL] Combine 3 commonTypes into one (#112220)
Combine 3 commonTypes into one.
1 parent e966d0d commit 5928582

File tree

15 files changed

+234
-155
lines changed

15 files changed

+234
-155
lines changed

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

Lines changed: 0 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import static org.elasticsearch.xpack.esql.core.type.DataType.UNSIGNED_LONG;
3939
import static org.elasticsearch.xpack.esql.core.type.DataType.VERSION;
4040
import static org.elasticsearch.xpack.esql.core.type.DataType.isDateTime;
41-
import static org.elasticsearch.xpack.esql.core.type.DataType.isPrimitiveAndSupported;
4241
import static org.elasticsearch.xpack.esql.core.type.DataType.isString;
4342
import static org.elasticsearch.xpack.esql.core.util.NumericUtils.UNSIGNED_LONG_MAX;
4443
import static org.elasticsearch.xpack.esql.core.util.NumericUtils.inUnsignedLongRange;
@@ -51,85 +50,6 @@ public final class DataTypeConverter {
5150

5251
private DataTypeConverter() {}
5352

54-
/**
55-
* Returns the type compatible with both left and right types
56-
* <p>
57-
* If one of the types is null - returns another type
58-
* If both types are numeric - returns type with the highest precision int &lt; long &lt; float &lt; double
59-
* If one of the types is string and another numeric - returns numeric
60-
*/
61-
public static DataType commonType(DataType left, DataType right) {
62-
if (left == right) {
63-
return left;
64-
}
65-
if (left == NULL) {
66-
return right;
67-
}
68-
if (right == NULL) {
69-
return left;
70-
}
71-
if (isString(left) && isString(right)) {
72-
if (left == TEXT || right == TEXT) {
73-
return TEXT;
74-
}
75-
if (left == KEYWORD) {
76-
return KEYWORD;
77-
}
78-
return right;
79-
}
80-
if (left.isNumeric() && right.isNumeric()) {
81-
int lsize = left.estimatedSize().orElseThrow();
82-
int rsize = right.estimatedSize().orElseThrow();
83-
// if one is int
84-
if (left.isWholeNumber()) {
85-
// promote the highest int
86-
if (right.isWholeNumber()) {
87-
if (left == UNSIGNED_LONG || right == UNSIGNED_LONG) {
88-
return UNSIGNED_LONG;
89-
}
90-
return lsize > rsize ? left : right;
91-
}
92-
// promote the rational
93-
return right;
94-
}
95-
// try the other side
96-
if (right.isWholeNumber()) {
97-
return left;
98-
}
99-
// promote the highest rational
100-
return lsize > rsize ? left : right;
101-
}
102-
if (isString(left)) {
103-
if (right.isNumeric()) {
104-
return right;
105-
}
106-
}
107-
if (isString(right)) {
108-
if (left.isNumeric()) {
109-
return left;
110-
}
111-
}
112-
113-
if (isDateTime(left) && isDateTime(right)) {
114-
return DATETIME;
115-
}
116-
117-
// none found
118-
return null;
119-
}
120-
121-
/**
122-
* Returns true if the from type can be converted to the to type, false - otherwise
123-
*/
124-
public static boolean canConvert(DataType from, DataType to) {
125-
// Special handling for nulls and if conversion is not requires
126-
if (from == to || from == NULL) {
127-
return true;
128-
}
129-
// only primitives are supported so far
130-
return isPrimitiveAndSupported(from) && isPrimitiveAndSupported(to) && converterFor(from, to) != null;
131-
}
132-
13353
/**
13454
* Get the conversion from one type to another.
13555
*/

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.compute.ann.Evaluator;
1313
import org.elasticsearch.compute.ann.Fixed;
1414
import org.elasticsearch.xpack.esql.core.expression.Expression;
15-
import org.elasticsearch.xpack.esql.core.expression.predicate.operator.arithmetic.BinaryComparisonInversible;
1615
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1716
import org.elasticsearch.xpack.esql.core.tree.Source;
1817
import org.elasticsearch.xpack.esql.core.util.NumericUtils;
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44
* 2.0; you may not use this file except in compliance with the Elastic License
55
* 2.0.
66
*/
7-
package org.elasticsearch.xpack.esql.core.expression.predicate.operator.arithmetic;
7+
package org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic;
88

99
import org.elasticsearch.xpack.esql.core.expression.Expression;
1010
import org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal;
1111
import org.elasticsearch.xpack.esql.core.expression.predicate.BinaryOperator;
12+
import org.elasticsearch.xpack.esql.core.expression.predicate.operator.arithmetic.BinaryArithmeticOperation;
1213
import org.elasticsearch.xpack.esql.core.tree.Source;
1314
import org.elasticsearch.xpack.esql.core.type.DataType;
14-
import org.elasticsearch.xpack.esql.core.type.DataTypeConverter;
1515

1616
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isNumeric;
17+
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.commonType;
1718

1819
public abstract class ArithmeticOperation extends BinaryOperator<Object, Object, Object, BinaryArithmeticOperation> {
1920

@@ -36,7 +37,7 @@ public ArithmeticOperation swapLeftAndRight() {
3637
@Override
3738
public DataType dataType() {
3839
if (dataType == null) {
39-
dataType = DataTypeConverter.commonType(left().dataType(), right().dataType());
40+
dataType = commonType(left().dataType(), right().dataType());
4041
}
4142
return dataType;
4243
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* 2.0.
66
*/
77

8-
package org.elasticsearch.xpack.esql.core.expression.predicate.operator.arithmetic;
8+
package org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic;
99

1010
import org.elasticsearch.xpack.esql.core.expression.Expression;
1111
import org.elasticsearch.xpack.esql.core.tree.Source;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.common.io.stream.StreamInput;
1212
import org.elasticsearch.compute.ann.Evaluator;
1313
import org.elasticsearch.xpack.esql.core.expression.Expression;
14-
import org.elasticsearch.xpack.esql.core.expression.predicate.operator.arithmetic.BinaryComparisonInversible;
1514
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1615
import org.elasticsearch.xpack.esql.core.tree.Source;
1716
import org.elasticsearch.xpack.esql.core.type.DataType;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,12 @@
1313
import org.elasticsearch.compute.operator.EvalOperator.ExpressionEvaluator;
1414
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
1515
import org.elasticsearch.xpack.esql.core.expression.Expression;
16-
import org.elasticsearch.xpack.esql.core.expression.predicate.operator.arithmetic.ArithmeticOperation;
1716
import org.elasticsearch.xpack.esql.core.expression.predicate.operator.arithmetic.BinaryArithmeticOperation;
1817
import org.elasticsearch.xpack.esql.core.tree.Source;
1918
import org.elasticsearch.xpack.esql.core.type.DataType;
2019
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper;
2120
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Cast;
2221
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
23-
import org.elasticsearch.xpack.esql.type.EsqlDataTypeRegistry;
2422

2523
import java.io.IOException;
2624
import java.util.List;
@@ -31,6 +29,7 @@
3129
import static org.elasticsearch.xpack.esql.core.type.DataType.INTEGER;
3230
import static org.elasticsearch.xpack.esql.core.type.DataType.LONG;
3331
import static org.elasticsearch.xpack.esql.core.type.DataType.UNSIGNED_LONG;
32+
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.commonType;
3433

3534
public abstract class EsqlArithmeticOperation extends ArithmeticOperation implements EvaluatorMapper {
3635
public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
@@ -133,7 +132,7 @@ public Object fold() {
133132

134133
public DataType dataType() {
135134
if (dataType == null) {
136-
dataType = EsqlDataTypeRegistry.INSTANCE.commonType(left().dataType(), right().dataType());
135+
dataType = commonType(left().dataType(), right().dataType());
137136
}
138137
return dataType;
139138
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.elasticsearch.common.io.stream.StreamInput;
1212
import org.elasticsearch.compute.ann.Evaluator;
1313
import org.elasticsearch.xpack.esql.core.expression.Expression;
14-
import org.elasticsearch.xpack.esql.core.expression.predicate.operator.arithmetic.BinaryComparisonInversible;
1514
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1615
import org.elasticsearch.xpack.esql.core.tree.Source;
1716
import org.elasticsearch.xpack.esql.core.util.NumericUtils;

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.elasticsearch.compute.ann.Evaluator;
1313
import org.elasticsearch.compute.ann.Fixed;
1414
import org.elasticsearch.xpack.esql.core.expression.Expression;
15-
import org.elasticsearch.xpack.esql.core.expression.predicate.operator.arithmetic.BinaryComparisonInversible;
1615
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1716
import org.elasticsearch.xpack.esql.core.tree.Source;
1817
import org.elasticsearch.xpack.esql.core.type.DataType;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Cast;
2323
import org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.EsqlArithmeticOperation;
2424
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
25-
import org.elasticsearch.xpack.esql.type.EsqlDataTypeRegistry;
2625

2726
import java.io.IOException;
2827
import java.time.ZoneId;
@@ -32,6 +31,7 @@
3231

3332
import static org.elasticsearch.common.logging.LoggerMessageFormat.format;
3433
import static org.elasticsearch.xpack.esql.core.type.DataType.UNSIGNED_LONG;
34+
import static org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter.commonType;
3535

3636
public abstract class EsqlBinaryComparison extends BinaryComparison implements EvaluatorMapper {
3737
public static List<NamedWriteableRegistry.Entry> getNamedWriteables() {
@@ -172,7 +172,7 @@ public EvalOperator.ExpressionEvaluator.Factory toEvaluator(
172172
Function<Expression, EvalOperator.ExpressionEvaluator.Factory> toEvaluator
173173
) {
174174
// Our type is always boolean, so figure out the evaluator type from the inputs
175-
DataType commonType = EsqlDataTypeRegistry.INSTANCE.commonType(left().dataType(), right().dataType());
175+
DataType commonType = commonType(left().dataType(), right().dataType());
176176
EvalOperator.ExpressionEvaluator.Factory lhs;
177177
EvalOperator.ExpressionEvaluator.Factory rhs;
178178

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.elasticsearch.xpack.esql.expression.function.scalar.EsqlScalarFunction;
2828
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Cast;
2929
import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
30-
import org.elasticsearch.xpack.esql.type.EsqlDataTypeRegistry;
30+
import org.elasticsearch.xpack.esql.type.EsqlDataTypeConverter;
3131

3232
import java.io.IOException;
3333
import java.util.BitSet;
@@ -269,7 +269,7 @@ private DataType commonType() {
269269
break;
270270
}
271271
}
272-
commonType = EsqlDataTypeRegistry.INSTANCE.commonType(commonType, e.dataType());
272+
commonType = EsqlDataTypeConverter.commonType(commonType, e.dataType());
273273
}
274274
return commonType;
275275
}

0 commit comments

Comments
 (0)