-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-9373][SQL] Support StructType in Tungsten projection #7689
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
321859a
[SPARK-9373][SQL] Support StructType in Tungsten projection [WIP]
rxin 525b95b
Merged with master, more documentation & test cases.
rxin 6b781fe
Reset the change in DataFrameSuite.
rxin 9f36216
Updated comment.
rxin ac203bf
More comments.
rxin ac4951d
Yay.
rxin 77e8d0e
Fixed NondeterministicSuite.
rxin 10c4b7c
Format generated code.
rxin be9f377
Fixed tests.
rxin 9162f42
Support IntervalType in UnsafeRow's getter.
rxin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,11 +34,13 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro | |
| private val StringWriter = classOf[UnsafeRowWriters.UTF8StringWriter].getName | ||
| private val BinaryWriter = classOf[UnsafeRowWriters.BinaryWriter].getName | ||
| private val IntervalWriter = classOf[UnsafeRowWriters.IntervalWriter].getName | ||
| private val StructWriter = classOf[UnsafeRowWriters.StructWriter].getName | ||
|
|
||
| /** Returns true iff we support this data type. */ | ||
| def canSupport(dataType: DataType): Boolean = dataType match { | ||
| case t: AtomicType if !t.isInstanceOf[DecimalType] => true | ||
| case _: IntervalType => true | ||
| case t: StructType => t.toSeq.forall(field => canSupport(field.dataType)) | ||
| case NullType => true | ||
| case _ => false | ||
| } | ||
|
|
@@ -55,15 +57,22 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro | |
|
|
||
| val ret = ev.primitive | ||
| ctx.addMutableState("UnsafeRow", ret, s"$ret = new UnsafeRow();") | ||
| val bufferTerm = ctx.freshName("buffer") | ||
| ctx.addMutableState("byte[]", bufferTerm, s"$bufferTerm = new byte[64];") | ||
| val cursorTerm = ctx.freshName("cursor") | ||
| val numBytesTerm = ctx.freshName("numBytes") | ||
| val buffer = ctx.freshName("buffer") | ||
| ctx.addMutableState("byte[]", buffer, s"$buffer = new byte[64];") | ||
| val cursor = ctx.freshName("cursor") | ||
| val numBytes = ctx.freshName("numBytes") | ||
|
|
||
| val exprs = expressions.map(_.gen(ctx)) | ||
| val exprs = expressions.zipWithIndex.map { case (e, i) => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the index |
||
| e.dataType match { | ||
| case st: StructType => | ||
| createCodeForStruct(ctx, e.gen(ctx), st) | ||
| case _ => | ||
| e.gen(ctx) | ||
| } | ||
| } | ||
| val allExprs = exprs.map(_.code).mkString("\n") | ||
| val fixedSize = 8 * exprs.length + UnsafeRow.calculateBitSetWidthInBytes(exprs.length) | ||
|
|
||
| val fixedSize = 8 * exprs.length + UnsafeRow.calculateBitSetWidthInBytes(exprs.length) | ||
| val additionalSize = expressions.zipWithIndex.map { case (e, i) => | ||
| e.dataType match { | ||
| case StringType => | ||
|
|
@@ -72,6 +81,8 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro | |
| s" + (${exprs(i).isNull} ? 0 : $BinaryWriter.getSize(${exprs(i).primitive}))" | ||
| case IntervalType => | ||
| s" + (${exprs(i).isNull} ? 0 : 16)" | ||
| case _: StructType => | ||
| s" + (${exprs(i).isNull} ? 0 : $StructWriter.getSize(${exprs(i).primitive}))" | ||
| case _ => "" | ||
| } | ||
| }.mkString("") | ||
|
|
@@ -81,11 +92,13 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro | |
| case dt if ctx.isPrimitiveType(dt) => | ||
| s"${ctx.setColumn(ret, dt, i, exprs(i).primitive)}" | ||
| case StringType => | ||
| s"$cursorTerm += $StringWriter.write($ret, $i, $cursorTerm, ${exprs(i).primitive})" | ||
| s"$cursor += $StringWriter.write($ret, $i, $cursor, ${exprs(i).primitive})" | ||
| case BinaryType => | ||
| s"$cursorTerm += $BinaryWriter.write($ret, $i, $cursorTerm, ${exprs(i).primitive})" | ||
| s"$cursor += $BinaryWriter.write($ret, $i, $cursor, ${exprs(i).primitive})" | ||
| case IntervalType => | ||
| s"$cursorTerm += $IntervalWriter.write($ret, $i, $cursorTerm, ${exprs(i).primitive})" | ||
| s"$cursor += $IntervalWriter.write($ret, $i, $cursor, ${exprs(i).primitive})" | ||
| case t: StructType => | ||
| s"$cursor += $StructWriter.write($ret, $i, $cursor, ${exprs(i).primitive})" | ||
| case NullType => "" | ||
| case _ => | ||
| throw new UnsupportedOperationException(s"Not supported DataType: ${e.dataType}") | ||
|
|
@@ -99,24 +112,139 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro | |
|
|
||
| s""" | ||
| $allExprs | ||
| int $numBytesTerm = $fixedSize $additionalSize; | ||
| if ($numBytesTerm > $bufferTerm.length) { | ||
| $bufferTerm = new byte[$numBytesTerm]; | ||
| int $numBytes = $fixedSize $additionalSize; | ||
| if ($numBytes > $buffer.length) { | ||
| $buffer = new byte[$numBytes]; | ||
| } | ||
|
|
||
| $ret.pointTo( | ||
| $bufferTerm, | ||
| $buffer, | ||
| org.apache.spark.unsafe.PlatformDependent.BYTE_ARRAY_OFFSET, | ||
| ${expressions.size}, | ||
| $numBytesTerm); | ||
| int $cursorTerm = $fixedSize; | ||
|
|
||
| $numBytes); | ||
| int $cursor = $fixedSize; | ||
|
|
||
| $writers | ||
| boolean ${ev.isNull} = false; | ||
| """ | ||
| } | ||
|
|
||
| /** | ||
| * Generates the Java code to convert a struct (backed by InternalRow) to UnsafeRow. | ||
| * | ||
| * This function also handles nested structs by recursively generating the code to do conversion. | ||
| * | ||
| * @param ctx code generation context | ||
| * @param input the input struct, identified by a [[GeneratedExpressionCode]] | ||
| * @param schema schema of the struct field | ||
| */ | ||
| // TODO: refactor createCode and this function to reduce code duplication. | ||
| private def createCodeForStruct( | ||
| ctx: CodeGenContext, | ||
| input: GeneratedExpressionCode, | ||
| schema: StructType): GeneratedExpressionCode = { | ||
|
|
||
| val isNull = input.isNull | ||
| val primitive = ctx.freshName("structConvert") | ||
| ctx.addMutableState("UnsafeRow", primitive, s"$primitive = new UnsafeRow();") | ||
| val buffer = ctx.freshName("buffer") | ||
| ctx.addMutableState("byte[]", buffer, s"$buffer = new byte[64];") | ||
| val cursor = ctx.freshName("cursor") | ||
|
|
||
| val exprs: Seq[GeneratedExpressionCode] = schema.map(_.dataType).zipWithIndex.map { | ||
| case (dt, i) => dt match { | ||
| case st: StructType => | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks underindented? |
||
| val nestedStructEv = GeneratedExpressionCode( | ||
| code = "", | ||
| isNull = s"${input.primitive}.isNullAt($i)", | ||
| primitive = s"${ctx.getColumn(input.primitive, dt, i)}" | ||
| ) | ||
| createCodeForStruct(ctx, nestedStructEv, st) | ||
| case _ => | ||
| GeneratedExpressionCode( | ||
| code = "", | ||
| isNull = s"${input.primitive}.isNullAt($i)", | ||
| primitive = s"${ctx.getColumn(input.primitive, dt, i)}" | ||
| ) | ||
| } | ||
| } | ||
| val allExprs = exprs.map(_.code).mkString("\n") | ||
|
|
||
| val fixedSize = 8 * exprs.length + UnsafeRow.calculateBitSetWidthInBytes(exprs.length) | ||
| val additionalSize = schema.toSeq.map(_.dataType).zip(exprs).map { case (dt, ev) => | ||
| dt match { | ||
| case StringType => | ||
| s" + (${ev.isNull} ? 0 : $StringWriter.getSize(${ev.primitive}))" | ||
| case BinaryType => | ||
| s" + (${ev.isNull} ? 0 : $BinaryWriter.getSize(${ev.primitive}))" | ||
| case IntervalType => | ||
| s" + (${ev.isNull} ? 0 : 16)" | ||
| case _: StructType => | ||
| s" + (${ev.isNull} ? 0 : $StructWriter.getSize(${ev.primitive}))" | ||
| case _ => "" | ||
| } | ||
| }.mkString("") | ||
|
|
||
| val writers = schema.toSeq.map(_.dataType).zip(exprs).zipWithIndex.map { case ((dt, ev), i) => | ||
| val update = dt match { | ||
| case _ if ctx.isPrimitiveType(dt) => | ||
| s"${ctx.setColumn(primitive, dt, i, exprs(i).primitive)}" | ||
| case StringType => | ||
| s"$cursor += $StringWriter.write($primitive, $i, $cursor, ${exprs(i).primitive})" | ||
| case BinaryType => | ||
| s"$cursor += $BinaryWriter.write($primitive, $i, $cursor, ${exprs(i).primitive})" | ||
| case IntervalType => | ||
| s"$cursor += $IntervalWriter.write($primitive, $i, $cursor, ${exprs(i).primitive})" | ||
| case t: StructType => | ||
| s"$cursor += $StructWriter.write($primitive, $i, $cursor, ${exprs(i).primitive})" | ||
| case NullType => "" | ||
| case _ => | ||
| throw new UnsupportedOperationException(s"Not supported DataType: $dt") | ||
| } | ||
| s""" | ||
| if (${exprs(i).isNull}) { | ||
| $primitive.setNullAt($i); | ||
| } else { | ||
| $update; | ||
| } | ||
| """ | ||
| }.mkString("\n ") | ||
|
|
||
| // Note that we add a shortcut here for performance: if the input is already an UnsafeRow, | ||
| // just copy the bytes directly into our buffer space without running any conversion. | ||
| // We also had to use a hack to introduce a "tmp" variable, to avoid the Java compiler from | ||
| // complaining that a GenericMutableRow (generated by expressions) cannot be cast to UnsafeRow. | ||
| val tmp = ctx.freshName("tmp") | ||
| val numBytes = ctx.freshName("numBytes") | ||
| val code = s""" | ||
| |${input.code} | ||
| |if (!${input.isNull}) { | ||
| | Object $tmp = (Object) ${input.primitive}; | ||
| | if ($tmp instanceof UnsafeRow) { | ||
| | $primitive = (UnsafeRow) $tmp; | ||
| | } else { | ||
| | $allExprs | ||
| | | ||
| | int $numBytes = $fixedSize $additionalSize; | ||
| | if ($numBytes > $buffer.length) { | ||
| | $buffer = new byte[$numBytes]; | ||
| | } | ||
| | | ||
| | $primitive.pointTo( | ||
| | $buffer, | ||
| | org.apache.spark.unsafe.PlatformDependent.BYTE_ARRAY_OFFSET, | ||
| | ${exprs.size}, | ||
| | $numBytes); | ||
| | int $cursor = $fixedSize; | ||
| | | ||
| | $writers | ||
| | } | ||
| |} | ||
| """.stripMargin | ||
|
|
||
| GeneratedExpressionCode(code, isNull, primitive) | ||
| } | ||
|
|
||
| protected def canonicalize(in: Seq[Expression]): Seq[Expression] = | ||
| in.map(ExpressionCanonicalizer.execute) | ||
|
|
||
|
|
@@ -159,7 +287,7 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro | |
| } | ||
| """ | ||
|
|
||
| logDebug(s"code for ${expressions.mkString(",")}:\n$code") | ||
| logDebug(s"code for ${expressions.mkString(",")}:\n${CodeFormatter.format(code)}") | ||
|
|
||
| val c = compile(code) | ||
| c.generate(ctx.references.toArray).asInstanceOf[UnsafeProjection] | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: string -> struct