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


##########
sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecutionNode.scala:
##########
@@ -649,3 +652,145 @@ class LoopStatementExec(
     body.reset()
   }
 }
+
+/**
+ * Executable node for ForStatement.
+ * @param query Executable node for the query.
+ * @param variableName Name of variable used for accessing current row during 
iteration.
+ * @param body Executable node for the body. If variableName is not None, will 
have DropVariable
+ *             as the last statement.
+ * @param label Label set to ForStatement by user or None otherwise.
+ * @param session Spark session that SQL script is executed within.
+ */
+class ForStatementExec(
+    query: SingleStatementExec,
+    variableName: Option[String],
+    body: CompoundBodyExec,
+    label: Option[String],
+    session: SparkSession) extends NonLeafStatementExec {
+
+  private object ForState extends Enumeration {
+    val VariableDeclaration, VariableAssignment, Body = Value
+  }
+  private var state = ForState.VariableDeclaration
+  private var currRow = 0
+  private var currVariable: Expression = null
+
+  private var queryResult: Array[Row] = null
+  private var isResultCacheValid = false
+  private def cachedQueryResult(): Array[Row] = {
+    if (!isResultCacheValid) {
+      query.isExecuted = true
+      queryResult = Dataset.ofRows(session, query.parsedPlan).collect()
+      isResultCacheValid = true
+    }
+    queryResult
+  }
+
+  /**
+   * Loop can be interrupted by LeaveStatementExec
+   */
+  private var interrupted: Boolean = false
+
+  private lazy val treeIterator: Iterator[CompoundStatementExec] =
+    new Iterator[CompoundStatementExec] {
+      override def hasNext: Boolean =
+        !interrupted && cachedQueryResult().length > 0 && currRow < 
cachedQueryResult().length
+
+      override def next(): CompoundStatementExec = state match {
+        case ForState.VariableDeclaration =>
+          // when there is no for variable, skip var declaration and iterate 
only the body
+          if (variableName.isEmpty) {
+            state = ForState.Body
+            body.reset()
+            return next()
+          }

Review Comment:
   when the variable name is not provided (just the query) what is the desired 
behavior? did we check this with Serge? I would say that at the moment query is 
useless and we cannot access the results in any way, but would expect there is 
some way to access the results that should be supported (otherwise the whole 
grammar wouldn't make sense)?



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