Skip to content
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

Eschew leniency when parsing time zones #77267

Merged
merged 8 commits into from
Sep 15, 2021

Conversation

not-napoleon
Copy link
Member

Resolves #76415

In #73955 we made DocValueFormat date formatters always timezone aware. This solved the problem we had with parsing epoch dates, but created a new problem when parsing dates that specified an offset. This solves that problem, in what I hope is the right way. Previously, we used TemporalQueries#zone() which gets the specified zone if available, and falls back to the offset if not. I think we want the opposite behavior though - if we parsed an offset, use that, otherwise use the zone.

@not-napoleon not-napoleon added >bug :Core/Infra/Core Core issues without another label v8.0.0 v7.16.0 labels Sep 3, 2021
@elasticmachine elasticmachine added the Team:Core/Infra Meta label for core/infra team label Sep 3, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@@ -0,0 +1,55 @@
setup:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be in the same file as the rest of the range agg tests, but I'm refactoring that file in a different branch, and wanted to save myself a merge conflict.

@@ -210,7 +210,9 @@ private Instant parseDateTime(String value, ZoneId timeZone, boolean roundUpIfNo
return DateFormatters.from(formatter.parse(value)).toInstant();
} else {
TemporalAccessor accessor = formatter.parse(value);
ZoneId zoneId = TemporalQueries.zone().queryFrom(accessor);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs describe the zone() query as "A lenient query for the ZoneId, falling back to the ZoneOffset", and how do we feel about leniency?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while this would fix the use case where a mapping expects an offset but the problem will re-highlight itself whe na mapping is using zoneId?

 public void testParseOffset() {
        // Format string from #76415
        DocValueFormat.DateTime parsesZone = new DocValueFormat.DateTime(
            DateFormatter.forPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSSVV"),
            ZoneOffset.UTC,
            Resolution.MILLISECONDS
        );
        long expected = 1628719200000L;
        ZonedDateTime sample = ZonedDateTime.of(2021, 8, 12, 0, 0, 0, 0, ZoneId.ofOffset("", ZoneOffset.ofHours(2)));
        assertEquals("GUARD: wrong initial millis", expected, sample.toEpochSecond() * 1000);
        //assertEquals("GUARD: wrong initial string", "2021-08-12T00:00:00.000000000+02:00", parsesZone.format(expected));
        long actualMillis = parsesZone.parseLong(
            "2021-08-12T00:00:00.000000000CET",
            false,
            () -> { throw new UnsupportedOperationException("don't use now"); }
        );
        assertEquals(expected, actualMillis);
    }

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I just tested this and it would work..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good test though, we should add it to the suite. Do you mind if I just copy it in?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind it at all :) it is a copy of your test with just patterns changed :D
problem is that it is prone to DST changes. ZoneId.ofOffset("", ZoneOffset.ofHours(2))); and 2021-08-12T00:00:00.000000000+02:00 will have to account for this

@elasticmachine elasticmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Sep 3, 2021
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-analytics-geo (Team:Analytics)

@not-napoleon
Copy link
Member Author

@elasticmachine run elasticsearch-ci/part-1

Copy link
Contributor

@pgomulka pgomulka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried that we introduced a bug in #73955 and we should not set the timezone on parser? but this would require special handling of epoch_seconds as you commented in that PR
I think the fix we have here is best for now

}
- match: { hits.total: 1 }
- length: { aggregations.myagg.buckets: 1 }
- match: { aggregations.myagg.buckets.0.from: 1628719200000 }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could add human readable date in a comment to help with readability?

@not-napoleon
Copy link
Member Author

@elasticmachine update branch

@not-napoleon not-napoleon merged commit 6f47fa3 into elastic:master Sep 15, 2021
@not-napoleon not-napoleon deleted the 76415-parsing-time-zones branch September 15, 2021 17:08
not-napoleon added a commit to not-napoleon/elasticsearch that referenced this pull request Sep 15, 2021
not-napoleon added a commit that referenced this pull request Sep 15, 2021
* Eschew leniency when parsing time zones (#77267)

* backport dance
@jakelandis jakelandis removed the v8.0.0 label Oct 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/Aggregations Aggregations >bug :Core/Infra/Core Core issues without another label Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) Team:Core/Infra Meta label for core/infra team v7.16.0 v8.0.0-beta1
Projects
None yet
4 participants