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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/ParameterHandler.scala:
##########
@@ -0,0 +1,327 @@
+/*
+ * 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.trees.ParameterSubstitutionInfo
+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 parameter substitution context in CurrentOrigin for error 
reporting.
+   * This creates a ParameterSubstitutionInfo object with position mapping 
data.
+   *
+   * @param originalSql The original SQL text before substitution
+   * @param substitutedSql The SQL text after substitution
+   * @param positionMapper The mapper for translating 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 parameter substitution info with the position mapper.
+    val substitutionInfo = ParameterSubstitutionInfo(
+      originalSql = originalSql,
+      isIdentity = isIdentity,
+      positionMapper = if (isIdentity) None else Some(positionMapper)
+    )
+
+    // Store the info in CurrentOrigin for position mapping during error 
reporting.
+    val currentOrigin = org.apache.spark.sql.catalyst.trees.CurrentOrigin.get
+    val updatedOrigin = currentOrigin.copy(parameterSubstitutionInfo = 
Some(substitutionInfo))
+    org.apache.spark.sql.catalyst.trees.CurrentOrigin.set(updatedOrigin)

Review Comment:
   I did. Here it seems that the `CurrentOrigin.set` is already an existing 
ThreadLocal. Here it seems we are updating the current `Origin` to add the 
parameter substitution info. This seems OK unless there is some problem with 
the approach? @cloud-fan what issue will the main parser have with the `Origin` 
if we do it this way, are we missing something?



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