[ 
https://issues.apache.org/jira/browse/SPARK-29638?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17240710#comment-17240710
 ] 

Takeshi Yamamuro commented on SPARK-29638:
------------------------------------------

'NaN' is a special value for float/double types, so I think the correct query is
{code:java}
 SELECT a, b, SUM(b) OVER(ORDER BY A ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
FROM (VALUES(1.0,1.0),(2.0,2.0),(3.0,(cast('nan' as 
float8))),(4.0,3.0),(5.0,4.0)) t(a,b);{code}
But, it seems the answer is the same with each other;
{code:java}
postgres=# SELECT a, b, SUM(b) OVER(ORDER BY A ROWS BETWEEN 1 PRECEDING AND 
CURRENT ROW)
postgres-# FROM (VALUES(1.0,1.0),(2.0,2.0),(3.0,(cast('nan' as 
float8))),(4.0,3.0),(5.0,4.0)) t(a,b);
  a  |  b  | sum 
-----+-----+-----
 1.0 |   1 |   1
 2.0 |   2 |   3
 3.0 | NaN | NaN
 4.0 |   3 | NaN
 5.0 |   4 |   7
(5 rows)

scala> sql("""
     | SELECT a, b, SUM(b) OVER(ORDER BY A ROWS BETWEEN 1 PRECEDING AND CURRENT 
ROW)
     | FROM (VALUES(1.0,1.0),(2.0,2.0),(3.0,(cast('nan' as 
double))),(4.0,3.0),(5.0,4.0)) t(a,b);
     | """).show()
+---+---+---------------------------------------------------------------------------------+
|  a|  b|sum(b) OVER (ORDER BY A ASC NULLS FIRST ROWS BETWEEN 1 PRECEDING AND 
CURRENT ROW)|
+---+---+---------------------------------------------------------------------------------+
|1.0|1.0|                                                                       
       1.0|
|2.0|2.0|                                                                       
       3.0|
|3.0|NaN|                                                                       
       NaN|
|4.0|3.0|                                                                       
       NaN|
|5.0|4.0|                                                                       
       7.0|
+---+---+---------------------------------------------------------------------------------+
 {code}

So, I will close this. If you reopen this issue, please update the description.

> Spark handles 'NaN' as 0 in sums
> --------------------------------
>
>                 Key: SPARK-29638
>                 URL: https://issues.apache.org/jira/browse/SPARK-29638
>             Project: Spark
>          Issue Type: Sub-task
>          Components: SQL
>    Affects Versions: 3.0.0
>            Reporter: Dylan Guedes
>            Priority: Major
>
> Currently, Spark handles 'NaN' as 0 in window functions, such that 3+'NaN'=3. 
> PgSQL, on the other hand, handles the entire result as 'NaN', as in 3+'NaN' = 
> 'NaN'
> I experienced this with the query below:
> {code:sql}
> SELECT a, b,
>        SUM(b) OVER(ORDER BY A ROWS BETWEEN 1 PRECEDING AND CURRENT ROW)
> FROM (VALUES(1,1),(2,2),(3,(cast('nan' as int))),(4,3),(5,4)) t(a,b);
> {code}



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to