spark-test commented on code in PR #60582:
URL: https://github.com/apache/spark/pull/60582#discussion_r4594760742
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/Cast.scala:
##########
@@ -714,6 +716,24 @@ object Cast extends QueryErrorsBase {
def resolvableNullability(from: Boolean, to: Boolean): Boolean = !from || to
+ // A cast from VARIANT puts NULL in any nested slot whose JSON value is null
or missing, so, as
+ // `resolvableNullability` does for a nullable source slot, reject a target
with a non-nullable
+ // nested slot: the cast would otherwise hold NULLs its type says cannot
occur.
+ private def variantCastTargetRejected(to: DataType): Boolean =
+ hasNonNullableNestedSlot(to) &&
+
!SQLConf.get.getConf(SQLConf.LEGACY_VARIANT_CAST_ALLOW_NON_NULLABLE_TARGET)
+
+ // Whether the type has a non-nullable struct field, array element or map
value at any depth.
+ private def hasNonNullableNestedSlot(dt: DataType): Boolean = dt match {
+ case StructType(fields) =>
+ fields.exists(f => !f.nullable || hasNonNullableNestedSlot(f.dataType))
+ case ArrayType(elementType, containsNull) =>
+ !containsNull || hasNonNullableNestedSlot(elementType)
+ case MapType(_, valueType, valueContainsNull) =>
+ !valueContainsNull || hasNonNullableNestedSlot(valueType)
Review Comment:
**Blocking (P1):** This counts every non-nullable array element and map
value as a slot the cast can fill with NULL, whatever its type. That does not
hold when the element or value type is VARIANT: `VariantGet.cast` returns a
fresh `VariantVal` for a VARIANT target before its `Type.NULL` check, so a JSON
`null` element becomes a variant null rather than SQL NULL. The shredded
Parquet readers (`ArrayReader`, `MapReader`, `VariantReader`) likewise always
produce a `VariantVal` or raise `MALFORMED_VARIANT`.
With `canAnsiCast`, `canCast` and `canTryCast` as written, casts that return
correct results on the base branch now fail analysis in ANSI, LEGACY and TRY
modes. Two examples are `col("v").cast(ArrayType(VariantType, containsNull =
false))` over `parse_json('[1, null]')` and a cast to `MapType(StringType,
VariantType, valueContainsNull = false)`. The same holds when such a type is a
nullable struct field of the target, or when the VARIANT source is nested in a
struct. These types come from programmatic `DataType`s in Scala, Java, PySpark
and Spark Connect, including schemas Spark derives itself:
`array(parse_json('1'))` has non-nullable VARIANT elements. The error renders
the target as `ARRAY<VARIANT>`, the same text as the nullable type that still
works. The only way back is the legacy conf, which also restores the wrong
results this PR fixes.
Counting a non-nullable element or value only when its type is not VARIANT
keeps every rejection the fix needs, including a non-nullable VARIANT struct
field, which a missing key does leave NULL:
```suggestion
(!containsNull && elementType != VariantType) ||
hasNonNullableNestedSlot(elementType)
case MapType(_, valueType, valueContainsNull) =>
(!valueContainsNull && valueType != VariantType) ||
hasNonNullableNestedSlot(valueType)
```
The comments above `variantCastTargetRejected` and this helper, the conf doc
and the migration note need the same correction. Each of them says a JSON null
or a missing key puts NULL into any non-nullable element or value.
**Verification:**
- **Regression:** Casting parse_json('[1, null]') to an array of VARIANT
with non-nullable elements, and parse_json('{"a": null}') to a string-keyed map
of VARIANT with non-nullable values, passes analysis with ANSI on and off and
as a try cast, and returns the elements or value as non-null variants whose
JSON is 1 and null. The same holds when such an array is a nullable struct
field of the target or the element of a nullable outer array, and when the
VARIANT source is nested in a struct.
- **Behavior:** A cast from VARIANT to a struct with a non-nullable VARIANT
field, at the top level and inside an array element, and to an array whose
non-nullable elements are arrays of VARIANT, fails analysis in ANSI, LEGACY and
TRY modes with the same error as the non-nullable BIGINT and struct element and
value targets.
- **Inspection:** The comment above the check, the legacy conf's doc and the
Spark 4.4 migration note each state that a non-nullable array element or map
value of type VARIANT is accepted, and none claims that a JSON null or a
missing key puts NULL into such a slot.
--
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]