cloud-fan commented on code in PR #52334: URL: https://github.com/apache/spark/pull/52334#discussion_r2425606639
########## sql/api/src/main/scala/org/apache/spark/sql/catalyst/parser/SubstituteParmsAstBuilder.scala: ########## @@ -0,0 +1,229 @@ +/* + * 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.parser + +import scala.collection.mutable + +import org.antlr.v4.runtime.tree.{ParseTree, RuleNode, TerminalNode} + +import org.apache.spark.sql.catalyst.parser.SqlBaseParser._ +import org.apache.spark.sql.catalyst.util.SparkParserUtils.withOrigin + +/** + * AST builder for extracting parameter markers and their locations from SQL parse trees. This + * builder traverses the parse tree and collects parameter information for substitution. + */ +class SubstituteParmsAstBuilder extends SqlBaseParserBaseVisitor[AnyRef] { + + private val namedParams = mutable.Set[String]() + private val positionalParams = mutable.ListBuffer[Int]() + private val namedParamLocations = mutable.Map[String, mutable.ListBuffer[ParameterLocation]]() + private val positionalParamLocations = mutable.ListBuffer[ParameterLocation]() + + /** + * Extract parameter location information from a parse context. This method traverses the parse + * tree and collects parameter locations for substitution. + */ + def extractParameterLocations(ctx: ParseTree): ParameterLocationInfo = { + // Clear previous state + namedParams.clear() + positionalParams.clear() + namedParamLocations.clear() + positionalParamLocations.clear() + + // Visit the context to collect parameters and their locations + visit(ctx) + + ParameterLocationInfo( + namedParamLocations.view.mapValues(_.toList).toMap, + positionalParamLocations.toList) + } + + /** + * Collect information about a named parameter in a literal context. Note: The return value is + * not used; this method operates via side effects. + */ + override def visitNamedParameterLiteral(ctx: NamedParameterLiteralContext): AnyRef = + withOrigin(ctx) { + val paramName = ctx.namedParameterMarker().identifier().getText + namedParams += paramName + + // Calculate the location of the entire parameter (including the colon) + val startIndex = ctx.getStart.getStartIndex + val stopIndex = ctx.getStop.getStopIndex + 1 + val locations = + namedParamLocations.getOrElseUpdate(paramName, mutable.ListBuffer[ParameterLocation]()) + locations += ParameterLocation(startIndex, stopIndex) + + null // Return value not used + } + + /** + * Collect information about a positional parameter in a literal context. Note: The return value + * is not used; this method operates via side effects. + */ + override def visitPosParameterLiteral(ctx: PosParameterLiteralContext): AnyRef = Review Comment: A high level question: for parameter markers that already work today (in the expression parser rule), do we need to handle it in the pre-parser? Or we leave it and still let it be bound during analysis time? -- 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]
