Skip to content

Commit f2e4113

Browse files
committed
Remove JsonBackedExponentialHistogram.java
1 parent e625bb4 commit f2e4113

File tree

5 files changed

+41
-112
lines changed

5 files changed

+41
-112
lines changed

libs/exponential-histogram/src/main/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramXContent.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package org.elasticsearch.exponentialhistogram;
2323

24+
import org.elasticsearch.core.Nullable;
2425
import org.elasticsearch.core.Types;
2526
import org.elasticsearch.xcontent.XContentBuilder;
2627
import org.elasticsearch.xcontent.XContentParser;
@@ -54,7 +55,11 @@ public class ExponentialHistogramXContent {
5455
* @param histogram the ExponentialHistogram to serialize
5556
* @throws IOException if the XContentBuilder throws an IOException
5657
*/
57-
public static void serialize(XContentBuilder builder, ExponentialHistogram histogram) throws IOException {
58+
public static void serialize(XContentBuilder builder, @Nullable ExponentialHistogram histogram) throws IOException {
59+
if (histogram == null) {
60+
builder.nullValue();
61+
return;
62+
}
5863
builder.startObject();
5964

6065
builder.field(SCALE_FIELD, histogram.scale());
@@ -118,6 +123,12 @@ private static void writeBuckets(XContentBuilder b, String fieldName, Exponentia
118123
* @throws IOException if the XContentParser throws an IOException
119124
*/
120125
public static ExponentialHistogram parseForTesting(XContentParser xContent) throws IOException {
126+
if (xContent.currentToken() == null) {
127+
xContent.nextToken();
128+
}
129+
if (xContent.currentToken() == XContentParser.Token.VALUE_NULL) {
130+
return null;
131+
}
121132
return parseForTesting(xContent.map());
122133
}
123134

@@ -130,7 +141,10 @@ public static ExponentialHistogram parseForTesting(XContentParser xContent) thro
130141
* @param xContent the serialized histogram as a map
131142
* @return the deserialized histogram
132143
*/
133-
public static ExponentialHistogram parseForTesting(Map<String, Object> xContent) {
144+
public static ExponentialHistogram parseForTesting(@Nullable Map<String, Object> xContent) {
145+
if (xContent == null) {
146+
return null;
147+
}
134148
int scale = ((Number) xContent.get(SCALE_FIELD)).intValue();
135149
ExponentialHistogramBuilder builder = ExponentialHistogram.builder(scale, ExponentialHistogramCircuitBreaker.noop());
136150

libs/exponential-histogram/src/test/java/org/elasticsearch/exponentialhistogram/ExponentialHistogramXContentTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333

3434
public class ExponentialHistogramXContentTests extends ExponentialHistogramTestCase {
3535

36+
public void testNullHistogram() {
37+
assertThat(toJson(null), equalTo("null"));
38+
checkRoundTrip(null);
39+
}
40+
3641
public void testEmptyHistogram() {
3742
ExponentialHistogram emptyHistogram = ExponentialHistogram.empty();
3843
assertThat(toJson(emptyHistogram), equalTo("{\"scale\":" + emptyHistogram.scale() + ",\"sum\":0.0}"));

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/CsvTestUtils.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,21 @@
2323
import org.elasticsearch.compute.data.ElementType;
2424
import org.elasticsearch.compute.data.Page;
2525
import org.elasticsearch.core.Booleans;
26+
import org.elasticsearch.core.Nullable;
2627
import org.elasticsearch.core.Releasable;
2728
import org.elasticsearch.core.Releasables;
2829
import org.elasticsearch.core.Strings;
2930
import org.elasticsearch.core.Tuple;
3031
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;
32+
import org.elasticsearch.exponentialhistogram.ExponentialHistogramXContent;
3133
import org.elasticsearch.geometry.utils.Geohash;
3234
import org.elasticsearch.h3.H3;
3335
import org.elasticsearch.logging.Logger;
3436
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils;
3537
import org.elasticsearch.test.VersionUtils;
38+
import org.elasticsearch.xcontent.XContentParser;
39+
import org.elasticsearch.xcontent.XContentParserConfiguration;
40+
import org.elasticsearch.xcontent.json.JsonXContent;
3641
import org.elasticsearch.xpack.esql.action.ResponseValueUtils;
3742
import org.elasticsearch.xpack.esql.core.type.DataType;
3843
import org.elasticsearch.xpack.esql.core.util.StringUtils;
@@ -499,7 +504,7 @@ public enum Type {
499504
AggregateMetricDoubleBlockBuilder.AggregateMetricDoubleLiteral.class
500505
),
501506
DENSE_VECTOR(Float::parseFloat, Float.class, false),
502-
EXPONENTIAL_HISTOGRAM(x -> x == null ? null : JsonBackedExponentialHistogram.createFromJson(x), ExponentialHistogram.class),
507+
EXPONENTIAL_HISTOGRAM(CsvTestUtils::parseExponentialHistogram, ExponentialHistogram.class),
503508
UNSUPPORTED(Type::convertUnsupported, Void.class);
504509

505510
private static Void convertUnsupported(String s) {
@@ -701,4 +706,15 @@ private static double scaledFloat(String value, String factor) {
701706
double scalingFactor = Double.parseDouble(factor);
702707
return new BigDecimal(value).multiply(BigDecimal.valueOf(scalingFactor)).longValue() / scalingFactor;
703708
}
709+
710+
private static ExponentialHistogram parseExponentialHistogram(@Nullable String json) {
711+
if (json == null) {
712+
return null;
713+
}
714+
try (XContentParser parser = JsonXContent.jsonXContent.createParser(XContentParserConfiguration.EMPTY, json)) {
715+
return ExponentialHistogramXContent.parseForTesting(parser);
716+
} catch (IOException e) {
717+
throw new IllegalArgumentException(e);
718+
}
719+
}
704720
}

x-pack/plugin/esql/qa/testFixtures/src/main/java/org/elasticsearch/xpack/esql/JsonBackedExponentialHistogram.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/action/EsqlQueryResponseTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.elasticsearch.core.Types;
4343
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;
4444
import org.elasticsearch.exponentialhistogram.ExponentialHistogramCircuitBreaker;
45+
import org.elasticsearch.exponentialhistogram.ExponentialHistogramXContent;
4546
import org.elasticsearch.geo.GeometryTestUtils;
4647
import org.elasticsearch.geo.ShapeTestUtils;
4748
import org.elasticsearch.geometry.Point;
@@ -62,7 +63,6 @@
6263
import org.elasticsearch.xcontent.XContentParser;
6364
import org.elasticsearch.xcontent.XContentType;
6465
import org.elasticsearch.xcontent.json.JsonXContent;
65-
import org.elasticsearch.xpack.esql.JsonBackedExponentialHistogram;
6666
import org.elasticsearch.xpack.esql.core.type.DataType;
6767
import org.elasticsearch.xpack.esql.planner.PlannerUtils;
6868
import org.elasticsearch.xpack.esql.type.UnsupportedEsFieldTests;
@@ -1268,7 +1268,8 @@ static Page valuesToPage(BlockFactory blockFactory, List<ColumnInfoImpl> columns
12681268
}
12691269
case EXPONENTIAL_HISTOGRAM -> {
12701270
ExponentialHistogramBlockBuilder expHistoBuilder = (ExponentialHistogramBlockBuilder) builder;
1271-
expHistoBuilder.append(JsonBackedExponentialHistogram.createFromMap(Types.forciblyCast(value)));
1271+
Map<String, Object> serializedHisto = Types.forciblyCast(value);
1272+
expHistoBuilder.append(ExponentialHistogramXContent.parseForTesting(serializedHisto));
12721273
}
12731274
}
12741275
}

0 commit comments

Comments
 (0)