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

Druid filters in configured timezone #3420

Closed

Conversation

moumny
Copy link

@moumny moumny commented Sep 29, 2016

Related to #3386 .
Filters in druid (intervals) are now made in the report timezone configured instead of UTC.

@salsakran
Copy link
Contributor

Awesome.

Could you add some tests that show the bug so we can make sure there are no regressions in the future?

(date-trunc unit (System/currentTimeMillis)))
(^java.sql.Timestamp [unit date]
(let [trunc-with-format (fn trunc-with-format
(^java.sql.Timestamp [unit date timezone-id]
Copy link
Member

Choose a reason for hiding this comment

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

This is definitely going to break things, there's lots of places in the code where we user this function and they're not passing the extra timezone arg

@camsaul
Copy link
Member

camsaul commented Sep 29, 2016

@moumny, the changes you made in metabase.util will definitely break some of our other drivers. date-trunc is used all over the place. Changing the arity of functions like that will break all of the other code that calls those functions.

@camsaul
Copy link
Member

camsaul commented Sep 29, 2016

The current iteration of date-trunc is a 1 or 2 arity function, so when you change it to 2 or 3 all the places that were calling the 1-arity version will throw an ArityException and all the places that call the 2-arity version will be passing the wrong arguments.

If you need to pass a third argument then a better solution would be to add a third arity of the function

@moumny
Copy link
Author

moumny commented Oct 1, 2016

Sorry should have put WIP in the title.
The functions are only used in druid and mongo driver.
I decided to update mongo driver to make it explicit that the timezone is UTC.

We should probably add some test in timeseries testsuite that take into account the report timezone and test the timestamp filters. Would need bit of help for that ^^. (especially that mongo for example filters still in UTC i guess)

Still todo: verifiy that i didnt break mongo driver

@moumny moumny changed the title Druid filters in configured timezone [WIP] Druid filters in configured timezone Oct 1, 2016
@moumny
Copy link
Author

moumny commented Oct 4, 2016

✅ mongodb tests are passing :)

@moumny moumny changed the title [WIP] Druid filters in configured timezone Druid filters in configured timezone Oct 4, 2016
@camsaul
Copy link
Member

camsaul commented Oct 4, 2016

@moumny this is looking good but as I mentioned above

If you need to pass a third argument then a better solution would be to add a third arity of the function

i.e. if "UTC" is basically the default timezone-id argument for things like relative-date and date-trunc I'd rather keep the existing arities and have those pass "UTC" as a default arg, and then add a new arity that takes timezone-id. For example, I'd prefer date-extract look like this after your changes:

(defn date-extract
  ([unit]
   (date-extract unit (System/currentTimeMillis)))
  ([unit date]
   (date-extract unit date "UTC"))
  ([unit date timezone-id])
   ...))

That way the Mongo driver wouldn't have to be changed at all. It looks like you did this with
->Calendar already

@@ -229,6 +231,7 @@
:year (extract:timeFormat "yyyy")))

(defn- unit->granularity [unit]
(let [timeZone (get-timezone-id)]
Copy link
Member

Choose a reason for hiding this comment

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

is this being used?

trunc-with-floor (fn [date amount-ms]
(let [orig-ms (.getTime (->Timestamp date))
trunced-ms (-> orig-ms (/ amount-ms) (math/floor) (* amount-ms))]
(new java.sql.Timestamp trunced-ms)))
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Actually you should probably just do (->Timestamp trunced-ms)

(expect #inst "2005-12-25-08:00" (date-trunc :week saturday-the-31st "US/Pacific"))
(expect #inst "2005-12-25-08:00" (date-trunc :week sunday-the-1st "US/Pacific"))
(expect #inst "2005-12-01-08:00" (date-trunc :month saturday-the-31st "US/Pacific"))
(expect #inst "2005-10-01-08:00" (date-trunc :quarter saturday-the-31st "US/Pacific"))
Copy link
Member

Choose a reason for hiding this comment

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

👍 on the extra tests

@moumny
Copy link
Author

moumny commented Oct 4, 2016

Thanks for your comment @camsaul !
I changed the arity to be consistent with the rest. The idea was to avoid defaults to UTC to avoid mistakes. Its convenient to call one of the first UTC methods in druid driver and its harder to spot than (date-op date "UTC") :)

Anyway let me know if you see something else. 👁

@camsaul
Copy link
Member

camsaul commented Oct 4, 2016

@moumny, this looks good 👍 . I'll run it on CI in a bit and if all the tests pass then we can merge this

Thanks for your contribution 😍

@camsaul
Copy link
Member

camsaul commented Oct 5, 2016

@moumny I had to make a couple last-minute tweaks to this so we can ship it as part of our 0.20 release. I opened a new PR for it, #3459, so I'm going to go ahead and close this one out.

I'll merge your fixes in as soon as the tests pass on CI. Thanks again for your contribution! 🎉

@camsaul camsaul closed this Oct 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants