miland-db commented on code in PR #47146:
URL: https://github.com/apache/spark/pull/47146#discussion_r1662555782


##########
sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4:
##########
@@ -68,6 +68,18 @@ singleStatement
     : statement SEMICOLON* EOF
     ;
 
+label
+    : multipartIdentifier
+    ;
+
+beginLabel

Review Comment:
   Hm, we can but `multipartIdentifier` directly in `beginLabel` and 
`endLabel`. This was done for clarity, but I agree it is unnecessary to add 
another level in parser.



##########
sql/api/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBaseParser.g4:
##########
@@ -68,6 +68,18 @@ singleStatement
     : statement SEMICOLON* EOF
     ;
 
+label
+    : multipartIdentifier
+    ;
+
+beginLabel

Review Comment:
   Hm, we can put `multipartIdentifier` directly in `beginLabel` and 
`endLabel`. This was done for clarity, but I agree it is unnecessary to add 
another level in parser.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -121,28 +121,43 @@ class AstBuilder extends DataTypeAstBuilder with 
SQLConfHelper with Logging {
       visit(s).asInstanceOf[CompoundBody]
     }.getOrElse {
       val logicalPlan = visitSingleStatement(ctx.singleStatement())
-      CompoundBody(Seq(SingleStatement(parsedPlan = logicalPlan)))
+      CompoundBody(Seq(SingleStatement(parsedPlan = logicalPlan)),
+        java.util.UUID.randomUUID.toString)
     }
   }
 
   override def visitSingleCompoundStatement(ctx: 
SingleCompoundStatementContext): CompoundBody = {
     visit(ctx.beginEndCompoundBlock()).asInstanceOf[CompoundBody]
   }
 
-  private def visitCompoundBodyImpl(ctx: CompoundBodyContext): CompoundBody = {
+  private def visitCompoundBodyImpl(ctx: CompoundBodyContext, label: String): 
CompoundBody = {
     val buff = ListBuffer[CompoundPlanStatement]()
     ctx.compoundStatements.forEach(compoundStatement => {
       buff += visit(compoundStatement).asInstanceOf[CompoundPlanStatement]
     })
-    CompoundBody(buff.toSeq)
+    CompoundBody(buff.toSeq, label)
   }
 
   override def visitBeginEndCompoundBlock(ctx: BeginEndCompoundBlockContext): 
CompoundBody = {
-    visitCompoundBodyImpl(ctx.compoundBody())
+    val beginLabelCtx = Option(ctx.beginLabel())
+    val endLabelCtx = Option(ctx.endLabel())
+
+    (beginLabelCtx, endLabelCtx) match {
+      case (Some(bl: BeginLabelContext), Some(el: EndLabelContext))
+        if bl.label().getText.nonEmpty && bl.label().getText != 
el.label().getText =>
+        throw SparkException.internalError("Both labels should be same.")
+      case (None, Some(_)) =>
+        throw SparkException.internalError("End label can't exist without 
begin label.")
+      case _ =>
+    }
+
+    val labelText = beginLabelCtx.
+      map(_.label().getText).getOrElse(java.util.UUID.randomUUID.toString)
+    visitCompoundBodyImpl(ctx.compoundBody(), labelText)
   }
 
   override def visitCompoundBody(ctx: CompoundBodyContext): CompoundBody = {
-    visitCompoundBodyImpl(ctx)
+    visitCompoundBodyImpl(ctx, "")

Review Comment:
   Done



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