Skip to content

Backport [SPARK-15062][SQL] fix list type infer serializer issue #15380

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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 @@ -485,6 +485,13 @@ object ScalaReflection extends ScalaReflection {
extractorFor(unwrapped, optType, newPath))
}

// Since List[_] also belongs to localTypeOf[Product], we put this case before
// "case t if t <:< localTypeOf[Product]" to make sure it will match to the
// case "localTypeOf[Seq[_]]"
case t if t <:< localTypeOf[Seq[_]] =>
val TypeRef(_, _, Seq(elementType)) = t
toCatalystArray(inputObject, elementType)

case t if t <:< localTypeOf[Product] =>
val params = getConstructorParameters(t)
val nonNullOutput = CreateNamedStruct(params.flatMap { case (fieldName, fieldType) =>
Expand All @@ -500,10 +507,6 @@ object ScalaReflection extends ScalaReflection {
val TypeRef(_, _, Seq(elementType)) = t
toCatalystArray(inputObject, elementType)

case t if t <:< localTypeOf[Seq[_]] =>
val TypeRef(_, _, Seq(elementType)) = t
toCatalystArray(inputObject, elementType)

case t if t <:< localTypeOf[Map[_, _]] =>
val TypeRef(_, _, Seq(keyType, valueType)) = t

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import java.math.BigInteger
import java.sql.{Date, Timestamp}

import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.expressions.{BoundReference, Literal, NewInstance}
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.types.UTF8String

case class PrimitiveData(
intField: Int,
Expand Down Expand Up @@ -229,4 +231,16 @@ class ScalaReflectionSuite extends SparkFunSuite {
assert(anyTypes.forall(!_.isPrimitive))
assert(anyTypes === Seq(classOf[java.lang.Object], classOf[java.lang.Object]))
}

test("SPARK-15062: Get correct serializer for List[_]") {
val list = List(1, 2, 3)
val serializer = extractorsFor[List[Int]](BoundReference(
0, ObjectType(list.getClass), nullable = false))
assert(serializer.children.size == 2)
assert(serializer.children.head.isInstanceOf[Literal])
assert(serializer.children.head.asInstanceOf[Literal].value === UTF8String.fromString("value"))
assert(serializer.children.last.isInstanceOf[NewInstance])
assert(serializer.children.last.asInstanceOf[NewInstance]
.cls.isInstanceOf[Class[org.apache.spark.sql.catalyst.util.GenericArrayData]])
}
}