Skip to content

[SPARK-59678][SQL] Fix pivot failing on array and struct columns with non-nullable nested fields - #58934

Open
jiwen624 wants to merge 2 commits into
apache:masterfrom
jiwen624:SPARK-59678
Open

jiwen624 wants to merge 2 commits into
apache:masterfrom
jiwen624:SPARK-59678

Conversation

@jiwen624

@jiwen624 jiwen624 commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

What changes were proposed in this pull request?

PivotTransformer checks and casts each pivot value against pivotColumn.dataType. This PR ignores nullability in that match: the check strips nullability from the value's type before comparing it with the pivot column's type, and values are evaluated against pivotColumn.dataType.asNullable. The If filters for non-PivotFirst aggregates fall back to asNullable only when the exact type can't be cast to, so existing plans are unchanged.

Why are the changes needed?

A pivot value fails analysis when its type allows nulls in a nested position where the pivot column's type does not, with an error that prints the same type on both sides:

 SELECT * FROM (SELECT named_struct('x', id) AS c, id AS v FROM range(2))
 PIVOT (sum(v) FOR c IN (named_struct('x', if(true, 1L, null))))
 -- [PIVOT_VALUE_DATA_TYPE_MISMATCH] Invalid pivot value '...': value data type
 -- struct<x:bigint> does not match pivot column data type struct<x:bigint>

The same happens with explicit DataFrame values, e.g. pivot($"a", Seq(Array(1.0d))) on a non-nullable-element array. Nullability is irrelevant here, as the value is a constant only compared against the column. (pivot() without values is covered by SPARK-59684.)

Note that for a non-nullable nested column, a value that can't be cast now fails with CAST_INVALID_INPUT (ANSI) or becomes null (non-ANSI), the same as the scalar and nullable cases, instead of PIVOT_VALUE_DATA_TYPE_MISMATCH.

Does this PR introduce any user-facing change?

Yes. Queries that failed with PIVOT_VALUE_DATA_TYPE_MISMATCH only because the value's type allows nulls in a nested position where the pivot column's type does not now run. Queries that already passed analysis are unaffected, and other rejected values keep the same error.

How was this patch tested?

Added UT cases.

Was this patch authored or co-authored using generative AI tooling?

Yes

@jiwen624 jiwen624 changed the title [SPARK-59678][SQL] Ignore nullability when matching pivot values to the pivot column [SPARK-59678][SQL] Fix pivot() failing on an array column with non-nullable elements Sep 20, 2026
@jiwen624 jiwen624 changed the title [SPARK-59678][SQL] Fix pivot() failing on an array column with non-nullable elements [SPARK-59678][SQL] Fix pivot failing on array and struct columns with non-nullable nested fields Sep 23, 2026
@jiwen624
jiwen624 marked this pull request as ready for review September 24, 2026 21:13
@jiwen624

Copy link
Copy Markdown
Contributor Author

Hi @uros-b @cloud-fan since you've reviewed pivot-related fixes recently, could you let me know what do you think about this fix when you get a chance? Thanks! 🙇

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant