[SPARK-59970][SQL] Reject a temporary variable read through an IDENTIFIER clause in a persistent SQL function #59226
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #59226"
general
weight: 0.35
stevomitric
What changes were proposed in this pull request?
CreateSQLFunctionCommandnow captures the temporary variables that the function body reads only inside IDENTIFIER clauses and treats them like any other temporary variable reference: a persistent function is rejected withINVALID_TEMP_OBJ_REFERENCE, and a temporary function records them in itsreferredTempVariableNamesproperty.The names are recorded by
ResolveIdentifierClauseinAnalysisContext.referredTempVariableNamesUnderIdentifier(SPARK-59323), but the command analyzed the body withAnalyzer.execute, which opens a freshAnalysisContextand discards the accumulator on exit. A newAnalyzer.executeWithReferredTempVariablesUnderIdentifierruns the same fixed-point analysis in a context it owns and returns the recorded names alongside the analyzed plan. It is the sibling ofexecuteAndCheckReferredTempVariablesUnderIdentifier, minuscheckAnalysis, because the function command inspects the analyzed body (return type inference, aggregate/window checks) before checking it. Both analysis sites (scalar and table function) use it, and the names are unioned intoverifyTemporaryObjectsNotExistsandgenerateFunctionProperties.Only the variables read by the function body itself are attributed to it. A SQL function that the body calls is expanded in a context of its own (a new
AnalysisContextflag,shareIdentifierVariablesWithNestedSQLFunctions, which the function-creation entry point turns off), so a variable that an existing function reads through an IDENTIFIER clause stays that function's dependency:CREATE FUNCTION g(x) RETURN f2(x)is not rejected forf2's variable.View bodies keep sharing the accumulator with the functions they expand, as SPARK-59323 designed.
spark.sql.legacy.allowSessionVariableInPersistedViewis honored for functions as it is for views: with it set, a persistent function may read the variable through an IDENTIFIER clause. As with persisted views, the dependency is not stored (a persistent function records no temporary variable names at all), so the function only resolves while the variable exists.Bodies without IDENTIFIER clauses are unaffected.
Why are the changes needed?
A persistent SQL function could capture a session-scoped dependency and then fail in another session:
A direct reference (
RETURN x + length(col)) was already rejected, becauseViewHelper.collectTemporaryVariablessees theVariableReferencein the analyzed plan. Under an IDENTIFIER clause the reference is gone by then: the placeholder is replaced by the expression or plan built from the evaluated name.SPARK-59323 (#58606) fixed the same gap for CREATE VIEW, ALTER VIEW, CACHE TABLE AS SELECT and metric views; its review noted the SQL function path as pre-existing and to be tracked separately.
Does this PR introduce _any_ user-facing change?
Yes.
CREATE FUNCTION(persistent) whose body reads a temporary variable only through an IDENTIFIER clause now fails withINVALID_TEMP_OBJ_REFERENCEat creation, matching a direct variable reference, instead of succeeding and failing later when the function is called from a session where the variable does not exist. Settingspark.sql.legacy.allowSessionVariableInPersistedViewrestores the previous behavior; a migration-guide entry is added under 4.3 to 4.4 (branch-4.3 predates the SPARK-59323 mechanism this builds on). Temporary functions are unchanged in behavior; their storedreferredTempVariableNamesnow also lists such variables.How was this patch tested?
New cases in
sql-udf.sql(regenerated throughSQLQueryTestSuite), in the existing "permanent functions with temp objects" section:both rejected with
INVALID_TEMP_OBJ_REFERENCEnaming the variable (both were accepted beforethis change);
FROM IDENTIFIER(v)), asession.-qualified variable name,and two variables in one body, all rejected;
with the flag off again, a function calling it is created and evaluates (the callee's variable
is not attributed to the caller).
SQLFunctionSuitegains two tests: the nested case (the callee is created under the legacy flag; the caller, created with the flag off, is accepted because the callee’s variable is not attributed to it; a direct IDENTIFIER read in the caller’s own body is rejected) and one that reads the stored function properties to pin that a temporary function records the variable inreferredTempVariableNameswhile a persistent function created under the legacy flag stores none.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude claude-opus-4-8