Repository navigation
[SIP-15] Fixing datetime conversion and SQL literal - #8464
Conversation
There was a problem hiding this comment.
@betodealmeida do you know why the TIMESTAMP type wasn't defined for BigQuery?
There was a problem hiding this comment.
@dpgaspar I know you recently added this and I was hoping you could validate the logic.
b65aa7a to
3122919
Compare
There was a problem hiding this comment.
We shouldn't be casting other types to TIMESTAMP. The reason for this function is to ensure that the LHS and RHS for the time filter comparison are equivalent types.
There was a problem hiding this comment.
This is now handled in dttm_sql_literal as we need to determine if the python-date-format logic needs to be invoked.
There was a problem hiding this comment.
We shouldn't be casting all types to a DATETIME. The reason for this function is to ensure that the LHS and RHS for the time filter comparison are equivalent types.
There was a problem hiding this comment.
Note there was no previous logic for handling the DATE type.
827ab4c to
4f1543a
Compare
villebro
left a comment
There was a problem hiding this comment.
A very quick first pass, will look in closer detail later.
3ea4968 to
fa2d521
Compare
Codecov Report
@@ Coverage Diff @@
## master #8464 +/- ##
==========================================
- Coverage 66.57% 66.52% -0.05%
==========================================
Files 449 449
Lines 22567 22595 +28
Branches 2367 2367
==========================================
+ Hits 15023 15032 +9
- Misses 7406 7425 +19
Partials 138 138
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
@cgivre can you check if these are valid and if there's anything missing? A quick googling didn't turn up any DATETIME type for Drill, just DATE, TIME and TIMESTAMP.
There was a problem hiding this comment.
There was a problem hiding this comment.
SQLite also supports time stored in REAL and INTEGER columns according to the docs:
TEXT as ISO8601 strings ("YYYY-MM-DD HH:MM:SS.SSS").
REAL as Julian day numbers, the number of days since noon in Greenwich on November 24, 4714 B.C. according to the proleptic Gregorian calendar.
INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Should we support the REAL and INTEGER conversions here?
There was a problem hiding this comment.
@willbarrett epoch is handled by specifying epoch_s in the python-date-format.
Also a REAL or INTEGER column could encode temporal information in a different form, i.e., one could be using REAL to store a UNIX timestamp with floating point precision though and thus we can’t blindly handle these types.
Note per the referenced PoC PR the future end goal is to provide an engine specific graph which maps between various SQLAlchemy and datetime types. Much of the time grain bucketing is potentially wrong for string columns.
fa2d521 to
52ae511
Compare
|
@villebro @willbarrett thanks for the feedback. I've hopefully addressed all your comments (either via code changes or replies to your comments). |
villebro
left a comment
There was a problem hiding this comment.
LGTM, and big thanks for the big effort with the unit tests. I expect something will break as a consequence of this big change, but these are easy to fix later on, so I don't see any reason not to merge this.
|
Thanks for the review @villebro. I agree there could be a regression, but i) there were clearly some ill-defined mappings for certain dialects, and ii) with the addition of unit tests after any fixes future regressions should be preventable. |
|
Absolutely @john-bodley ; even in the light of possible regressions I expect this to fix more bugs than it introduces. I propose merging this asap. |
| def convert_dttm(cls, target_type: str, dttm: datetime) -> Optional[str]: | ||
| if target_type.upper() == "DATETIME": | ||
| return f"""CAST('{dttm.isoformat(timespec="seconds")}' AS DATETIME)""" | ||
| return None |
There was a problem hiding this comment.
@john-bodley thanks for pointing out to this, the logic looks good and I've retested it and found no problems
CATEGORY
Choose one
SUMMARY
This PR updates a number of aspects related to the datetime SQL literal logic which is used for the RHS of the time filter comparisons. Specifically:
DbEngineSpec.convert_dttmoptionally returnNoneif a type conversion does not exist.dttm_sql_literalto first try to convert adatetimeobject to a SQL expression (1) prior to converting either a string on numerical type based on the python-date-format. The reason for this change is we should handle known types before falling back to using the python-date-format, as in theory these could be defined incorrectly for non-string/float types.convert_dttmlogic for a number of database engines. There were a handful of examples where this was simply returning either i) a string representation of thedatetimeobject rather than the necessary SQL expression for casting from adatetimeobject to the native temporal type, or ii) incorrectdatetimeformatting or casting logic. Note I'm not familiar with a number of these database engines and thus the updated conversions were the result of Googling. This logic should probably be validated.convert_dttmfunction.str.format(...).dttm.isoformat()[:10]withdttm.date().isoformat()in improve readability.Regarding timestamp precision some databases only seem to support seconds whereas others support microseconds. Where possible the
datetimeobject is formatted to the highest level precision available.Note for context I sense this PoC is probably the end state for correctly handling the necessary casting of types (#7682).
BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
TEST PLAN
CI and additional unit tests.
ADDITIONAL INFORMATION
REVIEWERS
to: @betodealmeida @dpgaspar @etr2460 @michellethomas @mistercrunch @villebro