-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-9620] [SQL] generated UnsafeProjection should support many columns or large exressions #8044
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 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
495e932
support wider table with more than 1k columns for generated projections
75ccd01
Merge branch 'master' of github.com:apache/spark into wider_table
4518e17
Merge branch 'master' of github.com:apache/spark into wider_table
77ed72d
address comments
1b95be4
put the generated class into sql package
ffcd132
address comments
737b3d3
Merge branch 'master' of github.com:apache/spark into wider_table
d1ef81a
fix failed tests
9192e6c
fix generated safe projection
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 |
|---|---|---|
|
|
@@ -56,19 +56,19 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro | |
|
|
||
| def genAdditionalSize(dt: DataType, ev: GeneratedExpressionCode): String = dt match { | ||
| case t: DecimalType if t.precision > Decimal.MAX_LONG_DIGITS => | ||
| s" + $DecimalWriter.getSize(${ev.primitive})" | ||
| s"$DecimalWriter.getSize(${ev.primitive})" | ||
| case StringType => | ||
| s" + (${ev.isNull} ? 0 : $StringWriter.getSize(${ev.primitive}))" | ||
| s"${ev.isNull} ? 0 : $StringWriter.getSize(${ev.primitive})" | ||
| case BinaryType => | ||
| s" + (${ev.isNull} ? 0 : $BinaryWriter.getSize(${ev.primitive}))" | ||
| s"${ev.isNull} ? 0 : $BinaryWriter.getSize(${ev.primitive})" | ||
| case CalendarIntervalType => | ||
| s" + (${ev.isNull} ? 0 : 16)" | ||
| s"${ev.isNull} ? 0 : 16" | ||
| case _: StructType => | ||
| s" + (${ev.isNull} ? 0 : $StructWriter.getSize(${ev.primitive}))" | ||
| s"${ev.isNull} ? 0 : $StructWriter.getSize(${ev.primitive})" | ||
| case _: ArrayType => | ||
| s" + (${ev.isNull} ? 0 : $ArrayWriter.getSize(${ev.primitive}))" | ||
| s"${ev.isNull} ? 0 : $ArrayWriter.getSize(${ev.primitive})" | ||
| case _: MapType => | ||
| s" + (${ev.isNull} ? 0 : $MapWriter.getSize(${ev.primitive}))" | ||
| s"${ev.isNull} ? 0 : $MapWriter.getSize(${ev.primitive})" | ||
| case _ => "" | ||
| } | ||
|
|
||
|
|
@@ -125,64 +125,68 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro | |
| */ | ||
| private def createCodeForStruct( | ||
| ctx: CodeGenContext, | ||
| row: String, | ||
| inputs: Seq[GeneratedExpressionCode], | ||
| inputTypes: Seq[DataType]): GeneratedExpressionCode = { | ||
|
|
||
| val fixedSize = 8 * inputTypes.length + UnsafeRow.calculateBitSetWidthInBytes(inputTypes.length) | ||
|
|
||
| val output = ctx.freshName("convertedStruct") | ||
| ctx.addMutableState("UnsafeRow", output, s"$output = new UnsafeRow();") | ||
| ctx.addMutableState("UnsafeRow", output, s"this.$output = new UnsafeRow();") | ||
| val buffer = ctx.freshName("buffer") | ||
| ctx.addMutableState("byte[]", buffer, s"$buffer = new byte[64];") | ||
| val numBytes = ctx.freshName("numBytes") | ||
| ctx.addMutableState("byte[]", buffer, s"this.$buffer = new byte[$fixedSize];") | ||
| val cursor = ctx.freshName("cursor") | ||
| ctx.addMutableState("int", cursor, s"this.$cursor = 0;") | ||
| val tmp = ctx.freshName("tmpBuffer") | ||
|
|
||
| val convertedFields = inputTypes.zip(inputs).map { case (dt, input) => | ||
| createConvertCode(ctx, input, dt) | ||
| } | ||
|
|
||
| val fixedSize = 8 * inputTypes.length + UnsafeRow.calculateBitSetWidthInBytes(inputTypes.length) | ||
| val additionalSize = inputTypes.zip(convertedFields).map { case (dt, ev) => | ||
| genAdditionalSize(dt, ev) | ||
| }.mkString("") | ||
|
|
||
| val fieldWriters = inputTypes.zip(convertedFields).zipWithIndex.map { case ((dt, ev), i) => | ||
| val update = genFieldWriter(ctx, dt, ev, output, i, cursor) | ||
| if (dt.isInstanceOf[DecimalType]) { | ||
| // Can't call setNullAt() for DecimalType | ||
| val convertedFields = inputTypes.zip(inputs).zipWithIndex.map { case ((dt, input), i) => | ||
| val ev = createConvertCode(ctx, input, dt) | ||
| val growBuffer = if (!UnsafeRow.isFixedLength(dt)) { | ||
| val numBytes = ctx.freshName("numBytes") | ||
| s""" | ||
| int $numBytes = $cursor + (${genAdditionalSize(dt, ev)}); | ||
| if ($buffer.length < $numBytes) { | ||
| // This will not happen frequently, because the buffer is re-used. | ||
| byte[] $tmp = new byte[$numBytes * 3 / 2]; | ||
|
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. we should do 8 byte alignment here |
||
| System.arraycopy($buffer, 0, $tmp, 0, $buffer.length); | ||
|
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. and use PlatformDependent.copyMemory |
||
| $buffer = $tmp; | ||
| } | ||
| $output.pointTo($buffer, $PlatformDependent.BYTE_ARRAY_OFFSET, | ||
| ${inputTypes.length}, $numBytes); | ||
| """ | ||
| } else { | ||
| "" | ||
| } | ||
| val update = dt match { | ||
| case dt: DecimalType if dt.precision > Decimal.MAX_LONG_DIGITS => | ||
| // Can't call setNullAt() for DecimalType | ||
| s""" | ||
| if (${ev.isNull}) { | ||
| $cursor += $DecimalWriter.write($output, $i, $cursor, null); | ||
| $cursor += $DecimalWriter.write($output, $i, $cursor, null); | ||
| } else { | ||
| $update; | ||
| ${genFieldWriter(ctx, dt, ev, output, i, cursor)}; | ||
| } | ||
| """ | ||
| } else { | ||
| s""" | ||
| case _ => | ||
| s""" | ||
| if (${ev.isNull}) { | ||
| $output.setNullAt($i); | ||
| } else { | ||
| $update; | ||
| ${genFieldWriter(ctx, dt, ev, output, i, cursor)}; | ||
| } | ||
| """ | ||
| } | ||
| }.mkString("\n") | ||
| s""" | ||
| ${ev.code} | ||
| $growBuffer | ||
| $update | ||
| """ | ||
| } | ||
|
|
||
| val code = s""" | ||
| ${convertedFields.map(_.code).mkString("\n")} | ||
|
|
||
| final int $numBytes = $fixedSize $additionalSize; | ||
| if ($numBytes > $buffer.length) { | ||
| $buffer = new byte[$numBytes]; | ||
| } | ||
|
|
||
| $output.pointTo( | ||
| $buffer, | ||
| $PlatformDependent.BYTE_ARRAY_OFFSET, | ||
| ${inputTypes.length}, | ||
| $numBytes); | ||
|
|
||
| int $cursor = $fixedSize; | ||
|
|
||
| $fieldWriters | ||
| $cursor = $fixedSize; | ||
| $output.pointTo($buffer, $PlatformDependent.BYTE_ARRAY_OFFSET, ${inputTypes.length}, $cursor); | ||
| ${ctx.splitExpressions(row, convertedFields)} | ||
| """ | ||
| GeneratedExpressionCode(code, "false", output) | ||
| } | ||
|
|
@@ -400,7 +404,7 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro | |
| val fieldIsNull = s"$tmp.isNullAt($i)" | ||
| GeneratedExpressionCode("", fieldIsNull, getFieldCode) | ||
| } | ||
| val converter = createCodeForStruct(ctx, fieldEvals, fieldTypes) | ||
| val converter = createCodeForStruct(ctx, tmp, fieldEvals, fieldTypes) | ||
| val code = s""" | ||
| ${input.code} | ||
| UnsafeRow $output = null; | ||
|
|
@@ -427,7 +431,7 @@ object GenerateUnsafeProjection extends CodeGenerator[Seq[Expression], UnsafePro | |
| def createCode(ctx: CodeGenContext, expressions: Seq[Expression]): GeneratedExpressionCode = { | ||
| val exprEvals = expressions.map(e => e.gen(ctx)) | ||
| val exprTypes = expressions.map(_.dataType) | ||
| createCodeForStruct(ctx, exprEvals, exprTypes) | ||
| createCodeForStruct(ctx, "i", exprEvals, exprTypes) | ||
| } | ||
|
|
||
| protected def canonicalize(in: Seq[Expression]): Seq[Expression] = | ||
|
|
||
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
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.
can you document what "input" is here?