davidm-db commented on code in PR #47462:
URL: https://github.com/apache/spark/pull/47462#discussion_r1688145246


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -133,39 +133,90 @@ class AstBuilder extends DataTypeAstBuilder with 
SQLConfHelper with Logging {
 
   private def visitCompoundBodyImpl(
       ctx: CompoundBodyContext,
-      label: Option[String]): CompoundBody = {
+      label: Option[String],
+      allowVarDeclare: Boolean): CompoundBody = {
     val buff = ListBuffer[CompoundPlanStatement]()
     ctx.compoundStatements.forEach(compoundStatement => {
       buff += visit(compoundStatement).asInstanceOf[CompoundPlanStatement]
     })
 
+    val compoundStatements = buff.toList
+
+    if (allowVarDeclare) {
+      val declareAfterPrefix = compoundStatements
+        .dropWhile(statement => statement.isInstanceOf[SingleStatement] &&
+          
statement.asInstanceOf[SingleStatement].parsedPlan.isInstanceOf[CreateVariable])
+        .filter(_.isInstanceOf[SingleStatement])
+        
.find(_.asInstanceOf[SingleStatement].parsedPlan.isInstanceOf[CreateVariable])
+
+      declareAfterPrefix match {
+        case Some(SingleStatement(parsedPlan)) =>
+          throw SqlScriptingErrors.variableDeclarationOnlyAtBeginning(
+            parsedPlan.asInstanceOf[CreateVariable].name.
+              asInstanceOf[UnresolvedIdentifier].nameParts.last,
+            parsedPlan.origin.line.get.toString)
+        case _ =>
+      }
+
+    } else {
+      val declare = compoundStatements
+        .filter(_.isInstanceOf[SingleStatement])
+        
.find(_.asInstanceOf[SingleStatement].parsedPlan.isInstanceOf[CreateVariable])
+
+      declare match {
+        case Some(SingleStatement(parsedPlan)) =>
+          throw SqlScriptingErrors.variableDeclarationOnlyAtBeginning(
+            parsedPlan.asInstanceOf[CreateVariable].name.
+              asInstanceOf[UnresolvedIdentifier].nameParts.last,
+            parsedPlan.origin.line.get.toString)
+        case _ =>
+      }
+    }
+
     CompoundBody(buff.toSeq, label)
   }
 
-  override def visitBeginEndCompoundBlock(ctx: BeginEndCompoundBlockContext): 
CompoundBody = {
-    val beginLabelCtx = Option(ctx.beginLabel())
-    val endLabelCtx = Option(ctx.endLabel())
+  private def generateLabelText(
+                                 beginLabelCtx: Option[BeginLabelContext],
+                                 endLabelCtx: Option[EndLabelContext]) = {
 
     (beginLabelCtx, endLabelCtx) match {
       case (Some(bl: BeginLabelContext), Some(el: EndLabelContext))
         if bl.multipartIdentifier().getText.nonEmpty &&
           bl.multipartIdentifier().getText.toLowerCase(Locale.ROOT) !=
-              el.multipartIdentifier().getText.toLowerCase(Locale.ROOT) =>
+            el.multipartIdentifier().getText.toLowerCase(Locale.ROOT) =>
         throw SqlScriptingErrors.labelsMismatch(
           bl.multipartIdentifier().getText, el.multipartIdentifier().getText)
       case (None, Some(el: EndLabelContext)) =>
         throw 
SqlScriptingErrors.endLabelWithoutBeginLabel(el.multipartIdentifier().getText)
       case _ =>
     }
 
-    val labelText = beginLabelCtx.
-      
map(_.multipartIdentifier().getText).getOrElse(java.util.UUID.randomUUID.toString).
-      toLowerCase(Locale.ROOT)
-    visitCompoundBodyImpl(ctx.compoundBody(), Some(labelText))
+    beginLabelCtx
+      
.map(_.multipartIdentifier().getText).getOrElse(java.util.UUID.randomUUID.toString)
+      .toLowerCase(Locale.ROOT)
+  }
+
+  override def visitBeginEndCompoundBlock(ctx: BeginEndCompoundBlockContext): 
CompoundBody = {
+    val labelText = generateLabelText(Option(ctx.beginLabel()), 
Option(ctx.endLabel()))
+
+    visitCompoundBodyImpl(ctx.compoundBody(), Some(labelText), allowVarDeclare 
= true)
+  }
+
+  override def visitWhileStatement(ctx: WhileStatementContext): WhileStatement 
= {
+    val labelText = generateLabelText(Option(ctx.beginLabel()), 
Option(ctx.endLabel()))
+    val boolExpr = ctx.booleanExpression()
+    WhileStatement(

Review Comment:
   use named parameters when having such complicated constructs, so it's a bit 
cleaner what each parameter means. also, maybe move `SingleStatement(` to the 
next line, so each parameter has a new level of identation.



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala:
##########
@@ -133,39 +133,90 @@ class AstBuilder extends DataTypeAstBuilder with 
SQLConfHelper with Logging {
 
   private def visitCompoundBodyImpl(
       ctx: CompoundBodyContext,
-      label: Option[String]): CompoundBody = {
+      label: Option[String],
+      allowVarDeclare: Boolean): CompoundBody = {
     val buff = ListBuffer[CompoundPlanStatement]()
     ctx.compoundStatements.forEach(compoundStatement => {
       buff += visit(compoundStatement).asInstanceOf[CompoundPlanStatement]
     })
 
+    val compoundStatements = buff.toList
+
+    if (allowVarDeclare) {
+      val declareAfterPrefix = compoundStatements
+        .dropWhile(statement => statement.isInstanceOf[SingleStatement] &&
+          
statement.asInstanceOf[SingleStatement].parsedPlan.isInstanceOf[CreateVariable])
+        .filter(_.isInstanceOf[SingleStatement])
+        
.find(_.asInstanceOf[SingleStatement].parsedPlan.isInstanceOf[CreateVariable])
+
+      declareAfterPrefix match {
+        case Some(SingleStatement(parsedPlan)) =>
+          throw SqlScriptingErrors.variableDeclarationOnlyAtBeginning(
+            parsedPlan.asInstanceOf[CreateVariable].name.
+              asInstanceOf[UnresolvedIdentifier].nameParts.last,
+            parsedPlan.origin.line.get.toString)
+        case _ =>
+      }
+
+    } else {
+      val declare = compoundStatements
+        .filter(_.isInstanceOf[SingleStatement])
+        
.find(_.asInstanceOf[SingleStatement].parsedPlan.isInstanceOf[CreateVariable])
+
+      declare match {
+        case Some(SingleStatement(parsedPlan)) =>
+          throw SqlScriptingErrors.variableDeclarationOnlyAtBeginning(
+            parsedPlan.asInstanceOf[CreateVariable].name.
+              asInstanceOf[UnresolvedIdentifier].nameParts.last,
+            parsedPlan.origin.line.get.toString)
+        case _ =>
+      }
+    }
+
     CompoundBody(buff.toSeq, label)
   }
 
-  override def visitBeginEndCompoundBlock(ctx: BeginEndCompoundBlockContext): 
CompoundBody = {
-    val beginLabelCtx = Option(ctx.beginLabel())
-    val endLabelCtx = Option(ctx.endLabel())
+  private def generateLabelText(
+                                 beginLabelCtx: Option[BeginLabelContext],
+                                 endLabelCtx: Option[EndLabelContext]) = {
 
     (beginLabelCtx, endLabelCtx) match {
       case (Some(bl: BeginLabelContext), Some(el: EndLabelContext))
         if bl.multipartIdentifier().getText.nonEmpty &&
           bl.multipartIdentifier().getText.toLowerCase(Locale.ROOT) !=
-              el.multipartIdentifier().getText.toLowerCase(Locale.ROOT) =>
+            el.multipartIdentifier().getText.toLowerCase(Locale.ROOT) =>
         throw SqlScriptingErrors.labelsMismatch(
           bl.multipartIdentifier().getText, el.multipartIdentifier().getText)
       case (None, Some(el: EndLabelContext)) =>
         throw 
SqlScriptingErrors.endLabelWithoutBeginLabel(el.multipartIdentifier().getText)
       case _ =>
     }
 
-    val labelText = beginLabelCtx.
-      
map(_.multipartIdentifier().getText).getOrElse(java.util.UUID.randomUUID.toString).
-      toLowerCase(Locale.ROOT)
-    visitCompoundBodyImpl(ctx.compoundBody(), Some(labelText))
+    beginLabelCtx
+      
.map(_.multipartIdentifier().getText).getOrElse(java.util.UUID.randomUUID.toString)
+      .toLowerCase(Locale.ROOT)
+  }
+
+  override def visitBeginEndCompoundBlock(ctx: BeginEndCompoundBlockContext): 
CompoundBody = {
+    val labelText = generateLabelText(Option(ctx.beginLabel()), 
Option(ctx.endLabel()))
+
+    visitCompoundBodyImpl(ctx.compoundBody(), Some(labelText), allowVarDeclare 
= true)
+  }
+
+  override def visitWhileStatement(ctx: WhileStatementContext): WhileStatement 
= {
+    val labelText = generateLabelText(Option(ctx.beginLabel()), 
Option(ctx.endLabel()))
+    val boolExpr = ctx.booleanExpression()
+    WhileStatement(

Review Comment:
   use named parameters when having such complicated constructs, so it's a bit 
cleaner what each parameter means. also, maybe move `SingleStatement(` to the 
next line, so each parameter has a new level of indentation.



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