Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-35329][SQL] Split generated switch code into pieces in ExpandExec #32457

Closed
wants to merge 7 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

package org.apache.spark.sql.execution

import scala.collection.mutable

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.expressions.codegen._
import org.apache.spark.sql.catalyst.expressions.codegen.Block._
import org.apache.spark.sql.catalyst.plans.physical.{Partitioning, UnknownPartitioning}
import org.apache.spark.sql.execution.metric.SQLMetrics

Expand Down Expand Up @@ -152,38 +153,59 @@ case class ExpandExec(
// This column is the same across all output rows. Just generate code for it here.
BindReferences.bindReference(firstExpr, attributeSeq).genCode(ctx)
} else {
val isNull = ctx.freshName("isNull")
val value = ctx.freshName("value")
val code = code"""
|boolean $isNull = true;
|${CodeGenerator.javaType(firstExpr.dataType)} $value =
| ${CodeGenerator.defaultValue(firstExpr.dataType)};
""".stripMargin
val isNull = ctx.addMutableState(
CodeGenerator.JAVA_BOOLEAN,
"resultIsNull",
v => s"$v = true;")
val value = ctx.addMutableState(
CodeGenerator.javaType(firstExpr.dataType),
"resultValue",
v => s"$v = ${CodeGenerator.defaultValue(firstExpr.dataType)};")

ExprCode(
code,
JavaCode.isNullVariable(isNull),
JavaCode.variable(value, firstExpr.dataType))
}
}

// Part 2: switch/case statements
val cases = projections.zipWithIndex.map { case (exprs, row) =>
var updateCode = ""
for (col <- exprs.indices) {
val updateCode = mutable.ArrayBuffer[String]()
exprs.indices.foreach { col =>
if (!sameOutput(col)) {
val ev = BindReferences.bindReference(exprs(col), attributeSeq).genCode(ctx)
updateCode +=
s"""
|${ev.code}
|${outputColumns(col).isNull} = ${ev.isNull};
|${outputColumns(col).value} = ${ev.value};
""".stripMargin
val boundExpr = BindReferences.bindReference(exprs(col), attributeSeq)
val ev = boundExpr.genCode(ctx)
val inputVars = CodeGenerator.getLocalInputVariableValues(ctx, boundExpr)._1.toSeq
val argList = inputVars.map { v =>
s"${CodeGenerator.typeName(v.javaType)} ${v.variableName}"
}
val paramLength = CodeGenerator.calculateParamLengthFromExprValues(inputVars)
if (CodeGenerator.isValidParamLength(paramLength)) {
val switchCaseFunc = ctx.freshName("switchCaseCode")
ctx.addNewFunction(switchCaseFunc,
s"""
|private void $switchCaseFunc(${argList.mkString(", ")}) {
| ${ev.code}
| ${outputColumns(col).isNull} = ${ev.isNull};
| ${outputColumns(col).value} = ${ev.value};
|}
""".stripMargin)

updateCode += s"$switchCaseFunc(${inputVars.map(_.variableName).mkString(", ")});"
} else {
updateCode +=
s"""
|${ev.code}
|${outputColumns(col).isNull} = ${ev.isNull};
|${outputColumns(col).value} = ${ev.value};
""".stripMargin
}
}
}

s"""
|case $row:
| ${updateCode.trim}
| ${updateCode.mkString("\n")}
| break;
""".stripMargin
}
Expand Down