dongjoon-hyun opened a new pull request, #60728:
URL: https://github.com/apache/spark/pull/60728
### What changes were proposed in this pull request?
This PR fixes a correctness issue where `Dataset.unpivot`, `rollup`, `cube`,
and `groupingSets` return a wrong value, such as `0`, instead of `NULL` for a
struct field, an array element, or a map value built from a column that an
outer join makes nullable.
`Expand` stores its output attributes, including their data types, when the
Resolution batch builds it for `UNPIVOT` or grouping sets. For the Dataset API,
`struct(right("b"))` is built from the non-nullable attribute of `right`, so
its field is non-nullable when `Expand` is built. `UpdateAttributeNullability`
later makes `right("b")` nullable after the outer join, and SPARK-62051
(#60695) widens the top-level nullability of the `Expand` output accordingly.
However, the nested nullability of the data types, such as the nullability of a
struct field, stays as it was, both in the `Expand` output and in the
references to it. As a result, the parent operators read a `NULL` field as a
non-nullable value.
This PR makes `UpdateAttributeNullability`:
- merge the nested nullability that the `Expand` projections gain into the
data types of the `Expand` output attributes. Only the projections whose data
types this rule changes are merged, so that `UNPIVOT` keeps taking the nested
nullability of the first value with
`spark.sql.legacy.unpivot.useFirstValueNestedNullability`. With the legacy
flag, the nested nullability that a later value gains this way is merged as
well, which only makes the value column more nullable.
- widen the nested nullability of a reference to a child output to that of
the child output when their data types differ only in nullability, in the same
way as it updates the top-level nullability. This is needed for grouping sets,
where both the `Expand` projections and the parent `Aggregate` refer to
attributes whose nested nullability becomes wider. It does not narrow the
nested nullability, since the references to the always nullable output of a
stateful streaming operator would then differ in data type from the references
in the operator, and `AttributeReference` compares data types for semantic
equality.
### 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
import org.apache.spark.sql.functions.{array, explode, struct}
val left = Seq((1, 10), (2, 20)).toDF("a", "x")
val right = Seq(2).toDF("b")
val joined = left.join(right, left("a") === right("b"), "left_outer")
// Issue 1: It should return [1,null,true] [2,2,false], but returns
[1,0,false] [2,2,false]
joined.unpivot(Array(left("a")), Array(struct(right("b")).as("s")), "c", "v")
.select($"a", $"v.b", $"v.b".isNull).orderBy("a").show()
// Issue 2: It should return [1,null,true] [2,2,false], but returns
[1,0,false] [2,2,false]
joined.unpivot(Array(left("a")), Array(array(right("b")).as("arr")), "c",
"v")
.select($"a", explode($"v").as("e")).select($"a", $"e",
$"e".isNull).orderBy("a").show()
// Issue 3: It should return [null,true,1] [2,false,1], but returns
[0,false,1] [2,false,1]
joined.groupingSets(Seq(Seq(struct(right("b")))), struct(right("b"))).count()
.toDF("s", "cnt").select($"s.b", $"s.b".isNull, $"cnt").orderBy("b").show()
```
`Dataset.rollup` and `Dataset.cube` with such a struct or array key return
wrong values in the same way.
### Does this PR introduce _any_ user-facing change?
Yes. The above queries return `NULL` correctly, and the struct field, array
element, or map value is reported as nullable in the schema.
As a result, an existing streaming query started before Spark 4.3 that keeps
such a struct, array, or map 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 cases:
- `DatasetUnpivotSuite`: unpivot of a struct value made nullable by an outer
join.
- `DataFrameAggregateSuite`: grouping sets of a struct made nullable by an
outer join.
In addition, the following existing tests fail with the alternatives:
- `DatasetUnpivotSuite`: `unpivot takes nested nullability of the first
value with the legacy flag` fails if the data types of all projections are
merged.
- `StreamingStatefulOperatorNullabilityDriftSuite`: `streaming aggregate
(Complete mode): no codegen NPE on state-restored null struct grouping key
after fix` fails with `PLAN_VALIDATION_FAILED_RULE_IN_BATCH` if the nested
nullability of the references is narrowed as well.
Lastly, I manually checked a streaming aggregation keyed by a struct value
of `unpivot` over a stream-static left outer join. Its checkpoint written by
Apache Spark 4.2.1 holds the wrong `[0]` key and restarts on Apache Spark
4.3.0, and fails to restart with `STATE_STORE_KEY_SCHEMA_NOT_COMPATIBLE` with
this PR. Its checkpoint written by Apache Spark 4.3.0 already has the `[null]`
key, and restarts with this PR.
### 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]