Skip to content

[Backport 7.x] Update boxplot to handle non-numeric values #6097

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 9 additions & 7 deletions src/Nest/Aggregations/AggregateFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,39 +251,41 @@ private IAggregate GetMatrixStatsAggregate(ref JsonReader reader, IJsonFormatter

private IAggregate GetBoxplotAggregate(ref JsonReader reader, IJsonFormatterResolver formatterResolver, IReadOnlyDictionary<string, object> meta)
{
var nullableDoubleFormatter = new StringDoubleFormatter();

var boxplot = new BoxplotAggregate
{
Min = reader.ReadDouble(),
Min = nullableDoubleFormatter.Deserialize(ref reader, formatterResolver),
Meta = meta
};
reader.ReadNext(); // ,
reader.ReadNext(); // "max"
reader.ReadNext(); // :
boxplot.Max = reader.ReadDouble();
boxplot.Max = nullableDoubleFormatter.Deserialize(ref reader, formatterResolver);
reader.ReadNext(); // ,
reader.ReadNext(); // "q1"
reader.ReadNext(); // :
boxplot.Q1 = reader.ReadDouble();
boxplot.Q1 = nullableDoubleFormatter.Deserialize(ref reader, formatterResolver);
reader.ReadNext(); // ,
reader.ReadNext(); // "q2"
reader.ReadNext(); // :
boxplot.Q2 = reader.ReadDouble();
boxplot.Q2 = nullableDoubleFormatter.Deserialize(ref reader, formatterResolver);
reader.ReadNext(); // ,
reader.ReadNext(); // "q3"
reader.ReadNext(); // :
boxplot.Q3 = reader.ReadDouble();
boxplot.Q3 = nullableDoubleFormatter.Deserialize(ref reader, formatterResolver);

var token = reader.GetCurrentJsonToken();
if (token != JsonToken.EndObject)
{
reader.ReadNext(); // ,
reader.ReadNext(); // "lower"
reader.ReadNext(); // :
boxplot.Lower = reader.ReadDouble();
boxplot.Lower = nullableDoubleFormatter.Deserialize(ref reader, formatterResolver);
reader.ReadNext(); // ,
reader.ReadNext(); // "upper"
reader.ReadNext(); // :
boxplot.Upper = reader.ReadDouble();
boxplot.Upper = nullableDoubleFormatter.Deserialize(ref reader, formatterResolver);
}

return boxplot;
Expand Down
9 changes: 9 additions & 0 deletions src/Nest/Aggregations/Metric/Boxplot/BoxplotAggregate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using Elasticsearch.Net.Utf8Json;

namespace Nest
{
public class BoxplotAggregate : MetricAggregateBase
{
[JsonFormatter(typeof(StringDoubleFormatter))]
public double Min { get; set; }

[JsonFormatter(typeof(StringDoubleFormatter))]
public double Max { get; set; }

[JsonFormatter(typeof(StringDoubleFormatter))]
public double Q1 { get; set; }

[JsonFormatter(typeof(StringDoubleFormatter))]
public double Q2 { get; set; }

[JsonFormatter(typeof(StringDoubleFormatter))]
public double Q3 { get; set; }

[JsonFormatter(typeof(StringDoubleFormatter))]
public double Lower { get; set; }

[JsonFormatter(typeof(StringDoubleFormatter))]
public double Upper { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,13 @@ internal class NullableStringDoubleFormatter : IJsonFormatter<double?>
return null;
case JsonToken.String:
var s = reader.ReadString();

if (s.Equals("Infinity", System.StringComparison.Ordinal))
return double.PositiveInfinity;

if (s.Equals("-Infinity", System.StringComparison.Ordinal))
return double.NegativeInfinity;

if (!double.TryParse(s, out var d))
throw new JsonParsingException($"Cannot parse {typeof(double).FullName} from: {s}");

Expand All @@ -194,4 +201,39 @@ public void Serialize(ref JsonWriter writer, double? value, IJsonFormatterResolv
writer.WriteDouble(value.Value);
}
}

internal class StringDoubleFormatter : IJsonFormatter<double>
{
public double Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
{
var token = reader.GetCurrentJsonToken();
switch (token)
{
case JsonToken.Null:
throw new JsonParsingException($"Cannot parse non-nullable double value from: {token}.");

case JsonToken.String:
var s = reader.ReadString();

if (s.Equals("Infinity", System.StringComparison.Ordinal))
return double.PositiveInfinity;

if (s.Equals("-Infinity", System.StringComparison.Ordinal))
return double.NegativeInfinity;

if (!double.TryParse(s, out var d))
throw new JsonParsingException($"Cannot parse {typeof(double).FullName} from: {s}");

return d;

case JsonToken.Number:
return reader.ReadDouble();

default:
throw new JsonParsingException($"Cannot parse {typeof(double).FullName} from: {token}");
}
}

public void Serialize(ref JsonWriter writer, double value, IJsonFormatterResolver formatterResolver) => writer.WriteDouble(value);
}
}
75 changes: 75 additions & 0 deletions tests/Tests.Reproduce/GitHubIssue6050.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System;
using System.Text;
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
using Elasticsearch.Net;
using FluentAssertions;
using Nest;

namespace Tests.Reproduce
{
public class GitHubIssue6050
{
private static readonly byte[] ResponseBytes = Encoding.UTF8.GetBytes(@"{
""took"" : 1,
""timed_out"" : false,
""_shards"" : {
""total"" : 1,
""successful"" : 1,
""skipped"" : 0,
""failed"" : 0
},
""hits"" : {
""total"" : {
""value"" : 0,
""relation"" : ""eq""
},
""max_score"" : null,
""hits"" : [ ]
},
""aggregations"" : {
""summary_boxplot"" : {
""min"" : ""Infinity"",
""max"" : ""-Infinity"",
""q1"" : ""NaN"",
""q2"" : ""NaN"",
""q3"" : ""NaN"",
""lower"" : ""NaN"",
""upper"" : ""-Infinity""
}
}
}");

[U]
public void BoxplotHandlesNaNValues()
{
var pool = new SingleNodeConnectionPool(new Uri($"http://localhost:9200"));
var settings = new ConnectionSettings(pool, new InMemoryConnection(ResponseBytes));
var client = new ElasticClient(settings);

var response = client.Search<TestData>(s => s
.Size(0)
.Index("test")
.Aggregations(a => a
.Boxplot("summary_boxplot", mt => mt.Field(f => f.Population))));

var boxplot = response.Aggregations.Boxplot("summary_boxplot");

double.IsNaN(boxplot.Lower).Should().BeTrue();
double.IsNaN(boxplot.Q1).Should().BeTrue();
double.IsNaN(boxplot.Q2).Should().BeTrue();
double.IsNaN(boxplot.Q3).Should().BeTrue();
double.IsInfinity(boxplot.Min).Should().BeTrue();
double.IsNegativeInfinity(boxplot.Max).Should().BeTrue();
double.IsNegativeInfinity(boxplot.Upper).Should().BeTrue();
}

private class TestData
{
public long Population { get; set; }
}
}
}