@@ -151,17 +151,16 @@ private[sql] case class PhysicalRDD(
151151 val exprs = output.zipWithIndex.map(x => new BoundReference (x._2, x._1.dataType, true ))
152152 val row = ctx.freshName(" row" )
153153 val numOutputRows = metricTerm(ctx, " numOutputRows" )
154- ctx.INPUT_ROW = row
155- ctx.currentVars = null
156- val columns = exprs.map(_.gen(ctx))
157154
158155 // The input RDD can either return (all) ColumnarBatches or InternalRows. We determine this
159156 // by looking at the first value of the RDD and then calling the function which will process
160157 // the remaining. It is faster to return batches.
161158 // TODO: The abstractions between this class and SqlNewHadoopRDD makes it difficult to know
162159 // here which path to use. Fix this.
163160
164-
161+ ctx.INPUT_ROW = row
162+ ctx.currentVars = null
163+ val columns1 = exprs.map(_.gen(ctx))
165164 val scanBatches = ctx.freshName(" processBatches" )
166165 ctx.addNewFunction(scanBatches,
167166 s """
@@ -170,11 +169,11 @@ private[sql] case class PhysicalRDD(
170169 | int numRows = $batch.numRows();
171170 | if ( $idx == 0) $numOutputRows.add(numRows);
172171 |
173- | while ( $idx < numRows) {
172+ | while (!shouldStop() && $idx < numRows) {
174173 | InternalRow $row = $batch.getRow( $idx++);
175- | ${consume(ctx, columns).trim}
176- | if (shouldStop()) return;
174+ | ${consume(ctx, columns1).trim}
177175 | }
176+ | if (shouldStop()) return;
178177 |
179178 | if (! $input.hasNext()) {
180179 | $batch = null;
@@ -185,16 +184,23 @@ private[sql] case class PhysicalRDD(
185184 | }
186185 | } """ .stripMargin)
187186
187+ ctx.INPUT_ROW = row
188+ ctx.currentVars = null
189+ val columns2 = exprs.map(_.gen(ctx))
190+ val inputRow = if (isUnsafeRow) row else null
188191 val scanRows = ctx.freshName(" processRows" )
189192 ctx.addNewFunction(scanRows,
190193 s """
191194 | private void $scanRows(InternalRow $row) throws java.io.IOException {
192- | while (true) {
195+ | boolean firstRow = true;
196+ | while (!shouldStop() && (firstRow || $input.hasNext())) {
197+ | if (firstRow) {
198+ | firstRow = false;
199+ | } else {
200+ | $row = (InternalRow) $input.next();
201+ | }
193202 | $numOutputRows.add(1);
194- | ${consume(ctx, columns).trim}
195- | if (shouldStop()) return;
196- | if (! $input.hasNext()) break;
197- | $row = (InternalRow) $input.next();
203+ | ${consume(ctx, columns2, inputRow).trim}
198204 | }
199205 | } """ .stripMargin)
200206
0 commit comments