HyukjinKwon opened a new pull request, #437: URL: https://github.com/apache/spark-connect-rust/pull/437
### What changes were proposed in this pull request? [SPARK-61316](https://issues.apache.org/jira/browse/SPARK-61316): Encode datetime literals exactly, as PySpark's TimestampType does. The drop-in converted a Python `datetime` to microseconds (for `lit()` and for the values of `createDataFrame`) with `datetime.timestamp() * 1e6`, rounded. The float of seconds loses the microseconds once the value is past 2^53 microseconds (about the year 2255), and a naive datetime near the year 9999 overflows in `timestamp()`. `datetime_to_micros` now computes the value in integers, as `TimestampType.toInternal` does in PySpark: - an aware datetime: `calendar.timegm(dt.utctimetuple()) * 1_000_000 + dt.microsecond`; - a naive datetime, in the local time zone of the client: `time.mktime(dt.timetuple())` truncated to whole seconds, then the same sum. Both callers (`functions.lit` and `session::py_to_value`) use it, so `lit()` and `createDataFrame` keep agreeing. The pandas `Timestamp` normalization is unchanged. The values are the same as official PySpark 4.3.0 for years 1969, 2300 and 9999, aware and naive. A naive datetime of year 1 raises `OverflowError` from `time.mktime` in official PySpark on macOS (the limit of the platform C library); the drop-in uses the same call, so it behaves the same there. ### Why are the changes needed? `F.lit(datetime(2300, 1, 1, 0, 0, 0, 123457, tzinfo=timezone.utc))` collected as `.123456`, and `F.lit(datetime(9999, 12, 31, 23, 59, 59, 999999))` raised `OverflowError: date value out of range`. Official PySpark gives `.123457` and `9999-12-31 23:59:59.999999` for both. ### Does this PR introduce _any_ user-facing change? Yes. Datetime literals and the values of `createDataFrame` past 2255 keep their microseconds, and the largest datetimes no longer raise `OverflowError`. Values before 2255 do not change. ### How was this patch tested? - `test_datetime_literals_are_exact_micros_as_pyspark` (offline, `test_dropin_offline.py`, before `test_lit_errors_as_pyspark`): the `Literal.timestamp` micros in the plan for years 1, 1969, 2300 and 9999 (aware), and for naive 1969, 2300 and 9999 in a zone nine hours east of UTC. The expected values are those of official PySpark 4.3.0 (`TimestampType().toInternal`). - `test_datetime_literals_collect_the_exact_values_of_pyspark` (live, `test_dropin_e2e.py`, before `test_window_frames_return_the_rows_of_pyspark`, client time zone Asia/Seoul): the collected values of the same literals equal those of official PySpark 4.3.0 in the same zone, including `OverflowError` on collect of the aware 9999 value (which is past the year 9999 in that zone). - Both tests fail on the base conversion (the float path) and pass with this change. - The build of all tests, the offline Python suites (2928 passed, 8 skipped) with the rebuilt extension, the new tests also under pytest 9.2, and the live drop-in tests `-k "timestamp or ntz or lit"` (11 passed) against a local Spark 4.3.0 server. Clippy reports no new diagnostics in the changed lines. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
