dusantism-db commented on code in PR #49445:
URL: https://github.com/apache/spark/pull/49445#discussion_r1945743963


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveCatalogs.scala:
##########
@@ -34,11 +37,29 @@ class ResolveCatalogs(val catalogManager: CatalogManager)
   override def apply(plan: LogicalPlan): LogicalPlan = plan 
resolveOperatorsDown {
     // We only support temp variables for now and the system catalog is not 
properly implemented
     // yet. We need to resolve `UnresolvedIdentifier` for variable commands 
specially.
-    case c @ CreateVariable(UnresolvedIdentifier(nameParts, _), _, _) =>
-      val resolved = resolveVariableName(nameParts)
-      c.copy(name = resolved)
+    case c @ CreateVariable(UnresolvedIdentifier(nameParts, _), _, _, _) =>
+      // From scripts we can only create local variables, which must be 
unqualified,
+      // and must not be DECLARE OR REPLACE.
+      if (SqlScriptingLocalVariableManager.get().isDefined) {
+        // TODO [SPARK-50785]: Uncomment this when For Statement starts 
properly using local vars.
+//        if (c.replace) {
+//          throw new AnalysisException(
+//            "INVALID_VARIABLE_DECLARATION.REPLACE_LOCAL_VARIABLE",
+//            Map("varName" -> toSQLId(nameParts))
+//          )
+//        }
+
+        if (nameParts.length != 1) {

Review Comment:
   You're right, added check we're not in EXECUTE IMMEDIATE if throwing this 
error.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ColumnResolutionHelper.scala:
##########
@@ -266,22 +275,46 @@ trait ColumnResolutionHelper extends Logging with 
DataTypeErrorsBase {
       }
     }
 
-    if (maybeTempVariableName(nameParts)) {
-      val variableName = if (conf.caseSensitiveAnalysis) {
-        nameParts.last
-      } else {
-        nameParts.last.toLowerCase(Locale.ROOT)
-      }
-      catalogManager.tempVariableManager.get(variableName).map { varDef =>
+    val namePartsCaseAdjusted = if (conf.caseSensitiveAnalysis) {
+      nameParts
+    } else {
+      nameParts.map(_.toLowerCase(Locale.ROOT))
+    }
+
+    SqlScriptingLocalVariableManager.get()
+      // If sessionOnly is set to true lookup only session variables.
+      .filterNot(_ => AnalysisContext.get.isExecuteImmediate)
+      // If variable name is qualified with system.session.<varName> treat it 
as a session variable.
+      .filterNot(_ =>
+        nameParts.length == 3
+          && nameParts.take(2).map(_.toLowerCase(Locale.ROOT)) == 
Seq("system", "session"))
+      // If variable name is qualified with session.<varName> treat it as a 
session variable.
+      .filterNot(_ =>
+        nameParts.length == 2
+          && nameParts.head.toLowerCase(Locale.ROOT) == "session")
+      // Local variable must be in format <varName> or <label>.<varName>
+      .filter(_ => namePartsCaseAdjusted.nonEmpty && 
namePartsCaseAdjusted.length <= 2)
+      .flatMap(_.get(namePartsCaseAdjusted))
+      .map { varDef =>
         VariableReference(
           nameParts,
           FakeSystemCatalog,
-          Identifier.of(Array(CatalogManager.SESSION_NAMESPACE), variableName),
+          Identifier.of(Array(varDef.identifier.namespace().last), 
namePartsCaseAdjusted.last),
           varDef)
       }
-    } else {
-      None
-    }
+      .orElse(
+        Option.when(maybeTempVariableName(nameParts)) {

Review Comment:
   Fixed.



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