MaxGekk commented on code in PR #49006:
URL: https://github.com/apache/spark/pull/49006#discussion_r1880405979


##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionContext.scala:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.scripting
+
+import scala.collection.mutable.ListBuffer
+
+import org.apache.spark.SparkException
+
+/**
+ * SQL scripting execution context - keeps track of the current execution 
state.
+ */
+class SqlScriptingExecutionContext {
+  // List of frames that are currently active.
+  val frames: ListBuffer[SqlScriptingExecutionFrame] = ListBuffer.empty
+
+  def enterScope(label: String): Unit = {
+    if (frames.isEmpty) {
+      throw SparkException.internalError(s"Cannot enter scope: no frames.")
+    }
+    frames.last.enterScope(label)
+  }
+
+  def exitScope(label: String): Unit = {
+    if (frames.isEmpty) {
+      throw SparkException.internalError(s"Cannot exit scope: no frames.")

Review Comment:
   interpolation is not needed here:
   ```suggestion
         throw SparkException.internalError("Cannot exit scope: no frames.")
   ```



##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionNode.scala:
##########
@@ -177,12 +180,54 @@ class NoOpStatementExec extends LeafStatementExec {
  *   Executable nodes for nested statements within the CompoundBody.
  * @param label
  *   Label set by user to CompoundBody or None otherwise.
+ * @param isScope
+ *   Flag that indicates whether Compound Body is scope or not.
+ * @param context
+ *   SqlScriptingExecutionContext keeps the execution state of current script.
  */
-class CompoundBodyExec(statements: Seq[CompoundStatementExec], label: 
Option[String] = None)
+class CompoundBodyExec(
+    statements: Seq[CompoundStatementExec],
+    label: Option[String] = None,
+    isScope: Boolean,
+    context: SqlScriptingExecutionContext)
   extends NonLeafStatementExec {
 
+  private object ScopeStatus extends Enumeration {
+    type ScopeStatus = Value
+    val NotEntered, Inside, Exited = Value

Review Comment:
   Enum values should be in the upper case, see 
https://github.com/databricks/scala-style-guide?tab=readme-ov-file#naming-convention



##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionContext.scala:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.scripting
+
+import scala.collection.mutable.ListBuffer
+
+import org.apache.spark.SparkException
+
+/**
+ * SQL scripting execution context - keeps track of the current execution 
state.
+ */
+class SqlScriptingExecutionContext {
+  // List of frames that are currently active.
+  val frames: ListBuffer[SqlScriptingExecutionFrame] = ListBuffer.empty
+
+  def enterScope(label: String): Unit = {
+    if (frames.isEmpty) {
+      throw SparkException.internalError(s"Cannot enter scope: no frames.")
+    }
+    frames.last.enterScope(label)
+  }
+
+  def exitScope(label: String): Unit = {
+    if (frames.isEmpty) {
+      throw SparkException.internalError(s"Cannot exit scope: no frames.")
+    }
+    frames.last.exitScope(label)
+  }
+}
+
+/**
+ * SQL scripting executor - executes script and returns result statements.
+ * This supports returning multiple result statements from a single script.
+ *
+ * @param executionPlan CompoundBody which need to be executed.
+ */
+class SqlScriptingExecutionFrame(
+    executionPlan: Iterator[CompoundStatementExec]) extends 
Iterator[CompoundStatementExec] {
+
+  // List of scopes that are currently active.
+  private val scopes: ListBuffer[SqlScriptingExecutionScope] = ListBuffer.empty
+
+  override def hasNext: Boolean = executionPlan.hasNext
+
+  override def next(): CompoundStatementExec = {
+    if (!hasNext) throw SparkException.internalError("No more elements to 
iterate through.")
+    executionPlan.next()
+  }
+
+  def enterScope(label: String): Unit = {
+    scopes.addOne(new SqlScriptingExecutionScope(label))
+  }
+
+  def exitScope(label: String): Unit = {
+    if (scopes.isEmpty) {
+      throw SparkException.internalError(s"Cannot exit scope: no scopes to 
exit.")

Review Comment:
   ```suggestion
         throw SparkException.internalError("Cannot exit scope: no scopes to 
exit.")
   ```



##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingInterpreter.scala:
##########
@@ -40,13 +40,14 @@ case class SqlScriptingInterpreter(session: SparkSession) {
    * @param args
    *   A map of parameter names to SQL literal expressions.
    * @return
-   *   Iterator through collection of statements to be executed.
+   *   Top level CompoundBodyExec representing SQL Script to be executed.
    */
   def buildExecutionPlan(
       compound: CompoundBody,
-      args: Map[String, Expression]): Iterator[CompoundStatementExec] = {
-    transformTreeIntoExecutable(compound, args)
-      .asInstanceOf[CompoundBodyExec].getTreeIterator
+      args: Map[String, Expression],
+      context: SqlScriptingExecutionContext): CompoundBodyExec = {

Review Comment:
   Need to fix the comment above `return statements` since the method returns a 
single statement now.



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