cloud-fan commented on code in PR #53530: URL: https://github.com/apache/spark/pull/53530#discussion_r2646496107
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveFetchCursor.scala: ########## @@ -0,0 +1,101 @@ +/* + * 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.catalyst.analysis + +import org.apache.spark.SparkException +import org.apache.spark.sql.AnalysisException +import org.apache.spark.sql.catalyst.expressions.VariableReference +import org.apache.spark.sql.catalyst.plans.logical.{FetchCursor, LogicalPlan, SingleStatement} +import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.trees.TreePattern.COMMAND +import org.apache.spark.sql.connector.catalog.CatalogManager +import org.apache.spark.sql.errors.QueryCompilationErrors.unresolvedVariableError + +/** + * Resolves the target SQL variables in FetchCursor command. + */ +class ResolveFetchCursor(val catalogManager: CatalogManager) extends Rule[LogicalPlan] + with ColumnResolutionHelper { + private val variableResolution = new VariableResolution(catalogManager.tempVariableManager) + + /** + * Checks for duplicate variable names and throws an exception if found. + * Names are normalized when the variables are created. + * No need for case insensitive comparison here. + */ + private def checkForDuplicateVariables(variables: Seq[VariableReference]): Unit = { + val dups = variables.groupBy(_.identifier).filter(kv => kv._2.length > 1) + if (dups.nonEmpty) { + throw new AnalysisException( + errorClass = "DUPLICATE_ASSIGNMENTS", + messageParameters = Map("nameList" -> + dups.keys.map(key => toSQLId(key.name())).mkString(", "))) + } + } + + override def apply(plan: LogicalPlan): LogicalPlan = plan.resolveOperatorsWithPruning( + _.containsPattern(COMMAND), ruleId) { + // Resolve FetchCursor wrapped in SingleStatement + case s @ SingleStatement(fetchCursor: FetchCursor) + if !fetchCursor.targetVariables.forall(_.resolved) => + val resolvedVars = fetchCursor.targetVariables.map { + case u: UnresolvedAttribute => + variableResolution.lookupVariable( + nameParts = u.nameParts + ) match { + case Some(variable) => variable.copy(canFold = false) + case _ => throw unresolvedVariableError(u.nameParts, Seq("SYSTEM", "SESSION")) + } + + case other => throw SparkException.internalError( + "Unexpected target variable expression in FetchCursor: " + other) + } + + s.copy(parsedPlan = fetchCursor.copy(targetVariables = resolvedVars)) + + // Check for duplicates after resolution + case s @ SingleStatement(fetchCursor: FetchCursor) + if fetchCursor.targetVariables.forall(_.resolved) => + val targetVariables = fetchCursor.targetVariables.map(_.asInstanceOf[VariableReference]) + checkForDuplicateVariables(targetVariables) Review Comment: can we do it right after we resolve the variables? why adding another case match? -- 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]
