cloud-fan commented on code in PR #49372: URL: https://github.com/apache/spark/pull/49372#discussion_r1907245911
########## sql/core/src/main/scala/org/apache/spark/sql/scripting/SqlScriptingExecution.scala: ########## @@ -42,32 +45,53 @@ class SqlScriptingExecution( val ctx = new SqlScriptingExecutionContext() val executionPlan = interpreter.buildExecutionPlan(sqlScript, args, ctx) // Add frame which represents SQL Script to the context. - ctx.frames.addOne(new SqlScriptingExecutionFrame(executionPlan.getTreeIterator)) + ctx.frames.append(new SqlScriptingExecutionFrame(executionPlan.getTreeIterator)) // Enter the scope of the top level compound. // We don't need to exit this scope explicitly as it will be done automatically // when the frame is removed during iteration. executionPlan.enterScope() ctx } - private var current: Option[DataFrame] = getNextResult + private var current: Option[DataFrame] = None - override def hasNext: Boolean = current.isDefined + /** + * Advances through the script and executes statements until a result statement or + * end of script is encountered. + * + * To know if there is result statement available, the hasNext needs to advance through + * script and execute statements until the result statement or end of script is encountered. + * For that reason hasNext must be called only once before each `next()` invocation, + * and the returned result must be executed before subsequent calls. Multiple calls without + * executing the intermediate results will lead to incorrect behavior. + * + * @return True if a result statement is available, False otherwise. + */ + override def hasNext: Boolean = { + current = getNextResult + current.isDefined + } + /** + * Returns the next result statement from the script. + * Multiple consecutive calls without calling `hasNext()` would return the same result statement. Review Comment: This violates the Java Iterator contract. Can we make sure it gets the next result? -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org