[SPARK-56945][SQL] Fix stale nullability in CTE references containing SQL UDFs#55986
Open
mikhailnik-db wants to merge 1 commit into
Open
[SPARK-56945][SQL] Fix stale nullability in CTE references containing SQL UDFs#55986mikhailnik-db wants to merge 1 commit into
mikhailnik-db wants to merge 1 commit into
Conversation
uros-db
reviewed
May 20, 2026
uros-db
reviewed
May 20, 2026
uros-db
reviewed
May 20, 2026
uros-db
approved these changes
May 20, 2026
Contributor
uros-db
left a comment
There was a problem hiding this comment.
Fix itself lgtm, but left a few comments.
c7f70eb to
6bbdde6
Compare
6bbdde6 to
a6ac515
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
Skip the non-recursive
CTERelationRefschema snapshot inResolveWithCTEwhile the matchingCTERelationDefstill contains an unresolvedSQLFunctionExpression. A subsequent fixed-point iteration retries the substitution onceResolveSQLFunctionshas inlined the UDF body.Why are the changes needed?
SQLFunctionExpressionhard-codesnullable = truebut isresolvedas soon as its inputs resolve.CTERelationRef.outputis avalsnapshot ofcteDef.output, so capturing it before the UDF inlines freezesnullable = true.For nested UDF calls like
wrap_int(non_null_one()), the outer placeholder survives one analyzer iteration (ResolveSQLFunctionsskips UDFs whose inputs themselves contain aSQLFunctionExpression).ResolveWithCTE, which runs later in the same batch, snapshots the still-incorrect output, and the!ref.resolvedgate prevents a fix-up on the next iteration.Single-level UDF cases inline fully in iter 1 and avoid the bug. Recursive CTEs are unaffected: they already force
withNullability(true)by design.Does this PR introduce any user-facing change?
Yes. CTE columns wrapping nested non-nullable SQL UDFs now report
nullable = falseinstead ofnullable = true. Row-level results are unchanged.Before:
After:
How was this patch tested?
New regression test in
SQLFunctionSuite(SPARK-56945: CTE preserves non-nullable SQL UDF body in materialized schema). Fails on master withx: integer (nullable = true), passes with this PR. No SQL UDF golden file diffs.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude (Anthropic, Claude Code, Opus 4.7)