-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-28445][SQL][PYTHON] Fix error when PythonUDF is used in both group by and aggregate expression #25215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
b3293fc
34531b4
33a5e0d
3b3472e
c852142
24c6744
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,6 +81,64 @@ object ExtractPythonUDFFromAggregate extends Rule[LogicalPlan] { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts PythonUDFs in logical aggregate, which are used in grouping keys, evaluate them | ||
| * before aggregate. | ||
| * This must be executed after `ExtractPythonUDFFromAggregate` rule and before `ExtractPythonUDFs`. | ||
HyukjinKwon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| */ | ||
| object ExtractGroupingPythonUDFFromAggregate extends Rule[LogicalPlan] { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the difference between this rule and ExtractPythonUDFFromAggregate?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ExtractPythonUDFFromAggregate pulls out Python UDFs which have aggregate expression or grouping key as input, like udf(sum(c)), and Python UDFs which have no input. Those UDFs pulled out are evaluated after aggregate. This rule, ExtractGroupingPythonUDFFromAggregate, pulls out Python UDFs which are used in grouping keys, like |
||
| private def hasScalarPythonUDF(e: Expression): Boolean = { | ||
| e.find(PythonUDF.isScalarPythonUDF).isDefined | ||
| } | ||
|
|
||
| private def extract(agg: Aggregate): LogicalPlan = { | ||
| val projList = new ArrayBuffer[NamedExpression]() | ||
| val groupingExpr = new ArrayBuffer[Expression]() | ||
| val attributeMap = mutable.HashMap[PythonUDF, NamedExpression]() | ||
|
|
||
| agg.groupingExpressions.foreach { expr => | ||
| if (hasScalarPythonUDF(expr)) { | ||
| val newE = expr transformDown { | ||
| case p: PythonUDF => | ||
| // This is just a sanity check, the rule PullOutNondeterministic should | ||
| // already pull out those nondeterministic expressions. | ||
| assert(p.udfDeterministic, "Non-determinstic PythonUDFs should not appear " + | ||
| "in grouping expression") | ||
| val alias = Alias(p, "groupingPythonUDF")() | ||
| projList += alias | ||
| attributeMap += ((p.canonicalized.asInstanceOf[PythonUDF], alias)) | ||
| alias.toAttribute | ||
| } | ||
| groupingExpr += newE | ||
| } else { | ||
| groupingExpr += expr | ||
| } | ||
| } | ||
| val aggExpr = agg.aggregateExpressions.map { expr => | ||
| expr.transformUp { | ||
| // PythonUDF over aggregate was pull out by ExtractPythonUDFFromAggregate. | ||
| // PythonUDF here should be either | ||
| // 1. Argument of an aggregate function. | ||
| // CheckAnalysis guarantees the arguments are deterministic. | ||
| // 2. PythonUDF in grouping key. Grouping key must be deterministic. | ||
| // 3. PythonUDF not in grouping key. It is either no arguments or with grouping key | ||
| // in its arguments. Such PythonUDF was pull out by ExtractPythonUDFFromAggregate, too. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tiny nit: spacing. |
||
| case p: PythonUDF if p.udfDeterministic => | ||
| val canonicalized = p.canonicalized.asInstanceOf[PythonUDF] | ||
| attributeMap.get(canonicalized).map(_.toAttribute).getOrElse(p) | ||
|
||
| }.asInstanceOf[NamedExpression] | ||
| } | ||
| agg.copy( | ||
| groupingExpressions = groupingExpr, | ||
| aggregateExpressions = aggExpr, | ||
| child = Project(projList ++ agg.child.output, agg.child)) | ||
| } | ||
|
|
||
| def apply(plan: LogicalPlan): LogicalPlan = plan transformUp { | ||
| case agg: Aggregate if agg.groupingExpressions.exists(hasScalarPythonUDF(_)) => | ||
| extract(agg) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Extracts PythonUDFs from operators, rewriting the query plan so that the UDF can be evaluated | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we still need to run
Canonicalize.execute?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this can be saved, yes.