Skip to content

Commit 529a3f0

Browse files
committed
random tests
1 parent f96fea6 commit 529a3f0

File tree

5 files changed

+136
-9
lines changed

5 files changed

+136
-9
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.xpack.esql.core.expression.Expression;
1717
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
1818
import org.elasticsearch.xpack.esql.core.tree.Source;
19+
import org.elasticsearch.xpack.esql.core.type.DataType;
1920
import org.elasticsearch.xpack.esql.expression.function.EsqlFunctionRegistry;
2021
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
2122
import org.elasticsearch.xpack.esql.expression.function.FunctionType;
@@ -25,6 +26,9 @@
2526
import java.io.IOException;
2627
import java.util.List;
2728

29+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.DEFAULT;
30+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType;
31+
2832
/**
2933
* This function is used internally by {@link Approximate}, and is not exposed
3034
* to users via the {@link EsqlFunctionRegistry}.
@@ -63,6 +67,11 @@ public ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
6367
return new RandomEvaluator.Factory(source(), toEvaluator.apply(field));
6468
}
6569

70+
@Override
71+
protected Expression.TypeResolution resolveType() {
72+
return isType(field, t -> t == DataType.INTEGER, sourceText(), DEFAULT, "int");
73+
}
74+
6675
@Override
6776
public final Expression replaceChildren(List<Expression> newChildren) {
6877
return new Random(source(), newChildren.get(0));
@@ -75,6 +84,6 @@ protected NodeInfo<? extends Expression> info() {
7584

7685
@Override
7786
public boolean foldable() {
78-
return false;
87+
return field.dataType() == DataType.NULL;
7988
}
8089
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/scalar/approximate/ConfidenceIntervalTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
1717
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
1818

19-
import java.util.ArrayList;
2019
import java.util.List;
2120
import java.util.function.Supplier;
2221
import java.util.stream.IntStream;
@@ -33,13 +32,14 @@ public class ConfidenceIntervalTests extends AbstractScalarFunctionTestCase {
3332

3433
@ParametersFactory
3534
public static Iterable<Object[]> parameters() {
36-
List<TestCaseSupplier> suppliers = new ArrayList<>();
37-
suppliers.add(randomBuckets());
38-
suppliers.add(allBucketsFilled());
39-
suppliers.add(nanBuckets_ignoreNan());
40-
suppliers.add(nanBuckets_zeroNan());
41-
suppliers.add(inconsistentData());
42-
suppliers.add(manyNans());
35+
List<TestCaseSupplier> suppliers = List.of(
36+
randomBuckets(),
37+
allBucketsFilled(),
38+
nanBuckets_ignoreNan(),
39+
nanBuckets_zeroNan(),
40+
inconsistentData(),
41+
manyNans()
42+
);
4343
return parameterSuppliersFromTypedDataWithDefaultChecks(false, suppliers);
4444
}
4545

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.expression.function.scalar.approximate;
9+
10+
import org.elasticsearch.xpack.esql.core.expression.Expression;
11+
import org.elasticsearch.xpack.esql.core.tree.Source;
12+
import org.elasticsearch.xpack.esql.core.type.DataType;
13+
import org.elasticsearch.xpack.esql.expression.function.ErrorsForCasesWithoutExamplesTestCase;
14+
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
15+
import org.hamcrest.Matcher;
16+
17+
import java.util.List;
18+
import java.util.Set;
19+
20+
import static org.hamcrest.Matchers.equalTo;
21+
22+
public class RandomErrorTests extends ErrorsForCasesWithoutExamplesTestCase {
23+
@Override
24+
protected List<TestCaseSupplier> cases() {
25+
return paramsToSuppliers(RandomTests.parameters());
26+
}
27+
28+
@Override
29+
protected Expression build(Source source, List<Expression> args) {
30+
return new Random(source, args.get(0));
31+
}
32+
33+
@Override
34+
protected Matcher<String> expectedTypeErrorMatcher(List<Set<DataType>> validPerPosition, List<DataType> signature) {
35+
return equalTo(typeErrorMessage(false, validPerPosition, signature, (v, i) -> "int"));
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.expression.function.scalar.approximate;
9+
10+
import org.elasticsearch.xpack.esql.expression.AbstractExpressionSerializationTests;
11+
12+
import java.io.IOException;
13+
14+
public class RandomSerializationTests extends AbstractExpressionSerializationTests<Random> {
15+
16+
@Override
17+
protected Random createTestInstance() {
18+
return new Random(randomSource(), randomChild());
19+
}
20+
21+
@Override
22+
protected Random mutateInstance(Random instance) throws IOException {
23+
return new Random(instance.source(), randomValueOtherThan(instance.field(), AbstractExpressionSerializationTests::randomChild));
24+
}
25+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.expression.function.scalar.approximate;
9+
10+
import com.carrotsearch.randomizedtesting.annotations.Name;
11+
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
12+
13+
import org.elasticsearch.xpack.esql.core.expression.Expression;
14+
import org.elasticsearch.xpack.esql.core.tree.Source;
15+
import org.elasticsearch.xpack.esql.core.type.DataType;
16+
import org.elasticsearch.xpack.esql.expression.function.AbstractScalarFunctionTestCase;
17+
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
18+
19+
import java.util.List;
20+
import java.util.function.Supplier;
21+
22+
import static org.hamcrest.Matchers.allOf;
23+
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
24+
import static org.hamcrest.Matchers.instanceOf;
25+
import static org.hamcrest.Matchers.lessThan;
26+
27+
public class RandomTests extends AbstractScalarFunctionTestCase {
28+
29+
@ParametersFactory
30+
public static Iterable<Object[]> parameters() {
31+
List<TestCaseSupplier> suppliers = List.of(new TestCaseSupplier("Basic Case", List.of(DataType.INTEGER), () -> {
32+
int bound = randomIntBetween(1, 100);
33+
return new TestCaseSupplier.TestCase(
34+
List.of(new TestCaseSupplier.TypedData(bound, DataType.INTEGER, "bound")),
35+
"RandomEvaluator[bound=Attribute[channel=0]]",
36+
DataType.INTEGER,
37+
allOf(instanceOf(Integer.class), greaterThanOrEqualTo(0), lessThan(bound))
38+
);
39+
}));
40+
return parameterSuppliersFromTypedDataWithDefaultChecks(false, suppliers);
41+
}
42+
43+
public RandomTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
44+
this.testCase = testCaseSupplier.get();
45+
}
46+
47+
@Override
48+
protected Expression build(Source source, List<Expression> args) {
49+
return new Random(source, args.get(0));
50+
}
51+
52+
@Override
53+
public void testFold() {
54+
// Random cannot be folded as it is non-deterministic.
55+
}
56+
}

0 commit comments

Comments
 (0)