dongjoon-hyun opened a new pull request, #60418:
URL: https://github.com/apache/spark/pull/60418

   ### What changes were proposed in this pull request?
   
   This PR aims to fix `FileSourceScanExec` to keep its columnar decision on a 
copy of the node.
   
   `supportsColumnar` now records its decision in a `TreeNodeTag` and reads it 
back when the tag is present. Since `makeCopy` and the tree transforms carry 
tags over to a new node, a copy keeps the decision that the columnar 
transitions around it were planned against, instead of deciding again from the 
conf at the time it is first asked. This is the same mechanism `UnionExec` uses 
to keep its decisions across copies (SPARK-59122, #58419).
   
   ### Why are the changes needed?
   
   `FileSourceScanExec.supportsColumnar` is a `lazy val` that reads the live 
session conf (`spark.sql.codegen.wholeStage`, `spark.sql.codegen.maxFields`, 
and the format's `supportBatch`). Without AQE, `ReuseExchangeAndSubquery` runs 
after `ApplyColumnarRulesAndInsertTransitions`, and when it rewrites a subquery 
of a scan, the scan is rebuilt by `makeCopy`. For example,
   - the dynamic pruning subquery of a fact table scan reuses the broadcast 
exchange of the join when the build side comes first (`BuildLeft`).
   - the second of two identical scalar subqueries becomes a 
`ReusedSubqueryExec`.
   
   The copy has a fresh `lazy val`, so it decides when it is first asked, which 
is at execution if nothing reads it before. If the conf changed between 
planning and execution, the copy disagrees with the `ColumnarToRowExec` planned 
above it, and the query fails.
   
   Before SPARK-52484 (#51183), the constructor assertion of 
`ColumnarToRowExec` read `child.supportsColumnar` when 
`ReuseExchangeAndSubquery` rebuilt it, which fixed the decision of the copy 
during the preparation. Since 
[v4.1.0](https://github.com/apache/spark/releases/tag/v4.1.0) (2025-12-11), the 
copy decides on its own. `Dataset.explain()` hides the issue because the scan 
metadata (`Batched`) reads the decision.
   
   ```scala
   spark.conf.set("spark.sql.adaptive.enabled", "false")
   spark.range(100).selectExpr("id", "id % 10 AS 
part").write.partitionBy("part").saveAsTable("fact")
   spark.range(10).selectExpr("id AS part", "CAST(id AS STRING) AS 
name").write.saveAsTable("dim")
   val df = spark.sql("SELECT /*+ BROADCAST(d) */ f.id FROM dim d JOIN fact f 
ON d.part = f.part WHERE d.name = '3'")
   df.queryExecution.executedPlan
   spark.conf.set("spark.sql.codegen.wholeStage", "false")
   df.collect()
   ```
   
   - [v4.0.5](https://github.com/apache/spark/releases/tag/v4.0.5) 
(2026-10-21): `Array(3, 13, ..., 93)`
   - [v4.1.4](https://github.com/apache/spark/releases/tag/v4.1.4) (2026-10-21) 
and [v4.3.0](https://github.com/apache/spark/releases/tag/v4.3.0) (2026-10-27): 
`java.util.NoSuchElementException: key not found: scanTime`
   
   The same happens with `Dataset.debug()`. In the opposite direction (planned 
with `spark.sql.codegen.wholeStage=false`, executed with `true`), the task 
fails with `ClassCastException: ColumnarBatch cannot be cast to InternalRow`. 
In the `master` branch, `Dataset.cache()` followed by a conf change and an 
action hits it too, since SPARK-59024 (#58314) made the name of the cached RDD 
lazy, which used to read the string of the cached plan at `cache()`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes, the query above fails before this PR and returns the correct result 
after this PR.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   


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