Skip to content
Snippets Groups Projects
Commit bc7041a4 authored by Takuya UESHIN's avatar Takuya UESHIN Committed by Michael Armbrust
Browse files

[SPARK-2287] [SQL] Make ScalaReflection be able to handle Generic case classes.

Author: Takuya UESHIN <ueshin@happy-camper.st>

Closes #1226 from ueshin/issues/SPARK-2287 and squashes the following commits:

32ef7c3 [Takuya UESHIN] Add execution of `SHOW TABLES` before `TestHive.reset()`.
541dc8d [Takuya UESHIN] Merge branch 'master' into issues/SPARK-2287
fac5fae [Takuya UESHIN] Remove unnecessary method receiver.
d306e60 [Takuya UESHIN] Merge branch 'master' into issues/SPARK-2287
7de5706 [Takuya UESHIN] Make ScalaReflection be able to handle Generic case classes.
parent 1e2c26c8
No related branches found
No related tags found
No related merge requests found
......@@ -47,10 +47,13 @@ object ScalaReflection {
val TypeRef(_, _, Seq(optType)) = t
Schema(schemaFor(optType).dataType, nullable = true)
case t if t <:< typeOf[Product] =>
val params = t.member("<init>": TermName).asMethod.paramss
val formalTypeArgs = t.typeSymbol.asClass.typeParams
val TypeRef(_, _, actualTypeArgs) = t
val params = t.member(nme.CONSTRUCTOR).asMethod.paramss
Schema(StructType(
params.head.map { p =>
val Schema(dataType, nullable) = schemaFor(p.typeSignature)
val Schema(dataType, nullable) =
schemaFor(p.typeSignature.substituteTypes(formalTypeArgs, actualTypeArgs))
StructField(p.name.toString, dataType, nullable)
}), nullable = true)
// Need to decide if we actually need a special type here.
......
......@@ -60,6 +60,9 @@ case class ComplexData(
mapField: Map[Int, String],
structField: PrimitiveData)
case class GenericData[A](
genericField: A)
class ScalaReflectionSuite extends FunSuite {
import ScalaReflection._
......@@ -128,4 +131,21 @@ class ScalaReflectionSuite extends FunSuite {
nullable = true))),
nullable = true))
}
test("generic data") {
val schema = schemaFor[GenericData[Int]]
assert(schema === Schema(
StructType(Seq(
StructField("genericField", IntegerType, nullable = false))),
nullable = true))
}
test("tuple data") {
val schema = schemaFor[(Int, String)]
assert(schema === Schema(
StructType(Seq(
StructField("_1", IntegerType, nullable = false),
StructField("_2", StringType, nullable = true))),
nullable = true))
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment