You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "anton5798 (via GitHub)" <gi...@apache.org> on 2026/04/24 19:19:32 UTC

[PR] [SPARK-XXXXX][SQL] Introduce SupportsReportCatalogStatistics mixin for Table [spark]

anton5798 opened a new pull request, #55546:
URL: https://github.com/apache/spark/pull/55546

   ### What changes were proposed in this pull request?
   
   This PR introduces `SupportsReportCatalogStatistics`, a new mix-in interface on `org.apache.spark.sql.connector.catalog.Table`. DSv2 connectors can implement it to report table-level (pre-filter, pre-pruning) statistics to Spark without going through a `Scan`.
   
   ```java
   @Evolving
   public interface SupportsReportCatalogStatistics extends Table {
     Statistics catalogStatistics();
   }
   ```
   
   `DataSourceV2ScanRelation.computeStats` is updated to prefer these catalog stats when the table implements the mixin *and* the returned stats carry a row count; otherwise it falls through to the existing `Scan.estimateStatistics()`-based path unchanged. The change is strictly additive: tables that don't implement the mixin see identical behavior.
   
   ### Why are the changes needed?
   
   DSv2 today conflates catalog-level (pre-filter, table-wide — analogous to DSv1's `CatalogTable.stats`) and scan-level (post-filter, post-pruning) statistics on `Scan.estimateStatistics()`. Reading catalog stats requires building a `ScanBuilder`, which can trigger unnecessary work (file listing, remote metadata fetches) and hides a logical property of the table inside the scan API.
   
   `SupportsReportCatalogStatistics` gives connectors a direct, scan-independent way to report table-level stats. It is a strict analog of DSv1's `CatalogStatistics` for the v2 catalog API and is a natural input to CBO decisions on the unfiltered relation (join reordering, broadcast thresholds), while scan statistics continue to tighten estimates once pushdown has happened.
   
   ### Does this PR introduce _any_ user-facing change?
   
   No. The new interface is additive and no Spark built-in `Table` implements it in this PR. Existing connectors and queries observe no behavior change.
   
   ### How was this patch tested?
   
   No new tests. The `computeStats` change is additive and preserves the existing Scan-based path verbatim; existing DSv2 suites (`DataSourceV2Suite`, `DataSourceV2SQLSuite`) continue to exercise it. Follow-up PRs that add an in-tree implementation of the mixin will add targeted coverage for the new path.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Opus 4.7)


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56626][SQL] Introduce SupportsReportCatalogStatistics mixin for Table [spark]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #55546:
URL: https://github.com/apache/spark/pull/55546#issuecomment-5161185710

   We're closing this PR because it hasn't been updated in a while. This isn't a judgement on the merit of the PR in any way. It's just a way of keeping the PR queue manageable.
   If you'd like to revive this PR, please reopen it and ask a committer to remove the Stale tag!


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56626][SQL] Introduce SupportsReportCatalogStatistics mixin for Table [spark]

Posted by "gengliangwang (via GitHub)" <gi...@apache.org>.
gengliangwang commented on code in PR #55546:
URL: https://github.com/apache/spark/pull/55546#discussion_r3140228653


##########
sql/catalyst/src/main/java/org/apache/spark/sql/connector/catalog/SupportsReportCatalogStatistics.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.catalog;
+
+import org.apache.spark.annotation.Evolving;
+import org.apache.spark.sql.connector.read.Statistics;
+
+/**
+ * A mix-in interface for {@link Table}. Data sources can implement this interface to report
+ * table-level statistics to Spark.
+ * <p>
+ * The statistics returned here describe the table as a whole, independent of any {@code Scan}
+ * and independent of any pushed operators (filters, partition pruning, column pruning,
+ * aggregate pushdown). They are analogous to DSv1's {@code CatalogStatistics} exposed via
+ * {@code CatalogTable.stats}: a stable, pre-filter, pre-pruning property of the table.
+ * <p>
+ * This is distinct from
+ * {@link org.apache.spark.sql.connector.read.SupportsReportStatistics} on {@code Scan}, which
+ * reports <em>post-pushdown</em> statistics that reflect the data the scan will actually
+ * produce. A table may implement both: catalog statistics drive CBO decisions on the
+ * unfiltered relation (join reordering, broadcast thresholds), while scan statistics tighten
+ * estimates once pushdown has happened.
+ * <p>
+ * Implementations should return statistics without triggering expensive work such as file
+ * listing or remote metadata fetches beyond what is already cached on the {@code Table}
+ * instance. Consumers may call this method during optimization, potentially more than once
+ * per query, and expect a consistent result for a given {@code Table} instance.
+ *
+ * @since 4.2.0
+ */
+@Evolving
+public interface SupportsReportCatalogStatistics extends Table {

Review Comment:
   We need usage of this new trait. E.g. DataSourceV2Relation.
   
   Also, there should be test cases for the changes.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56626][SQL] Introduce SupportsReportCatalogStatistics mixin for Table [spark]

Posted by "anton5798 (via GitHub)" <gi...@apache.org>.
anton5798 commented on code in PR #55546:
URL: https://github.com/apache/spark/pull/55546#discussion_r3140093824


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala:
##########
@@ -199,6 +199,20 @@ case class DataSourceV2ScanRelation(
   }
 
   override def computeStats(): Statistics = {
+    // If the table reports catalog-level (pre-filter, pre-pruning) statistics via
+    // SupportsReportCatalogStatistics and those stats carry a row count, prefer them:
+    // they describe the full table independent of any pushdown and are typically the
+    // strongest stats available to CBO. Otherwise fall through to the scan's own
+    // post-pushdown statistics as before.
+    relation.table match {

Review Comment:
   Yeah I'm fine with that as well - we can omit the consumer path here whatsoever and make a separate change later if needed.



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56626][SQL] Introduce SupportsReportCatalogStatistics mixin for Table [spark]

Posted by "yyanyy (via GitHub)" <gi...@apache.org>.
yyanyy commented on code in PR #55546:
URL: https://github.com/apache/spark/pull/55546#discussion_r3139925343


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala:
##########
@@ -199,6 +199,20 @@ case class DataSourceV2ScanRelation(
   }
 
   override def computeStats(): Statistics = {
+    // If the table reports catalog-level (pre-filter, pre-pruning) statistics via
+    // SupportsReportCatalogStatistics and those stats carry a row count, prefer them:
+    // they describe the full table independent of any pushdown and are typically the
+    // strongest stats available to CBO. Otherwise fall through to the scan's own
+    // post-pushdown statistics as before.
+    relation.table match {

Review Comment:
   I’m a bit worried that this could unintentionally change behavior for new connectors implementing this interface without realizing the implications. Today, DSv2 stats are generally expected to reflect pushed-down filters, given the existing behavior of e.g. fully pushed-down filters are removed from the plan. With this change, any stats updates done in the scan implementation by connectors using the new interface would effectively be ignored.
   
   Long term, I think the optimizer estimation rule should distinguish between catalog stats and stats that incorporate pushdown, so connectors supporting either/both model can be handled correctly and the most accurate stats can be used. Without that optimizer improvement, though, this behavior change feels non-obvious and potentially confusing. What do you think?



-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Re: [PR] [SPARK-56626][SQL] Introduce SupportsReportCatalogStatistics mixin for Table [spark]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] closed pull request #55546: [SPARK-56626][SQL] Introduce SupportsReportCatalogStatistics mixin for Table
URL: https://github.com/apache/spark/pull/55546


-- 
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: reviews-unsubscribe@spark.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org