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

   ### What changes were proposed in this pull request?
   
   In Spark Connect, `_bin_op` and `Column.__eq__` in 
`python/pyspark/sql/connect/column.py` only turned built-in Python values 
(`bool`, `int`, `float`, `str`, date/time, `Decimal`) into literals and treated 
anything else as a `Column`. So a NumPy scalar operand failed with 
`AttributeError: 'numpy.int64' object has no attribute '_expr'`.
   
   This PR also sends NumPy scalars (`np.generic`) to 
`LiteralExpression._from_value`, which already supports them (it is what 
`F.lit(np.int64(1))` uses).
   
   ### Why are the changes needed?
   
   Classic PySpark accepts NumPy scalars as operands, so the same code fails 
only in Spark Connect:
   
   ```python
   df = spark.range(3).select(sf.col("id").alias("x"))
   df.select(sf.col("x") + np.int64(1)).collect()
   # Classic: [1, 2, 3]
   # Connect (before): AttributeError: 'numpy.int64' object has no attribute 
'_expr'
   ```
   
   The same happened for `np.int8/16/32`, `np.float32` and `np.bool_` with 
arithmetic and comparison operators, `==`, `!=`, `eqNullSafe`, `bitwiseOR` and 
`between`. `np.float64` worked only because it is a subclass of Python `float`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. In Spark Connect, Column operators now accept NumPy scalar operands, as 
in classic PySpark.
   
   ### How was this patch tested?
   
   Added `test_numpy_scalar_operands` to `ColumnTestsMixin`, so it runs on both 
classic (`ColumnTests`) and Spark Connect (`ColumnParityTests`). It fails on 
Connect without this change and passes with it. `pyspark.sql.tests.test_column` 
and `pyspark.sql.tests.connect.test_parity_column` pass.
   
   Note: unsigned NumPy scalars such as `np.uint8` are still not supported by 
Connect's literal inference (also with `F.lit`), so they now raise 
`UNSUPPORTED_DATA_TYPE` instead of the `AttributeError`. That is a separate gap 
and can be a follow-up.
   
   ### 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