Skip to content

Commit ee1bb7a

Browse files
committed
Some more infra for new tests
1 parent a813c78 commit ee1bb7a

File tree

7 files changed

+58
-10
lines changed

7 files changed

+58
-10
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9191000
1+
9192000
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
add_cross_cluster_api_key_signature,9191000
1+
esql_date_range_created_version,9192000

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/operator/topn/ValueExtractor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.compute.data.Block;
1212
import org.elasticsearch.compute.data.BooleanBlock;
1313
import org.elasticsearch.compute.data.BytesRefBlock;
14+
import org.elasticsearch.compute.data.DateRangeBlock;
1415
import org.elasticsearch.compute.data.DocBlock;
1516
import org.elasticsearch.compute.data.DoubleBlock;
1617
import org.elasticsearch.compute.data.ElementType;
@@ -53,6 +54,7 @@ static ValueExtractor extractorFor(ElementType elementType, TopNEncoder encoder,
5354
case NULL -> new ValueExtractorForNull();
5455
case DOC -> new ValueExtractorForDoc(encoder, ((DocBlock) block).asVector());
5556
case AGGREGATE_METRIC_DOUBLE -> new ValueExtractorForAggregateMetricDouble(encoder, (AggregateMetricDoubleBlock) block);
57+
case DATE_RANGE -> new ValueExtractorForDateRange(encoder, (DateRangeBlock) block);
5658
default -> {
5759
assert false : "No value extractor for [" + block.elementType() + "]";
5860
throw new UnsupportedOperationException("No value extractor for [" + block.elementType() + "]");
Lines changed: 37 additions & 0 deletions
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.compute.operator.topn;
9+
10+
import org.elasticsearch.compute.data.DateRangeBlock;
11+
import org.elasticsearch.compute.operator.BreakingBytesRefBuilder;
12+
13+
public class ValueExtractorForDateRange implements ValueExtractor {
14+
private final DateRangeBlock block;
15+
16+
ValueExtractorForDateRange(TopNEncoder encoder, DateRangeBlock block) {
17+
assert encoder == TopNEncoder.DEFAULT_UNSORTABLE;
18+
this.block = block;
19+
}
20+
21+
@Override
22+
public void writeValue(BreakingBytesRefBuilder values, int position) {
23+
TopNEncoder.DEFAULT_UNSORTABLE.encodeVInt(1, values);
24+
if (block.isNull(position)) {
25+
TopNEncoder.DEFAULT_UNSORTABLE.encodeBoolean(false, values);
26+
} else {
27+
TopNEncoder.DEFAULT_UNSORTABLE.encodeBoolean(true, values);
28+
TopNEncoder.DEFAULT_UNSORTABLE.encodeDouble(block.getFromBlock().getLong(position), values);
29+
TopNEncoder.DEFAULT_UNSORTABLE.encodeDouble(block.getToBlock().getLong(position), values);
30+
}
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "ValueExtractorForDateRange";
36+
}
37+
}

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/operator/topn/ExtractorTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ public static Iterable<Object[]> parameters() {
6666
)
6767
);
6868
}
69-
case DATE_RANGE -> cases.add(
70-
valueTestCase("date_range with nulls", e, TopNEncoder.DEFAULT_UNSORTABLE, ExtractorTests::randomDateRange)
71-
);
69+
case DATE_RANGE -> {
70+
cases.add(valueTestCase("date_range with nulls", e, TopNEncoder.DEFAULT_UNSORTABLE, () -> randomDateRange(true)));
71+
cases.add(valueTestCase("date_range with nulls", e, TopNEncoder.DEFAULT_UNSORTABLE, () -> randomDateRange(false)));
72+
}
7273
case FLOAT -> {
7374
}
7475
case BYTES_REF -> {
@@ -262,9 +263,9 @@ public static AggregateMetricDoubleLiteral randomAggregateMetricDouble(boolean a
262263
);
263264
}
264265

265-
private static DateRangeBlockBuilder.DateRangeLiteral randomDateRange() {
266+
private static DateRangeBlockBuilder.DateRangeLiteral randomDateRange(boolean isNull) {
266267
var from = randomMillisUpToYear9999();
267268
var to = randomLongBetween(from + 1, MAX_MILLIS_BEFORE_9999);
268-
return new DateRangeBlockBuilder.DateRangeLiteral(from, to);
269+
return isNull ? null : new DateRangeBlockBuilder.DateRangeLiteral(from, to);
269270
}
270271
}

x-pack/plugin/esql/compute/src/test/java/org/elasticsearch/compute/operator/topn/TopNOperatorTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import static org.elasticsearch.compute.data.ElementType.BOOLEAN;
7575
import static org.elasticsearch.compute.data.ElementType.BYTES_REF;
7676
import static org.elasticsearch.compute.data.ElementType.COMPOSITE;
77+
import static org.elasticsearch.compute.data.ElementType.DATE_RANGE;
7778
import static org.elasticsearch.compute.data.ElementType.DOUBLE;
7879
import static org.elasticsearch.compute.data.ElementType.FLOAT;
7980
import static org.elasticsearch.compute.data.ElementType.INT;
@@ -602,7 +603,7 @@ public void testCollectAllValues_RandomMultiValues() {
602603

603604
for (int type = 0; type < blocksCount; type++) {
604605
ElementType e = randomFrom(ElementType.values());
605-
if (e == ElementType.UNKNOWN || e == COMPOSITE || e == AGGREGATE_METRIC_DOUBLE) {
606+
if (e == ElementType.UNKNOWN || e == COMPOSITE || e == AGGREGATE_METRIC_DOUBLE || e == DATE_RANGE) {
606607
continue;
607608
}
608609
elementTypes.add(e);
@@ -1024,7 +1025,7 @@ public void testRandomMultiValuesTopN() {
10241025

10251026
for (int type = 0; type < blocksCount; type++) {
10261027
ElementType e = randomValueOtherThanMany(
1027-
t -> t == ElementType.UNKNOWN || t == ElementType.DOC || t == COMPOSITE || t == AGGREGATE_METRIC_DOUBLE,
1028+
t -> t == ElementType.UNKNOWN || t == ElementType.DOC || t == COMPOSITE || t == AGGREGATE_METRIC_DOUBLE || t == DATE_RANGE,
10281029
() -> randomFrom(ElementType.values())
10291030
);
10301031
elementTypes.add(e);

x-pack/plugin/esql/compute/test/src/main/java/org/elasticsearch/compute/test/BlockTestUtils.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.util.List;
3636
import java.util.Map;
3737

38+
import static org.elasticsearch.common.time.DateUtils.MAX_MILLIS_BEFORE_9999;
3839
import static org.elasticsearch.compute.data.BlockUtils.toJavaObject;
3940
import static org.elasticsearch.test.ESTestCase.between;
4041
import static org.elasticsearch.test.ESTestCase.randomBoolean;
@@ -43,6 +44,8 @@
4344
import static org.elasticsearch.test.ESTestCase.randomInt;
4445
import static org.elasticsearch.test.ESTestCase.randomIntBetween;
4546
import static org.elasticsearch.test.ESTestCase.randomLong;
47+
import static org.elasticsearch.test.ESTestCase.randomLongBetween;
48+
import static org.elasticsearch.test.ESTestCase.randomMillisUpToYear9999;
4649
import static org.elasticsearch.test.ESTestCase.randomNonNegativeInt;
4750
import static org.elasticsearch.test.ESTestCase.randomRealisticUnicodeOfCodepointLengthBetween;
4851
import static org.hamcrest.Matchers.equalTo;
@@ -66,14 +69,18 @@ public static Object randomValue(ElementType e) {
6669
randomDouble(),
6770
randomNonNegativeInt()
6871
);
72+
case DATE_RANGE -> {
73+
var from = randomMillisUpToYear9999();
74+
var to = randomLongBetween(from + 1, MAX_MILLIS_BEFORE_9999);
75+
yield new DateRangeBlockBuilder.DateRangeLiteral(from, to);
76+
}
6977
case DOC -> new BlockUtils.Doc(
7078
randomIntBetween(0, 255), // Shard ID should be small and non-negative.
7179
randomInt(),
7280
between(0, Integer.MAX_VALUE)
7381
);
7482
case NULL -> null;
7583
case COMPOSITE -> throw new IllegalArgumentException("can't make random values for composite");
76-
case DATE_RANGE -> throw new IllegalArgumentException("can't make random values for date range");
7784
case UNKNOWN -> throw new IllegalArgumentException("can't make random values for [" + e + "]");
7885
};
7986
}

0 commit comments

Comments
 (0)