maropu commented on issue #26420: [SPARK-27986][SQL] Support ANSI SQL filter 
predicate for aggregate expression.
URL: https://github.com/apache/spark/pull/26420#issuecomment-551834128
 
 
   I just thought a super simple case like this;
   ```
   postgres=# select * from t;
    k | v1 | v2 
   ---+----+----
    1 |  2 |  3
    1 |  4 |  5
    1 |    |  9
    2 |  3 |   
    2 |  5 |  8
   (5 rows)
   
   postgres=# select k, sum(v1) filter (where v1 > 2), avg(v2) filter (where v2 
< 6) from t group by k;
    k | sum |        avg         
   ---+-----+--------------------
    2 |   8 |                   
    1 |   4 | 4.0000000000000000
   (2 rows)
   ```
   The query above might be transformed into...
   ```
   scala> sql("select k, sum(v1), avg(v2) from (select k, if(v1 > 2, v1, null) 
v1, if(v2 < 6, v2, null) v2 from t) group by k").show()
   +---+-------+-------+
   |  k|sum(v1)|avg(v2)|
   +---+-------+-------+
   |  1|      4|    4.0|
   |  2|      8|   null|
   +---+-------+-------+
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to