srielau commented on code in PR #52334:
URL: https://github.com/apache/spark/pull/52334#discussion_r2417993779


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/ParameterHandler.scala:
##########
@@ -0,0 +1,352 @@
+/*
+ * 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.util.{Failure, Success, Try}
+
+import org.apache.spark.sql.catalyst.expressions.{Expression, Literal}
+import org.apache.spark.sql.catalyst.parser.ParseException
+import org.apache.spark.sql.catalyst.util.LiteralToSqlConverter
+import org.apache.spark.sql.errors.QueryCompilationErrors
+
+/**
+ * Handler for parameter substitution across different Spark SQL contexts.
+ *
+ * This class consolidates the common parameter handling logic used by 
SparkSqlParser,
+ * SparkConnectPlanner, and ExecuteImmediate. It provides a single, consistent 
API
+ * for all parameter substitution operations in Spark SQL.
+ *
+ * Key features:
+ * - Automatic parameter type detection (named vs positional)
+ * - Uses CompoundOrSingleStatement parsing for all SQL constructs
+ * - Consistent error handling and validation
+ * - Support for complex data types (arrays, maps, nested structures)
+ * - Thread-safe operations with position-aware error context
+ *
+ * The handler integrates with the parser through callback mechanisms stored in
+ * CurrentOrigin to ensure error positions are correctly mapped back to the 
original SQL text.
+ *
+ * @example Basic usage:
+ * {{{
+ * val handler = new ParameterHandler()
+ * val context = NamedParameterContext(Map("param1" -> Literal(42)))
+ * val result = handler.substituteParameters("SELECT :param1", context)
+ * // result: "SELECT 42"
+ * }}}
+ *
+ * @example Optional context:
+ * {{{
+ * val handler = new ParameterHandler()
+ * val context = Some(NamedParameterContext(Map("param1" -> Literal(42))))
+ * val result = handler.substituteParametersIfNeeded("SELECT :param1", context)
+ * // result: "SELECT 42"
+ * }}}
+ *
+ * @see [[SubstituteParamsParser]] for the underlying parameter substitution 
logic
+ */
+class ParameterHandler {
+
+  // Compiled regex pattern for efficient parameter marker detection.
+  private val parameterMarkerPattern = java.util.regex.Pattern.compile("[?:]")
+
+
+  /**
+   * Helper method to perform parameter substitution and store position mapper.
+   *
+   * @param sqlText The SQL text containing parameter markers
+   * @param namedParams Optional named parameters map
+   * @param positionalParams Optional positional parameters list
+   * @return The SQL text with parameters substituted
+   */
+  private def performSubstitution(
+      sqlText: String,
+      namedParams: Map[String, String] = Map.empty,
+      positionalParams: List[String] = List.empty): String = {
+
+    // Quick pre-check: if there are no parameter markers in the text, skip 
parsing entirely.
+    if (!parameterMarkerPattern.matcher(sqlText).find()) {
+      val identityMapper = PositionMapper.identity(sqlText)
+      setupSubstitutionContext(sqlText, sqlText, identityMapper, isIdentity = 
true)
+      return sqlText
+    }
+
+    val substitutor = new SubstituteParamsParser()
+    val (substituted, _, positionMapper) = substitutor.substitute(sqlText,
+      namedParams = namedParams, positionalParams = positionalParams)
+
+    setupSubstitutionContext(sqlText, substituted, positionMapper, isIdentity 
= false)
+    substituted
+  }
+
+  /**
+   * Set up position mapping context for error reporting.
+   * Creates a callback function and stores it in CurrentOrigin for position 
translation
+   * between original and substituted SQL text.
+   *
+   * @param originalSql The original SQL text before parameter substitution
+   * @param substitutedSql The SQL text after parameter substitution
+   * @param positionMapper The position mapper for translating error positions
+   * @param isIdentity Whether this is an identity mapping (no substitution 
occurred)
+   */
+  private[sql] def setupSubstitutionContext(
+      originalSql: String,
+      substitutedSql: String,
+      positionMapper: PositionMapper,
+      isIdentity: Boolean): Unit = {
+
+    // Create callback function for position mapping.
+    // The positionMapper is captured in the closure for efficient position 
translation.
+    val callback: (org.antlr.v4.runtime.Token, org.antlr.v4.runtime.Token, 
String,

Review Comment:
   It turns out the solution is to move it all to sql-api. I hope that's OK.
   @dtenedor 



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