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

Fix filtering on LocalDates #40530

Merged
merged 7 commits into from Mar 26, 2024
Merged

Fix filtering on LocalDates #40530

merged 7 commits into from Mar 26, 2024

Conversation

camsaul
Copy link
Member

@camsaul camsaul commented Mar 22, 2024

Stop doing dumb things with local dates that give you unexpected results.

  1. Don't parse a local date literal like 2024-03-20 to an OffsetDateTime or ZonedDateTime. Previously, we'd try to parse it to something like #t "2024-03-20T00:00:00-07:00[US/Pacific]" using the QP results timezone (either report-timezone if the DB supports session timezones, or the database timezone if it does not). Predictably, this caused bugs. Just parse it to a LocalDate instead.

  2. Don't try to optimize a LocalDate = LocalDate or day(LocalDate) = day(LocalDate) filters to :between filters; we can leave them as =. E.g. instead of generating

    WHERE date_column >= timestamp '2024-03-20T00:00:00-07:00` 
      AND date_column < timestamp  '2024-03-21T00:00:00-07:00'

    we should just generate

    WHERE date_column = date '2024-03-20'

    This causes way less problems and is more performant, and is nicer to look at.

Fixes #39769
Fixes #40332

This was split off from #40416, which is a more significant overhaul that fixes more things, but too big to backport.

@camsaul camsaul added the backport Automatically create PR on current release branch on merge label Mar 22, 2024
@metabase-bot metabase-bot bot added the .Team/QueryProcessor :hammer_and_wrench: label Mar 22, 2024
@camsaul camsaul requested a review from a team March 22, 2024 19:36
Copy link

replay-io bot commented Mar 22, 2024

Status Complete ↗︎
Commit af5811d
Results
⚠️ 3 Flaky
2378 Passed

@camsaul camsaul enabled auto-merge (squash) March 26, 2024 17:01
@camsaul camsaul added backport Automatically create PR on current release branch on merge and removed backport Automatically create PR on current release branch on merge labels Mar 26, 2024
@lbrdnk lbrdnk self-requested a review March 26, 2024 19:35
(let [t (parse-temporal-string-literal-to-class s OffsetDateTime)]
[:absolute-datetime t target-unit]))

(defmethod parse-temporal-string-literal :type/DateTimeWithZoneID
Copy link
Contributor

Choose a reason for hiding this comment

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

wrap-temporal-string-literal may be a better name.

;; with it.
t))

(defmethod coerce-temporal [LocalDate LocalTime] [_t _target-class] (throw-invalid-time))
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel that a few comment blocks explaining some of the things to consider would be helpful.

  • All Local to Offset/Zoned conversions must consider results-timezone-id
  • All Offset/Zoned to Local conversions do not need to consider results-timezone-id because session timezone will already be applied (is that correct?)
  • Others?

(defmethod coerce-temporal [LocalTime ZonedDateTime] [_t _target-class] (throw-invalid-datetime))
(defmethod coerce-temporal [OffsetTime ZonedDateTime] [_t _target-class] (throw-invalid-datetime))
(defmethod coerce-temporal [LocalDateTime ZonedDateTime] [t _target-class] (t/zoned-date-time t (t/zone-id (qp.timezone/results-timezone-id))))
(defmethod coerce-temporal [OffsetDateTime ZonedDateTime] [t _target-class] t) ; OffsetDateTime is perfectly fine.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure it is perfectly fine, I believe adding/comparing time to an offsetdatetime across a daylight savings shift could give an incorrect result. I'm not sure when we would actually get into this conversion though to test this scenario.


 snoe> snoe=# set timezone = "America/Edmonton";
SET
 snoe> snoe=# select ('2024-03-09T23:59'::timestamp) at time zone 'America/Chicago' + '3 hour'::interval;
        ?column?
------------------------
 2024-03-10 01:59:00-07
(1 row)

 snoe> snoe=# select ('2024-03-09T23:59'::timestamp) at time zone 'America/Chicago' + '4 hour'::interval;
        ?column?
------------------------
 2024-03-10 03:59:00-06
(1 row)

 snoe> snoe=# set timezone = "MST";
SET
 snoe> snoe=# select ('2024-03-09T23:59'::timestamp) at time zone 'America/Chicago' + '3 hour'::interval;
        ?column?
------------------------
 2024-03-10 01:59:00-07
(1 row)

 snoe> snoe=# select ('2024-03-09T23:59'::timestamp) at time zone 'America/Chicago' + '4 hour'::interval;
        ?column?
------------------------
 2024-03-10 02:59:00-07
(1 row)

@camsaul camsaul merged commit 4543a7f into master Mar 26, 2024
129 of 132 checks passed
@camsaul camsaul deleted the fix-39769-small-edition branch March 26, 2024 19:57
Copy link

@camsaul Did you forget to add a milestone to the issue for this PR? When and where should I add a milestone?

camsaul added a commit that referenced this pull request Mar 27, 2024
* Split from #40146: only the filter changes

* Don't include format-rows changes here.

* Test fixes 🔧

* Test fix 🔧

* Misc test fixes 🔧

* Test fix 🔧

* Tweak SQL Server fix
camsaul added a commit that referenced this pull request Mar 27, 2024
* Add backport resolution script

* Fix filtering on LocalDates (#40530)

* Split from #40146: only the filter changes

* Don't include format-rows changes here.

* Test fixes 🔧

* Test fix 🔧

* Misc test fixes 🔧

* Test fix 🔧

* Tweak SQL Server fix

* Fix Kondo errors

* Updated api/dataset tests

* Test fix 🔧

---------

Co-authored-by: Metabase bot <metabase-bot@metabase.com>
Co-authored-by: Cam Saul <1455846+camsaul@users.noreply.github.com>
Co-authored-by: Cam Saul <github@camsaul.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport Automatically create PR on current release branch on merge .Team/QueryProcessor :hammer_and_wrench:
Projects
None yet
2 participants