weiting-chen commented on code in PR #13164:
URL: https://github.com/apache/gluten/pull/13164#discussion_r4255314962


##########
gluten-ut/spark42/src/test/scala/org/apache/spark/sql/connector/GlutenDataSourceV2EnhancedPartitionFilterSuite.scala:
##########
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.sql.connector
+
+import org.apache.gluten.execution.FilterExecTransformerBase
+
+import org.apache.spark.sql.{DataFrame, GlutenSQLTestsTrait, Row}
+import org.apache.spark.sql.connector.catalog.{BufferedRows, 
InMemoryEnhancedPartitionFilterTable}
+import org.apache.spark.sql.execution.ExplainUtils.stripAQEPlan
+import org.apache.spark.sql.execution.FilterExec
+import org.apache.spark.sql.execution.datasources.v2.BatchScanExec
+
+class GlutenDataSourceV2EnhancedPartitionFilterSuite
+  extends DataSourceV2EnhancedPartitionFilterSuite
+  with GlutenSQLTestsTrait {
+
+  // The vanilla "case 9" tests (excluded in VeloxTestSettings) require a 
Spark FilterExec for the
+  // partition filter that the source returns in the first pass, but Gluten 
can offload that
+  // post-scan filter to a native FilterExecTransformer. These variants keep 
the answer and
+  // pushdown checks and accept either filter.
+  testGluten(
+    "case 9: partition filter pushed but returned in first pass is not 
re-pushed second pass") {
+    withTable(partFilterTableName) {
+      sql(
+        s"CREATE TABLE $partFilterTableName (part_col string, data string) 
USING $v2Source " +
+          "PARTITIONED BY (part_col) " +
+          "TBLPROPERTIES('return-accepted-partition-predicates' = 'true')")
+      sql(s"INSERT INTO $partFilterTableName VALUES ('a', 'x'), ('b', 'y'), 
('c', 'z')")
+
+      val df = sql(s"SELECT * FROM $partFilterTableName WHERE part_col = 'b'")
+      checkAnswer(df, Seq(Row("b", "y")))
+      assertNoSecondPassPartitionPredicates(df)
+      assertScannedPartitionKeys(df, Set("b"))
+      assertPostScanFilter(df)
+    }
+  }
+
+  testGluten(
+    "nested identity partition: case 9 partition filter pushed but returned in 
first pass " +
+      "is not re-pushed second pass") {
+    withTable(partFilterTableName) {
+      sql(
+        s"CREATE TABLE $partFilterTableName " +
+          s"(s struct<tz: string, x: int>, data string) USING $v2Source " +
+          "PARTITIONED BY (s.tz) " +
+          "TBLPROPERTIES('return-accepted-partition-predicates' = 'true')")
+      sql(
+        s"INSERT INTO $partFilterTableName VALUES " +
+          "(named_struct('tz', 'LA', 'x', 1), 'a'), " +
+          "(named_struct('tz', 'NY', 'x', 2), 'b')")
+
+      val df = sql(s"SELECT * FROM $partFilterTableName WHERE s.tz = 'LA'")
+      checkAnswer(df, Seq(Row(Row("LA", 1), "a")))
+      assertNoSecondPassPartitionPredicates(df)
+      assertScannedPartitionKeys(df, Set("LA"))
+      assertPostScanFilter(df)
+    }
+  }
+
+  private def batchScan(df: DataFrame): BatchScanExec = {
+    stripAQEPlan(df.queryExecution.executedPlan)
+      .collectFirst { case b: BatchScanExec => b }
+      .getOrElse(fail("Expected BatchScanExec in plan"))
+  }
+
+  private def assertNoSecondPassPartitionPredicates(df: DataFrame): Unit = {
+    val predicates = batchScan(df).batch match {
+      case s: 
InMemoryEnhancedPartitionFilterTable#InMemoryEnhancedPartitionFilterBatchScan =>
+        s.getPushedPartitionPredicates
+      case _ => Seq.empty
+    }
+    assert(predicates.isEmpty, s"Expected no pushed partition predicates, got 
$predicates")
+  }
+
+  private def assertScannedPartitionKeys(df: DataFrame, expectedKeys: 
Set[String]): Unit = {
+    val partitions = batchScan(df).batch.planInputPartitions()
+    assert(partitions.length === expectedKeys.size)
+    assert(partitions.map(_.asInstanceOf[BufferedRows].keyString()).toSet === 
expectedKeys)
+  }
+
+  private def assertPostScanFilter(df: DataFrame): Unit = {
+    val hasFilter = stripAQEPlan(df.queryExecution.executedPlan).exists {
+      case _: FilterExec | _: FilterExecTransformerBase => true

Review Comment:
   **Optional: assert the returned equality, not just a filter class**
   
   **Target Location:** 
`GlutenDataSourceV2EnhancedPartitionFilterSuite.scala:95-101`.
   
   **Problem:** Both fixtures already prune exactly to `b` / `LA` in the first 
pass. If a future regression drops the returned equality but leaves only an 
`IsNotNull` residual, the answer, partition-key, empty second-pass-predicate, 
and filter-class assertions would all still pass. This is a **nonblocking 
test-oracle improvement**: the same weakness exists in the upstream assertion, 
and there is no claim that the current implementation drops the equality. 
Accepting the native filter is the appropriate fix for the previous finding.
   
   **Evidence:**
   ```scala
   val hasFilter = stripAQEPlan(df.queryExecution.executedPlan).exists {
     case _: FilterExec | _: FilterExecTransformerBase => true
     case _ => false
   }
   ```
   This checks the operator type but not its condition; an `IsNotNull` filter 
is indistinguishable from the required returned equality.
   
   **Suggested Fix:** For these two single-filter queries, obtain the expected 
equality from the analyzed plan, before the pushdown being tested, and require 
an equivalent conjunct in either supported physical filter representation:
   ```scala
   private def assertPostScanFilter(df: DataFrame): Unit = {
     val expected = df.queryExecution.analyzed.collectFirst {
       case f: org.apache.spark.sql.catalyst.plans.logical.Filter => f.condition
     }.getOrElse(fail("Expected analyzed query filter"))
   
     val hasReturnedEquality = 
stripAQEPlan(df.queryExecution.executedPlan).exists {
       case f: FilterExec =>
         
splitConjunctivePredicates(f.condition).exists(_.semanticEquals(expected))
       case f: FilterExecTransformerBase =>
         splitConjunctivePredicates(f.cond).exists(_.semanticEquals(expected))
       case _ => false
     }
     assert(hasReturnedEquality, "Returned equality should remain in the 
post-scan filter")
   }
   ```
   `PredicateHelper` is already inherited. Matching conjuncts tolerates 
additional null guards while retaining Spark/native flexibility. This snippet 
is source-checked, not compiled or runtime-tested; verify both `Gluten - case 
9` variants, and ideally a negative control retaining only `IsNotNull`, which 
should fail the stronger assertion.
   



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