diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/TestCaseSupplier.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/TestCaseSupplier.java index 1413d1193acc3..7cfe950bb3144 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/TestCaseSupplier.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/TestCaseSupplier.java @@ -427,6 +427,30 @@ public static List forBinaryNotCasting( List rhsSuppliers, List warnings, boolean symmetric + ) { + return forBinaryNotCasting( + name, + lhsName, + rhsName, + expected, + expectedType, + lhsSuppliers, + rhsSuppliers, + (lhs, rhs) -> warnings, + symmetric + ); + } + + public static List forBinaryNotCasting( + String name, + String lhsName, + String rhsName, + BinaryOperator expected, + DataType expectedType, + List lhsSuppliers, + List rhsSuppliers, + BiFunction> warnings, + boolean symmetric ) { List suppliers = new ArrayList<>(); casesCrossProduct( @@ -434,7 +458,7 @@ public static List forBinaryNotCasting( lhsSuppliers, rhsSuppliers, (lhsType, rhsType) -> name + "[" + lhsName + "=Attribute[channel=0], " + rhsName + "=Attribute[channel=1]]", - (lhs, rhs) -> warnings, + warnings, suppliers, expectedType, symmetric @@ -970,6 +994,12 @@ public static List dateCases() { // 2286-11-20T17:46:40Z - +292278994-08-17T07:12:55.807Z () -> ESTestCase.randomLongBetween(10 * (long) 10e11, Long.MAX_VALUE), DataTypes.DATETIME + ), + new TypedDataSupplier( + "", + // very close to +292278994-08-17T07:12:55.807Z, the maximum supported millis since epoch + () -> ESTestCase.randomLongBetween(Long.MAX_VALUE / 100 * 99, Long.MAX_VALUE), + DataTypes.DATETIME ) ); } diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/predicate/operator/arithmetic/AddTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/predicate/operator/arithmetic/AddTests.java index 2596959c449db..2daf2688d6631 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/predicate/operator/arithmetic/AddTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/predicate/operator/arithmetic/AddTests.java @@ -21,9 +21,12 @@ import java.math.BigInteger; import java.time.Duration; import java.time.Period; +import java.time.temporal.TemporalAmount; import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.function.BiFunction; +import java.util.function.BinaryOperator; import java.util.function.Supplier; import static org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic.AbstractArithmeticTestCase.arithmeticExceptionOverflowCase; @@ -116,29 +119,35 @@ public static Iterable parameters() { ) ); + BinaryOperator result = (lhs, rhs) -> { + try { + return addDatesAndTemporalAmount(lhs, rhs); + } catch (ArithmeticException e) { + return null; + } + }; + BiFunction> warnings = (lhs, rhs) -> { + try { + addDatesAndTemporalAmount(lhs.data(), rhs.data()); + return List.of(); + } catch (ArithmeticException e) { + return List.of( + "Line -1:-1: evaluation of [] failed, treating result as null. Only first 20 failures recorded.", + "Line -1:-1: java.lang.ArithmeticException: long overflow" + ); + } + }; suppliers.addAll( TestCaseSupplier.forBinaryNotCasting( // TODO: There is an evaluator for Datetime + Period, so it should be tested. Similarly below. "No evaluator, the tests only trigger the folding code since Period is not representable", "lhs", "rhs", - (lhs, rhs) -> { - // this weird casting dance makes the expected value lambda symmetric - Long date; - Period period; - if (lhs instanceof Long) { - date = (Long) lhs; - period = (Period) rhs; - } else { - date = (Long) rhs; - period = (Period) lhs; - } - return asMillis(asDateTime(date).plus(period)); - }, + result, DataTypes.DATETIME, TestCaseSupplier.dateCases(), TestCaseSupplier.datePeriodCases(), - List.of(), + warnings, true ) ); @@ -148,23 +157,11 @@ public static Iterable parameters() { "No evaluator, the tests only trigger the folding code since Duration is not representable", "lhs", "rhs", - (lhs, rhs) -> { - // this weird casting dance makes the expected value lambda symmetric - Long date; - Duration duration; - if (lhs instanceof Long) { - date = (Long) lhs; - duration = (Duration) rhs; - } else { - date = (Long) rhs; - duration = (Duration) lhs; - } - return asMillis(asDateTime(date).plus(duration)); - }, + result, DataTypes.DATETIME, TestCaseSupplier.dateCases(), TestCaseSupplier.timeDurationCases(), - List.of(), + warnings, true ) ); @@ -269,6 +266,20 @@ private static String addErrorMessageString(boolean includeOrdinal, List args) { return new Add(source, args.get(0), args.get(1));