cloud-fan commented on code in PR #60690:
URL: https://github.com/apache/spark/pull/60690#discussion_r4652054922


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnAliasing.scala:
##########
@@ -466,7 +526,7 @@ object GeneratorNestedColumnAliasing {
       // as a whole or partially by higher projection, pruning it here will 
cause unresolved
       // query plan.
       NestedColumnAliasing.rewritePlanIfSubsetFieldsUsed(
-        plan, g.generator.children, g.requiredChildOutput)
+        plan, g.generator.children, g.requiredChildOutput, 
NestedColumnAliasing.collectorBelow(g))

Review Comment:
   **Non-blocking (P2):** This collector now rejects a non-deterministic 
generator argument too, so for `SELECT c FROM t LATERAL VIEW 
explode(shuffle(s.arr)[0].inner) g AS c` only `s.arr` is aliased below the 
Generate instead of the whole argument. By reading the code, the scan then 
reads every field of the elements of `s.arr`. On the base branch the alias 
`shuffle(s.arr)[0].inner` let schema pruning read only `s.arr.inner`, because 
SelectedField passes through a GetArrayItem with a constant index and through 
Shuffle (SelectedField.scala:143 and 171-172).
   
   Excluding a generator argument corrects nothing: GenerateExec initializes 
the generator for each partition and evaluates it once per input row 
(GenerateExec.scala:81-83 and 104), in the same task and order as a Project 
below it, so the alias gave the same values. The extra scan grows with rows 
times the unread element fields, while results are unchanged. For an index such 
as `CAST(rand() * 10 AS INT)` the base-branch scan already read every element 
field, so today only shuffle() under a constant index loses pruning. The same 
holds for the arguments that the Project-over-Generate case collects together 
with its Project list (NestedColumnAliasing.scala:424).
   
   **Recommended change:** In GeneratorNestedColumnAliasing, collect the 
Generate's own arguments with a collector that also returns non-deterministic 
accesses, as the base branch did. That is the collector collectorBelow returns 
when spark.sql.optimizer.nestedColumnAliasing.excludeNondeterministic is false. 
Do this in the bare Generate case (NestedColumnAliasing.scala:528-529) and for 
the argument part of the Project-over-Generate case (lines 423-424). In the 
Project-over-Generate case, keep collectorBelow(g) for the Project list, and 
leave out any non-deterministic argument candidate that is canonically equal to 
a nested field access anywhere in the Project list, including one inside a 
larger expression, because getNewProjectList replaces every such access. Update 
the scaladoc of collectorBelow and the conf's doc to say that a generator's own 
arguments are aliased below the Generate whatever their determinism, because 
the Generate evaluates them once per input row. Add nested colum
 n aliasing optimizer tests and a schema pruning test for these cases. The 
argument sites took the exclusion when commit d89ee449972 changed the default 
collector, and commit 6fd36a544bb made it explicit with collectorBelow(g). 
Neither commit gives a reason specific to generator arguments.
   
   **Why this works:** An alias in the Project directly below a Generate is 
evaluated once per input row of the Generate, in the same partition and order 
in which the Generate evaluates its arguments. So aliasing a non-deterministic 
argument there returns the same values as evaluating it inside the Generate. It 
also puts the whole access back over the scan, where schema pruning narrows the 
read to the field the access uses. Leaving out a non-deterministic argument 
candidate that is canonically equal to any nested field access in the Project 
list, at any depth, keeps those accesses evaluated above the Generate, once per 
output row.
   
   **Compatibility:** With 
spark.sql.optimizer.nestedColumnAliasing.excludeNondeterministic false, both 
rules still produce the base branch's plans, and streaming queries restarted 
from a checkpoint without the conf keep their plans through the offset log. A 
bare Generate, and a Project over a Generate, output the same columns as at the 
reviewed head, so the state schema of a streaming query that started with this 
PR does not change.
   
   **Risks:** Plans go back to the base branch's for every non-deterministic 
generator argument, index-based ones included. Those plans return the same 
results, because the Generate evaluates its arguments once per input row, and 
setting the conf to false already produces them. In the Project-over-Generate 
case, an aliased argument that is canonically equal to a nested field access 
anywhere in the Project list, such as one inside `access + 1`, would also 
replace that access, because the Project list is rewritten at every depth, and 
the rows built from one input row would then share its value. Leaving such an 
argument unaliased, as the change does, prevents this.
   
   **Constraints:** A non-deterministic access in the Project list above a 
Generate stays evaluated above the Generate, once per output row, including one 
that is canonically equal to a generator argument, whether it is a whole 
Project-list expression or part of a larger one. Every other call site keeps 
collectorBelow, so the behavior at Join, Window, Sort, the limits, Sample, the 
repartitions, Aggregate and Expand, and the input-file exemption, stay as they 
are.
   
   **Exclusions:**
   - Carrying only the field a non-deterministic access reads above other 
operators, which the TODO in collectRootReferenceAndExtractValue defers, stays 
outside this repair.
   
   **Verification:**
   - **Regression:** An optimizer plan test shows that, with the conf at its 
default, a non-deterministic nested access used only as a generator argument, 
such as `shuffle(s.arr)[0].inner` inside explode, is aliased whole in the 
Project below the Generate. It checks this both when the rule handles the 
Generate alone and when a Project that reads all of the Generate's output sits 
above it. Today only `s.arr` is aliased there.
   - **Compatibility:** An optimizer plan test shows that a non-deterministic 
nested access in the Project list above a Generate that is canonically equal to 
one of the Generate's arguments, for example both indexed by `rand(0)`, is 
still evaluated above the Generate and is not replaced by the argument's alias, 
both when the access is a whole Project-list expression and when it is part of 
a larger one such as `access + 1`.
   - **Behavior:** Over a file table whose array elements have a field the 
access does not read, a query that explodes a field of `shuffle(s.arr)[0]` 
returns the same rows with nested column aliasing on and off, and its scan 
reads only the exploded field of the elements.
   - **Inspection:** The scaladoc of collectorBelow and the doc of 
spark.sql.optimizer.nestedColumnAliasing.excludeNondeterministic say that a 
generator's own arguments are aliased below the Generate whatever their 
determinism, and say why.



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