Use Decimal formatter for Numeric ValuesSourceTypes (#54366) (#55470)

Co-authored-by: Igor Motov <igor@motovs.org>
This commit is contained in:
Mark Tozzi 2020-04-20 17:05:38 -04:00 committed by GitHub
parent cabff65aec
commit ff55d761f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -75,6 +75,22 @@ public enum CoreValuesSourceType implements ValuesSourceType {
Number missing = docValueFormat.parseDouble(rawMissing.toString(), false, now);
return MissingValues.replaceMissing((ValuesSource.Numeric) valuesSource, missing);
}
@Override
public DocValueFormat getFormatter(String format, ZoneId tz) {
/* TODO: this silently ignores a timezone argument, whereas NumberFieldType#docValueFormat throws if given a time zone.
Before we can solve this, we need to resolve https://github.com/elastic/elasticsearch/issues/47469 which deals
with the fact that the same formatter is used for input and output values. We want to support a use case in SQL
(and elsewhere) that allows for passing a long value milliseconds since epoch into date aggregations. In that case,
the timezone is sensible as part of the bucket key format.
*/
if (format == null) {
return DocValueFormat.RAW;
} else {
return new DocValueFormat.Decimal(format);
}
}
},
BYTES() {
@Override

View File

@ -354,6 +354,26 @@ public class WeightedAvgAggregatorTests extends AggregatorTestCase {
"Use a script to combine multiple weights-per-doc into a single value."));
}
public void testFormatter() throws IOException {
MultiValuesSourceFieldConfig valueConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("value_field").build();
MultiValuesSourceFieldConfig weightConfig = new MultiValuesSourceFieldConfig.Builder().setFieldName("weight_field").build();
WeightedAvgAggregationBuilder aggregationBuilder = new WeightedAvgAggregationBuilder("_name")
.value(valueConfig)
.weight(weightConfig)
.format("0.00%");
testCase(new MatchAllDocsQuery(), aggregationBuilder, iw -> {
iw.addDocument(Arrays.asList(new SortedNumericDocValuesField("value_field", 7),
new SortedNumericDocValuesField("weight_field", 1)));
iw.addDocument(Arrays.asList(new SortedNumericDocValuesField("value_field", 2),
new SortedNumericDocValuesField("weight_field", 1)));
iw.addDocument(Arrays.asList(new SortedNumericDocValuesField("value_field", 3),
new SortedNumericDocValuesField("weight_field", 1)));
}, avg -> {
assertEquals(4, avg.getValue(), 0);
assertTrue(AggregationInspectionHelper.hasValue(avg));
assertEquals("400.00%", avg.getValueAsString());
});
}
public void testSummationAccuracy() throws IOException {
// Summing up a normal array and expect an accurate value