Skip to content

Commit 7344279

Browse files
committed
Add SampleTests
1 parent e42689d commit 7344279

File tree

2 files changed

+122
-10
lines changed

2 files changed

+122
-10
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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.aggregate;
9+
10+
import com.carrotsearch.randomizedtesting.annotations.Name;
11+
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
12+
13+
import org.apache.lucene.search.Multiset;
14+
import org.elasticsearch.xpack.esql.core.expression.Expression;
15+
import org.elasticsearch.xpack.esql.core.tree.Source;
16+
import org.elasticsearch.xpack.esql.core.type.DataType;
17+
import org.elasticsearch.xpack.esql.expression.function.AbstractAggregationTestCase;
18+
import org.elasticsearch.xpack.esql.expression.function.MultiRowTestCaseSupplier;
19+
import org.elasticsearch.xpack.esql.expression.function.TestCaseSupplier;
20+
import org.hamcrest.BaseMatcher;
21+
import org.hamcrest.Description;
22+
import org.hamcrest.Matcher;
23+
24+
import java.util.ArrayList;
25+
import java.util.Collection;
26+
import java.util.List;
27+
import java.util.function.Supplier;
28+
import java.util.stream.Collectors;
29+
import java.util.stream.Stream;
30+
31+
import static org.hamcrest.Matchers.equalTo;
32+
import static org.hamcrest.Matchers.nullValue;
33+
34+
public class SampleTests extends AbstractAggregationTestCase {
35+
public SampleTests(@Name("TestCase") Supplier<TestCaseSupplier.TestCase> testCaseSupplier) {
36+
this.testCase = testCaseSupplier.get();
37+
}
38+
39+
@ParametersFactory
40+
public static Iterable<Object[]> parameters() {
41+
var suppliers = new ArrayList<TestCaseSupplier>();
42+
43+
for (var limitCaseSupplier : TestCaseSupplier.intCases(1, 100, false)) {
44+
Stream.of(
45+
MultiRowTestCaseSupplier.intCases(1, 1000, Integer.MIN_VALUE, Integer.MAX_VALUE, true),
46+
MultiRowTestCaseSupplier.longCases(1, 1000, Long.MIN_VALUE, Long.MAX_VALUE, true),
47+
MultiRowTestCaseSupplier.doubleCases(1, 1000, -Double.MAX_VALUE, Double.MAX_VALUE, true),
48+
MultiRowTestCaseSupplier.dateCases(1, 1000),
49+
MultiRowTestCaseSupplier.dateNanosCases(1, 1000),
50+
MultiRowTestCaseSupplier.booleanCases(1, 1000),
51+
MultiRowTestCaseSupplier.ipCases(1, 1000),
52+
MultiRowTestCaseSupplier.versionCases(1, 1000),
53+
MultiRowTestCaseSupplier.stringCases(1, 1000, DataType.KEYWORD),
54+
MultiRowTestCaseSupplier.stringCases(1, 1000, DataType.TEXT),
55+
MultiRowTestCaseSupplier.geoPointCases(1, 1000, MultiRowTestCaseSupplier.IncludingAltitude.NO),
56+
MultiRowTestCaseSupplier.cartesianPointCases(1, 1000, MultiRowTestCaseSupplier.IncludingAltitude.NO),
57+
MultiRowTestCaseSupplier.geoShapeCasesWithoutCircle(1, 100, MultiRowTestCaseSupplier.IncludingAltitude.NO),
58+
MultiRowTestCaseSupplier.cartesianShapeCasesWithoutCircle(1, 100, MultiRowTestCaseSupplier.IncludingAltitude.NO)
59+
).flatMap(List::stream)
60+
.map(fieldCaseSupplier -> makeSupplier(fieldCaseSupplier, limitCaseSupplier))
61+
.collect(Collectors.toCollection(() -> suppliers));
62+
}
63+
return parameterSuppliersFromTypedDataWithDefaultChecksNoErrors(suppliers);
64+
}
65+
66+
@Override
67+
protected Expression build(Source source, List<Expression> args) {
68+
return new Sample(source, args.get(0), args.get(1));
69+
}
70+
71+
private static TestCaseSupplier makeSupplier(TestCaseSupplier.TypedDataSupplier fieldSupplier, TestCaseSupplier.TypedDataSupplier limitCaseSupplier
72+
) {
73+
return new TestCaseSupplier(fieldSupplier.name(), List.of(fieldSupplier.type(), limitCaseSupplier.type()), () -> {
74+
var fieldTypedData = fieldSupplier.get();
75+
var limitTypedData = limitCaseSupplier.get().forceLiteral();
76+
var limit = (int) limitTypedData.getValue();
77+
78+
var rows = fieldTypedData.multiRowData();
79+
80+
return new TestCaseSupplier.TestCase(
81+
List.of(fieldTypedData, limitTypedData),
82+
"Sample[field=Attribute[channel=0], limit=Attribute[channel=1]]",
83+
fieldSupplier.type(),
84+
subsetOfSize(rows, limit)
85+
);
86+
});
87+
}
88+
89+
private static <T> Matcher<Object> subsetOfSize(Collection<T> data, int size) {
90+
if (data == null || data.isEmpty()) {
91+
return nullValue();
92+
}
93+
if (data.size() == 1) {
94+
return equalTo(data.iterator().next());
95+
}
96+
// New Matcher, as `containsInAnyOrder` returns Matcher<Iterable<...>> instead of Matcher<Object>
97+
return new BaseMatcher<>() {
98+
@Override
99+
public void describeTo(Description description) {
100+
description.appendText("subset of size ").appendValue(size).appendText(" of ").appendValue(data);
101+
}
102+
103+
@Override
104+
public boolean matches(Object object) {
105+
Iterable<?> items = object instanceof Iterable ? (Iterable<?>) object : List.of(object);
106+
Multiset<T> dataSet = new Multiset<>();
107+
dataSet.addAll(data);
108+
for (Object item : items) {
109+
if (dataSet.remove(item) == false) {
110+
return false;
111+
}
112+
}
113+
return true;
114+
}
115+
};
116+
}
117+
}

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/expression/function/aggregate/ValuesTests.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.ArrayList;
2424
import java.util.Collection;
25+
import java.util.HashSet;
2526
import java.util.List;
2627
import java.util.function.Supplier;
2728
import java.util.stream.Collectors;
@@ -67,27 +68,21 @@ protected Expression build(Source source, List<Expression> args) {
6768
return new Values(source, args.get(0));
6869
}
6970

70-
@SuppressWarnings("unchecked")
7171
private static TestCaseSupplier makeSupplier(TestCaseSupplier.TypedDataSupplier fieldSupplier) {
7272
return new TestCaseSupplier(fieldSupplier.name(), List.of(fieldSupplier.type()), () -> {
7373
var fieldTypedData = fieldSupplier.get();
74-
75-
var expected = fieldTypedData.multiRowData()
76-
.stream()
77-
.map(v -> (Comparable<? super Comparable<?>>) v)
78-
.collect(Collectors.toSet());
79-
74+
var expected = new HashSet<>(fieldTypedData.multiRowData());
8075
return new TestCaseSupplier.TestCase(
8176
List.of(fieldTypedData),
8277
"Values[field=Attribute[channel=0]]",
8378
fieldSupplier.type(),
84-
expected.isEmpty() ? nullValue() : valuesInAnyOrder(expected)
79+
valuesInAnyOrder(expected)
8580
);
8681
});
8782
}
8883

89-
private static <T, C extends T> Matcher<Object> valuesInAnyOrder(Collection<T> data) {
90-
if (data == null) {
84+
private static <T> Matcher<Object> valuesInAnyOrder(Collection<T> data) {
85+
if (data == null || data.isEmpty()) {
9186
return nullValue();
9287
}
9388
if (data.size() == 1) {

0 commit comments

Comments
 (0)