|
@@ -10,7 +10,6 @@ package org.elasticsearch.xpack.esql.expression.predicate.operator.arithmetic;
|
|
|
import com.carrotsearch.randomizedtesting.annotations.Name;
|
|
|
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
|
|
|
|
|
|
-import org.elasticsearch.compute.data.Block;
|
|
|
import org.elasticsearch.xpack.esql.VerificationException;
|
|
|
import org.elasticsearch.xpack.esql.core.expression.Expression;
|
|
|
import org.elasticsearch.xpack.esql.core.expression.Literal;
|
|
@@ -18,6 +17,7 @@ import org.elasticsearch.xpack.esql.core.tree.Source;
|
|
|
import org.elasticsearch.xpack.esql.core.type.DataType;
|
|
|
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
|
|
|
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
|
|
|
+import org.hamcrest.Matchers;
|
|
|
|
|
|
import java.time.Duration;
|
|
|
import java.time.Period;
|
|
@@ -25,7 +25,6 @@ import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
-import static org.elasticsearch.compute.data.BlockUtils.toJavaObject;
|
|
|
import static org.elasticsearch.xpack.esql.EsqlTestUtils.randomLiteral;
|
|
|
import static org.hamcrest.Matchers.equalTo;
|
|
|
|
|
@@ -97,19 +96,19 @@ public class NegTests extends AbstractScalarFunctionTestCase {
|
|
|
suppliers.addAll(List.of(new TestCaseSupplier("Duration", List.of(DataType.TIME_DURATION), () -> {
|
|
|
Duration arg = (Duration) randomLiteral(DataType.TIME_DURATION).value();
|
|
|
return new TestCaseSupplier.TestCase(
|
|
|
- List.of(new TestCaseSupplier.TypedData(arg, DataType.TIME_DURATION, "arg")),
|
|
|
- "No evaluator since this expression is only folded",
|
|
|
+ List.of(new TestCaseSupplier.TypedData(arg, DataType.TIME_DURATION, "arg").forceLiteral()),
|
|
|
+ Matchers.startsWith("LiteralsEvaluator[lit="),
|
|
|
DataType.TIME_DURATION,
|
|
|
equalTo(arg.negated())
|
|
|
- );
|
|
|
+ ).withoutEvaluator();
|
|
|
}), new TestCaseSupplier("Period", List.of(DataType.DATE_PERIOD), () -> {
|
|
|
Period arg = (Period) randomLiteral(DataType.DATE_PERIOD).value();
|
|
|
return new TestCaseSupplier.TestCase(
|
|
|
- List.of(new TestCaseSupplier.TypedData(arg, DataType.DATE_PERIOD, "arg")),
|
|
|
- "No evaluator since this expression is only folded",
|
|
|
+ List.of(new TestCaseSupplier.TypedData(arg, DataType.DATE_PERIOD, "arg").forceLiteral()),
|
|
|
+ Matchers.startsWith("LiteralsEvaluator[lit="),
|
|
|
DataType.DATE_PERIOD,
|
|
|
equalTo(arg.negated())
|
|
|
- );
|
|
|
+ ).withoutEvaluator();
|
|
|
})));
|
|
|
return parameterSuppliersFromTypedDataWithDefaultChecks(false, suppliers, (v, p) -> "numeric, date_period or time_duration");
|
|
|
}
|
|
@@ -126,25 +125,25 @@ public class NegTests extends AbstractScalarFunctionTestCase {
|
|
|
if (testCaseType == DataType.DATE_PERIOD) {
|
|
|
Period maxPeriod = Period.of(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE);
|
|
|
Period negatedMaxPeriod = Period.of(-Integer.MAX_VALUE, -Integer.MAX_VALUE, -Integer.MAX_VALUE);
|
|
|
- assertEquals(negatedMaxPeriod, process(maxPeriod));
|
|
|
+ assertEquals(negatedMaxPeriod, foldTemporalAmount(maxPeriod));
|
|
|
|
|
|
Period minPeriod = Period.of(Integer.MIN_VALUE, Integer.MIN_VALUE, Integer.MIN_VALUE);
|
|
|
VerificationException e = expectThrows(
|
|
|
VerificationException.class,
|
|
|
"Expected exception when negating minimal date period.",
|
|
|
- () -> process(minPeriod)
|
|
|
+ () -> foldTemporalAmount(minPeriod)
|
|
|
);
|
|
|
assertEquals(e.getMessage(), "arithmetic exception in expression []: [integer overflow]");
|
|
|
} else if (testCaseType == DataType.TIME_DURATION) {
|
|
|
Duration maxDuration = Duration.ofSeconds(Long.MAX_VALUE, 0);
|
|
|
Duration negatedMaxDuration = Duration.ofSeconds(-Long.MAX_VALUE, 0);
|
|
|
- assertEquals(negatedMaxDuration, process(maxDuration));
|
|
|
+ assertEquals(negatedMaxDuration, foldTemporalAmount(maxDuration));
|
|
|
|
|
|
Duration minDuration = Duration.ofSeconds(Long.MIN_VALUE, 0);
|
|
|
VerificationException e = expectThrows(
|
|
|
VerificationException.class,
|
|
|
"Expected exception when negating minimal time duration.",
|
|
|
- () -> process(minDuration)
|
|
|
+ () -> foldTemporalAmount(minDuration)
|
|
|
);
|
|
|
assertEquals(
|
|
|
e.getMessage(),
|
|
@@ -153,16 +152,9 @@ public class NegTests extends AbstractScalarFunctionTestCase {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private Object process(Object val) {
|
|
|
- if (testCase.canBuildEvaluator()) {
|
|
|
- Neg neg = new Neg(Source.EMPTY, field("val", typeOf(val)));
|
|
|
- try (Block block = evaluator(neg).get(driverContext()).eval(row(List.of(val)))) {
|
|
|
- return toJavaObject(block, 0);
|
|
|
- }
|
|
|
- } else { // just fold if type is not representable
|
|
|
- Neg neg = new Neg(Source.EMPTY, new Literal(Source.EMPTY, val, typeOf(val)));
|
|
|
- return neg.fold();
|
|
|
- }
|
|
|
+ private Object foldTemporalAmount(Object val) {
|
|
|
+ Neg neg = new Neg(Source.EMPTY, new Literal(Source.EMPTY, val, typeOf(val)));
|
|
|
+ return neg.fold();
|
|
|
}
|
|
|
|
|
|
private static DataType typeOf(Object val) {
|