Skip to content

Commit e522df6

Browse files
authored
ESQL: replace ExponentialHistogramBlock accessor with scratch (#137368)
1 parent e3bfec1 commit e522df6

File tree

9 files changed

+59
-61
lines changed

9 files changed

+59
-61
lines changed

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/BlockUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ yield new AggregateMetricDoubleLiteral(
312312
}
313313
case EXPONENTIAL_HISTOGRAM -> {
314314
ExponentialHistogramBlock histoBlock = (ExponentialHistogramBlock) block;
315-
ExponentialHistogram histogram = new ExponentialHistogramBlockAccessor(histoBlock).get(offset);
315+
ExponentialHistogram histogram = histoBlock.getExponentialHistogram(offset, new ExponentialHistogramScratch());
316316
// return a copy so that the returned value is not bound to the lifetime of the block
317317
yield ExponentialHistogram.builder(histogram, ExponentialHistogramCircuitBreaker.noop()).build();
318318
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ConstantNullBlock.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.io.stream.StreamOutput;
1313
import org.elasticsearch.common.unit.ByteSizeValue;
1414
import org.elasticsearch.core.ReleasableIterator;
15+
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;
1516

1617
import java.io.IOException;
1718

@@ -293,6 +294,12 @@ public long getLong(int valueIndex) {
293294
throw new UnsupportedOperationException("null block");
294295
}
295296

297+
@Override
298+
public ExponentialHistogram getExponentialHistogram(int valueIndex, ExponentialHistogramScratch scratch) {
299+
assert false : "null block";
300+
throw new UnsupportedOperationException("null block");
301+
}
302+
296303
@Override
297304
public int getTotalValueCount() {
298305
return 0;

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ExponentialHistogramArrayBlock.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.elasticsearch.common.unit.ByteSizeValue;
1313
import org.elasticsearch.core.ReleasableIterator;
1414
import org.elasticsearch.core.Releasables;
15-
import org.elasticsearch.exponentialhistogram.CompressedExponentialHistogram;
15+
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;
1616

1717
import java.io.IOException;
1818
import java.util.List;
@@ -78,15 +78,17 @@ private List<Block> getSubBlocks() {
7878
return List.of(sums, valueCounts, zeroThresholds, encodedHistograms, minima, maxima);
7979
}
8080

81-
void loadValue(int valueIndex, CompressedExponentialHistogram resultHistogram, BytesRef tempBytesRef) {
82-
BytesRef bytes = encodedHistograms.getBytesRef(encodedHistograms.getFirstValueIndex(valueIndex), tempBytesRef);
81+
@Override
82+
public ExponentialHistogram getExponentialHistogram(int valueIndex, ExponentialHistogramScratch scratch) {
83+
BytesRef bytes = encodedHistograms.getBytesRef(encodedHistograms.getFirstValueIndex(valueIndex), scratch.bytesRefScratch);
8384
double zeroThreshold = zeroThresholds.getDouble(zeroThresholds.getFirstValueIndex(valueIndex));
8485
long valueCount = valueCounts.getLong(valueCounts.getFirstValueIndex(valueIndex));
8586
double sum = sums.getDouble(sums.getFirstValueIndex(valueIndex));
8687
double min = valueCount == 0 ? Double.NaN : minima.getDouble(minima.getFirstValueIndex(valueIndex));
8788
double max = valueCount == 0 ? Double.NaN : maxima.getDouble(maxima.getFirstValueIndex(valueIndex));
8889
try {
89-
resultHistogram.reset(zeroThreshold, valueCount, sum, min, max, bytes);
90+
scratch.reusedHistogram.reset(zeroThreshold, valueCount, sum, min, max, bytes);
91+
return scratch.reusedHistogram;
9092
} catch (IOException e) {
9193
throw new IllegalStateException("error loading histogram", e);
9294
}
@@ -374,4 +376,5 @@ public int hashCode() {
374376
// this ensures proper equality with null blocks and should be unique enough for practical purposes
375377
return encodedHistograms.hashCode();
376378
}
379+
377380
}

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ExponentialHistogramBlock.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,22 @@
1111

1212
/**
1313
* A block that holds {@link ExponentialHistogram} values.
14-
* Position access is done through {@link ExponentialHistogramBlockAccessor}.
1514
*/
1615
public sealed interface ExponentialHistogramBlock extends Block permits ConstantNullBlock, ExponentialHistogramArrayBlock {
1716

17+
/**
18+
* Returns the {@link ExponentialHistogram} value at the given index.
19+
* In order to be allocation free, this method requires a scratch object to be passed in,
20+
* whose memory will be used to hold the state of the returned histogram.
21+
* Therefore, the return value of this method is only valid until either the block is closed
22+
* or the same scratch instance is passed to another call to this method on any block.
23+
*
24+
* @param valueIndex the index of the histogram to get
25+
* @param scratch the scratch to use as storage for the returned histogram
26+
* @return the exponential histogram at the given index
27+
*/
28+
ExponentialHistogram getExponentialHistogram(int valueIndex, ExponentialHistogramScratch scratch);
29+
1830
static boolean equals(ExponentialHistogramBlock blockA, ExponentialHistogramBlock blockB) {
1931
if (blockA == blockB) {
2032
return true;

x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/data/ExponentialHistogramBlockAccessor.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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.data;
9+
10+
import org.apache.lucene.util.BytesRef;
11+
import org.elasticsearch.exponentialhistogram.CompressedExponentialHistogram;
12+
13+
/**
14+
* Reusable storage to be passed to {@link ExponentialHistogramBlock#getExponentialHistogram(int, ExponentialHistogramScratch)}.
15+
*/
16+
public class ExponentialHistogramScratch {
17+
18+
final BytesRef bytesRefScratch = new BytesRef();
19+
final CompressedExponentialHistogram reusedHistogram = new CompressedExponentialHistogram();
20+
21+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import org.elasticsearch.compute.data.DoubleBlock;
2121
import org.elasticsearch.compute.data.ElementType;
2222
import org.elasticsearch.compute.data.ExponentialHistogramBlock;
23-
import org.elasticsearch.compute.data.ExponentialHistogramBlockAccessor;
2423
import org.elasticsearch.compute.data.ExponentialHistogramBlockBuilder;
24+
import org.elasticsearch.compute.data.ExponentialHistogramScratch;
2525
import org.elasticsearch.compute.data.FloatBlock;
2626
import org.elasticsearch.compute.data.IntBlock;
2727
import org.elasticsearch.compute.data.LongBlock;
@@ -313,7 +313,10 @@ public static List<List<Object>> valuesAtPositions(Block block, int from, int to
313313
yield literal;
314314

315315
}
316-
case EXPONENTIAL_HISTOGRAM -> new ExponentialHistogramBlockAccessor((ExponentialHistogramBlock) block).get(i);
316+
case EXPONENTIAL_HISTOGRAM -> ((ExponentialHistogramBlock) block).getExponentialHistogram(
317+
i++,
318+
new ExponentialHistogramScratch()
319+
);
317320
default -> throw new IllegalArgumentException("unsupported element type [" + block.elementType() + "]");
318321
});
319322
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/action/PositionToXContent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.elasticsearch.compute.data.BytesRefBlock;
1717
import org.elasticsearch.compute.data.DoubleBlock;
1818
import org.elasticsearch.compute.data.ExponentialHistogramBlock;
19-
import org.elasticsearch.compute.data.ExponentialHistogramBlockAccessor;
19+
import org.elasticsearch.compute.data.ExponentialHistogramScratch;
2020
import org.elasticsearch.compute.data.FloatBlock;
2121
import org.elasticsearch.compute.data.IntBlock;
2222
import org.elasticsearch.compute.data.LongBlock;
@@ -174,12 +174,12 @@ protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Pa
174174
};
175175
case EXPONENTIAL_HISTOGRAM -> new PositionToXContent(block) {
176176

177-
ExponentialHistogramBlockAccessor accessor = new ExponentialHistogramBlockAccessor((ExponentialHistogramBlock) block);
177+
ExponentialHistogramScratch scratch = new ExponentialHistogramScratch();
178178

179179
@Override
180180
protected XContentBuilder valueToXContent(XContentBuilder builder, ToXContent.Params params, int valueIndex)
181181
throws IOException {
182-
ExponentialHistogram histogram = accessor.get(valueIndex);
182+
ExponentialHistogram histogram = ((ExponentialHistogramBlock) block).getExponentialHistogram(valueIndex, scratch);
183183
ExponentialHistogramXContent.serialize(builder, histogram);
184184
return builder;
185185
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/type/EsqlDataTypeConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder.Metric;
2222
import org.elasticsearch.compute.data.DoubleBlock;
2323
import org.elasticsearch.compute.data.ExponentialHistogramBlock;
24-
import org.elasticsearch.compute.data.ExponentialHistogramBlockAccessor;
24+
import org.elasticsearch.compute.data.ExponentialHistogramScratch;
2525
import org.elasticsearch.compute.data.IntBlock;
2626
import org.elasticsearch.core.Booleans;
2727
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;
@@ -799,7 +799,7 @@ public static String exponentialHistogramToString(ExponentialHistogram histo) {
799799
}
800800

801801
public static String exponentialHistogramBlockToString(ExponentialHistogramBlock histoBlock, int index) {
802-
ExponentialHistogram histo = new ExponentialHistogramBlockAccessor(histoBlock).get(index);
802+
ExponentialHistogram histo = histoBlock.getExponentialHistogram(index, new ExponentialHistogramScratch());
803803
return exponentialHistogramToString(histo);
804804
}
805805

0 commit comments

Comments
 (0)