cloud-fan commented on code in PR #49445:
URL: https://github.com/apache/spark/pull/49445#discussion_r1926988202


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/Analyzer.scala:
##########
@@ -139,6 +139,9 @@ object FakeV2SessionCatalog extends TableCatalog with 
FunctionCatalog with Suppo
  *                              even if a temp view `t` has been created.
  * @param outerPlan The query plan from the outer query that can be used to 
resolve star
  *                  expressions in a subquery.
+ * @param isExecuteImmediate Whether the current plan is created by EXECUTE 
IMMEDIATE. Used when
+ *                           resolving variables, as SQL Scripting local 
variables should not be
+ *                           visible from EXECUTE IMMEDIATE.

Review Comment:
   `AnalysisContext` is a singleton instance and it's very hacky and fragile to 
update a global bool flag to implement this fix. I think the key problem here 
is still passing around state, and I have a new idea:
   
   In `SubstituteExecuteImmediate`, after we parse the query body, we tag all 
`UnresolvedAttribute`s within the query to indicate that they are inside 
EXECUTE IMMEDIATE and they should not resolved to local variables. Something 
like this
   ```
   val EXEC_IMMEDIATE_TAG = new TreeNodeTag[Unit]("inside_execute_immediate")
   
   def tagVariables(plan: LogicalPlan): Unit = {
     plan.expressions.foreach(_.foreach {
       case u: UnresolvedAttribute =>
         u.setTagValue(PARAM_QUERY_TAG, ())
     })
     plan.subqueries.foreach(tagVariables)
     plan.children.foreach(tagVariables)
   }
   
   val executeImmediateQuery = ...
   tagVariables(executeImmediateQuery)
   ```
   
   In the places that match `UnresolvedAttribute` and try to look up variables, 
we skip local variables if the tag is present.
   
   In the new single-pass analyzer, we can have a better way to pass around 
states: when we top-down transverse the plan tree and see 
`ExecuteImmediateQuery`, we set a flag in the scope to indicate it's under 
`ExecuteImmediateQuery` and keep traversing.
   
   cc @vladimirg-db 



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to