@@ -507,32 +507,38 @@ case class SortMergeJoinExec(
507
507
}
508
508
509
509
/**
510
- * Creates variables for left part of result row.
510
+ * Creates variables and declarations for left part of result row.
511
511
*
512
512
* In order to defer the access after condition and also only access once in the loop,
513
513
* the variables should be declared separately from accessing the columns, we can't use the
514
514
* codegen of BoundReference here.
515
515
*/
516
- private def createLeftVars (ctx : CodegenContext , leftRow : String ): Seq [ExprCode ] = {
516
+ private def createLeftVars (ctx : CodegenContext , leftRow : String ): ( Seq [ExprCode ], Seq [ String ]) = {
517
517
ctx.INPUT_ROW = leftRow
518
518
left.output.zipWithIndex.map { case (a, i) =>
519
519
val value = ctx.freshName(" value" )
520
520
val valueCode = ctx.getValue(leftRow, a.dataType, i.toString)
521
- // declare it as class member, so we can access the column before or in the loop.
522
- ctx.addMutableState(ctx.javaType( a.dataType), value )
521
+ val javaType = ctx.javaType(a.dataType)
522
+ val defaultValue = ctx.defaultValue( a.dataType)
523
523
if (a.nullable) {
524
524
val isNull = ctx.freshName(" isNull" )
525
- ctx.addMutableState(ctx.JAVA_BOOLEAN , isNull)
526
525
val code =
527
526
s """
528
527
| $isNull = $leftRow.isNullAt( $i);
529
- | $value = $isNull ? ${ctx. defaultValue(a.dataType)} : ( $valueCode);
528
+ | $value = $isNull ? $defaultValue : ( $valueCode);
530
529
""" .stripMargin
531
- ExprCode (code, isNull, value)
530
+ val leftVarsDecl =
531
+ s """
532
+ |boolean $isNull = false;
533
+ | $javaType $value = $defaultValue;
534
+ """ .stripMargin
535
+ (ExprCode (code, isNull, value), leftVarsDecl)
532
536
} else {
533
- ExprCode (s " $value = $valueCode; " , " false" , value)
537
+ val code = s " $value = $valueCode; "
538
+ val leftVarsDecl = s """ $javaType $value = $defaultValue; """
539
+ (ExprCode (code, " false" , value), leftVarsDecl)
534
540
}
535
- }
541
+ }.unzip
536
542
}
537
543
538
544
/**
@@ -580,7 +586,7 @@ case class SortMergeJoinExec(
580
586
val (leftRow, matches) = genScanner(ctx)
581
587
582
588
// Create variables for row from both sides.
583
- val leftVars = createLeftVars(ctx, leftRow)
589
+ val ( leftVars, leftVarDecl) = createLeftVars(ctx, leftRow)
584
590
val rightRow = ctx.freshName(" rightRow" )
585
591
val rightVars = createRightVar(ctx, rightRow)
586
592
@@ -617,6 +623,7 @@ case class SortMergeJoinExec(
617
623
618
624
s """
619
625
|while (findNextInnerJoinRows( $leftInput, $rightInput)) {
626
+ | ${leftVarDecl.mkString(" \n " )}
620
627
| ${beforeLoop.trim}
621
628
| scala.collection.Iterator<UnsafeRow> $iterator = $matches.generateIterator();
622
629
| while ( $iterator.hasNext()) {
0 commit comments