Skip to content

Commit 6906c53

Browse files
committed
Review fixes
1 parent bae17ea commit 6906c53

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,34 @@
1919

2020
final class ExponentialHistogramArrayBlock extends AbstractNonThreadSafeRefCounted implements ExponentialHistogramBlock {
2121

22+
// Exponential histograms consist of several components that we store in separate blocks
23+
// due to (a) better compression in the field mapper for disk storage and (b) faster computations if only one sub-component is needed
24+
// What are the semantics of positions, multi-value counts and nulls in the exponential histogram block and
25+
// how do they relate to the sub-blocks?
26+
// ExponentialHistogramBlock need to adhere to the contract of Blocks for the access patterns:
27+
//
28+
// for (int position = 0; position < block.getPositionCount(); position++) {
29+
// ...int valueCount = block.getValueCount(position);
30+
// ...for (int valueIndex = 0; valueIndex < valueCount; valueIndex++) {
31+
// ......ExponentialHistogram histo = block.getExponentialHistogram(valueIndex, scratch);
32+
// ...}
33+
// }
34+
//
35+
// That implies that given only a value-index, we need to be able to retrieve all components of the histogram.
36+
// Because we can't make any assumptions on how value indices are laid out in the sub-blocks for multi-values,
37+
// we enforce that the sub-blocks have at most one value per position (i.e., no multi-values).
38+
// Based on this, we can define the valueIndex for ExponentialHistogramArrayBlock to correspond to positions in the sub-blocks.
39+
// So basically the sub-blocks are the "flattened" components of the histograms.
40+
// If we later add multi-value support to ExponentialHistogramArrayBlock,
41+
// we can't use the multi-value support of the sub-blocks to implement that.
42+
// Instead, we need to maintain a firstValueIndex array ourselves in ExponentialHistogramArrayBlock.
43+
2244
private final DoubleBlock minima;
2345
private final DoubleBlock maxima;
2446
private final DoubleBlock sums;
47+
/**
48+
Holds the number of values in each histogram. Note that this is a different concept from getValueCount(position)!
49+
*/
2550
private final LongBlock valueCounts;
2651
private final DoubleBlock zeroThresholds;
2752
private final BytesRefBlock encodedHistograms;
@@ -96,6 +121,8 @@ public ExponentialHistogram getExponentialHistogram(int valueIndex, ExponentialH
96121

97122
@Override
98123
public void serializeExponentialHistogram(int valueIndex, SerializedOutput out, BytesRef scratch) {
124+
// not that this value count is different from getValueCount(position)!
125+
// this value count represents the number of individual samples the histogram was computed for
99126
long valueCount = valueCounts.getLong(valueCounts.getFirstValueIndex(valueIndex));
100127
out.appendLong(valueCounts.getLong(valueCounts.getFirstValueIndex(valueIndex)));
101128
out.appendDouble(sums.getDouble(sums.getFirstValueIndex(valueIndex)));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ interface SerializedInput {
7373

7474
long readLong();
7575

76-
BytesRef readBytesRef(BytesRef tempBytesRef);
76+
BytesRef readBytesRef(BytesRef scratch);
7777
}
7878

7979
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void close() {
5454
builder.close();
5555
}
5656

57-
private class ReusableTopNEncoderInput implements ExponentialHistogramBlock.SerializedInput {
57+
private static final class ReusableTopNEncoderInput implements ExponentialHistogramBlock.SerializedInput {
5858
BytesRef inputValues;
5959

6060
@Override
@@ -68,8 +68,8 @@ public long readLong() {
6868
}
6969

7070
@Override
71-
public BytesRef readBytesRef(BytesRef tempBytesRef) {
72-
return TopNEncoder.DEFAULT_UNSORTABLE.decodeBytesRef(inputValues, tempBytesRef);
71+
public BytesRef readBytesRef(BytesRef scratch) {
72+
return TopNEncoder.DEFAULT_UNSORTABLE.decodeBytesRef(inputValues, scratch);
7373
}
7474
}
7575
}

0 commit comments

Comments
 (0)