Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
8a0a14f
Implemented basic ES|QL exponential histogram type
JonasKunz Sep 5, 2025
b19aed7
Update x-pack/plugin/esql/compute/test/src/main/java/org/elasticsearc…
JonasKunz Sep 5, 2025
af6efdb
Review fixes
JonasKunz Sep 5, 2025
e625bb4
Create deserialization utility in exp histo lib
JonasKunz Sep 5, 2025
f2e4113
Remove JsonBackedExponentialHistogram.java
JonasKunz Sep 5, 2025
6d05f43
Merge remote-tracking branch 'elastic/main' into exp-histo-esql
JonasKunz Sep 22, 2025
5dabd2e
Remove unnecessarry special case in hashcode
JonasKunz Sep 22, 2025
114cc58
[CI] Auto commit changes from spotless
Sep 22, 2025
723c5aa
Set of review fixes
JonasKunz Sep 22, 2025
4972c7b
Merge remote-tracking branch 'origin/exp-histo-esql' into exp-histo-esql
JonasKunz Sep 22, 2025
283da4d
Add equality test with random blocks.
JonasKunz Sep 23, 2025
51a5a84
[CI] Update transport version definitions
Sep 23, 2025
5a13b25
Add ToDo for NamedWriteable registration
JonasKunz Sep 23, 2025
76bf585
Merge branch 'main' into exp-histo-esql
JonasKunz Sep 23, 2025
6b84972
Merge branch 'main' into exp-histo-esql
JonasKunz Sep 23, 2025
0d8f467
Merge remote-tracking branch 'elastic/main' into exp-histo-esql
JonasKunz Oct 1, 2025
d5476c7
[CI] Auto commit changes from spotless
Oct 1, 2025
0c70b4f
Exclude from AllSupportedFieldsTestCase
JonasKunz Oct 1, 2025
5fc3538
Merge branch 'main' into exp-histo-esql
JonasKunz Oct 1, 2025
3a003ed
Fix rebase induced test failure
JonasKunz Oct 2, 2025
19a72a5
Merge remote-tracking branch 'elastic/main' into exp-histo-esql
JonasKunz Oct 17, 2025
08488a7
Fix merge conflicts
JonasKunz Oct 17, 2025
379dc91
Add accessor to minimize allocations
JonasKunz Oct 20, 2025
90e5862
Make block builder not accept null values
JonasKunz Oct 20, 2025
ad7c10b
Remove double closed check
JonasKunz Oct 20, 2025
4a10962
Revert "Remove csv tests as blockloader is not included in this PR"
JonasKunz Sep 5, 2025
4cf6417
Implemented BlockLoader
JonasKunz Sep 5, 2025
fa9082e
Fix EsqlSpecIT
JonasKunz Sep 23, 2025
14cf740
Add ignoredOrder, spotless
JonasKunz Sep 24, 2025
daa7934
Refactor: Move exponential histogram compression into shared library
JonasKunz Oct 22, 2025
858fd60
[CI] Auto commit changes from spotless
Oct 22, 2025
77762a5
Revert accidentally added character
JonasKunz Oct 22, 2025
e0a2888
Switch to block directly using doc values in disk format
JonasKunz Oct 22, 2025
275c05a
Implement block and block loader using disk layout
JonasKunz Oct 22, 2025
9af71b4
Extract multi value handling from ArrayBlock into base class
JonasKunz Oct 23, 2025
72c4732
Revert "Extract multi value handling from ArrayBlock into base class"
JonasKunz Oct 23, 2025
f69c492
Remove multi-value support
JonasKunz Oct 23, 2025
bedea36
Fix tests
JonasKunz Oct 23, 2025
d586acc
Merge remote-tracking branch 'elastic/main' into exp-histo-esql-v2
JonasKunz Oct 23, 2025
621b472
Revert BlockLoader related changes
JonasKunz Oct 23, 2025
537114f
Fix opentelemety default histogram size to actual values
JonasKunz Oct 23, 2025
d3c3bdf
Properly implement constant block, make invariants correctly handle n…
JonasKunz Oct 23, 2025
c3aa8fa
Avoid COnstantBytesRefBlock as it does not support serialization yet
JonasKunz Oct 23, 2025
f1b6b50
[CI] Auto commit changes from spotless
Oct 23, 2025
3578b0f
Move accessor outside of block, revert constant block implementation
JonasKunz Oct 23, 2025
4005297
[CI] Auto commit changes from spotless
Oct 23, 2025
9e955ff
Merge branch 'main' into exp-histo-esql
JonasKunz Oct 24, 2025
d366a4f
Adjust block visibility
JonasKunz Oct 24, 2025
c1eaa00
Clean up accessor terminology, javadoc
JonasKunz Oct 24, 2025
9790ee1
Merge branch 'main' into exp-histo-esql
JonasKunz Oct 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@

package org.elasticsearch.exponentialhistogram;

import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.Types;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

/**
* Handles the serialization of an {@link ExponentialHistogram} to XContent.
Expand All @@ -48,7 +55,11 @@ public class ExponentialHistogramXContent {
* @param histogram the ExponentialHistogram to serialize
* @throws IOException if the XContentBuilder throws an IOException
*/
public static void serialize(XContentBuilder builder, ExponentialHistogram histogram) throws IOException {
public static void serialize(XContentBuilder builder, @Nullable ExponentialHistogram histogram) throws IOException {
if (histogram == null) {
builder.nullValue();
return;
}
builder.startObject();

builder.field(SCALE_FIELD, histogram.scale());
Expand Down Expand Up @@ -101,4 +112,63 @@ private static void writeBuckets(XContentBuilder b, String fieldName, Exponentia
b.endObject();
}

/**
* Parses an {@link ExponentialHistogram} from the provided {@link XContentParser}.
* This method is neither optimized, nor does it do any validation of the parsed content.
* No estimation for missing sum/min/max is done.
* Therefore only intended for testing!
*
* @param xContent the serialized histogram to read
* @return the deserialized histogram
* @throws IOException if the XContentParser throws an IOException
*/
public static ExponentialHistogram parseForTesting(XContentParser xContent) throws IOException {
if (xContent.currentToken() == null) {
xContent.nextToken();
}
if (xContent.currentToken() == XContentParser.Token.VALUE_NULL) {
return null;
}
return parseForTesting(xContent.map());
}

/**
* Parses an {@link ExponentialHistogram} from a {@link Map}.
* This method is neither optimized, nor does it do any validation of the parsed content.
* No estimation for missing sum/min/max is done.
* Therefore only intended for testing!
*
* @param xContent the serialized histogram as a map
* @return the deserialized histogram
*/
public static ExponentialHistogram parseForTesting(@Nullable Map<String, Object> xContent) {
if (xContent == null) {
return null;
}
int scale = ((Number) xContent.get(SCALE_FIELD)).intValue();
ExponentialHistogramBuilder builder = ExponentialHistogram.builder(scale, ExponentialHistogramCircuitBreaker.noop());

Map<String, Number> zero = Types.forciblyCast(xContent.getOrDefault(ZERO_FIELD, Collections.emptyMap()));
double zeroThreshold = zero.getOrDefault(ZERO_THRESHOLD_FIELD, 0).doubleValue();
long zeroCount = zero.getOrDefault(ZERO_COUNT_FIELD, 0).longValue();
builder.zeroBucket(ZeroBucket.create(zeroThreshold, zeroCount));

builder.sum(((Number) xContent.getOrDefault(SUM_FIELD, 0)).doubleValue());
builder.min(((Number) xContent.getOrDefault(MIN_FIELD, Double.NaN)).doubleValue());
builder.max(((Number) xContent.getOrDefault(MAX_FIELD, Double.NaN)).doubleValue());

parseBuckets(Types.forciblyCast(xContent.getOrDefault(NEGATIVE_FIELD, Collections.emptyMap())), builder::setNegativeBucket);
parseBuckets(Types.forciblyCast(xContent.getOrDefault(POSITIVE_FIELD, Collections.emptyMap())), builder::setPositiveBucket);

return builder.build();
}

private static void parseBuckets(Map<String, List<Number>> serializedBuckets, BiConsumer<Long, Long> bucketSetter) {
List<Number> indices = serializedBuckets.getOrDefault(BUCKET_INDICES_FIELD, Collections.emptyList());
List<Number> counts = serializedBuckets.getOrDefault(BUCKET_COUNTS_FIELD, Collections.emptyList());
assert indices.size() == counts.size();
for (int i = 0; i < indices.size(); i++) {
bucketSetter.accept(indices.get(i).longValue(), counts.get(i).longValue());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import org.elasticsearch.common.Strings;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentParserConfiguration;
import org.elasticsearch.xcontent.json.JsonXContent;

import java.io.IOException;
Expand All @@ -31,6 +33,11 @@

public class ExponentialHistogramXContentTests extends ExponentialHistogramTestCase {

public void testNullHistogram() {
assertThat(toJson(null), equalTo("null"));
checkRoundTrip(null);
}

public void testEmptyHistogram() {
ExponentialHistogram emptyHistogram = ExponentialHistogram.empty();
assertThat(toJson(emptyHistogram), equalTo("{\"scale\":" + emptyHistogram.scale() + ",\"sum\":0.0}"));
Expand Down Expand Up @@ -62,18 +69,21 @@ public void testFullHistogram() {
+ "}"
)
);
checkRoundTrip(histo);
}

public void testOnlyZeroThreshold() {
ExponentialHistogram histo = createAutoReleasedHistogram(b -> b.scale(3).sum(1.1).zeroBucket(ZeroBucket.create(5.0, 0)));
assertThat(toJson(histo), equalTo("{\"scale\":3,\"sum\":1.1,\"zero\":{\"threshold\":5.0}}"));
checkRoundTrip(histo);
}

public void testOnlyZeroCount() {
ExponentialHistogram histo = createAutoReleasedHistogram(
b -> b.zeroBucket(ZeroBucket.create(0.0, 7)).scale(2).sum(1.1).min(0).max(0)
);
assertThat(toJson(histo), equalTo("{\"scale\":2,\"sum\":1.1,\"min\":0.0,\"max\":0.0,\"zero\":{\"count\":7}}"));
checkRoundTrip(histo);
}

public void testOnlyPositiveBuckets() {
Expand All @@ -84,6 +94,7 @@ public void testOnlyPositiveBuckets() {
toJson(histo),
equalTo("{\"scale\":4,\"sum\":1.1,\"min\":0.5,\"max\":2.5,\"positive\":{\"indices\":[-1,2],\"counts\":[3,5]}}")
);
checkRoundTrip(histo);
}

public void testOnlyNegativeBuckets() {
Expand All @@ -94,6 +105,7 @@ public void testOnlyNegativeBuckets() {
toJson(histo),
equalTo("{\"scale\":5,\"sum\":1.1,\"min\":-0.5,\"max\":-0.25,\"negative\":{\"indices\":[-1,2],\"counts\":[4,6]}}")
);
checkRoundTrip(histo);
}

private static String toJson(ExponentialHistogram histo) {
Expand All @@ -105,4 +117,17 @@ private static String toJson(ExponentialHistogram histo) {
}
}

private static void checkRoundTrip(ExponentialHistogram histo) {
try (XContentBuilder builder = JsonXContent.contentBuilder()) {
ExponentialHistogramXContent.serialize(builder, histo);
String json = Strings.toString(builder);
try (XContentParser parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, json)) {
ExponentialHistogram parsed = ExponentialHistogramXContent.parseForTesting(parser);
assertThat(parsed, equalTo(histo));
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

package org.elasticsearch.xpack.esql.core.plugin;

import org.elasticsearch.common.util.FeatureFlag;
import org.elasticsearch.plugins.ExtensiblePlugin;
import org.elasticsearch.plugins.Plugin;

public class EsqlCorePlugin extends Plugin implements ExtensiblePlugin {

public static final FeatureFlag EXPONENTIAL_HISTOGRAM_FEATURE_FLAG = new FeatureFlag("esql_exponential_histogram");
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ public enum DataType implements Writeable {
.estimatedSize(Double.BYTES * 3 + Integer.BYTES)
.supportedSince(DataTypesTransportVersions.ESQL_AGGREGATE_METRIC_DOUBLE_CREATED_VERSION)
),

EXPONENTIAL_HISTOGRAM(
builder().esType("exponential_histogram")
.estimatedSize(16 * 160)// guess 160 buckets (OTEL default for positive values only histograms) with 16 bytes per bucket
.docValues()
.underConstruction()
),

/**
* Fields with this type are dense vectors, represented as an array of float values.
*/
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugin/esql/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies {
implementation project('compute:ann')
implementation project(':libs:dissect')
implementation project(':libs:grok')
implementation project(':libs:exponential-histogram')
api "org.apache.lucene:lucene-spatial3d:${versions.lucene}"
api project(":libs:h3")
implementation project('arrow')
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugin/esql/compute/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies {
compileOnly project(xpackModule('ml'))
annotationProcessor project('gen')
implementation 'com.carrotsearch:hppc:0.8.1'
api project(':libs:exponential-histogram')

testImplementation(project(':modules:analysis-common'))
testImplementation(project(':test:framework'))
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugin/esql/compute/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
requires org.elasticsearch.geo;
requires org.elasticsearch.xcore;
requires hppc;
requires org.elasticsearch.exponentialhistogram;

exports org.elasticsearch.compute;
exports org.elasticsearch.compute.aggregation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.elasticsearch.common.util.BigArrays;
import org.elasticsearch.common.util.BytesRefArray;
import org.elasticsearch.compute.data.Block.MvOrdering;
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;

import java.util.BitSet;

Expand Down Expand Up @@ -467,6 +468,19 @@ public final AggregateMetricDoubleBlock newConstantAggregateMetricDoubleBlock(
}
}

public ExponentialHistogramBlockBuilder newExponentialHistogramBlockBuilder(int estimatedSize) {
return new ExponentialHistogramBlockBuilder(estimatedSize, this);
}

public final ExponentialHistogramBlock newConstantExponentialHistogramBlock(ExponentialHistogram value, int positionCount) {
try (ExponentialHistogramBlockBuilder builder = newExponentialHistogramBlockBuilder(positionCount)) {
for (int i = 0; i < positionCount; i++) {
builder.append(value);
}
return builder.build();
}
}

public final AggregateMetricDoubleBlock newAggregateMetricDoubleBlock(
double[] minValues,
double[] maxValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.elasticsearch.compute.data.AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral;
import org.elasticsearch.core.Releasable;
import org.elasticsearch.core.Releasables;
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;
import org.elasticsearch.exponentialhistogram.ExponentialHistogramCircuitBreaker;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -224,6 +226,7 @@ public static void appendValue(Block.Builder builder, Object val, ElementType ty
case DOUBLE -> ((DoubleBlock.Builder) builder).appendDouble((Double) val);
case BOOLEAN -> ((BooleanBlock.Builder) builder).appendBoolean((Boolean) val);
case AGGREGATE_METRIC_DOUBLE -> ((AggregateMetricDoubleBlockBuilder) builder).appendLiteral((AggregateMetricDoubleLiteral) val);
case EXPONENTIAL_HISTOGRAM -> ((ExponentialHistogramBlockBuilder) builder).append((ExponentialHistogram) val);
default -> throw new UnsupportedOperationException("unsupported element type [" + type + "]");
}
}
Expand Down Expand Up @@ -253,6 +256,7 @@ private static Block constantBlock(BlockFactory blockFactory, ElementType type,
case BOOLEAN -> blockFactory.newConstantBooleanBlockWith((boolean) val, size);
case AGGREGATE_METRIC_DOUBLE -> blockFactory.newConstantAggregateMetricDoubleBlock((AggregateMetricDoubleLiteral) val, size);
case FLOAT -> blockFactory.newConstantFloatBlockWith((float) val, size);
case EXPONENTIAL_HISTOGRAM -> blockFactory.newConstantExponentialHistogramBlock((ExponentialHistogram) val, size);
default -> throw new UnsupportedOperationException("unsupported element type [" + type + "]");
};
}
Expand Down Expand Up @@ -306,6 +310,12 @@ yield new AggregateMetricDoubleLiteral(
aggBlock.countBlock().getInt(offset)
);
}
case EXPONENTIAL_HISTOGRAM -> {
ExponentialHistogramBlock histoBlock = (ExponentialHistogramBlock) block;
ExponentialHistogram histogram = new ExponentialHistogramBlockAccessor(histoBlock).get(offset);
// return a copy so that the returned value is not bound to the lifetime of the block
yield ExponentialHistogram.builder(histogram, ExponentialHistogramCircuitBreaker.noop()).build();
}
case UNKNOWN -> throw new IllegalArgumentException("can't read values from [" + block + "]");
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public final class ConstantNullBlock extends AbstractNonThreadSafeRefCounted
FloatBlock,
DoubleBlock,
BytesRefBlock,
AggregateMetricDoubleBlock {
AggregateMetricDoubleBlock,
ExponentialHistogramBlock {

private static final long BASE_RAM_BYTES_USED = RamUsageEstimator.shallowSizeOfInstance(ConstantNullBlock.class);
private final int positionCount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.elasticsearch.TransportVersion;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;

import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -64,6 +65,16 @@ public enum ElementType {
"AggregateMetricDouble",
BlockFactory::newAggregateMetricDoubleBlockBuilder,
AggregateMetricDoubleArrayBlock::readFrom
),

/**
* Blocks that contain exponential_histograms.
*/
EXPONENTIAL_HISTOGRAM(
11,
"ExponentialHistogram",
BlockFactory::newExponentialHistogramBlockBuilder,
ExponentialHistogramArrayBlock::readFrom
);

private static final TransportVersion ESQL_SERIALIZE_BLOCK_TYPE_CODE = TransportVersion.fromName("esql_serialize_block_type_code");
Expand Down Expand Up @@ -113,6 +124,8 @@ public static ElementType fromJava(Class<?> type) {
elementType = BOOLEAN;
} else if (type == AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral.class) {
elementType = AGGREGATE_METRIC_DOUBLE;
} else if (type != null && ExponentialHistogram.class.isAssignableFrom(type)) {
elementType = EXPONENTIAL_HISTOGRAM;
} else if (type == null || type == Void.class) {
elementType = NULL;
} else {
Expand Down
Loading