raviston commented on PR #60298:
URL: https://github.com/apache/spark/pull/60298#issuecomment-6787852274

   Thanks for the fix. One question about decimals.
   
   `DecimalOps` deliberately raises `TypeError` for `<`, `<=`, `>` and `>=`, so 
on master both directions fail for a decimal Series. With this change, 
`_transform_for_numeric_comparison` treats `DecimalType` as numeric and casts 
the boolean operand to it, so only one direction starts working:
   
   ```python
   pdf = pd.DataFrame({"b": [True, True, False], "d": [Decimal(1), Decimal(2), 
Decimal(3)]})
   psdf = ps.from_pandas(pdf)
   
   psdf.b < psdf.d   # master: AnalysisException; with this PR: [False, True, 
True]
   psdf.d > psdf.b   # master and this PR: TypeError: > can not be applied to 
decimal.
   psdf.d > 1        # master and this PR: TypeError: > can not be applied to 
decimal.
   ```
   
   The cast also uses the decimal's own precision and scale, so it fails when 
the decimal has no integer digits:
   
   ```python
   psdf = ps.from_pandas(pd.DataFrame({"b": [True, False], "d": 
[Decimal("0.10"), Decimal("0.20")]}))
   psdf["d"] = psdf["d"].spark.transform(lambda c: c.cast("decimal(2,2)"))
   psdf.b < psdf.d
   # ArithmeticException: [NUMERIC_VALUE_OUT_OF_RANGE.WITH_SUGGESTION] 1 cannot 
be represented as Decimal(2, 2)
   # pandas: [False, True]
   ```
   
   I reproduced these on master by calling 
`transform_boolean_operand_to_numeric(left, spark_type=right.spark.data_type)` 
and then `column_op(PySparkColumn.__lt__)`, which is what the new 
`BooleanOps.lt` does for a decimal `right`.
   
   Would it make sense to leave `DecimalType` out of 
`_transform_for_numeric_comparison`, so that decimal ordering stays unsupported 
in both directions as in `DecimalOps`? The new test only covers the `int32`, 
`int`, `float32`, `float` and `float_nan` columns, so a `decimal` case there 
would pin down whichever behavior you choose.
   
   The rest looks good to me. I checked that `float_nan` behaves like pandas: 
`from_pandas` stores the NaN as null, so `False < NaN` gives `False` as in 
pandas.
   


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