Raviston6296 opened a new pull request, #60275:
URL: https://github.com/apache/spark/pull/60275

   ### What changes were proposed in this pull request?
   
   `ArrowToPandasConversion._convert_array_legacy` converts an Arrow column to 
pandas with `to_pandas(coerce_temporal_nanoseconds=True)`. For a `duration[us]` 
column, which is how a `DayTimeIntervalType` value reaches the Python worker, 
pyarrow converts to nanoseconds without checking for overflow. This PR first 
casts such a column to `duration[ns]` with `pyarrow.compute.cast`, which does 
check, so a value outside the range of `timedelta64[ns]` raises an error 
instead of wrapping around. The cast drops the array's name, so the Series is 
renamed back to it.
   
   `DayTimeIntervalType` isn't one of the types that `_prefer_convert_numpy` 
sends to the numpy path, so every pandas UDF type and `mapInPandas` take this 
legacy path for an interval column, whether it's a top-level column or a field 
of the struct that `df_for_struct` splits into columns.
   
   ### Why are the changes needed?
   
   `INTERVAL DAY TO SECOND` holds up to +-106751991 days (int64 microseconds), 
but `timedelta64[ns]` only reaches +-106751 days. Today, a larger value passed 
to pandas silently becomes a different value:
   
   ```python
   import pandas as pd
   from pyspark.sql.functions import pandas_udf
   
   @pandas_udf("long")
   def days(s: pd.Series) -> pd.Series:
       return s.dt.days
   
   spark.sql("SELECT make_dt_interval(d) AS iv FROM VALUES (106752), (200000), 
(-200000) AS t(d)") \
       .select(days("iv")).show()
   # -106752, -13504, 13503 (expected 106752, 200000, -200000)
   ```
   
   pyarrow checks the same conversion for timestamps: a `TIMESTAMP` outside the 
range of `datetime64[ns]` already raises `ArrowInvalid: Casting from 
timestamp[us] to timestamp[ns] would result in out of bounds timestamp` on this 
path. `pyarrow.compute.cast` gives durations the same check:
   
   ```python
   import pyarrow as pa, pyarrow.compute as pc
   from datetime import timedelta
   arr = pa.array([timedelta(days=200000)], pa.duration("us"))
   arr.to_pandas(coerce_temporal_nanoseconds=True)  # Timedelta('-13504 days 
+00:25:26.290448384')
   pc.cast(arr, pa.duration("ns"))  # ArrowInvalid: ... would result in out of 
bounds timestamp
   ```
   
   `toPandas()` with Arrow uses the same flag in 
`pyspark/sql/pandas/conversion.py`. That line is being changed in #59740 
(SPARK-60499), so this PR leaves it as it is.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. A pandas UDF or `mapInPandas` whose input has an `INTERVAL DAY TO 
SECOND` value beyond +-106751 days now fails with `ArrowInvalid: Casting from 
duration[us] to duration[ns] would result in out of bounds timestamp: ...`. It 
used to receive a wrong value with no error. Values within the range are 
converted exactly as before.
   
   ### How was this patch tested?
   
   - New 
`ArrowToPandasConversionTests.test_day_time_interval_outside_timedelta64_ns_range`
 in `test_conversion.py`: +-106751 days and null convert as before (for an 
`Array` and a `ChunkedArray`), the column name is kept, and +-106752 and 
+-200000 days raise. It fails without the fix ("ArrowInvalid not raised").
   - New `test_pandas_udf_day_time_interval_outside_timedelta64_ns_range` in 
`test_pandas_udf.py`, for a scalar pandas UDF and `mapInPandas`, with the 
boundary values and the values from the JIRA. It fails without the fix 
("Exception not raised"). It is in `PandasUDFTestsMixin`, so it also runs in 
the Spark Connect parity suite, `PandasUDFParityTests`; that one was skipped 
locally for a missing `grpc_status` module and is left to CI.
   - `python/run-tests --testnames 
'pyspark.sql.tests.test_conversion,pyspark.sql.tests.pandas.test_pandas_udf'` 
passes locally (Python 3.12, pandas 2.2.3, pyarrow 18.1.0, JDK 21).
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
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]

Reply via email to