This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 35e7aa8be8 [spark] Avoid listing files for unfiltered Format Table 
statistics (#11304)
35e7aa8be8 is described below

commit 35e7aa8be8ad106650dab53fe44232535f017a90
Author: Joey <[email protected]>
AuthorDate: Fri Nov 20 22:33:29 2026 +0800

    [spark] Avoid listing files for unfiltered Format Table statistics (#11304)
---
 .../paimon/spark/FormatTableScanBuilder.scala      | 46 ++++++++++++---
 .../paimon/spark/format/PaimonFormatTable.scala    |  7 +--
 .../spark/format/CatalogManagedPartitionTest.scala | 67 +++++++++++++++++++++-
 3 files changed, 105 insertions(+), 15 deletions(-)

diff --git 
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/FormatTableScanBuilder.scala
 
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/FormatTableScanBuilder.scala
index b92476a085..9b5c4c641e 100644
--- 
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/FormatTableScanBuilder.scala
+++ 
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/FormatTableScanBuilder.scala
@@ -20,13 +20,45 @@ package org.apache.paimon.spark
 
 import org.apache.paimon.table.FormatTable
 
+import org.apache.spark.sql.connector.read.Statistics
+import org.apache.spark.sql.types.StructType
+
+import java.util.OptionalLong
+
 case class FormatTableScanBuilder(table: FormatTable) extends 
PaimonBaseScanBuilder {
 
-  override def build(): PaimonFormatTableScan =
-    PaimonFormatTableScan(
-      table,
-      requiredSchema,
-      pushedPartitionFilters,
-      pushedDataFilters,
-      pushedLimit)
+  private var columnPruningApplied = false
+
+  override def pruneColumns(requiredSchema: StructType): Unit = {
+    super.pruneColumns(requiredSchema)
+    columnPruningApplied = true
+  }
+
+  override def build(): PaimonFormatTableScan = {
+    // Spark calls pruneColumns before building a query scan, even when it 
needs every column.
+    // A fresh scan built for relation statistics should not list every 
catalog-managed partition.
+    if (
+      !columnPruningApplied && table.partitionManager() != null &&
+      pushedPartitionFilters.isEmpty && pushedDataFilters.isEmpty && 
pushedLimit.isEmpty
+    ) {
+      new PaimonFormatTableScan(
+        table,
+        requiredSchema,
+        pushedPartitionFilters,
+        pushedDataFilters,
+        pushedLimit) {
+        override def estimateStatistics: Statistics = new Statistics {
+          override def sizeInBytes(): OptionalLong = OptionalLong.empty()
+          override def numRows(): OptionalLong = OptionalLong.empty()
+        }
+      }
+    } else {
+      PaimonFormatTableScan(
+        table,
+        requiredSchema,
+        pushedPartitionFilters,
+        pushedDataFilters,
+        pushedLimit)
+    }
+  }
 }
diff --git 
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/format/PaimonFormatTable.scala
 
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/format/PaimonFormatTable.scala
index 90fdd7a988..1f23339622 100644
--- 
a/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/format/PaimonFormatTable.scala
+++ 
b/paimon-spark/paimon-spark-common/src/main/scala/org/apache/paimon/spark/format/PaimonFormatTable.scala
@@ -98,11 +98,8 @@ case class PaimonFormatTable(table: FormatTable)
     properties
   }
 
-  override def newScanBuilder(caseInsensitiveStringMap: 
CaseInsensitiveStringMap): ScanBuilder = {
-    val scanBuilder = 
FormatTableScanBuilder(table.copy(caseInsensitiveStringMap))
-    scanBuilder.pruneColumns(schema)
-    scanBuilder
-  }
+  override def newScanBuilder(caseInsensitiveStringMap: 
CaseInsensitiveStringMap): ScanBuilder =
+    FormatTableScanBuilder(table.copy(caseInsensitiveStringMap))
 
   override def newWriteBuilder(info: LogicalWriteInfo): WriteBuilder = {
     require(
diff --git 
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/format/CatalogManagedPartitionTest.scala
 
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/format/CatalogManagedPartitionTest.scala
index 103aae3aea..38a94f86e4 100644
--- 
a/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/format/CatalogManagedPartitionTest.scala
+++ 
b/paimon-spark/paimon-spark-ut/src/test/scala/org/apache/paimon/spark/format/CatalogManagedPartitionTest.scala
@@ -22,6 +22,11 @@ import 
org.apache.paimon.spark.{PaimonSparkTestWithRestCatalogBase, SparkCatalog
 
 import org.apache.spark.sql.Row
 import org.apache.spark.sql.connector.catalog.{Identifier => SparkIdentifier}
+import org.apache.spark.sql.connector.read.SupportsReportStatistics
+import org.apache.spark.sql.execution.joins.BroadcastHashJoinExec
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+import java.util.Collections
 
 class CatalogManagedPartitionTest extends PaimonSparkTestWithRestCatalogBase {
 
@@ -68,6 +73,61 @@ class CatalogManagedPartitionTest extends 
PaimonSparkTestWithRestCatalogBase {
     }
   }
 
+  test("Format Table statistics before pushdown do not list catalog-managed 
partitions") {
+    val tableName = "catalog_partition_unfiltered_stats"
+    withTable(tableName) {
+      sql(
+        s"CREATE TABLE $tableName (id INT, dt STRING) USING CSV " +
+          "TBLPROPERTIES ('format-table.implementation'='paimon', " +
+          "'metastore.partitioned-table'='true') PARTITIONED BY (dt)")
+
+      val catalog = 
spark.sessionState.catalogManager.catalog("paimon").asInstanceOf[SparkCatalog]
+      val table = catalog
+        .loadTable(SparkIdentifier.of(Array(dbName0), tableName))
+        .asInstanceOf[PaimonFormatTable]
+      val scan = table
+        .newScanBuilder(new 
CaseInsensitiveStringMap(Collections.emptyMap[String, String]()))
+        .build()
+        .asInstanceOf[SupportsReportStatistics]
+
+      restCatalogServer.setPartitionListingSupported(false)
+      try {
+        val stats = scan.estimateStatistics()
+        assert(!stats.numRows().isPresent)
+        assert(!stats.sizeInBytes().isPresent)
+      } finally {
+        restCatalogServer.setPartitionListingSupported(true)
+      }
+    }
+  }
+
+  test("a full-schema unfiltered Format Table scan can be broadcast after 
pushdown") {
+    val tableName = "catalog_partition_broadcast_dim"
+    withTable(tableName) {
+      sql(s"""CREATE TABLE $tableName (id INT, dt STRING)
+             |USING CSV
+             |PARTITIONED BY (dt)
+             |TBLPROPERTIES (
+             |  'format-table.implementation' = 'paimon',
+             |  'metastore.partitioned-table' = 'true')
+             |""".stripMargin)
+      sql(s"INSERT INTO $tableName VALUES (1, '20260715')")
+
+      withSparkSQLConf(
+        "spark.sql.adaptive.enabled" -> "false",
+        "spark.sql.autoBroadcastJoinThreshold" -> "1048576") {
+        val plan = spark
+          .sql(s"""SELECT r.id, d.id, d.dt
+                  |FROM range(1000000) r LEFT JOIN $tableName d
+                  |ON r.id = coalesce(d.id, -1)
+                  |""".stripMargin)
+          .queryExecution
+          .executedPlan
+        assert(plan.collectFirst { case join: BroadcastHashJoinExec => join 
}.isDefined, plan)
+      }
+    }
+  }
+
   test("catalog-managed Format Table sends partition predicate to REST 
filtered listing") {
     val tableName = "catalog_partition_filter_pushdown"
     withTable(tableName) {
@@ -83,11 +143,12 @@ class CatalogManagedPartitionTest extends 
PaimonSparkTestWithRestCatalogBase {
              |""".stripMargin)
 
       clearFilteredListingRequests()
-      checkAnswer(
-        sql(s"SELECT id FROM $tableName WHERE year = 2025 AND month > 10 ORDER 
BY id"),
-        Seq(Row(3), Row(4)))
+      val query = s"SELECT id FROM $tableName WHERE year = 2025 AND month > 10 
ORDER BY id"
+      checkAnswer(sql(query), Seq(Row(3), Row(4)))
 
       assert(filteredListingWasRequested, "the filtered listing endpoint was 
not called")
+      val stats = 
getScan(query).asInstanceOf[SupportsReportStatistics].estimateStatistics()
+      assert(stats.sizeInBytes().isPresent)
     }
   }
 }

Reply via email to