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


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

Review Comment:
   nit: indentation for parameters. also, even though it's private function, 
it's nice to provide return type for function declaration.



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