Skip to content
Closed
Changes from all commits
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 @@ -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)
Copy link
Member

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

val deserializer =
      NewInstance(cls, childrenDeserializers, ObjectType(cls), propagateNull = false)

So it misses the If(IsNull(..), null, ...) pattern. We should wrap the NewInstance with If(IsNull(..), null) at L139.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch!

} else {
newDeserializer
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

}
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))
}

Expand Down