GitHub user davies opened a pull request:

    https://github.com/apache/spark/pull/13682

    [SPARK-15888] [SQL] fix Python UDF with aggregate

    ## What changes were proposed in this pull request?
    
    After we move the ExtractPythonUDF rule into physical plan, Python UDF 
can't work on top of aggregate anymore, because they can't be evaluated before 
aggregate, should be evaluated after aggregate. This PR add another rule to 
extract these kind of Python UDF from logical aggregate, create a Project on 
top of Aggregate.
    
    ## How was this patch tested?
    
    Added regression tests. The plan of added test query looks like this:
    ```
    == Parsed Logical Plan ==
    'Project [<lambda>('k, 's) AS t#26]
    +- Aggregate [<lambda>(key#5L)], [<lambda>(key#5L) AS k#17, 
sum(cast(<lambda>(value#6) as bigint)) AS s#22L]
       +- LogicalRDD [key#5L, value#6]
    
    == Analyzed Logical Plan ==
    t: int
    Project [<lambda>(k#17, s#22L) AS t#26]
    +- Aggregate [<lambda>(key#5L)], [<lambda>(key#5L) AS k#17, 
sum(cast(<lambda>(value#6) as bigint)) AS s#22L]
       +- LogicalRDD [key#5L, value#6]
    
    == Optimized Logical Plan ==
    Project [<lambda>(agg#29, agg#30L) AS t#26]
    +- Aggregate [<lambda>(key#5L)], [<lambda>(key#5L) AS agg#29, 
sum(cast(<lambda>(value#6) as bigint)) AS agg#30L]
       +- LogicalRDD [key#5L, value#6]
    
    == Physical Plan ==
    *Project [pythonUDF0#37 AS t#26]
    +- BatchEvalPython [<lambda>(agg#29, agg#30L)], [agg#29, agg#30L, 
pythonUDF0#37]
       +- *HashAggregate(key=[<lambda>(key#5L)#31], 
functions=[sum(cast(<lambda>(value#6) as bigint))], output=[agg#29,agg#30L])
          +- Exchange hashpartitioning(<lambda>(key#5L)#31, 200)
             +- *HashAggregate(key=[pythonUDF0#34 AS <lambda>(key#5L)#31], 
functions=[partial_sum(cast(pythonUDF1#35 as bigint))], 
output=[<lambda>(key#5L)#31,sum#33L])
                +- BatchEvalPython [<lambda>(key#5L), <lambda>(value#6)], 
[key#5L, value#6, pythonUDF0#34, pythonUDF1#35]
                   +- Scan ExistingRDD[key#5L,value#6]
    ```
    
    


You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/davies/spark fix_py_udf

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/13682.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #13682
    
----
commit 4d5f075a044efabd9b7a28acf0cbad8551b44e3b
Author: Davies Liu <dav...@databricks.com>
Date:   2016-06-14T23:50:46Z

    fix Python UDF with aggregate

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to