forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ESQL] Moving argument compatibility checking for Equals (elastic#107546
) Fixed the conflicts, and re-submitting. Please see elastic#105217 for full details, history, and discussion. I'll use the commit message from that PR as well. Continuing my work from elastic#104490, this PR moves the parameter compatibility checking for Equals into the type resolution check. This is a somewhat bigger change than for Add, as there was no ES|QL base class for binary comparison operators before this. I've added EsqlBinaryComparison as that base class, and migrated all of the binary comparisons to be based off of that (except for NullEquals, see note below). In order to maintain compatibility with the current behavior, I've kept it so that unsigned longs are only inter-operable with other unsigned longs. We've talked a lot about changing that, and I consider this work a prerequisite for that. I've also added a bunch of test cases to Equals and NotEquals, which should have the side effect of filling out the type support table in the equals docs. As noted in the comments, I'll have follow up PRs for the other binary comparisons to add tests, but this PR is already too long. Note about NullEquals: There is an ES|QL NullEquals class, which inherits from the QL version, but I don't think it works. I didn't see any tests or docs for it, and trying it out in the demo instance gave me a syntax error. I think we need to delve into what's going on there, but this PR isn't the right place for it. This reverts commit 225edaf.
- Loading branch information
1 parent
e6d421c
commit 73a17a1
Showing
23 changed files
with
872 additions
and
262 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
...lasticsearch/xpack/esql/evaluator/predicate/operator/comparison/EsqlBinaryComparison.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
package org.elasticsearch.xpack.esql.evaluator.predicate.operator.comparison; | ||
|
||
import org.elasticsearch.compute.operator.EvalOperator; | ||
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException; | ||
import org.elasticsearch.xpack.esql.evaluator.mapper.EvaluatorMapper; | ||
import org.elasticsearch.xpack.esql.expression.function.scalar.math.Cast; | ||
import org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.EsqlArithmeticOperation; | ||
import org.elasticsearch.xpack.esql.type.EsqlDataTypeRegistry; | ||
import org.elasticsearch.xpack.ql.expression.Expression; | ||
import org.elasticsearch.xpack.ql.expression.TypeResolutions; | ||
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.BinaryComparison; | ||
import org.elasticsearch.xpack.ql.expression.predicate.operator.comparison.BinaryComparisonProcessor; | ||
import org.elasticsearch.xpack.ql.tree.Source; | ||
import org.elasticsearch.xpack.ql.type.DataType; | ||
import org.elasticsearch.xpack.ql.type.DataTypes; | ||
|
||
import java.time.ZoneId; | ||
import java.util.Map; | ||
import java.util.function.Function; | ||
|
||
import static org.elasticsearch.common.logging.LoggerMessageFormat.format; | ||
import static org.elasticsearch.xpack.ql.type.DataTypes.UNSIGNED_LONG; | ||
|
||
public abstract class EsqlBinaryComparison extends BinaryComparison implements EvaluatorMapper { | ||
|
||
private final Map<DataType, EsqlArithmeticOperation.BinaryEvaluator> evaluatorMap; | ||
|
||
protected EsqlBinaryComparison( | ||
Source source, | ||
Expression left, | ||
Expression right, | ||
/* TODO: BinaryComparisonOperator is an enum with a bunch of functionality we don't really want. We should extract an interface and | ||
create a symbol only version like we did for BinaryArithmeticOperation. Ideally, they could be the same class. | ||
*/ | ||
BinaryComparisonProcessor.BinaryComparisonOperation operation, | ||
Map<DataType, EsqlArithmeticOperation.BinaryEvaluator> evaluatorMap | ||
) { | ||
this(source, left, right, operation, null, evaluatorMap); | ||
} | ||
|
||
protected EsqlBinaryComparison( | ||
Source source, | ||
Expression left, | ||
Expression right, | ||
BinaryComparisonProcessor.BinaryComparisonOperation operation, | ||
// TODO: We are definitely not doing the right thing with this zoneId | ||
ZoneId zoneId, | ||
Map<DataType, EsqlArithmeticOperation.BinaryEvaluator> evaluatorMap | ||
) { | ||
super(source, left, right, operation, zoneId); | ||
this.evaluatorMap = evaluatorMap; | ||
} | ||
|
||
@Override | ||
public EvalOperator.ExpressionEvaluator.Factory toEvaluator( | ||
Function<Expression, EvalOperator.ExpressionEvaluator.Factory> toEvaluator | ||
) { | ||
// Our type is always boolean, so figure out the evaluator type from the inputs | ||
DataType commonType = EsqlDataTypeRegistry.INSTANCE.commonType(left().dataType(), right().dataType()); | ||
EvalOperator.ExpressionEvaluator.Factory lhs; | ||
EvalOperator.ExpressionEvaluator.Factory rhs; | ||
|
||
if (commonType.isNumeric()) { | ||
lhs = Cast.cast(source(), left().dataType(), commonType, toEvaluator.apply(left())); | ||
rhs = Cast.cast(source(), right().dataType(), commonType, toEvaluator.apply(right())); | ||
} else { | ||
lhs = toEvaluator.apply(left()); | ||
rhs = toEvaluator.apply(right()); | ||
} | ||
|
||
if (evaluatorMap.containsKey(commonType) == false) { | ||
throw new EsqlIllegalArgumentException("Unsupported type " + left().dataType()); | ||
} | ||
return evaluatorMap.get(commonType).apply(source(), lhs, rhs); | ||
} | ||
|
||
@Override | ||
public Boolean fold() { | ||
return (Boolean) EvaluatorMapper.super.fold(); | ||
} | ||
|
||
@Override | ||
protected TypeResolution resolveType() { | ||
TypeResolution typeResolution = super.resolveType(); | ||
if (typeResolution.unresolved()) { | ||
return typeResolution; | ||
} | ||
|
||
return checkCompatibility(); | ||
} | ||
|
||
@Override | ||
protected TypeResolution resolveInputType(Expression e, TypeResolutions.ParamOrdinal paramOrdinal) { | ||
return TypeResolutions.isType( | ||
e, | ||
evaluatorMap::containsKey, | ||
sourceText(), | ||
paramOrdinal, | ||
evaluatorMap.keySet().stream().map(DataType::typeName).toArray(String[]::new) | ||
); | ||
} | ||
|
||
/** | ||
* Check if the two input types are compatible for this operation | ||
* | ||
* @return TypeResolution.TYPE_RESOLVED iff the types are compatible. Otherwise, an appropriate type resolution error. | ||
*/ | ||
protected TypeResolution checkCompatibility() { | ||
DataType leftType = left().dataType(); | ||
DataType rightType = right().dataType(); | ||
|
||
// Unsigned long is only interoperable with other unsigned longs | ||
if ((rightType == UNSIGNED_LONG && (false == (leftType == UNSIGNED_LONG || leftType == DataTypes.NULL))) | ||
|| (leftType == UNSIGNED_LONG && (false == (rightType == UNSIGNED_LONG || rightType == DataTypes.NULL)))) { | ||
return new TypeResolution(formatIncompatibleTypesMessage()); | ||
} | ||
|
||
if ((leftType.isNumeric() && rightType.isNumeric()) | ||
|| (DataTypes.isString(leftType) && DataTypes.isString(rightType)) | ||
|| leftType.equals(rightType) | ||
|| DataTypes.isNull(leftType) | ||
|| DataTypes.isNull(rightType)) { | ||
return TypeResolution.TYPE_RESOLVED; | ||
} | ||
return new TypeResolution(formatIncompatibleTypesMessage()); | ||
} | ||
|
||
public String formatIncompatibleTypesMessage() { | ||
if (left().dataType().equals(UNSIGNED_LONG)) { | ||
return format( | ||
null, | ||
"first argument of [{}] is [unsigned_long] and second is [{}]. " | ||
+ "[unsigned_long] can only be operated on together with another [unsigned_long]", | ||
sourceText(), | ||
right().dataType().typeName() | ||
); | ||
} | ||
if (right().dataType().equals(UNSIGNED_LONG)) { | ||
return format( | ||
null, | ||
"first argument of [{}] is [{}] and second is [unsigned_long]. " | ||
+ "[unsigned_long] can only be operated on together with another [unsigned_long]", | ||
sourceText(), | ||
left().dataType().typeName() | ||
); | ||
} | ||
return format( | ||
null, | ||
"first argument of [{}] is [{}] so second argument must also be [{}] but was [{}]", | ||
sourceText(), | ||
left().dataType().isNumeric() ? "numeric" : left().dataType().typeName(), | ||
left().dataType().isNumeric() ? "numeric" : left().dataType().typeName(), | ||
right().dataType().typeName() | ||
); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.