davidm-db commented on code in PR #52173:
URL: https://github.com/apache/spark/pull/52173#discussion_r2313348706


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -233,6 +235,71 @@ trait ColumnResolutionHelper extends Logging with 
DataTypeErrorsBase {
     }
   }
 
+  /**
+   * Look up variable by nameParts.
+   * If in SQL Script, first check local variables, unless in EXECUTE IMMEDIATE
+   * (EXECUTE IMMEDIATE generated query cannot access local variables).
+   * if not found fall back to session variables.
+   * @param nameParts NameParts of the variable.
+   * @return Reference to the variable.
+   */
+  def lookupVariable(nameParts: Seq[String]): Option[VariableReference] = {
+
+    // The temp variables live in `SYSTEM.SESSION`, and the name can be 
qualified or not.
+    def maybeTempVariableName(nameParts: Seq[String]): Boolean = {
+      nameParts.length == 1 || {
+        if (nameParts.length == 2) {
+          nameParts.head.equalsIgnoreCase(CatalogManager.SESSION_NAMESPACE)
+        } else if (nameParts.length == 3) {
+          nameParts(0).equalsIgnoreCase(CatalogManager.SYSTEM_CATALOG_NAME) &&
+            nameParts(1).equalsIgnoreCase(CatalogManager.SESSION_NAMESPACE)
+        } else {
+          false
+        }
+      }
+    }
+
+    val namePartsCaseAdjusted = if (conf.caseSensitiveAnalysis) {
+      nameParts
+    } else {
+      nameParts.map(_.toLowerCase(Locale.ROOT))
+    }
+
+    SqlScriptingContextManager.get().map(_.getVariableManager)
+      // In EXECUTE IMMEDIATE context, only allow session variables 
(system.session.X),
+      // not local variables
+      .filterNot { _ =>
+        AnalysisContext.get.isExecuteImmediate && 
!maybeTempVariableName(nameParts)

Review Comment:
   Also, don't get the addition of `!maybeTempVariableName(nameParts)` here - 
this case already exists in the last `orElse` below. Here, the goal is to drop 
scripting variable manager (according to the comment) if we are in Execute 
Immediate. For this, the first condition is sufficient.



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