Skip to content

Commit

Permalink
Validate stats formatting in standard InternalStats constructor (#107678
Browse files Browse the repository at this point in the history
)

We want to validate stats formatting before we serialize to XContent, as chunked x-content serialization
 assumes that we don't throw exceptions at that point. It is not necessary to do it in the StreamInput constructor 
as this one has been serialise from an already checked object.

This commit adds starts formatting validation to the standard InternalStats constructor.
  • Loading branch information
iverase authored Apr 24, 2024
1 parent d34f0b1 commit 04bf642
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/107678.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 107678
summary: Validate stats formatting in standard `InternalStats` constructor
area: Aggregations
type: bug
issues:
- 107671
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
setup:
- do:
indices.create:
index: test_date
body:
mappings:
properties:
date_field:
type : date
format: date_hour_minute_second_millis


- do:
bulk:
refresh: true
body:
- index:
_index: test_date
_id: "1"
- date_field: 9999-01-01T00:00:00.000
- index:
_index: test_date
_id: "2"
- date_field: 9999-01-01T00:00:00.000

---
"fail formatting":

- skip:
version: "- 8.14.99"
reason: fixed in 8.15.0
- do:
catch: /Cannot format stat \[sum\] with format \[DocValueFormat.DateTime\(format\[date_hour_minute_second_millis\] locale\[\], Z, MILLISECONDS\)\]/
search:
index: test_date
body:
size: 0
aggs:
the_stats:
stats:
field: date_field

Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ public InternalStats(
this.sum = sum;
this.min = min;
this.max = max;
verifyFormattingStats();
}

private void verifyFormattingStats() {
if (format != DocValueFormat.RAW) {
verifyFormattingStat(Fields.MIN, format, min);
verifyFormattingStat(Fields.MAX, format, max);
verifyFormattingStat(Fields.AVG, format, getAvg());
verifyFormattingStat(Fields.SUM, format, sum);
}
}

private static void verifyFormattingStat(String stat, DocValueFormat format, double value) {
try {
format.format(value);
} catch (Exception e) {
throw new IllegalArgumentException("Cannot format stat [" + stat + "] with format [" + format.toString() + "]", e);
}
}

/**
Expand Down

0 comments on commit 04bf642

Please sign in to comment.