dongjoon-hyun commented on code in PR #60695:
URL: https://github.com/apache/spark/pull/60695#discussion_r4624640208


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/UpdateAttributeNullability.scala:
##########
@@ -54,6 +54,7 @@ object UpdateAttributeNullability extends Rule[LogicalPlan] {
       }
       newPlan match {
         case m: MergeRows => widenOutputNullability(m)
+        case e: Expand => widenOutputNullability(e)

Review Comment:
   Thank you. I confirmed it with your INSERT and an insert-only MERGE from the 
same VALUES list. Both store the rows on master and fail with `INTERNAL_ERROR` 
with the previous commit.
   
   As you suggested, I fixed it in the producer. 
`EvaluateUnresolvedInlineTable.toExpand` now takes the output nullability from 
the projections, so the write resolution adds an `AssertNotNull` that passes, 
and the `Expand` case of this rule changes nothing for such an `Expand`. I 
added a test case to `DataSourceV2SQLSuite`, which fails with `INTERNAL_ERROR` 
without the change of `toExpand`.
   
   A `SELECT` over the same VALUES list now reports the column as nullable and 
returns both rows. A VALUES list whose casts are not nullable, such as `VALUES 
(1, rand()), (2L, rand())`, keeps the non-nullable column as before.
   



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/UpdateAttributeNullability.scala:
##########
@@ -69,4 +70,18 @@ object UpdateAttributeNullability extends Rule[LogicalPlan] {
     }
     if (newOutput == m.output) m else m.copy(output = newOutput)
   }
+
+  // Expand derives the nullability of its new output attributes from its 
projections when it is
+  // built, for instance, for grouping sets or UNPIVOT. The projections may 
become nullable later
+  // when this rule updates the attributes they reference, e.g., a column of a 
Dataset that an
+  // outer join makes nullable, so widen the nullability of the output 
attributes accordingly,
+  // keeping their expression IDs.
+  private def widenOutputNullability(e: Expand): Expand = {
+    val projections = e.projections.map(_.toIndexedSeq)
+    val newOutput = e.output.zipWithIndex.map { case (attr, index) =>
+      attr.withNullability(

Review Comment:
   Thank you. I confirmed that a struct or array value of `unpivot`, and a 
struct key of `groupingSets`, over such a column still return wrong results. 
However, they return the same results on master, so this is an existing issue 
rather than a regression of this PR.
   
   Since the recommended change makes `UpdateAttributeNullability` take the 
data types of the child outputs for the references in every operator, not only 
around `Expand`, I'd like to handle the nested nullability separately and keep 
this PR to the top-level nullability.
   



##########
sql/core/src/test/scala/org/apache/spark/sql/DatasetUnpivotSuite.scala:
##########
@@ -780,6 +780,16 @@ class DatasetUnpivotSuite extends SharedSparkSession {
     }
   }
 
+  test("SPARK-62051: unpivot of a column made nullable by an outer join") {
+    val left = Seq(1, 2).toDF("a")
+    val right = Seq(2).toDF("b")
+    // `right("b")` is resolved as non-nullable, and becomes nullable after 
resolution
+    checkAnswer(
+      left.join(right, left("a") === right("b"), "left_outer")
+        .unpivot(Array(left("a")), Array(right("b")), "col", "value"),

Review Comment:
   Thank you. I updated the test to unpivot two values, where only the second 
one comes from the null-supplying side. I confirmed that it fails when the 
widening uses `forall` or only the first projection.
   



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