-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Epoch millis and second formats accept float implicitly #26119
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
Changes from all commits
5d3f8e5
daac500
5d8229f
8c17da4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,6 +260,10 @@ public void testThatEpochsCanBeParsed() { | |
| } else { | ||
| assertThat(dateTime.getMillisOfSecond(), is(0)); | ||
| } | ||
|
|
||
| // test floats get truncated | ||
| String epochFloatValue = String.format(Locale.US, "%d.%d", dateTime.getMillis() / (parseMilliSeconds ? 1L : 1000L), randomNonNegativeLong()); | ||
| assertThat(formatter.parser().parseDateTime(epochFloatValue).getMillis(), is(dateTime.getMillis())); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we also should support european decimal separators like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should accept numbers as defined by javascript/JSON schema, and not formatted string representation of numbers. The issue is that we didn't accept the numbers from javascript which does not have an integer datatype. Accepting string representation of valid javascript numbers is the bonus part since all dates are parsed as strings anyway.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
| } | ||
|
|
||
| public void testThatNegativeEpochsCanBeParsed() { | ||
|
|
@@ -281,16 +285,26 @@ public void testThatNegativeEpochsCanBeParsed() { | |
| assertThat(dateTime.getSecondOfMinute(), is(20)); | ||
| } | ||
|
|
||
| // test floats get truncated | ||
| String epochFloatValue = String.format(Locale.US, "%d.%d", dateTime.getMillis() / (parseMilliSeconds ? 1L : 1000L), randomNonNegativeLong()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be a negative long in this test?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is negative since
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I get it now, thanks. |
||
| assertThat(formatter.parser().parseDateTime(epochFloatValue).getMillis(), is(dateTime.getMillis())); | ||
|
|
||
| // every negative epoch must be parsed, no matter if exact the size or bigger | ||
| if (parseMilliSeconds) { | ||
| formatter.parser().parseDateTime("-100000000"); | ||
| formatter.parser().parseDateTime("-999999999999"); | ||
| formatter.parser().parseDateTime("-1234567890123"); | ||
| formatter.parser().parseDateTime("-1234567890123456789"); | ||
|
|
||
| formatter.parser().parseDateTime("-1234567890123.9999"); | ||
| formatter.parser().parseDateTime("-1234567890123456789.9999"); | ||
| } else { | ||
| formatter.parser().parseDateTime("-100000000"); | ||
| formatter.parser().parseDateTime("-1234567890"); | ||
| formatter.parser().parseDateTime("-1234567890123456"); | ||
|
|
||
| formatter.parser().parseDateTime("-1234567890.9999"); | ||
| formatter.parser().parseDateTime("-1234567890123456.9999"); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1014,17 +1014,20 @@ public void testRangeWithFormatNumericValue() throws Exception { | |
| assertBucket(buckets.get(0), 2L, "1000-3000", 1000000L, 3000000L); | ||
| assertBucket(buckets.get(1), 1L, "3000-4000", 3000000L, 4000000L); | ||
|
|
||
| // however, e-notation should and fractional parts provided as string | ||
| // should be parsed and error if not compatible | ||
| Exception e = expectThrows(Exception.class, () -> client().prepareSearch(indexName).setSize(0) | ||
| .addAggregation(dateRange("date_range").field("date").addRange("1.0e3", "3.0e3").addRange("3.0e3", "4.0e3")).get()); | ||
| assertThat(e.getCause(), instanceOf(ElasticsearchParseException.class)); | ||
| assertEquals("failed to parse date field [1.0e3] with format [epoch_second]", e.getCause().getMessage()); | ||
|
|
||
| e = expectThrows(Exception.class, () -> client().prepareSearch(indexName).setSize(0) | ||
| .addAggregation(dateRange("date_range").field("date").addRange("1000.123", "3000.8").addRange("3000.8", "4000.3")).get()); | ||
| assertThat(e.getCause(), instanceOf(ElasticsearchParseException.class)); | ||
| assertEquals("failed to parse date field [1000.123] with format [epoch_second]", e.getCause().getMessage()); | ||
| // also e-notation and floats provided as string also be truncated (see: #14641) | ||
| searchResponse = client().prepareSearch(indexName).setSize(0) | ||
| .addAggregation(dateRange("date_range").field("date").addRange("1.0e3", "3.0e3").addRange("3.0e3", "4.0e3")).get(); | ||
| assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L)); | ||
| buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2); | ||
| assertBucket(buckets.get(0), 2L, "1000-3000", 1000000L, 3000000L); | ||
| assertBucket(buckets.get(1), 1L, "3000-4000", 3000000L, 4000000L); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great this works |
||
|
|
||
| searchResponse = client().prepareSearch(indexName).setSize(0) | ||
| .addAggregation(dateRange("date_range").field("date").addRange("1000.123", "3000.8").addRange("3000.8", "4000.3")).get(); | ||
| assertThat(searchResponse.getHits().getTotalHits(), equalTo(3L)); | ||
| buckets = checkBuckets(searchResponse.getAggregations().get("date_range"), "date_range", 2); | ||
| assertBucket(buckets.get(0), 2L, "1000-3000", 1000000L, 3000000L); | ||
| assertBucket(buckets.get(1), 1L, "3000-4000", 3000000L, 4000000L); | ||
|
|
||
| // using different format should work when to/from is compatible with | ||
| // format in aggregation | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, so this can handle all kinds of formats it seems.