cloud-fan commented on code in PR #60240:
URL: https://github.com/apache/spark/pull/60240#discussion_r4513930281


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/joins/SortMergeAsOfJoinExec.scala:
##########
@@ -413,6 +414,17 @@ private[joins] class SortMergeAsOfJoinScanner(
     if (needsCopy) rightRow.copy() else rightRow
   }
 
+  /**
+   * Closes a right-group iterator, which the scans below often leave before 
its end. Once the
+   * group has spilled, the iterator holds a reader open on every spill file 
until it ends or is
+   * closed, so an unclosed one per left row would keep them all open until 
the task ends. A kept
+   * match is already a copy then, so closing the iterator does not change it.
+   */
+  private def closeIterator(iter: Iterator[UnsafeRow]): Unit = iter match {

Review Comment:
   **Non-blocking (P2):** This closes the iterator for the ASOF buffered scans, 
but other consumers of `ExternalAppendOnlyUnsafeRowArray` still have the same 
leak. Each of them creates an iterator per streamed row, output row or block 
and stops before its end:
   
   - Sort-merge `LeftSemi`, `LeftAnti` and `ExistenceJoin` with a residual 
condition stop at the first passing buffered row. This happens in 
`SortMergeJoinEvaluatorFactory` (the loops at lines 219, 272 and 327) and in 
the generated `codegenSemi` / `codegenAnti` / `codegenExistence` loops. With a 
condition the whole group is buffered (`onlyBufferFirstMatchedRow` is false), 
so it can spill under the same `spark.sql.sortMergeJoinExec.buffer.*` settings 
this scan uses. Example: `SELECT * FROM l WHERE EXISTS (SELECT 1 FROM r WHERE 
r.k = l.k AND r.v > l.v)`.
   - With `spark.sql.join.splitStreamedSideJoinCondition` enabled, the 
generated code creates the iterator (`SortMergeJoinExec.scala:617`) before the 
streamed-only guard, and the guard's `continue` drops it unread.
   - `UnboundedFollowingWindowFunctionFrame.write`, the default frame for `... 
AND UNBOUNDED FOLLOWING` aggregates, reads one row and drops the iterator 
whenever the frame's lower bound does not move, for example a `RANGE` frame 
over duplicate order keys.
   - With `spark.sql.window.segmentTree.enabled` set, 
`WindowSegmentTree.buildBlockLevels` reads one block and drops the iterator.
   
   Once the buffer has spilled, my reading of the code is that each of these 
iterators keeps a reader open on every remaining spill file until the task 
ends. That is the same L * S growth your new test measures for the ASOF scans, 
so these queries can still fail with "Too many open files", and a retry hits 
the same data.
   
   Could you close the iterator at these sites too, the same way 
`closeIterator` does here and `SegmentTreeWindowFunctionFrame` and 
`SlidingWindowMinMaxFunctionFrame` already do? In generated Java that is an 
`instanceof java.io.Closeable` check. Also, in codegen, create the iterator 
only after the streamed-only guard passes. The ASOF change itself looks correct.
   
   **Verification:**
   - **Regression:** With one join key whose buffered rows spill into several 
files and many streamed rows of that key, where each streamed row stops at the 
first buffered row that passes a residual condition, LeftSemi, LeftAnti and 
ExistenceJoin queries do not grow the task's open-file count with the number of 
streamed rows, with whole-stage codegen both on and off, and they return the 
same rows as without the change.
   - **Regression:** With spark.sql.join.splitStreamedSideJoinCondition enabled 
and a streamed-side-only predicate that fails for many streamed rows of a 
spilled key, LeftOuter, RightOuter, LeftAnti and ExistenceJoin queries under 
whole-stage codegen do not grow the task's open-file count with those rows, and 
they return the same rows as without the change.
   - **Regression:** With spark.sql.window.segmentTree.enabled at its default 
of false, an aggregate over a spilled window partition with an 
unbounded-following frame whose lower bound stays the same across many rows, 
such as a range frame over duplicate order keys, does not grow the task's 
open-file count with the rows processed. With 
spark.sql.window.segmentTree.enabled set and a 
spark.sql.window.segmentTree.blockSize small enough that a spilled partition 
spans many blocks, a segment-tree frame does not grow it with the blocks built. 
Both return the same results as without the change.



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