-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-25746][SQL][followup] do not add unnecessary If expression #22898
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -89,59 +89,51 @@ object ExpressionEncoder { | |
| */ | ||
| def tuple(encoders: Seq[ExpressionEncoder[_]]): ExpressionEncoder[_] = { | ||
| // TODO: check if encoders length is more than 22 and throw exception for it. | ||
|
|
||
| encoders.foreach(_.assertUnresolved()) | ||
|
|
||
| val schema = StructType(encoders.zipWithIndex.map { | ||
| case (e, i) => | ||
| StructField(s"_${i + 1}", e.objSerializer.dataType, e.objSerializer.nullable) | ||
| }) | ||
|
|
||
| val cls = Utils.getContextOrSparkClassLoader.loadClass(s"scala.Tuple${encoders.size}") | ||
|
|
||
| val newSerializerInput = BoundReference(0, ObjectType(cls), nullable = true) | ||
| val serializers = encoders.zipWithIndex.map { case (enc, index) => | ||
| val boundRefs = enc.objSerializer.collect { case b: BoundReference => b }.distinct | ||
| assert(boundRefs.size == 1, "object serializer should have only one bound reference but " + | ||
| s"there are ${boundRefs.size}") | ||
|
|
||
| val originalInputObject = boundRefs.head | ||
| val newInputObject = Invoke( | ||
| BoundReference(0, ObjectType(cls), nullable = true), | ||
| newSerializerInput, | ||
| s"_${index + 1}", | ||
| originalInputObject.dataType, | ||
| returnNullable = originalInputObject.nullable) | ||
|
|
||
| val newSerializer = enc.objSerializer.transformUp { | ||
| case b: BoundReference => newInputObject | ||
| case BoundReference(0, _, _) => newInputObject | ||
| } | ||
|
|
||
| Alias(newSerializer, s"_${index + 1}")() | ||
| } | ||
| val newSerializer = CreateStruct(serializers) | ||
|
|
||
| val childrenDeserializers = encoders.zipWithIndex.map { case (enc, index) => | ||
| val newDeserializerInput = GetColumnByOrdinal(0, newSerializer.dataType) | ||
| val deserializers = encoders.zipWithIndex.map { case (enc, index) => | ||
| val getColExprs = enc.objDeserializer.collect { case c: GetColumnByOrdinal => c }.distinct | ||
| assert(getColExprs.size == 1, "object deserializer should have only one " + | ||
| s"`GetColumnByOrdinal`, but there are ${getColExprs.size}") | ||
|
|
||
| val input = GetStructField(GetColumnByOrdinal(0, schema), index) | ||
| val newDeserializer = enc.objDeserializer.transformUp { | ||
| val input = GetStructField(newDeserializerInput, index) | ||
| enc.objDeserializer.transformUp { | ||
| case GetColumnByOrdinal(0, _) => input | ||
| } | ||
| if (schema(index).nullable) { | ||
| If(IsNull(input), Literal.create(null, newDeserializer.dataType), newDeserializer) | ||
| } else { | ||
| newDeserializer | ||
| } | ||
|
||
| } | ||
| val newDeserializer = NewInstance(cls, deserializers, ObjectType(cls), propagateNull = false) | ||
|
|
||
| val serializer = If(IsNull(BoundReference(0, ObjectType(cls), nullable = true)), | ||
| Literal.create(null, schema), CreateStruct(serializers)) | ||
| val deserializer = | ||
| NewInstance(cls, childrenDeserializers, ObjectType(cls), propagateNull = false) | ||
| def nullSafe(input: Expression, result: Expression): Expression = { | ||
| If(IsNull(input), Literal.create(null, result.dataType), result) | ||
| } | ||
|
|
||
| new ExpressionEncoder[Any]( | ||
| serializer, | ||
| deserializer, | ||
| nullSafe(newSerializerInput, newSerializer), | ||
| nullSafe(newDeserializerInput, newDeserializer), | ||
| ClassTag(cls)) | ||
| } | ||
|
|
||
|
|
||
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.
oh, I see. If the child deserializer is a tuple deserializer, it is just
So it misses the
If(IsNull(..), null, ...)pattern. We should wrap theNewInstancewithIf(IsNull(..), null)at L139.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.
good catch!