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

   ### What changes were proposed in this pull request?
   add a dedicated expression for `product`:
   
   1. for integral inputs, directly use `LongType` to avoid the rounding error:
   2. when ignoreNA is true, short circuit when meet a `zero`;
   3. when ignoreNA is false, short circuit when meet a `zero` or `null`;
   
   
   ### Why are the changes needed?
   
   1. existing computation logic is too complex in the PySpark side, with a 
dedicated expression, we can simplify the PySpark side and apply it in more 
cases.
   2. existing computation of `product` is likely to introduce rounding error 
for integral inputs, for example `55108 x 55108 x 55108 x 55108` in the 
following case:
   
   before:
   ```
   In [14]: df = pd.DataFrame({"a": [55108, 55108, 55108, 55108], "b": 
[55108.0, 55108.0, 55108.0, 55108.0], "c": [1, 2, 3, 4]})
   
   In [15]: df.a.prod()
   Out[15]: 9222710978872688896
   
   In [16]: type(df.a.prod())
   Out[16]: numpy.int64
   
   In [17]: df.b.prod()
   Out[17]: 9.222710978872689e+18
   
   In [18]: type(df.b.prod())
   Out[18]: numpy.float64
   
   In [19]: 
   
   In [19]: psdf = ps.from_pandas(df)
   
   In [20]: psdf.a.prod()
   Out[20]: 9222710978872658944
   
   In [21]: type(psdf.a.prod())
   Out[21]: int
   
   In [22]: psdf.b.prod()
   Out[22]: 9.222710978872659e+18
   
   In [23]: type(psdf.b.prod())
   Out[23]: float
   
   
   In [24]: df.a.prod() - psdf.a.prod()
   Out[24]: 29952
   ```
   
   after:
   ```
   In [1]: import pyspark.pandas as ps
   
   In [2]: import pandas as pd
   
   In [3]: df = pd.DataFrame({"a": [55108, 55108, 55108, 55108], "b": [55108.0, 
55108.0, 55108.0, 55108.0], "c": [1, 2, 3, 4]})
   
   In [4]: df.a.prod()
   Out[4]: 9222710978872688896
   
   In [5]: psdf = ps.from_pandas(df)
   
   In [6]: psdf.a.prod()
   Out[6]: 9222710978872688896                                                  
   
   
   In [7]: df.a.prod() - psdf.a.prod()
   Out[7]: 0
   ```
   
   ### Does this PR introduce _any_ user-facing change?
   No
   
   
   ### How was this patch tested?
   existing UT
   


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