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


##########
sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnAliasingSuite.scala:
##########
@@ -969,6 +972,129 @@ class NestedColumnAliasingSuite extends SchemaPruningTest 
{
 
     comparePlans(optimized, expected)
   }
+
+  private val withArray = LocalRelation(
+    $"id".int,
+    $"s".struct(StructType.fromDDL("arr array<struct<x: int, y: int>>, e 
int")),
+    $"seq".array(IntegerType))
+
+  // `arr[index].x`.
+  private def elementX(arr: Expression, index: Expression): Expression =
+    GetStructField(GetArrayItem(arr, index), 0, Some("x"))
+
+  private val randomIndex = Cast(Multiply(Rand(Literal(0L)), Literal(1000.0)), 
IntegerType)
+
+  test("SPARK-62042: Do not alias non-deterministic nested field accesses 
below a Join") {
+    val other = LocalRelation($"rid".int)
+    // The second index mixes an input file function with rand(), so it is not 
exempt.

Review Comment:
   **Nit (P3):** Below a Join, collectorBelow turns the exemption off 
(NestedColumnAliasing.scala:232), so canAlias rejects this index on 
ev.deterministic alone (NestedColumnAliasing.scala:246) and 
isNondeterministicOnlyThroughInputFile never runs for it; the case reached that 
check only before 93b35e25580. The Generate test uses only the rand() index and 
the Repartition test only indexes built from an input file function alone, so 
if the check were changed to accept any access that contains an input file 
function, a mixed index above a Generate or a Sample with replacement would be 
aliased below the operator again, giving every row built from one input row the 
same rand() value, and every changed test would still pass. Running this index 
in the Generate test too, where the exemption applies and the expected plan is 
the same as for the rand() index, would give the check a failing test, and this 
comment would no longer need to say that the Join case checks the exemption.
   
   **Verification:**
   - **Regression:** Above an operator where the input-file exemption applies, 
such as a Generate, an access whose index adds rand() to 
input_file_block_start() has only its deterministic part aliased below the 
operator, and the test fails when isNondeterministicOnlyThroughInputFile is 
changed to accept any access that contains an input file function.



##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/SchemaPruningSuite.scala:
##########
@@ -811,6 +822,70 @@ abstract class SchemaPruningSuite
         Row(null, null) :: Nil)
   }
 
+  testSchemaPruning("SPARK-62042: non-deterministic nested field accesses give 
the same " +
+      "results with and without nested column aliasing") {
+    withDataSourceTable(elementRows, "elements") {
+      withDataSourceTable(joinKeys, "join_keys") {

Review Comment:
   **Nit (P3):** Each withDataSourceTable reads its file through readFile, 
which runs the body once with the vectorized reader off and twice with it on 
(FileBasedDataSourceTest.scala:50-62), and testSchemaPruning registers four 
variants that differ only in the vectorized key, which this inner readFile 
overrides, and in the contacts data, which the test does not read 
(SchemaPruningSuite.scala:1083-1104). The reader is chosen when each query is 
planned, so the outer loops reach no configuration the inner one does not, yet 
each suite runs the body, with its eight compared queries and the scan check, 
36 times and writes join_keys 12 times. Writing join_keys once with 
withTempPath and a plain read registered as a temp view, and registering the 
test with test, as the suite already does for other tests, would run the body 
once for each of the three reader configurations of the elements helper.
   
   **Verification:**
   - **Behavior:** The SPARK-62042 end-to-end comparison still runs its join, 
explode, sample and broadcast local-relation join queries with nested column 
aliasing off and on, and its scan check, under the vectorized reader off, on 
with nested vectorization, and on without it, running the body once for each of 
these configurations in each Parquet and ORC, V1 and V2 suite.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/optimizer/NestedColumnAliasing.scala:
##########
@@ -218,25 +223,69 @@ object NestedColumnAliasing {
     case _ => false
   }
 
-  private def canAlias(ev: Expression): Boolean = {
+  /**
+   * The collector of the candidates to alias below `child`. Below a [[Join]], 
it does not return
+   * an access whose only sources of non-determinism are input file functions: 
see
+   * [[isNondeterministicOnlyThroughInputFile]].
+   */
+  private def collectorBelow(child: LogicalPlan): (Expression) => 
Seq[Expression] = {
+    val exemptInputFile = !child.isInstanceOf[Join]
+    collectRootReferenceAndExtractValue(_, exemptInputFile)
+  }
+
+  private def canAlias(ev: Expression, exemptInputFile: Boolean): Boolean = {
     // we can not alias the attr from lambda variable whose expr id is not 
available
-    !ev.exists(_.isInstanceOf[NamedLambdaVariable]) && ev.references.size == 1
+    !ev.exists(_.isInstanceOf[NamedLambdaVariable]) && ev.references.size == 1 
&&
+      // An alias is evaluated once per row of the operator's input. Below an 
operator that
+      // repeats input rows, such as a Join, a Generate or a Sample with 
replacement, a
+      // non-deterministic access would then give every row built from one 
input row the same
+      // value. So a non-deterministic access is not aliased, unless 
`exemptInputFile` is true
+      // and its only sources of non-determinism are input file functions. For 
an access that is
+      // not aliased, collectRootReferenceAndExtractValue returns the 
extractions and columns
+      // below it that canAlias accepts, which the callers may alias instead.

Review Comment:
   **Nit (P3):** For an access canAlias rejects, the collector returns a bare 
column without calling canAlias (line 275), and getAttributeToExtractValues 
puts that column in otherRootReferences (line 307), so none of its nested 
fields is aliased (lines 310-314) and the operator carries the column whole, as 
it does for `a[CAST(rand(0) * 1000 AS INT)].b` over a top-level array column 
`a`. The comment tells a reader that such a column passed canAlias and may be 
aliased. Its description of the extractions is right and worth keeping: while 
the input-file exemption applies, an extraction returned for a rejected access 
can itself be input-file-only, such as `s.arr[CAST(input_file_block_start() AS 
INT)].inner` inside `s.arr[CAST(input_file_block_start() AS 
INT)].inner[CAST(rand() AS INT)].z`, so it should not be called deterministic. 
One possible wording:
   
   ```suggestion
         // and its only sources of non-determinism are input file functions. 
For an access that is
         // not aliased, collectRootReferenceAndExtractValue returns the 
extractions below it that
         // canAlias accepts, which the callers may alias instead, and any 
column it reaches, which
         // canAlias does not check and the callers do not alias, so the 
operator carries it whole.
   ```
   
   **Verification:**
   - **Inspection:** The comment in canAlias in NestedColumnAliasing.scala 
matches collectRootReferenceAndExtractValue and getAttributeToExtractValues: it 
no longer says that canAlias accepts the columns the fallback returns or that 
callers may alias them, it says that such a column is carried whole, and it 
does not call the returned extractions deterministic, since an input-file-only 
extraction such as `s.arr[CAST(input_file_block_start() AS INT)].inner` inside 
`s.arr[CAST(input_file_block_start() AS INT)].inner[CAST(rand() AS INT)].z` is 
still returned while the input-file exemption applies.



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