Github user kiszk commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11636#discussion_r56840405
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/ExistingRDD.scala ---
    @@ -217,19 +241,29 @@ private[sql] case class DataSourceScan(
         // TODO: The abstractions between this class and SqlNewHadoopRDD makes 
it difficult to know
         // here which path to use. Fix this.
     
    -    ctx.INPUT_ROW = row
    +    // These assignments avoids accesses to instance variables in a 
while-loop
    +    // Since they are loop invariant variables (LIVs), host them outside 
the while-loop
    +    val colLocalVars = output.zipWithIndex.map(x => ctx.freshName("col" + 
x._2))
    +    val columnLIVAssigns = (colLocalVars zip colVars).map { case 
(localName, name) =>
    +      s"$columnVectorClz $localName = $name;" }
    +
         ctx.currentVars = null
    -    val columns1 = exprs.map(_.gen(ctx))
    +    val columns1 = (output zip colLocalVars).map { case (attr, colVar) =>
    +      genCodeColumnVector(ctx, colVar, rowidx, attr.dataType, 
attr.nullable) }
         val scanBatches = ctx.freshName("processBatches")
         ctx.addNewFunction(scanBatches,
           s"""
           | private void $scanBatches() throws java.io.IOException {
           |  while (true) {
           |     int numRows = $batch.numRows();
    -      |     if ($idx == 0) $numOutputRows.add(numRows);
    +      |     if ($idx == 0) {
    +      |       ${columnAssigns.mkString("", "\n", "\n")}
    +      |       $numOutputRows.add(numRows);
    +      |     }
           |
    +      |     ${columnLIVAssigns.mkString("", "\n", "\n")}
           |     while (!shouldStop() && $idx < numRows) {
    -      |       InternalRow $row = $batch.getRow($idx++);
    +      |       int $rowidx = $idx++;
    --- End diff --
    
    @viirya , we need this. Here is an example of the generated code. In the 
code, line 93 corresponds to what you pointed out.
    Lines 95 and 97 refers to ```scan_rowIdx``` that keeps pre-increment value 
of ```scan_batchIndex```. If we use ```batchIndex``` that has been already 
incremented, the argument for ```getUTF8String()``` and ```getInt()``` is 
incorrect.
    
    ```java
    /* 092 */       while (!shouldStop() && scan_batchIdx < numRows) {
    /* 093 */         int scan_rowIdx = scan_batchIdx++;
    /* 094 */         /* columnVector[scan_col0, scan_rowIdx, string] */
    /* 095 */         UTF8String scan_value = 
scan_col0.getUTF8String(scan_rowIdx);
    /* 096 */         /* columnVector[scan_col1, scan_rowIdx, int] */
    /* 097 */         int scan_value1 = scan_col1.getInt(scan_rowIdx);
    /* 098 */         scan_holder.reset();
    /* 099 */         
    /* 100 */         scan_rowWriter.write(0, scan_value);
    /* 101 */         
    /* 102 */         scan_rowWriter.write(1, scan_value1);
    /* 103 */         scan_result.setTotalSize(scan_holder.totalSize());
    /* 104 */         append(scan_result);
    /* 105 */       }
    ```


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to