szehon-ho commented on code in PR #54459:
URL: https://github.com/apache/spark/pull/54459#discussion_r2893254858


##########
sql/core/src/test/scala/org/apache/spark/sql/connector/DataSourceV2EnhancedPartitionFilterSuite.scala:
##########
@@ -0,0 +1,288 @@
+/*
+ * 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 java.util.Locale
+
+import org.scalatest.BeforeAndAfter
+
+import org.apache.spark.sql.{QueryTest, Row}
+import org.apache.spark.sql.connector.catalog.BufferedRows
+import 
org.apache.spark.sql.connector.catalog.InMemoryTableEnhancedPartitionFilterCatalog
+import org.apache.spark.sql.connector.catalog.TestPartitionPredicateScan
+import org.apache.spark.sql.connector.expressions.filter.PartitionPredicate
+import org.apache.spark.sql.execution.datasources.v2.BatchScanExec
+import org.apache.spark.sql.test.SharedSparkSession
+
+/**
+ * Tests for enhanced partition filter pushdown with tables whose scan builder 
handles
+ * PartitionPredicates in a second pass of partition filter pushdown, for those
+ * Catalyst Expression filters that are not translatable to DSV2, or are 
returned by DSV2
+ * in the first pushdown.
+ */
+class DataSourceV2EnhancedPartitionFilterSuite
+  extends QueryTest with SharedSparkSession with BeforeAndAfter {
+
+  protected val v2Source = classOf[FakeV2ProviderWithCustomSchema].getName
+  protected val partFilterTableName = "testpartfilter.t"
+
+  protected def registerCatalog(name: String, clazz: Class[_]): Unit = {
+    spark.conf.set(s"spark.sql.catalog.$name", clazz.getName)
+  }
+
+  before {
+    registerCatalog("testpartfilter", 
classOf[InMemoryTableEnhancedPartitionFilterCatalog])
+  }
+
+  after {
+    spark.sessionState.catalogManager.reset()
+  }
+
+  private def getBatchScan(df: org.apache.spark.sql.DataFrame): BatchScanExec 
= {
+    df.queryExecution.executedPlan.collectFirst {
+      case b: BatchScanExec => b
+    }.getOrElse(fail("Expected BatchScanExec in plan"))
+  }
+
+  /** Number of input partitions in the executed plan. */
+  private def getInputPartitionCount(df: org.apache.spark.sql.DataFrame): Int 
= {
+    getBatchScan(df).batch.planInputPartitions().length
+  }
+
+  /**
+   * Collects pushed partition predicates from the plan when the scan is our
+   * test in-memory scan.
+   */
+  private def getPushedPartitionPredicates(
+      df: org.apache.spark.sql.DataFrame): Seq[PartitionPredicate] = {
+    getBatchScan(df).batch match {
+      case s: TestPartitionPredicateScan => s.getPushedPartitionPredicates
+      case _ => Seq.empty
+    }
+  }
+
+  private def assertPushedPartitionPredicateOrdinals(
+      df: org.apache.spark.sql.DataFrame,
+      expected: Array[Int]): Unit = {
+    getPushedPartitionPredicates(df).foreach { p =>
+      assert(p.referencedPartitionColumnOrdinals().sameElements(expected))
+    }
+  }
+
+  /**
+   * Asserts the number of pushed partition predicates and that each has the 
given
+   * referencedPartitionColumnOrdinals.
+   */
+  private def assertPushedPartitionPredicates(
+      df: org.apache.spark.sql.DataFrame,
+      expectedCount: Int,
+      expectedOrdinals: Array[Int]): Unit = {
+    val predicates = getPushedPartitionPredicates(df)
+    assert(predicates.length === expectedCount)
+    assertPushedPartitionPredicateOrdinals(df, expectedOrdinals)
+  }
+
+  test("first pass partition filter still works (e.g. part_col = value)") {
+    withTable(partFilterTableName) {
+      sql(s"CREATE TABLE $partFilterTableName (part_col string, data string) 
USING $v2Source " +
+        "PARTITIONED BY (part_col)")
+      sql(s"INSERT INTO $partFilterTableName VALUES ('a', 'x'), ('b', 'y'), 
('c', 'z')")
+
+      // Simple partition equality is pushed in the first pass and used to 
prune partitions
+      val df = sql(s"SELECT * FROM $partFilterTableName WHERE part_col = 'b'")
+      checkAnswer(df, Seq(Row("b", "y")))
+
+      assert(getInputPartitionCount(df) === 1,
+        "First-pass pushed predicate (part_col = 'b') should prune to one 
partition")
+      val partitions = getBatchScan(df).batch.planInputPartitions()
+      assert(partitions.head.asInstanceOf[BufferedRows].keyString() === "b")
+      assertPushedPartitionPredicates(df, expectedCount = 1, Array(0))

Review Comment:
   yes its a bug in the InMemoryTableEnhancedPartitionFilter added above.  It's 
logic to was accept pushdown Predicate did not match correctly the Predicate  
name for V2 Predicate IS_NOT_NULL.
   
   Spark generate both `part_col IS NOT NULL` and `part_col = 'b'` , so it was 
flagging the IS_NOT_NULL is second pass.  Fixed, so now both are accept in the 
first pass



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