linhongliu-db opened a new pull request, #60582:
URL: https://github.com/apache/spark/pull/60582
### What changes were proposed in this pull request?
A cast from VARIANT to a type with a non-nullable nested slot, for example
`CAST(v AS STRUCT<user_id: BIGINT NOT NULL, country: STRING>)` (also
`v::STRUCT<...>`, `try_cast`, and
the same struct nested in an ARRAY or MAP), is accepted, and its result type
keeps `user_id NOT NULL`.
`VariantGet.cast` still puts NULL into that field when the JSON value is
`null` or the key is missing,
so everything downstream reads the NULL as 0.
`Cast.canAnsiCast` and `Cast.canCast` accept any VARIANT target that
`VariantGet.checkDataType`
accepts, which ignores nullability. This PR rejects a VARIANT target with a
non-nullable nested struct
field, array element or map value in both, as `resolvableNullability`
already does for a nullable
source slot (including a nested VARIANT, e.g. `ARRAY<VARIANT>` to `ARRAY<INT
NOT NULL>`). A new legacy
conf `spark.sql.legacy.variant.allowCastToNonNullableTarget` (default
`false`) restores the previous
behavior.
### Why are the changes needed?
Wrong results. With rows `{"user_id": 42}`, `{"user_id": null}` and `{}`:
```sql
SELECT event_id, p.user_id, p.user_id IS NULL
FROM (SELECT DISTINCT event_id, payload::STRUCT<user_id: BIGINT NOT NULL> AS
p FROM events)
-- returns (2, 0, false) and (3, 0, false) for the null and missing user_id
```
`IS NULL` is false, `coalesce` is skipped, and an `INSERT` into a `NOT NULL`
column stores 0.
`variant_get`, which types itself `targetType.asNullable`, returns the NULLs.
### Does this PR introduce _any_ user-facing change?
Yes, documented in `docs/sql-migration-guide.md`. Such casts now fail
analysis with
`DATATYPE_MISMATCH.CAST_WITHOUT_SUGGESTION`, even when the JSON values are
all present. Casting to the
nullable type, or using `variant_get`, keeps working. Set
`spark.sql.legacy.variant.allowCastToNonNullableTarget=true` to restore the
old behavior.
### How was this patch tested?
New `VariantSuite` tests: `CAST`, `TRY_CAST` and DataFrame casts to struct,
nested struct, array and
map targets with a non-nullable slot fail analysis with ANSI on and off;
nullable targets and
`variant_get` return the NULLs and keep a genuine 0; with the legacy conf
the NULLs are read as 0. The
first test fails without the fix. Locally the variant expression, cast (ANSI
on/off, TRY) and SQL
variant suites pass and scalastyle is clean on JDK 21.
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Isaac)
This pull request and its description were written by Isaac.
--
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]