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


##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionNode.scala:
##########
@@ -177,12 +180,52 @@ 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
+ * @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 var localIterator = statements.iterator
   private var curr = if (localIterator.hasNext) Some(localIterator.next()) 
else None
+  private var scopeEntered = false
+  private var scopeExited = false
+
+  /**
+   * Enter scope represented by this compound statement.
+   *
+   * This operation needs to be idempotent because it is called multiple times 
during
+   * iteration, but it should be executed only once when compound body that 
represent
+   * scope is encountered for the first time.
+   */
+  protected def enterScope(): Unit = {
+    // This check makes this operation idempotent.
+    if (isScope && !scopeEntered) {
+      scopeEntered = true
+      scopeExited = false
+      context.enterScope(label.get)
+    }
+  }
+
+  /**
+   * Exit scope represented by this compound statement.
+   *
+   * Even though this operation is called exactly once, we are making it 
idempotent.
+   */
+  protected def exitScope(): Unit = {
+    // This check makes this operation idempotent.
+    if (isScope && scopeEntered && !scopeExited) {

Review Comment:
   ideally we should check to a particular state.



##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionNode.scala:
##########
@@ -177,12 +180,52 @@ 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
+ * @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 var localIterator = statements.iterator
   private var curr = if (localIterator.hasNext) Some(localIterator.next()) 
else None
+  private var scopeEntered = false
+  private var scopeExited = false

Review Comment:
   How about to introduce a state explicitly ( sealed trait or enum) instead of 
manipulating a few of booleans. 



##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionNode.scala:
##########
@@ -177,12 +180,52 @@ 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

Review Comment:
   not clear what does it mean. so, `CompoundBodyExec(isScope = false)` is not 
a compound body?



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