cloud-fan commented on code in PR #49445: URL: https://github.com/apache/spark/pull/49445#discussion_r1929957118
########## sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingVariableManager.scala: ########## @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql.scripting + +import org.apache.spark.SparkException +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.catalog.{VariableDefinition, VariableManager} +import org.apache.spark.sql.catalyst.expressions.Literal +import org.apache.spark.sql.connector.catalog.Identifier +import org.apache.spark.sql.errors.DataTypeErrorsBase + import org.apache.spark.sql.errors.QueryCompilationErrors.unresolvedVariableError + +class SqlScriptingVariableManager(context: SqlScriptingExecutionContext) + extends VariableManager with DataTypeErrorsBase { + + override def create( + identifier: Identifier, + defaultValueSQL: String, + initValue: Literal, + overrideIfExists: Boolean): Unit = { + val name = identifier.name() + + // overrideIfExists should not be supported because local variables don't support + // DECLARE OR REPLACE. However ForStatementExec currently uses this to handle local vars, + // so we support it for now. + // TODO [SPARK-50785]: Refactor ForStatementExec to use local variables properly. + if (!overrideIfExists && context.currentScope.variables.contains(name)) { + throw new AnalysisException( + errorClass = "VARIABLE_ALREADY_EXISTS", + messageParameters = Map( + "variableName" -> toSQLId(Seq(context.currentScope.label, name)))) + } + context.currentScope.variables.put( + name, + VariableDefinition( + identifier, + defaultValueSQL, + initValue + )) + } + + override def set( + nameParts: Seq[String], + defaultValueSQL: String, + initValue: Literal, + identifier: Identifier): Unit = { + def varDef = VariableDefinition(identifier, defaultValueSQL, initValue) + nameParts match { + // Unqualified case. + case Seq(name) => + context.currentFrame.scopes + .findLast(_.variables.contains(name)) + // Throw error if variable is not found. This shouldn't happen as the check is already + // done in SetVariableExec. + .orElse(throw unresolvedVariableError(nameParts, identifier.namespace().toIndexedSeq)) + .map(_.variables.put(name, varDef)) + // Qualified case. + case Seq(label, name) => + context.currentFrame.scopes + .findLast(_.label == label) + .filter(_.variables.contains(name)) + // Throw error if variable is not found. This shouldn't happen as the check is already + // done in SetVariableExec. + .orElse(throw unresolvedVariableError(nameParts, identifier.namespace().toIndexedSeq)) + .map(_.variables.put(name, varDef)) + case _ => + throw SparkException.internalError("ScriptingVariableManager.set expects 1 or 2 nameParts.") Review Comment: can users hit this error? -- 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