peter-toth commented on code in PR #60009:
URL: https://github.com/apache/spark/pull/60009#discussion_r4527047029
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/DataSourceScanExec.scala:
##########
@@ -1034,7 +1044,10 @@ case class FileSourceScanExec(
None,
disableBucketedScan,
markedForSingleTaskExecution,
- QueryPlan.normalizePredicates(storageFilters, output))
+ QueryPlan.normalizePredicates(storageFilters, output),
+ // Carry the frozen bucketing decision into the canonical form instead
of re-reading the live
+ // config, so canonicalization preserves the partitioning the scan was
planned with.
+ bucketingEnabled)
Review Comment:
**Finding 1.** Before this change the canonical form had no bucketing flag.
Two scans of the same table, built before and after a change of
`spark.sql.sources.bucketing.enabled`, were `sameResult`. Now they are not,
also for tables without a bucket spec, where the flag changes nothing. That
breaks DPP in the scenario this PR targets:
1. The DPP subquery's plan is built at planning and keeps `true`.
2. When AQE re-optimizes after an earlier stage, it re-plans the broadcast
side that is not materialized yet. `FileSourceStrategy` then freezes the live
`false`.
3. `PlanAdaptiveDynamicPruningFilters.reusableBroadcast` finds no
`candidatePlan.sameResult(exchange)`.
4. With the default `reuseBroadcastOnly`, the filter becomes
`DynamicPruningExpression(true)`.
I ran this query with AQE on, planned with bucketing on and collected with
it off. `fact` is partitioned by `p` into 4 partitions, and nothing is bucketed:
```sql
SELECT /*+ BROADCAST(d) */ f.id, f.p
FROM fact f JOIN (
SELECT /*+ BROADCAST(b) */ a.p FROM dimA a JOIN dimB b ON a.x = b.x WHERE
b.f = 'x') d
ON f.p = d.p
```
| | DPP filter | `fact` files read |
|---|---|---|
| master | kept | 2 |
| this PR | `true` | 8 |
| this PR, flag normalized as below | kept | 2 |
Planned and run with bucketing on, all three keep DPP and read 2 files. With
the normalization, `BucketedReadWithoutHiveSupportSuite` (including the new
test), `SameResultSuite` and `CoalesceBucketsInJoinSuite` pass.
For a bucketed relation the flag still differs, as `disableBucketedScan`
already does. Likewise, the `keyGroupedPartitioning` that SPARK-60260 freezes
only changes the canonical form of V2 scans that report a key.
```suggestion
bucketingEnabled && relation.bucketSpec.isDefined)
```
A test along the lines of the query above would pin it.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/DataSourceScanExec.scala:
##########
@@ -450,7 +456,7 @@ trait FileSourceScanLike extends DataSourceScanExec with
SessionStateHelper {
// exposed for testing
lazy val bucketedScan: Boolean = {
- if (getSqlConf(relation.sparkSession).bucketingEnabled &&
relation.bucketSpec.isDefined
+ if (bucketingEnabled && relation.bucketSpec.isDefined
Review Comment:
**Finding 2.** `bucketedScan` now follows the frozen flag. But
`DisableUnnecessaryBucketedScan.apply`
(`sql/core/src/main/scala/org/apache/spark/sql/execution/bucketing/DisableUnnecessaryBucketedScan.scala:149`)
still returns early on the live `!conf.bucketingEnabled`. So when a scan is
built with bucketing on and the plan is prepared with it off, the scan stays
bucketed and the rule never looks at it.
To reproduce:
- a table bucketed into 8, the query `SELECT * FROM t WHERE id > 3`,
`autoBucketedScan` on;
- force `df.queryExecution.sparkPlan` with bucketing on;
- collect with bucketing off.
| | AQE on | AQE off |
|---|---|---|
| master | not bucketed, 2 partitions | not bucketed, 2 partitions |
| this PR | bucketed, 8 partitions | bucketed, 8 partitions |
| this PR, check dropped | disabled by the rule, 2 partitions | disabled by
the rule, 2 partitions |
The window is narrow, since building and preparing the plan have to straddle
the change. But the fix is one condition, because `hasBucketedScan` already
reads each scan's own flag. With the change below,
`BucketedReadWithoutHiveSupportSuite`, both
`DisableUnnecessaryBucketedScanWithoutHiveSupportSuite` variants and
`CoalesceBucketsInJoinSuite` pass.
```scala
if (!conf.autoBucketedScanEnabled || !hasBucketedScan) {
```
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/DataSourceScanExec.scala:
##########
@@ -755,7 +763,8 @@ case class FileSourceScanExec(
override val tableIdentifier: Option[TableIdentifier],
override val disableBucketedScan: Boolean = false,
override val markedForSingleTaskExecution: Boolean = false,
- override val storageFilters: Seq[Expression] = Nil)
+ override val storageFilters: Seq[Expression] = Nil,
+ override val bucketingEnabled: Boolean = SQLConf.get.bucketingEnabled)
Review Comment:
**Finding 3.** Two things about this default:
- **Capture point.** `FileSourceStrategy`
(`sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileSourceStrategy.scala:380`)
does not pass the value. So the freeze relies on the default being evaluated
while the planning session is active. Any other construction that leaves the
argument out reads the live config again. `doCanonicalize` only avoids that
because it passes the value by hand. SPARK-60260 decides the V2 counterpart in
`DataSourceV2Strategy` and passes it in. Doing the same here (`bucketingEnabled
= conf.bucketingEnabled` in `FileSourceStrategy`) and dropping the default
makes the decision visible and impossible to omit.
- **Source.** Before, `bucketedScan` read
`getSqlConf(relation.sparkSession)`. `SQLConf.get` is the planning session's
conf, and the two differ when the relation belongs to another session. I
created a Dataset-API global temp view over a table bucketed into 4, with
`autoBucketedScan` off:
- view created with bucketing off, queried from a session with it on:
master reads it unbucketed (2 partitions), this PR bucketed (4 partitions);
- settings swapped: the reverse.
Following the querying session matches what `EnsureRequirements` and
`DisableUnnecessaryBucketedScan` read, so it looks like the better choice. It
is still a behavior change, and the description should mention it.
--
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]