cloud-fan commented on code in PR #53530:
URL: https://github.com/apache/spark/pull/53530#discussion_r2646486865


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -6573,6 +6575,100 @@ class AstBuilder extends DataTypeAstBuilder
     )
   }
 
+  /**
+   * Create a [[DeclareCursor]] command wrapped in SingleStatement.
+   *
+   * For example:
+   * {{{
+   *   DECLARE cursor_name CURSOR FOR SELECT * FROM table;
+   * }}}
+   */
+  override def visitDeclareCursorStatement(
+      ctx: DeclareCursorStatementContext): LogicalPlan = withOrigin(ctx) {
+    val cursorName = ctx.strictIdentifier().getText
+    // Extract original SQL text to preserve parameter markers
+    val queryText = getOriginalText(ctx.query())
+
+    // Use a placeholder plan (LocalRelation) - the actual query will be 
parsed at execution time
+    // This prevents parameter markers from being resolved during script 
analysis
+    val query: LogicalPlan = LocalRelation()
+
+    val asensitive = ctx.ASENSITIVE() != null || ctx.INSENSITIVE() != null
+    SingleStatement(DeclareCursor(cursorName, query, queryText, asensitive))
+  }
+
+  /**
+   * Create an [[OpenCursor]] command wrapped in SingleStatement.
+   *
+   * For example:
+   * {{{
+   *   OPEN cursor_name;
+   *   OPEN cursor_name USING expr1, expr2;
+   *   OPEN cursor_name USING (expr1 AS param1, expr2 AS param2);
+   * }}}
+   */
+  override def visitOpenCursorStatement(
+      ctx: OpenCursorStatementContext): LogicalPlan = withOrigin(ctx) {
+    val cursorName = 
ctx.multipartIdentifier().parts.asScala.map(_.getText).mkString(".")
+
+    // Parse optional USING clause parameters
+    // Extract both expressions and their names (if aliased)
+    val (args, paramNames) = Option(ctx.params).map { params =>
+      val namedExprs = 
params.namedExpression().asScala.toSeq.map(visitNamedExpression)
+      val exprs = namedExprs.map {
+        case alias: Alias => alias.child  // Extract child expression
+        case expr => expr
+      }
+      val names = namedExprs.map {
+        case alias: Alias => alias.name  // Extract alias name
+        case _ => ""  // Positional parameter (no name)
+      }
+      (exprs, names)
+    }.getOrElse((Seq.empty, Seq.empty))
+
+    SingleStatement(OpenCursor(cursorName, args, paramNames))
+  }
+
+  /**
+   * Create a [[FetchCursor]] command wrapped in SingleStatement.
+   *
+   * For example:
+   * {{{
+   *   FETCH cursor_name INTO var1, var2;
+   * }}}
+   */
+  override def visitFetchCursorStatement(
+      ctx: FetchCursorStatementContext): LogicalPlan = withOrigin(ctx) {
+    val cursorName = 
ctx.multipartIdentifier().parts.asScala.map(_.getText).mkString(".")
+    val targetVariables = ctx.identifierReference().asScala.map { varIdent =>
+      val varName = if (varIdent.expression() != null) {
+        // IDENTIFIER(expression) case - not supported for variables
+        throw new ParseException(
+          errorClass = "INVALID_SQL_SYNTAX.IDENTIFIER_CLAUSE_NOT_ALLOWED",
+          messageParameters = Map.empty,
+          varIdent)
+      } else {
+        visitMultipartIdentifier(varIdent.multipartIdentifier())
+      }
+      UnresolvedAttribute(varName)
+    }.toSeq
+    SingleStatement(FetchCursor(cursorName, targetVariables))
+  }
+
+  /**
+   * Create a [[CloseCursor]] command wrapped in SingleStatement.
+   *
+   * For example:
+   * {{{
+   *   CLOSE cursor_name;
+   * }}}
+   */
+  override def visitCloseCursorStatement(
+      ctx: CloseCursorStatementContext): LogicalPlan = withOrigin(ctx) {
+    val cursorName = 
ctx.multipartIdentifier().parts.asScala.map(_.getText).mkString(".")

Review Comment:
   ditto



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