Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/6479#discussion_r31302536
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/GenerateProjection.scala
---
@@ -38,201 +44,215 @@ object GenerateProjection extends
CodeGenerator[Seq[Expression], Projection] {
// Make Mutablility optional...
protected def create(expressions: Seq[Expression]): Projection = {
- val tupleLength = ru.Literal(Constant(expressions.length))
- val lengthDef = q"final val length = $tupleLength"
-
/* TODO: Configurable...
val nullFunctions =
- q"""
+ s"""
private final val nullSet = new
org.apache.spark.util.collection.BitSet(length)
final def setNullAt(i: Int) = nullSet.set(i)
final def isNullAt(i: Int) = nullSet.get(i)
"""
*/
- val nullFunctions =
- q"""
- private[this] var nullBits = new
Array[Boolean](${expressions.size})
- override def setNullAt(i: Int) = { nullBits(i) = true }
- override def isNullAt(i: Int) = nullBits(i)
- """.children
+ val ctx = newCodeGenContext()
+ val columns = expressions.zipWithIndex.map {
+ case (e, i) =>
+ s"private ${primitiveForType(e.dataType)} c$i =
${defaultPrimitive(e.dataType)};\n"
+ }.mkString("\n")
- val tupleElements = expressions.zipWithIndex.flatMap {
+ val tupleElements = expressions.zipWithIndex.map {
case (e, i) =>
- val elementName = newTermName(s"c$i")
- val evaluatedExpression = expressionEvaluator(e)
- val iLit = ru.Literal(Constant(i))
+ val evaluatedExpression = expressionEvaluator(e, ctx)
- q"""
- var ${newTermName(s"c$i")}: ${termForType(e.dataType)} = _
+ s"""
{
- ..${evaluatedExpression.code}
- if(${evaluatedExpression.nullTerm})
- setNullAt($iLit)
- else {
- nullBits($iLit) = false
- $elementName = ${evaluatedExpression.primitiveTerm}
+ ${evaluatedExpression.code}
+ if(${evaluatedExpression.nullTerm}) {
+ setNullAt($i);
+ } else {
+ nullBits[$i] = false;
+ c$i = ${evaluatedExpression.primitiveTerm};
}
}
- """.children : Seq[Tree]
- }
+ """
+ }.mkString("\n")
- val accessorFailure = q"""scala.sys.error("Invalid ordinal:" + i)"""
- val applyFunction = {
- val cases = (0 until expressions.size).map { i =>
- val ordinal = ru.Literal(Constant(i))
- val elementName = newTermName(s"c$i")
- val iLit = ru.Literal(Constant(i))
-
- q"if(i == $ordinal) { if(isNullAt($i)) return null else return
$elementName }"
- }
- q"override def apply(i: Int): Any = { ..$cases; $accessorFailure }"
- }
-
- val updateFunction = {
- val cases = expressions.zipWithIndex.map {case (e, i) =>
- val ordinal = ru.Literal(Constant(i))
- val elementName = newTermName(s"c$i")
- val iLit = ru.Literal(Constant(i))
-
- q"""
- if(i == $ordinal) {
- if(value == null) {
- setNullAt(i)
- } else {
- nullBits(i) = false
- $elementName = value.asInstanceOf[${termForType(e.dataType)}]
- }
- return
- }"""
- }
- q"override def update(i: Int, value: Any): Unit = { ..$cases;
$accessorFailure }"
- }
+ val getCases = (0 until expressions.size).map { i =>
+ s"""if(i == $i) return c$i;\n"""
+ }.mkString("\n")
+ val updateCases = expressions.zipWithIndex.map {case (e, i) =>
+ s"""
+ if(i == $i) {
+ c$i = (${termForType(e.dataType)})value;
+ return;
+ }"""
+ }.mkString("\n")
val specificAccessorFunctions = nativeTypes.map { dataType =>
- val ifStatements = expressions.zipWithIndex.flatMap {
+ val ifStatements = expressions.zipWithIndex.map {
// getString() is not used by expressions
case (e, i) if e.dataType == dataType && dataType != StringType =>
- val elementName = newTermName(s"c$i")
// TODO: The string of ifs gets pretty inefficient as the row
grows in size.
- // TODO: Optional null checks?
- q"if(i == $i) return $elementName" :: Nil
- case _ => Nil
- }
- dataType match {
- // Row() need this interface to compile
- case StringType =>
- q"""
- override def getString(i: Int): String = {
- $accessorFailure
- }"""
- case other =>
- q"""
- override def ${accessorForType(dataType)}(i: Int):
${termForType(dataType)} = {
- ..$ifStatements;
- $accessorFailure
+ s"if(i == $i) return c$i;"
+ case _ => ""
+ }.mkString("\n")
+ if (ifStatements.count(_ != '\n') > 0) {
+ s"""
+ @Override
+ public ${primitiveForType(dataType)}
${accessorForType(dataType)}(int i) {
+ if (isNullAt(i)) {
+ return ${defaultPrimitive(dataType)};
+ }
+ $ifStatements
+ return ${defaultPrimitive(dataType)};
}"""
+ } else {
+ ""
}
- }
+ }.mkString("\n")
val specificMutatorFunctions = nativeTypes.map { dataType =>
- val ifStatements = expressions.zipWithIndex.flatMap {
+ val ifStatements = expressions.zipWithIndex.map {
// setString() is not used by expressions
case (e, i) if e.dataType == dataType && dataType != StringType =>
- val elementName = newTermName(s"c$i")
// TODO: The string of ifs gets pretty inefficient as the row
grows in size.
- // TODO: Optional null checks?
- q"if(i == $i) { nullBits($i) = false; $elementName = value;
return }" :: Nil
- case _ => Nil
+ s"if(i == $i) { c$i = value; return; }"
+ case _ => ""
+ }.mkString("\n")
+ if (ifStatements.count(_ != '\n') > 0) {
+ s"""
+ @Override
+ public void ${mutatorForType(dataType)}(int i,
${primitiveForType(dataType)} value) {
+ nullBits[i] = false;
+ $ifStatements
+ }"""
+ } else {
+ ""
}
- dataType match {
- case StringType =>
- // MutableRow() need this interface to compile
- q"""
- override def setString(i: Int, value: String) {
- $accessorFailure
- }"""
- case other =>
- q"""
- override def ${mutatorForType(dataType)}(i: Int, value:
${termForType(dataType)}) {
- ..$ifStatements;
- $accessorFailure
- }"""
- }
- }
+ }.mkString("\n")
val hashValues = expressions.zipWithIndex.map { case (e,i) =>
val elementName = newTermName(s"c$i")
val nonNull = e.dataType match {
- case BooleanType => q"if ($elementName) 0 else 1"
- case ByteType | ShortType | IntegerType => q"$elementName.toInt"
- case LongType => q"($elementName ^ ($elementName >>> 32)).toInt"
- case FloatType => q"java.lang.Float.floatToIntBits($elementName)"
+ case BooleanType => s"$elementName? 0 : 1"
+ case ByteType | ShortType | IntegerType => s"$elementName"
+ case LongType => s"$elementName ^ ($elementName >>> 32)"
+ case FloatType => s"Float.floatToIntBits($elementName)"
case DoubleType =>
- q"{ val b = java.lang.Double.doubleToLongBits($elementName); (b
^ (b >>>32)).toInt }"
- case _ => q"$elementName.hashCode"
+ s"Double.doubleToLongBits($elementName) ^
(Double.doubleToLongBits($elementName) >>>32)"
+ case _ => s"$elementName.hashCode()"
}
- q"if (isNullAt($i)) 0 else $nonNull"
+ s"isNullAt($i) ? 0 : ($nonNull)"
}
- val hashUpdates: Seq[Tree] = hashValues.map(v => q"""result = 37 *
result + $v""": Tree)
-
- val hashCodeFunction =
- q"""
- override def hashCode(): Int = {
- var result: Int = 37
- ..$hashUpdates
- result
- }
+ val hashUpdates: String = hashValues.map(v =>
+ s"""
+ result *= 37;
+ result += $v;
"""
+ ).mkString("\n")
val columnChecks = (0 until expressions.size).map { i =>
val elementName = newTermName(s"c$i")
- q"if (this.$elementName != specificType.$elementName) return false"
- }
+ s"if (this.$elementName != specificType.$elementName) return
false;\n"
+ }.mkString("\n")
- val equalsFunction =
- q"""
- override def equals(other: Any): Boolean = other match {
- case specificType: SpecificRow =>
- ..$columnChecks
- return true
- case other => super.equals(other)
- }
- """
+ val copyColumns = (0 until expressions.size).map { i =>
+ val elementName = newTermName(s"c$i")
+ s"if (!isNullAt($i)) arr[$i] = (Object)$elementName;\n"
+ }.mkString("\n")
- val allColumns = (0 until expressions.size).map { i =>
- val iLit = ru.Literal(Constant(i))
- q"if(isNullAt($iLit)) { null } else { ${newTermName(s"c$i")} }"
- }
+ val code = s"""
+ import org.apache.spark.sql.Row;
+ import scala.collection.Seq;
+ import scala.collection.mutable.ArraySeq;
- val copyFunction =
- q"override def copy() = new
$genericRowType(Array[Any](..$allColumns))"
-
- val toSeqFunction =
- q"override def toSeq: Seq[Any] = Seq(..$allColumns)"
-
- val classBody =
- nullFunctions ++ (
- lengthDef +:
- applyFunction +:
- updateFunction +:
- equalsFunction +:
- hashCodeFunction +:
- copyFunction +:
- toSeqFunction +:
- (tupleElements ++ specificAccessorFunctions ++
specificMutatorFunctions))
-
- val code = q"""
- final class SpecificRow(i: $rowType) extends $mutableRowType {
- ..$classBody
+ public SpecificProjection generate($exprType[] expr) {
+ return new SpecificProjection(expr);
}
- new $projectionType { def apply(r: $rowType) = new SpecificRow(r) }
+ class SpecificProjection extends ${typeOf[BaseProject]} {
+ private $exprType[] expressions = null;
+
+ public SpecificProjection($exprType[] expr) {
+ expressions = expr;
+ }
+
+ @Override
+ public Object apply(Object r) {
+ return new SpecificRow(expressions, (Row)r);
+ }
+ }
+
+ final class SpecificRow extends ${typeOf[BaseMutableRow]} {
--- End diff --
ah - i think we were discussing dropping this, didn't we?
---
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]