Skip to content
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

[SPARK-48311][SQL] Fix nested pythonUDF in groupBy and aggregate in Binding Exception #50183

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ object ExtractGroupingPythonUDFFromAggregate extends Rule[LogicalPlan] {
}
}
val aggExpr = agg.aggregateExpressions.map { expr =>
expr.transformUp {
expr.transformDown {
// PythonUDF over aggregate was pull out by ExtractPythonUDFFromAggregate.
// PythonUDF here should be either
// 1. Argument of an aggregate function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package org.apache.spark.sql.execution.python

import org.apache.spark.sql.{AnalysisException, IntegratedUDFTestUtils, QueryTest, Row}
import org.apache.spark.sql.functions.{array, col, count, transform}
import org.apache.spark.sql.functions.{array, col, count, countDistinct, transform}
import org.apache.spark.sql.test.SharedSparkSession
import org.apache.spark.sql.types.LongType

Expand Down Expand Up @@ -139,4 +139,34 @@ class PythonUDFSuite extends QueryTest with SharedSparkSession {
checkAnswer(df, Row(0, 1, 1, 0, 1, 1))
}
}

test("SPARK-48311: Nested pythonUDF in groupBy and aggregate") {
assume(shouldTestPythonUDFs)
withTempView("testCacheTable") {
// Define data
val data = Seq(Some("1")).toDF("col3")
data.createOrReplaceTempView("testCacheTable")
val df = spark.sql("SELECT DISTINCT col3 FROM testCacheTable")
// Define groupBy columns
val groupByCols = Seq("col4", "col5", "col3")

val pythonTestUDF1 = TestPythonUDF(name = "pyUDF1")
val pythonTestUDF2 = TestPythonUDF(name = "pyUDF2")
// Apply transformations
val df1 = df
.withColumn("col4", pythonTestUDF1(df("col3")))
val resultPython = df1.withColumn("col5", pythonTestUDF2(df1("col4")))
.groupBy(groupByCols.head, groupByCols.tail: _*).agg(countDistinct("col5").alias("col6"))

val scalaTestUDF = TestScalaUDF(name = "scalaUDF")
val scalaTestUDF1 = TestScalaUDF(name = "scalaUDF1")
// Apply transformations
val df2 = df
.withColumn("col4", scalaTestUDF(df("col3")))
val resultScala = df2.withColumn("col5", scalaTestUDF1(df2("col4")))
.groupBy(groupByCols.head, groupByCols.tail: _*).agg(countDistinct("col5").alias("col6"))

checkAnswer(resultScala, resultPython)
}
}
}