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

   ### What changes were proposed in this pull request?
   
   This PR fixes a correctness issue where `Dataset.unpivot` returns `-1` 
(whole-stage codegen) or `0` (interpreted) instead of `NULL` when a value 
column is resolved as non-nullable and an outer join makes it nullable later.
   
   `Expand` stores its output attributes, and `UnpivotTransformer` derives 
their nullability from the projections when the Resolution batch builds it. For 
the Dataset API, `right("b")` refers to the non-nullable attribute of `right`, 
and `UpdateAttributeNullability` later updates the references in the 
projections to be nullable after the outer join. However, it does not update 
the new output attributes of `Expand` because no child produces their 
expression IDs. As a result, the parent operators read the `NULL` slot as a 
non-nullable value.
   
   This PR makes `UpdateAttributeNullability` widen the nullability of the 
`Expand` output attributes from its projections, keeping their expression IDs, 
in the same way as it does for `MergeRows`.
   
   ### Why are the changes needed?
   
   To fix wrong results. This is a long-standing issue, reproduced with Apache 
Spark 3.5.9 and 4.3.0.
   
   ```scala
   val left = Seq(1, 2).toDF("a")
   val right = Seq(2).toDF("b")
   val unpivoted = left.join(right, left("a") === right("b"), "left_outer")
     .unpivot(Array(left("a")), Array(right("b")), "c", "v")
   
   // Issue 1: It should return [1,b,null] [2,b,2], but returns [1,b,-1] [2,b,2]
   unpivoted.orderBy("a").show()
   
   // Issue 2: It should return [1,b,null], but returns nothing
   unpivoted.where($"v".isNull).show()
   ```
   
   With `spark.sql.codegen.wholeStage=false`, Issue 1 returns `0` instead of 
`-1`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The above queries return `NULL` correctly, and the value column is 
reported as nullable in the schema.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case in `DatasetUnpivotSuite`.
   
   ### 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