dongjoon-hyun commented on code in PR #60696:
URL: https://github.com/apache/spark/pull/60696#discussion_r4624814494


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/PartitionPruning.scala:
##########
@@ -60,6 +60,9 @@ object PartitionPruning extends Rule[LogicalPlan] with 
PredicateHelper with Join
   def getFilterableTableScan(a: Expression, plan: LogicalPlan): 
Option[LogicalPlan] = {
     val srcInfo: Option[(Expression, LogicalPlan)] = 
findExpressionAndTrackLineageDown(a, plan)
     srcInfo.flatMap {
+      // The "Extract UDFs" batch evaluates such a UDF in a node of its own, 
between a DPP
+      // filter on this expression and the scan, so the scan could not prune 
with the filter.
+      case (resExp, _) if needsUDFExtraction(Seq(resExp)) => None

Review Comment:
   Thank you. You're right: the check looked only at the first lineage, so the 
union lost the pruning of its plain branch. In 39d25ba0421, I took the 
alternative from your cross-cutting note instead: `getFilterableTableScan` now 
checks the join key as written. The key `f.store_id` of the union has no UDF, 
so the DPP filter is kept as before this PR, and `fact_stats` prunes its 
partitions in either order of the branches. The new test `SPARK-62052: keep DPP 
on a pruning key with a Python UDF only through an alias` checks your query, 
and fails with the lineage check. A union whose branches all have the UDF in 
the alias of the key keeps the filter too, as before this PR, since it stays 
above the projection that evaluates the UDF in each branch.
   



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/dynamicpruning/PartitionPruning.scala:
##########
@@ -60,6 +60,9 @@ object PartitionPruning extends Rule[LogicalPlan] with 
PredicateHelper with Join
   def getFilterableTableScan(a: Expression, plan: LogicalPlan): 
Option[LogicalPlan] = {
     val srcInfo: Option[(Expression, LogicalPlan)] = 
findExpressionAndTrackLineageDown(a, plan)
     srcInfo.flatMap {
+      // The "Extract UDFs" batch evaluates such a UDF in a node of its own, 
between a DPP
+      // filter on this expression and the scan, so the scan could not prune 
with the filter.
+      case (resExp, _) if needsUDFExtraction(Seq(resExp)) => None

Review Comment:
   Agreed. With the join key as written (39d25ba0421), a key that has the UDF 
only through an alias keeps its DPP filter and its row reduction, since, as you 
pointed out, that filter adds no evaluation of the UDF by default. For a key 
written with the UDF in the join condition, the comment on the check and the PR 
description now state the trade-off: the filter dropped rows before the join, 
at the cost of one more evaluation of the UDF on every row of the scan. This is 
the same trade-off that `CleanupDynamicPruningFilters` makes for a DPP filter 
that is not pushed down to a scan. The PR description also names the rejected 
alternative, the check on the lineage, and why.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/DynamicPartitionPruningSuite.scala:
##########
@@ -2009,6 +2010,55 @@ abstract class DynamicPartitionPruningSuiteBase
       
assert(collectDynamicPruningExpressions(df.queryExecution.executedPlan).size 
=== 1)
     }
   }
+
+  // These tests drop their function themselves, as `withUserDefinedFunction` 
checks the drop with
+  // a lookup that fails in the V2 suites, whose default catalog has no 
functions.
+  test("SPARK-62052: no DPP filter on a pruning key with a Python UDF") {

Review Comment:
   Thank you. The new union test covers this. Its first branch applies `pyudf` 
in a projection of the pruning plan, so a check widened to the pruning plan's 
expressions would drop the DPP filter and fail the assertion that the 
`fact_stats` scan keeps it.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/DynamicPartitionPruningSuite.scala:
##########
@@ -2009,6 +2010,55 @@ abstract class DynamicPartitionPruningSuiteBase
       
assert(collectDynamicPruningExpressions(df.queryExecution.executedPlan).size 
=== 1)
     }
   }
+
+  // These tests drop their function themselves, as `withUserDefinedFunction` 
checks the drop with
+  // a lookup that fails in the V2 suites, whose default catalog has no 
functions.
+  test("SPARK-62052: no DPP filter on a pruning key with a Python UDF") {
+    import IntegratedUDFTestUtils._
+    assume(shouldTestPythonUDFs)
+    try {
+      registerTestUDF(TestPythonUDF("pyudf"), spark)
+      Seq(
+        "fact_sk f JOIN dim_store s ON pyudf(f.store_id) = s.store_id",
+        "(SELECT date_id, pyudf(store_id) AS store_id FROM fact_sk) f " +
+          "JOIN dim_store s ON f.store_id = s.store_id").foreach { join =>
+        val df = sql(s"SELECT f.date_id, f.store_id FROM $join WHERE s.country 
= 'NL'")
+        checkAnswer(df, Row(1000, 1) :: Row(1010, 2) :: Row(1020, 2) :: Nil)
+        // A scan cannot evaluate the UDF to prune partitions, so a DPP filter 
would only filter
+        // rows, above another evaluation of the UDF.

Review Comment:
   Thank you. The aliased query is no longer in that test, as such a key now 
keeps its DPP filter. So the comment now applies only to the key written with 
the UDF in the join condition.
   



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to