comphead commented on code in PR #7761:
URL: https://github.com/apache/datafusion-comet/pull/7761#discussion_r4778320669


##########
spark/src/test/scala/org/apache/comet/exec/CometExecSuite.scala:
##########
@@ -1367,6 +1368,130 @@ class CometExecSuite extends CometTestBase {
     }
   }
 
+  // A scan Comet bridges into Arrow sits in CometScanWrapper.originalPlan, 
not among the
+  // children, while the rule converts the operators above it. Its DPP 
subquery still has to be
+  // rewritten: under AQE, or Spark's PlanAdaptiveDynamicPruningFilters drops 
the pruning
+  // (#4145); without AQE, or the subquery cannot reuse the join's Comet 
broadcast.
+  test("DPP: scan bridged by CometSparkToColumnarExec keeps DPP") {
+    withTempDir { dir =>
+      val path = dir.getAbsolutePath
+      withSQLConf(CometConf.COMET_EXEC_ENABLED.key -> "false") {
+        spark
+          .range(100)
+          .selectExpr("cast(id % 10 as int) as store_id", "cast(id as int) as 
amount")
+          .write
+          .partitionBy("store_id")
+          .parquet(s"$path/fact")
+        spark
+          .range(10)
+          .selectExpr("cast(id as int) as store_id", "cast(id as string) as 
country")
+          .write
+          .parquet(s"$path/dim")
+      }
+
+      for (aqe <- Seq(true, false)) {
+        withSQLConf(
+          SQLConf.USE_V1_SOURCE_LIST.key -> "parquet",
+          SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> aqe.toString,
+          CometConf.COMET_NATIVE_SCAN_ENABLED.key -> "false",
+          CometConf.COMET_CONVERT_FROM_PARQUET_ENABLED.key -> "true") {
+          
spark.read.parquet(s"$path/fact").createOrReplaceTempView("dpp_bridged_fact")
+          
spark.read.parquet(s"$path/dim").createOrReplaceTempView("dpp_bridged_dim")
+
+          val df = spark.sql("""SELECT f.amount, f.store_id
+              |FROM dpp_bridged_fact f JOIN dpp_bridged_dim d ON f.store_id = 
d.store_id
+              |WHERE d.country = '3'""".stripMargin)
+          val (_, cometPlan) = checkSparkAnswer(df)
+
+          val bridged = collect(cometPlan) { case c: CometSparkToColumnarExec 
=>
+            c.child
+          }.collect {
+            case s: FileSourceScanExec
+                if 
s.partitionFilters.exists(_.isInstanceOf[DynamicPruningExpression]) =>
+              s
+          }
+          assert(
+            bridged.size == 1,
+            s"aqe=$aqe: expected one bridged DPP 
scan:\n${cometPlan.treeString}")
+          assert(
+            collect(cometPlan) { case j: CometBroadcastHashJoinExec => j 
}.nonEmpty,
+            s"aqe=$aqe: expected a Comet broadcast hash 
join:\n${cometPlan.treeString}")
+          if (aqe) {
+            assertAqeDppShape(cometPlan, expectedCometSubqueryBroadcasts = 
Some(1))
+          } else {
+            // Without the rewrite, the subquery keeps Spark's 
SubqueryBroadcastExec, which still
+            // prunes but cannot reuse the join's CometBroadcastExchangeExec.
+            val subqueries = collectWithSubqueries(cometPlan) {
+              case s: CometSubqueryBroadcastExec => s
+            }
+            assert(
+              subqueries.size == 1,
+              s"aqe=$aqe: expected one 
CometSubqueryBroadcastExec:\n${cometPlan.treeString}")
+            val reused = collectWithSubqueries(cometPlan) { case e: 
ReusedExchangeExec => e }
+            assert(
+              reused.size == 1,
+              s"aqe=$aqe: expected the join's broadcast to be 
reused:\n${cometPlan.treeString}")
+          }
+          // The dimension filter keeps store 3 only, so DPP reads 1 of the 10 
partitions.
+          assert(
+            bridged.head.metrics("numPartitions").value == 1,
+            s"aqe=$aqe: expected DPP to prune to 1 
partition:\n${cometPlan.treeString}")
+        }
+      }
+    }
+  }
+
+  // The V2 form of the test above (#4145): a DSv2 scan bridged by 
CometSparkToColumnarExec keeps
+  // its AQE DPP subquery in runtimeFilters, which the rule must rewrite 
inside the bridge too.
+  test("AQE DPP: V2 BatchScan bridged by CometSparkToColumnarExec keeps DPP") {
+    import testImplicits._
+    withSQLConf(
+      "spark.sql.catalog.testcat" -> classOf[InMemoryTableCatalog].getName,
+      "spark.sql.defaultCatalog" -> "testcat",
+      SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",

Review Comment:
   The fix also rewrites a non-AQE `SubqueryBroadcastExec` in a bridged 
`BatchScanExec`'s `runtimeFilters`, but only the V1 test checks the non-AQE 
broadcast reuse. Looping over `aqe <- Seq(true, false)` here, with the same 
`CometSubqueryBroadcastExec` and `ReusedExchangeExec` checks as the V1 test, 
would cover V2 too. If the test name drops `AQE`, the cross-reference in the 
comment of `AQE DPP: V2 BatchScan broadcast query stage creation order 
(SPARK-34637)` needs the new name.



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