Github user dongjoon-hyun commented on a diff in the pull request:

    https://github.com/apache/spark/pull/16440#discussion_r94286661
  
    --- Diff: 
sql/hive-thriftserver/src/main/scala/org/apache/spark/sql/hive/thriftserver/SparkExecuteStatementOperation.scala
 ---
    @@ -111,9 +115,15 @@ private[hive] class SparkExecuteStatementOperation(
     
         // Reset iter to header when fetching start from first row
         if (order.equals(FetchOrientation.FETCH_FIRST)) {
    -      val (ita, itb) = iterHeader.duplicate
    -      iter = ita
    -      iterHeader = itb
    +      iter = if (useIncrementalCollect) {
    +        resultList = None
    +        result.toLocalIterator.asScala
    +      } else {
    +        if (resultList.isEmpty) {
    --- End diff --
    
    Correct. There are two cases and this PR targets `incrementalCollect=true` 
mainly.
    > you also buffer the whole result into memory locally.
    
    First of all, before SPARK-16563, `FETCH_FIRST` is not supported correctly 
because **iterator** can be traversed once.
    
    * Case 1) `incrementalCollect=false`
    **Creating** a whole result in a memory **once** by **result.collect** was 
the the original Spark way before `SPARK-16563`.
    If we can create a whole result once during the query processing, I think 
we can keep that for `FETCH_FIRST` with less side effect.
    So, I keep them. If this is not allowed, we have to go `Case 2`.
    
    * Case 2) `incrementalCollect=true`
    In this case, **by definition**, we cannot create the whole result set in a 
memory at any time during the query processing. There is no way to find the 
first row with **iterator**. `result.toLocalIterator.asScala` should be used 
whenever `FETCH_FIRST` is used.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to