dusantism-db commented on code in PR #49445: URL: https://github.com/apache/spark/pull/49445#discussion_r1944736691
########## sql/core/src/main/scala/org/apache/spark/sql/execution/command/v2/SetVariableExec.scala: ########## @@ -47,21 +50,46 @@ case class SetVariableExec(variables: Seq[VariableReference], query: SparkPlan) val row = values(0) variables.zipWithIndex.foreach { case (v, index) => val value = row.get(index, v.dataType) - createVariable(variableManager, v, value) + setVariable(v, value, sessionVariablesOnly) } } Seq.empty } - private def createVariable( - variableManager: TempVariableManager, + private def setVariable( variable: VariableReference, - value: Any): Unit = { - variableManager.create( - variable.identifier.name, + value: Any, + sessionVariablesOnly: Boolean): Unit = { + val namePartsCaseAdjusted = if (session.sessionState.conf.caseSensitiveAnalysis) { + variable.originalNameParts + } else { + variable.originalNameParts.map(_.toLowerCase(Locale.ROOT)) + } + + val tempVariableManager = session.sessionState.catalogManager.tempVariableManager + val scriptingVariableManager = SqlScriptingLocalVariableManager.get() + + val variableManager = scriptingVariableManager + .filterNot(_ => sessionVariablesOnly) + // If a local variable with nameParts exists, set it using scriptingVariableManager. + .filter(_.get(namePartsCaseAdjusted).isDefined) + // If a local variable with nameParts doesn't exist, check if a session variable exists + // with those nameParts and set it using tempVariableManager. + .orElse( + Option.when(tempVariableManager.get(namePartsCaseAdjusted).isDefined) { + tempVariableManager + } + // If neither local or session variable exists, throw error. This shouldn't happen as + // existence of this variable is already checked in ResolveSetVariable. + ).getOrElse( + throw unresolvedVariableError(namePartsCaseAdjusted, Seq("SYSTEM", "SESSION")) + ) + + variableManager.set( Review Comment: Yes, it's different in SqlScriptingLocalVariableManager. Especially so because local variables do not support overrideIfExists, this will be enforced when FOR statement is refactored to use proper local variables. ########## sql/core/src/main/scala/org/apache/spark/sql/execution/command/v2/SetVariableExec.scala: ########## @@ -47,21 +50,46 @@ case class SetVariableExec(variables: Seq[VariableReference], query: SparkPlan) val row = values(0) variables.zipWithIndex.foreach { case (v, index) => val value = row.get(index, v.dataType) - createVariable(variableManager, v, value) + setVariable(v, value, sessionVariablesOnly) } } Seq.empty } - private def createVariable( - variableManager: TempVariableManager, + private def setVariable( variable: VariableReference, - value: Any): Unit = { - variableManager.create( - variable.identifier.name, + value: Any, + sessionVariablesOnly: Boolean): Unit = { + val namePartsCaseAdjusted = if (session.sessionState.conf.caseSensitiveAnalysis) { + variable.originalNameParts + } else { + variable.originalNameParts.map(_.toLowerCase(Locale.ROOT)) + } + + val tempVariableManager = session.sessionState.catalogManager.tempVariableManager + val scriptingVariableManager = SqlScriptingLocalVariableManager.get() + + val variableManager = scriptingVariableManager + .filterNot(_ => sessionVariablesOnly) + // If a local variable with nameParts exists, set it using scriptingVariableManager. + .filter(_.get(namePartsCaseAdjusted).isDefined) + // If a local variable with nameParts doesn't exist, check if a session variable exists + // with those nameParts and set it using tempVariableManager. + .orElse( + Option.when(tempVariableManager.get(namePartsCaseAdjusted).isDefined) { + tempVariableManager + } + // If neither local or session variable exists, throw error. This shouldn't happen as + // existence of this variable is already checked in ResolveSetVariable. + ).getOrElse( + throw unresolvedVariableError(namePartsCaseAdjusted, Seq("SYSTEM", "SESSION")) + ) + + variableManager.set( Review Comment: Yes, it's different in `SqlScriptingLocalVariableManager`. Especially so because local variables do not support overrideIfExists, this will be enforced when FOR statement is refactored to use proper local variables. -- 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