Skip to content

Commit 3c4bae7

Browse files
committed
polish code
1 parent e4d533c commit 3c4bae7

File tree

7 files changed

+69
-43
lines changed

7 files changed

+69
-43
lines changed

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/SampleBooleanAggregator.java

+11-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/SampleBytesRefAggregator.java

+10-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/SampleDoubleAggregator.java

+11-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/SampleIntAggregator.java

+11-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/generated-src/org/elasticsearch/compute/aggregation/SampleLongAggregator.java

+10-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/aggregation/X-SampleAggregator.java.st

+10-6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import org.elasticsearch.core.Releasables;
2626
import org.elasticsearch.search.sort.SortOrder;
2727

2828
import java.util.SplittableRandom;
29+
import java.util.random.RandomGenerator;
2930

3031
/**
3132
* Sample N field values for $type$.
@@ -119,18 +120,21 @@ class Sample$Type$Aggregator {
119120
public static class GroupingState implements GroupingAggregatorState {
120121
private final CircuitBreaker breaker;
121122
private final BytesRefBucketedSort sort;
123+
private final BreakingBytesRefBuilder bytesRefBuilder;
124+
private final RandomGenerator random;
122125

123126
private GroupingState(BigArrays bigArrays, int limit) {
124127
this.breaker = bigArrays.breakerService().getBreaker(CircuitBreaker.REQUEST);
125128
this.sort = new BytesRefBucketedSort(breaker, "sample", bigArrays, SortOrder.ASC, limit);
129+
this.bytesRefBuilder = new BreakingBytesRefBuilder(breaker, "sample");
130+
this.random = new SplittableRandom();
126131
}
127132

128133
public void add(int groupId, $type$ value) {
129-
try (BreakingBytesRefBuilder builder = new BreakingBytesRefBuilder(breaker, "sample")) {
130-
ENCODER.encodeLong(new SplittableRandom().nextLong(), builder);
131-
ENCODER.encode$Type$(value, builder);
132-
sort.collect(builder.bytesRefView(), groupId);
133-
}
134+
ENCODER.encodeLong(random.nextLong(), bytesRefBuilder);
135+
ENCODER.encode$Type$(value, bytesRefBuilder);
136+
sort.collect(bytesRefBuilder.bytesRefView(), groupId);
137+
bytesRefBuilder.clear();
134138
}
135139

136140
public void merge(int groupId, GroupingState other, int otherGroupId) {
@@ -153,7 +157,7 @@ class Sample$Type$Aggregator {
153157

154158
@Override
155159
public void close() {
156-
Releasables.closeExpectNoException(sort);
160+
Releasables.closeExpectNoException(sort, bytesRefBuilder);
157161
}
158162
}
159163

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public static Iterable<Object[]> parameters() {
5656
MultiRowTestCaseSupplier.cartesianPointCases(1, 1000, MultiRowTestCaseSupplier.IncludingAltitude.NO),
5757
MultiRowTestCaseSupplier.geoShapeCasesWithoutCircle(1, 100, MultiRowTestCaseSupplier.IncludingAltitude.NO),
5858
MultiRowTestCaseSupplier.cartesianShapeCasesWithoutCircle(1, 100, MultiRowTestCaseSupplier.IncludingAltitude.NO)
59-
).flatMap(List::stream)
59+
)
60+
.flatMap(List::stream)
6061
.map(fieldCaseSupplier -> makeSupplier(fieldCaseSupplier, limitCaseSupplier))
6162
.collect(Collectors.toCollection(() -> suppliers));
6263
}
@@ -68,8 +69,10 @@ protected Expression build(Source source, List<Expression> args) {
6869
return new Sample(source, args.get(0), args.get(1));
6970
}
7071

71-
private static TestCaseSupplier makeSupplier(TestCaseSupplier.TypedDataSupplier fieldSupplier, TestCaseSupplier.TypedDataSupplier limitCaseSupplier
72-
) {
72+
private static TestCaseSupplier makeSupplier(
73+
TestCaseSupplier.TypedDataSupplier fieldSupplier,
74+
TestCaseSupplier.TypedDataSupplier limitCaseSupplier
75+
) {
7376
return new TestCaseSupplier(fieldSupplier.name(), List.of(fieldSupplier.type(), limitCaseSupplier.type()), () -> {
7477
var fieldTypedData = fieldSupplier.get();
7578
var limitTypedData = limitCaseSupplier.get().forceLiteral();

0 commit comments

Comments
 (0)