Skip to content

Commit 6ab6b23

Browse files
[ESQL] Add the ability to assert warnings in the new test generation logic (#99381)
This extends the test case generation functions to take expected warnings, and demonstrates the use of that functionality by testing expected nulls for log10. We can build on this to get proper null handling and tests for the rest of the math functions. Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
1 parent b364659 commit 6ab6b23

File tree

20 files changed

+324
-110
lines changed

20 files changed

+324
-110
lines changed

x-pack/plugin/esql/qa/testFixtures/src/main/resources/math.csv-spec

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -266,38 +266,30 @@ d: double | s:double
266266
;
267267

268268
log10ofNegative
269-
row d = -1.0 | eval s = is_nan(log10(d));
269+
row d = -1.0 | eval s = log10(d);
270+
warning:Line 1:25: evaluation of [log10(d)] failed, treating result as null. Only first 20 failures recorded.
271+
warning:java.lang.ArithmeticException: Log of non-positive number
270272

271-
d:double | s:boolean
272-
-1.0 | true
273-
;
274-
275-
log10ofNan
276-
row d = 0.0/0.0 | eval s = is_nan(log10(d));
277-
278-
d:double | s:boolean
279-
NaN | true
273+
d:double | s:double
274+
-1.0 | null
280275
;
281276

282277
log10ofZero
283-
row d = 0.0 |eval s = is_infinite(log10(d));
278+
row d = 0.0 | eval s = log10(d);
279+
warning:Line 1:24: evaluation of [log10(d)] failed, treating result as null. Only first 20 failures recorded.
280+
warning:java.lang.ArithmeticException: Log of non-positive number
284281

285-
d:double | s:boolean
286-
0.0 | true
282+
d:double | s:double
283+
0.0 | null
287284
;
288285

289286
log10ofNegativeZero
290-
row d = -0.0 |eval s = is_infinite(log10(d));
291-
292-
d:double | s:boolean
293-
-0.0 | true
294-
;
287+
row d = -0.0 | eval s = log10(d);
288+
warning:Line 1:25: evaluation of [log10(d)] failed, treating result as null. Only first 20 failures recorded.
289+
warning:java.lang.ArithmeticException: Log of non-positive number
295290

296-
log10ofInfinite
297-
row d = 1/0.0 | eval s = is_infinite(log10(d));
298-
299-
d:double | s:boolean
300-
Infinity | true
291+
d:double | s:double
292+
-0.0 | null
301293
;
302294

303295
log10ofLong

x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/math/Log10DoubleEvaluator.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,28 @@
44
// 2.0.
55
package org.elasticsearch.xpack.esql.expression.function.scalar.math;
66

7+
import java.lang.ArithmeticException;
78
import java.lang.Override;
89
import java.lang.String;
910
import org.elasticsearch.compute.data.Block;
1011
import org.elasticsearch.compute.data.DoubleBlock;
1112
import org.elasticsearch.compute.data.DoubleVector;
1213
import org.elasticsearch.compute.data.Page;
1314
import org.elasticsearch.compute.operator.EvalOperator;
15+
import org.elasticsearch.xpack.esql.expression.function.Warnings;
16+
import org.elasticsearch.xpack.ql.tree.Source;
1417

1518
/**
1619
* {@link EvalOperator.ExpressionEvaluator} implementation for {@link Log10}.
1720
* This class is generated. Do not edit it.
1821
*/
1922
public final class Log10DoubleEvaluator implements EvalOperator.ExpressionEvaluator {
23+
private final Warnings warnings;
24+
2025
private final EvalOperator.ExpressionEvaluator val;
2126

22-
public Log10DoubleEvaluator(EvalOperator.ExpressionEvaluator val) {
27+
public Log10DoubleEvaluator(Source source, EvalOperator.ExpressionEvaluator val) {
28+
this.warnings = new Warnings(source);
2329
this.val = val;
2430
}
2531

@@ -34,7 +40,7 @@ public Block eval(Page page) {
3440
if (valVector == null) {
3541
return eval(page.getPositionCount(), valBlock);
3642
}
37-
return eval(page.getPositionCount(), valVector).asBlock();
43+
return eval(page.getPositionCount(), valVector);
3844
}
3945

4046
public DoubleBlock eval(int positionCount, DoubleBlock valBlock) {
@@ -44,15 +50,25 @@ public DoubleBlock eval(int positionCount, DoubleBlock valBlock) {
4450
result.appendNull();
4551
continue position;
4652
}
47-
result.appendDouble(Log10.process(valBlock.getDouble(valBlock.getFirstValueIndex(p))));
53+
try {
54+
result.appendDouble(Log10.process(valBlock.getDouble(valBlock.getFirstValueIndex(p))));
55+
} catch (ArithmeticException e) {
56+
warnings.registerException(e);
57+
result.appendNull();
58+
}
4859
}
4960
return result.build();
5061
}
5162

52-
public DoubleVector eval(int positionCount, DoubleVector valVector) {
53-
DoubleVector.Builder result = DoubleVector.newVectorBuilder(positionCount);
63+
public DoubleBlock eval(int positionCount, DoubleVector valVector) {
64+
DoubleBlock.Builder result = DoubleBlock.newBlockBuilder(positionCount);
5465
position: for (int p = 0; p < positionCount; p++) {
55-
result.appendDouble(Log10.process(valVector.getDouble(p)));
66+
try {
67+
result.appendDouble(Log10.process(valVector.getDouble(p)));
68+
} catch (ArithmeticException e) {
69+
warnings.registerException(e);
70+
result.appendNull();
71+
}
5672
}
5773
return result.build();
5874
}

x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/math/Log10IntEvaluator.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44
// 2.0.
55
package org.elasticsearch.xpack.esql.expression.function.scalar.math;
66

7+
import java.lang.ArithmeticException;
78
import java.lang.Override;
89
import java.lang.String;
910
import org.elasticsearch.compute.data.Block;
1011
import org.elasticsearch.compute.data.DoubleBlock;
11-
import org.elasticsearch.compute.data.DoubleVector;
1212
import org.elasticsearch.compute.data.IntBlock;
1313
import org.elasticsearch.compute.data.IntVector;
1414
import org.elasticsearch.compute.data.Page;
1515
import org.elasticsearch.compute.operator.EvalOperator;
16+
import org.elasticsearch.xpack.esql.expression.function.Warnings;
17+
import org.elasticsearch.xpack.ql.tree.Source;
1618

1719
/**
1820
* {@link EvalOperator.ExpressionEvaluator} implementation for {@link Log10}.
1921
* This class is generated. Do not edit it.
2022
*/
2123
public final class Log10IntEvaluator implements EvalOperator.ExpressionEvaluator {
24+
private final Warnings warnings;
25+
2226
private final EvalOperator.ExpressionEvaluator val;
2327

24-
public Log10IntEvaluator(EvalOperator.ExpressionEvaluator val) {
28+
public Log10IntEvaluator(Source source, EvalOperator.ExpressionEvaluator val) {
29+
this.warnings = new Warnings(source);
2530
this.val = val;
2631
}
2732

@@ -36,7 +41,7 @@ public Block eval(Page page) {
3641
if (valVector == null) {
3742
return eval(page.getPositionCount(), valBlock);
3843
}
39-
return eval(page.getPositionCount(), valVector).asBlock();
44+
return eval(page.getPositionCount(), valVector);
4045
}
4146

4247
public DoubleBlock eval(int positionCount, IntBlock valBlock) {
@@ -46,15 +51,25 @@ public DoubleBlock eval(int positionCount, IntBlock valBlock) {
4651
result.appendNull();
4752
continue position;
4853
}
49-
result.appendDouble(Log10.process(valBlock.getInt(valBlock.getFirstValueIndex(p))));
54+
try {
55+
result.appendDouble(Log10.process(valBlock.getInt(valBlock.getFirstValueIndex(p))));
56+
} catch (ArithmeticException e) {
57+
warnings.registerException(e);
58+
result.appendNull();
59+
}
5060
}
5161
return result.build();
5262
}
5363

54-
public DoubleVector eval(int positionCount, IntVector valVector) {
55-
DoubleVector.Builder result = DoubleVector.newVectorBuilder(positionCount);
64+
public DoubleBlock eval(int positionCount, IntVector valVector) {
65+
DoubleBlock.Builder result = DoubleBlock.newBlockBuilder(positionCount);
5666
position: for (int p = 0; p < positionCount; p++) {
57-
result.appendDouble(Log10.process(valVector.getInt(p)));
67+
try {
68+
result.appendDouble(Log10.process(valVector.getInt(p)));
69+
} catch (ArithmeticException e) {
70+
warnings.registerException(e);
71+
result.appendNull();
72+
}
5873
}
5974
return result.build();
6075
}

x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/math/Log10LongEvaluator.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44
// 2.0.
55
package org.elasticsearch.xpack.esql.expression.function.scalar.math;
66

7+
import java.lang.ArithmeticException;
78
import java.lang.Override;
89
import java.lang.String;
910
import org.elasticsearch.compute.data.Block;
1011
import org.elasticsearch.compute.data.DoubleBlock;
11-
import org.elasticsearch.compute.data.DoubleVector;
1212
import org.elasticsearch.compute.data.LongBlock;
1313
import org.elasticsearch.compute.data.LongVector;
1414
import org.elasticsearch.compute.data.Page;
1515
import org.elasticsearch.compute.operator.EvalOperator;
16+
import org.elasticsearch.xpack.esql.expression.function.Warnings;
17+
import org.elasticsearch.xpack.ql.tree.Source;
1618

1719
/**
1820
* {@link EvalOperator.ExpressionEvaluator} implementation for {@link Log10}.
1921
* This class is generated. Do not edit it.
2022
*/
2123
public final class Log10LongEvaluator implements EvalOperator.ExpressionEvaluator {
24+
private final Warnings warnings;
25+
2226
private final EvalOperator.ExpressionEvaluator val;
2327

24-
public Log10LongEvaluator(EvalOperator.ExpressionEvaluator val) {
28+
public Log10LongEvaluator(Source source, EvalOperator.ExpressionEvaluator val) {
29+
this.warnings = new Warnings(source);
2530
this.val = val;
2631
}
2732

@@ -36,7 +41,7 @@ public Block eval(Page page) {
3641
if (valVector == null) {
3742
return eval(page.getPositionCount(), valBlock);
3843
}
39-
return eval(page.getPositionCount(), valVector).asBlock();
44+
return eval(page.getPositionCount(), valVector);
4045
}
4146

4247
public DoubleBlock eval(int positionCount, LongBlock valBlock) {
@@ -46,15 +51,25 @@ public DoubleBlock eval(int positionCount, LongBlock valBlock) {
4651
result.appendNull();
4752
continue position;
4853
}
49-
result.appendDouble(Log10.process(valBlock.getLong(valBlock.getFirstValueIndex(p))));
54+
try {
55+
result.appendDouble(Log10.process(valBlock.getLong(valBlock.getFirstValueIndex(p))));
56+
} catch (ArithmeticException e) {
57+
warnings.registerException(e);
58+
result.appendNull();
59+
}
5060
}
5161
return result.build();
5262
}
5363

54-
public DoubleVector eval(int positionCount, LongVector valVector) {
55-
DoubleVector.Builder result = DoubleVector.newVectorBuilder(positionCount);
64+
public DoubleBlock eval(int positionCount, LongVector valVector) {
65+
DoubleBlock.Builder result = DoubleBlock.newBlockBuilder(positionCount);
5666
position: for (int p = 0; p < positionCount; p++) {
57-
result.appendDouble(Log10.process(valVector.getLong(p)));
67+
try {
68+
result.appendDouble(Log10.process(valVector.getLong(p)));
69+
} catch (ArithmeticException e) {
70+
warnings.registerException(e);
71+
result.appendNull();
72+
}
5873
}
5974
return result.build();
6075
}

x-pack/plugin/esql/src/main/java/generated/org/elasticsearch/xpack/esql/expression/function/scalar/math/Log10UnsignedLongEvaluator.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@
44
// 2.0.
55
package org.elasticsearch.xpack.esql.expression.function.scalar.math;
66

7+
import java.lang.ArithmeticException;
78
import java.lang.Override;
89
import java.lang.String;
910
import org.elasticsearch.compute.data.Block;
1011
import org.elasticsearch.compute.data.DoubleBlock;
11-
import org.elasticsearch.compute.data.DoubleVector;
1212
import org.elasticsearch.compute.data.LongBlock;
1313
import org.elasticsearch.compute.data.LongVector;
1414
import org.elasticsearch.compute.data.Page;
1515
import org.elasticsearch.compute.operator.EvalOperator;
16+
import org.elasticsearch.xpack.esql.expression.function.Warnings;
17+
import org.elasticsearch.xpack.ql.tree.Source;
1618

1719
/**
1820
* {@link EvalOperator.ExpressionEvaluator} implementation for {@link Log10}.
1921
* This class is generated. Do not edit it.
2022
*/
2123
public final class Log10UnsignedLongEvaluator implements EvalOperator.ExpressionEvaluator {
24+
private final Warnings warnings;
25+
2226
private final EvalOperator.ExpressionEvaluator val;
2327

24-
public Log10UnsignedLongEvaluator(EvalOperator.ExpressionEvaluator val) {
28+
public Log10UnsignedLongEvaluator(Source source, EvalOperator.ExpressionEvaluator val) {
29+
this.warnings = new Warnings(source);
2530
this.val = val;
2631
}
2732

@@ -36,7 +41,7 @@ public Block eval(Page page) {
3641
if (valVector == null) {
3742
return eval(page.getPositionCount(), valBlock);
3843
}
39-
return eval(page.getPositionCount(), valVector).asBlock();
44+
return eval(page.getPositionCount(), valVector);
4045
}
4146

4247
public DoubleBlock eval(int positionCount, LongBlock valBlock) {
@@ -46,15 +51,25 @@ public DoubleBlock eval(int positionCount, LongBlock valBlock) {
4651
result.appendNull();
4752
continue position;
4853
}
49-
result.appendDouble(Log10.processUnsignedLong(valBlock.getLong(valBlock.getFirstValueIndex(p))));
54+
try {
55+
result.appendDouble(Log10.processUnsignedLong(valBlock.getLong(valBlock.getFirstValueIndex(p))));
56+
} catch (ArithmeticException e) {
57+
warnings.registerException(e);
58+
result.appendNull();
59+
}
5060
}
5161
return result.build();
5262
}
5363

54-
public DoubleVector eval(int positionCount, LongVector valVector) {
55-
DoubleVector.Builder result = DoubleVector.newVectorBuilder(positionCount);
64+
public DoubleBlock eval(int positionCount, LongVector valVector) {
65+
DoubleBlock.Builder result = DoubleBlock.newBlockBuilder(positionCount);
5666
position: for (int p = 0; p < positionCount; p++) {
57-
result.appendDouble(Log10.processUnsignedLong(valVector.getLong(p)));
67+
try {
68+
result.appendDouble(Log10.processUnsignedLong(valVector.getLong(p)));
69+
} catch (ArithmeticException e) {
70+
warnings.registerException(e);
71+
result.appendNull();
72+
}
5873
}
5974
return result.build();
6075
}

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,38 +41,50 @@ public Supplier<EvalOperator.ExpressionEvaluator> toEvaluator(
4141
var eval = field.get();
4242

4343
if (fieldType == DataTypes.DOUBLE) {
44-
return () -> new Log10DoubleEvaluator(eval);
44+
return () -> new Log10DoubleEvaluator(source(), eval);
4545
}
4646
if (fieldType == DataTypes.INTEGER) {
47-
return () -> new Log10IntEvaluator(eval);
47+
return () -> new Log10IntEvaluator(source(), eval);
4848
}
4949
if (fieldType == DataTypes.LONG) {
50-
return () -> new Log10LongEvaluator(eval);
50+
return () -> new Log10LongEvaluator(source(), eval);
5151
}
5252
if (fieldType == DataTypes.UNSIGNED_LONG) {
53-
return () -> new Log10UnsignedLongEvaluator(eval);
53+
return () -> new Log10UnsignedLongEvaluator(source(), eval);
5454
}
5555

5656
throw EsqlIllegalArgumentException.illegalDataType(fieldType);
5757
}
5858

59-
@Evaluator(extraName = "Double")
59+
@Evaluator(extraName = "Double", warnExceptions = ArithmeticException.class)
6060
static double process(double val) {
61+
if (val <= 0d) {
62+
throw new ArithmeticException("Log of non-positive number");
63+
}
6164
return Math.log10(val);
6265
}
6366

64-
@Evaluator(extraName = "Long")
67+
@Evaluator(extraName = "Long", warnExceptions = ArithmeticException.class)
6568
static double process(long val) {
69+
if (val <= 0L) {
70+
throw new ArithmeticException("Log of non-positive number");
71+
}
6672
return Math.log10(val);
6773
}
6874

69-
@Evaluator(extraName = "UnsignedLong")
75+
@Evaluator(extraName = "UnsignedLong", warnExceptions = ArithmeticException.class)
7076
static double processUnsignedLong(long val) {
77+
if (val == NumericUtils.ZERO_AS_UNSIGNED_LONG) {
78+
throw new ArithmeticException("Log of non-positive number");
79+
}
7180
return Math.log10(NumericUtils.unsignedLongToDouble(val));
7281
}
7382

74-
@Evaluator(extraName = "Int")
83+
@Evaluator(extraName = "Int", warnExceptions = ArithmeticException.class)
7584
static double process(int val) {
85+
if (val <= 0) {
86+
throw new ArithmeticException("Log of non-positive number");
87+
}
7688
return Math.log10(val);
7789
}
7890

0 commit comments

Comments
 (0)