peter-toth commented on code in PR #60729:
URL: https://github.com/apache/spark/pull/60729#discussion_r4644780446


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/UpdateAttributeNullability.scala:
##########
@@ -110,4 +111,20 @@ object UpdateAttributeNullability extends 
Rule[LogicalPlan] {
     }
     if (newOutput == e.output) e else e.copy(output = newOutput)
   }
+
+  // Generate derives the nullability of its generator output attributes from 
the element schema
+  // of its generator when it is built. The element schema may become nullable 
later when this
+  // rule updates the attributes the generator references, e.g., a column of a 
Dataset that an
+  // outer join makes nullable, so widen the nullability of the generator 
output attributes
+  // accordingly, keeping their expression IDs. Only the fields that this rule 
makes nullable are
+  // considered, as some generators declare all fields nullable while their 
output attributes
+  // keep a narrower nullability, e.g., `ReplicateRows` built for EXCEPT ALL.
+  private def widenOutputNullability(g: Generate, oldGenerate: Generate): 
Generate = {
+    val fields = g.generator.elementSchema.fields
+    val oldFields = oldGenerate.generator.elementSchema.fields
+    val newOutput = g.generatorOutput.zipWithIndex.map { case (attr, index) =>
+      attr.withNullability(attr.nullable || (fields(index).nullable && 
!oldFields(index).nullable))

Review Comment:
   **Finding 1.** This pass can also make a field nested in the element schema 
nullable, and `generatorOutput` keeps the old data type then. So a struct field 
or an array element built from `right("b")` still reads `0`:
   
   ```scala
   left.join(right, left("a") === right("b"), "left_outer")
     .select(left("a"), explode(array(struct(right("b").as("x")))).as("e"))
     .select($"a", $"e.x")
   ```
   
   It returns `[1,0], [2,2]` on master and on this head, with and without 
whole-stage codegen. `explode(array(array(right("b"))))` followed by `e[0]` 
returns the same. Merging the changed element type into the output, as the 
`Expand` arm above does, fixes both:
   
   ```suggestion
         val (field, oldField) = (fields(index), oldFields(index))
         val newAttr = attr.withNullability(attr.nullable || (field.nullable && 
!oldField.nullable))
         if (field.dataType != oldField.dataType) {
           newAttr.withDataType(StructType.unionLikeMerge(attr.dataType, 
field.dataType))
         } else {
           newAttr
         }
   ```
   
   With it, the query returns `[1,null], [2,2]`, and `GeneratorFunctionSuite` 
and `DataFrameSetOperationsSuite` pass. The query could go into the new test as 
a second `checkAnswer`, and the migration guide entry would then name the 
nested case too.
   



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