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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/execution/datasources/v2/DataSourceV2Relation.scala:
##########
@@ -215,6 +221,37 @@ case class DataSourceV2ScanRelation(
   // skip adding IsNotNull when the scan already implies it, or infer new 
filters across
   // joins), so plan stability testing is needed first.
 
+  /** Resolved table fields whose static Catalyst filters the source fully 
evaluates. */
+  lazy val fullyPushedFilterAttrs: ExpressionSet = relation.table match {
+    case table: SupportsCatalystFilterGuarantees =>
+      val refs = table.fullyPushedFilterAttributes().toImmutableArraySeq
+      val tableSchema = fromAttributes(relation.output)

Review Comment:
   Fixed in 7dc48f8234c. Validation now uses 
`relation.withMetadataColumns().output`, so both validation and the error 
schema include metadata columns that the query does not reference. Valid fields 
absent from the pruned scan output are then ignored. The capability 
documentation now explicitly allows metadata columns.
   
   Added coverage using metadata column `_j` for all eight combinations of 
metadata references, inference, and valid/unknown declarations. It checks query 
results, whether the metadata predicate remains in `FilterExec`, and the 
connector-specific error with the complete schema.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/internal/connector/SupportsCatalystFilterGuarantees.scala:
##########
@@ -0,0 +1,43 @@
+/*
+ * 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.internal.connector
+
+import org.apache.spark.sql.connector.catalog.Table
+import org.apache.spark.sql.connector.expressions.NamedReference
+
+/** A table that declares when its scans fully evaluate Catalyst predicates. */
+trait SupportsCatalystFilterGuarantees extends Table {
+
+  /**
+   * Returns attributes for which this table's scans fully evaluate Catalyst 
filters.
+   *
+   * For scans reporting inferred filters, Spark may omit static conjuncts 
over these attributes
+   * that are unchanged from returned residuals or adjustment predicates 
recorded during pushdown.
+   * Eligible conjuncts must be deterministic and contain no subqueries or 
Python UDFs. The source
+   * must fully enforce the query filters it receives over these attributes, 
including residuals
+   * returned to Spark, and reflect their enforcement in scan equality for 
scan reuse.

Review Comment:
   Fixed in 7dc48f8234c. Both docs now distinguish `Scan` equality for logical 
comparison from equality/hashCode of the `Batch` returned by `Scan.toBatch`, or 
the V1 relation returned by `toV1TableScan`, for physical reuse. The capability 
is now named `SupportsFullyPushedCatalystFilters`.
   
   The test scan/batch equality and hashCode now include its enforced filters, 
bound to source ordinals and canonicalized, alongside the read schema. Added a 
filtered/unfiltered aggregate `UNION ALL` regression with exchange reuse 
enabled and disabled, covering both returned-residual and fully-pushed modes. 
It checks results and asserts two distinct shuffle exchanges with no 
`ReusedExchangeExec`.



##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala:
##########
@@ -147,6 +147,9 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan] 
with PredicateHelper {
       // future use in constraint propagation, and are read by a Spark-side 
scan merge (see
       // TableCapability.SCAN_MERGING) to compare and re-enforce a scan's 
filters.
       val postScanFilterSet = ExpressionSet(postScanFiltersWithoutSubquery)
+      sHolder.postScanFilterExpressions = normalizedFiltersWithoutSubquery

Review Comment:
   Added the planner regression in 7dc48f8234c. The builder returns an 
unchanged `value > 0` residual and a derived `value < 10` residual, while 
reporting inferred `value >= 0`.
   
   The test runs the actual `V2ScanRelationPushDown` rule, verifies that only 
the unchanged residual is recorded among those two removal candidates, and 
checks that `FilterExec` evaluates only the derived residual. Recording the 
returned expressions verbatim would fail these assertions.



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