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

   ### What changes were proposed in this pull request?
   
   The Python worker finds the partition boundaries of a UDTF `TABLE` argument 
with `PARTITION BY` by comparing the partition key of each row with the 
previous one using Python `!=`. This PR adds a helper `_partition_values_equal` 
that treats NaN as equal to NaN, also when it is nested inside an array or a 
struct, and uses it in all three places that compare partition keys:
   
   - `UDTFWithPartitions._check_partition_boundaries` (Python UDTFs, including 
the Arrow-optimized ones)
   - `ArrowUDTFWithPartition._detect_partition_boundaries` (`@arrow_udtf`, rows 
inside one batch)
   - `ArrowUDTFWithPartition._process_arrow_batch_by_partitions` 
(`@arrow_udtf`, the first partition of a batch compared with the last partition 
of the previous batch)
   
   ### Why are the changes needed?
   
   In Python, `float("nan") != float("nan")` is `True`, so every row with a NaN 
partition key starts a new partition, and `terminate()` is called once per row:
   
   ```python
   from pyspark.sql.functions import udtf
   
   @udtf(returnType="n: int")
   class CountRows:
       def __init__(self):
           self.n = 0
       def eval(self, row):
           self.n += 1
       def terminate(self):
           yield (self.n,)
   
   spark.udtf.register("count_rows", CountRows)
   df = spark.createDataFrame([(float("nan"), 1), (float("nan"), 2), (1.0, 3), 
(1.0, 4)], "k double, v int")
   df.createOrReplaceTempView("t")
   spark.sql("SELECT * FROM count_rows(TABLE(t) PARTITION BY k)").collect()
   # before: [Row(n=2), Row(n=1), Row(n=1)]
   # after:  [Row(n=2), Row(n=2)]
   ```
   
   Everywhere else (`GROUP BY`, window `PARTITION BY`) Spark puts all NaN 
values in one group, and Catalyst already places the NaN rows next to each 
other for the UDTF, so only the Python-side comparison is wrong.
   
   Collated string keys (e.g. `'a'` and `'A'` with `UTF8_LCASE`), also 
mentioned in the JIRA, are not handled here, because the worker would need to 
know the collation of each key. That can be a follow-up.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, a correctness fix. Python UDTFs and Arrow UDTFs with a `TABLE` argument 
and `PARTITION BY` now process all rows with a NaN partition key (or a key 
containing NaN inside an array or struct) as one partition, consistent with 
`GROUP BY`.
   
   ### How was this patch tested?
   
   New tests:
   - `test_udtf_with_table_argument_and_partition_by_nan` in 
`BaseUDTFTestsMixin`, so it runs for classic Python UDTFs, Arrow-optimized 
UDTFs (both variants) and Spark Connect (`test_parity_udtf`). It partitions by 
a NaN double, an array containing NaN and a struct containing NaN.
   - `test_arrow_udtf_with_partition_by_nan_values` in `test_arrow_udtf.py`, 
the same for `@arrow_udtf`.
   
   Both fail without this change (`[Row(n=1), Row(n=1), Row(n=2)]`) and pass 
with it. `pyspark.sql.tests.test_udtf`, 
`pyspark.sql.tests.arrow.test_arrow_udtf` and 
`pyspark.sql.tests.connect.test_parity_udtf` pass, and `./dev/lint-python 
--compile --ruff` passes.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   


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