Skip to content

Validate stats formatting in standard InternalStats constructor #107678

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 6 commits into from
Apr 24, 2024
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
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