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

   ### What changes were proposed in this pull request?
   
   This PR fixes a correctness issue where a generator such as `explode` 
returns `-1` (whole-stage codegen) or `0` (interpreted) instead of `NULL` when 
its input is computed from a column that is resolved as non-nullable and that 
an outer join makes nullable later.
   
   `Generate` stores the output attributes of its generator, and 
`ResolveGenerate` derives their nullability from the element schema of the 
generator when it builds the node. For the Dataset API, `right("b")` refers to 
the non-nullable attribute of `right`, and `UpdateAttributeNullability` later 
updates the reference in the generator input to be nullable after the outer 
join. However, it does not update the output attributes of `Generate` 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 
`Generate` output attributes, keeping their expression IDs, when the element 
schema of the generator becomes nullable by this rule, in the same way as it 
does for `Expand` and `MergeRows`. It compares the element schema before and 
after the rule updates the references, because some generators declare all 
fields nullable while their output attributes keep a narrower nullability, 
e.g., `ReplicateRows` built for `EXCEPT ALL` and `INTERSECT ALL`.
   
   ### 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 exploded = left.join(right, left("a") === right("b"), "left_outer")
     .select(left("a"), explode(array(right("b"))).as("e"))
   
   // Issue 1: It should return [1,null] [2,2], but returns [1,-1] [2,2]
   exploded.orderBy("a").show()
   
   // Issue 2: It should return [1,null], but returns nothing
   exploded.where($"e".isNull).show()
   ```
   
   With `spark.sql.codegen.wholeStage=false`, Issue 1 returns `0` instead of 
`-1`. The same holds for `posexplode` and `explode` of a map value, and for 
`inline` when `spark.sql.analyzer.failAmbiguousSelfJoin` is `false`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The above queries return `NULL` correctly, and the generator output 
column is reported as nullable in the schema.
   
   When such a column is written into a `NOT NULL` column of a table, 
`DataFrameWriter.insertInto` and `INSERT INTO` from a temporary view of the 
DataFrame fail with `NOT_NULL_ASSERT_VIOLATION` instead of writing `-1`. 
`DataFrameWriterV2.append` fails with `INTERNAL_ERROR` instead of writing `-1`, 
in the same way as it already does for a column that the outer join makes 
nullable without a generator, e.g., `right("b").as("e")`.
   
   In addition, an existing streaming query started before Spark 4.3 that keeps 
such a column in its state, for example as a grouping key of a streaming 
aggregation, fails to restart with `STATE_STORE_KEY_SCHEMA_NOT_COMPATIBLE`. 
This PR documents it with the recovery options in the Structured Streaming 
migration guide.
   
   ### How was this patch tested?
   
   Pass the CIs with the newly added test case in `GeneratorFunctionSuite`.
   
   In addition, I manually checked a streaming aggregation keyed by the output 
of `explode` over a stream-static left outer join. Its checkpoint written by 
Apache Spark 4.2.1 restarts on Apache Spark 4.3.0 with the wrong `-1` key, and 
fails to restart with `STATE_STORE_KEY_SCHEMA_NOT_COMPATIBLE` with this PR. Its 
checkpoint written by Apache Spark 4.3.0 restarts with this PR and puts the new 
`NULL` value under a separate `NULL` key.
   
   ### 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