stevomitric opened a new pull request, #58606:
URL: https://github.com/apache/spark/pull/58606

   ### What changes were proposed in this pull request?
   
   A temporary variable read only inside a view's `IDENTIFIER` clause is now 
recorded as a variable the view refers to, so it stays resolvable when the 
stored view text is analyzed again.
   
   `ResolveIdentifierClause` replaces the placeholder with the plan built from 
the *evaluated* name, which drops the identifier expression. The 
`VariableReference` is therefore absent from the analyzed plan, and 
`ViewHelper.collectTemporaryVariables`
   - the only source for the view's `view.referredTempVariablesNames` property 
- cannot see it. On re-analysis `ColumnResolutionHelper.resolveVariables` 
restricts variable lookup to that property whenever 
`AnalysisContext.catalogAndNamespace` is non-empty, which is always true inside 
a view body, so the variable resolves to nothing and the identifier expression 
is reported as not constant.
   
   The names are now recorded while they are still visible, in a new 
`AnalysisContext.referredTempVariableNamesUnderIdentifier` accumulator, and 
persisted by `CreateViewCommand` and `AlterViewAsCommand` via `markAsAnalyzed`. 
The accumulator is reset when entering a nested view or SQL function body so an 
inner object's variables are not attributed to the object being created.
   
   This also fixes `collectTemporaryVariablesInExpressionTree`, which had an 
extra `children` hop and so skipped the expression's own root node.
   
   ### Why are the changes needed?
   
   The view is unusable after creation:
   
   ```sql
   CREATE OR REPLACE TEMPORARY VIEW t AS SELECT 1 AS c1;
   DECLARE OR REPLACE VARIABLE relation_name STRING DEFAULT 't';
   CREATE OR REPLACE TEMPORARY VIEW v AS SELECT * FROM 
IDENTIFIER(relation_name);
   SELECT * FROM v;
   ```
   
   fails with
   
   ```
   [NOT_A_CONSTANT_STRING.NOT_CONSTANT] The expression relation_name used for 
the
   routine or clause IDENTIFIER must be a constant STRING which is NOT NULL.
   ```
   
   `CREATE VIEW` succeeds, so the failure only shows up on every later 
reference.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The query above now returns the rows of `t` instead of failing. A 
view's stored properties may now list a referred temporary variable that was 
previously omitted.
   
   ### How was this patch tested?
   
   New case in `identifier-clause.sql`. It fails on master with 
`NOT_A_CONSTANT_STRING.NOT_CONSTANT` and passes with this change.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   Co-authored-by: Claude Opus 4.8


-- 
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]

Reply via email to