Skip to content

Commit bc7041a

Browse files
ueshinmarmbrus
authored andcommitted
[SPARK-2287] [SQL] Make ScalaReflection be able to handle Generic case classes.
Author: Takuya UESHIN <[email protected]> Closes apache#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.
1 parent 1e2c26c commit bc7041a

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/ScalaReflection.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ object ScalaReflection {
4747
val TypeRef(_, _, Seq(optType)) = t
4848
Schema(schemaFor(optType).dataType, nullable = true)
4949
case t if t <:< typeOf[Product] =>
50-
val params = t.member("<init>": TermName).asMethod.paramss
50+
val formalTypeArgs = t.typeSymbol.asClass.typeParams
51+
val TypeRef(_, _, actualTypeArgs) = t
52+
val params = t.member(nme.CONSTRUCTOR).asMethod.paramss
5153
Schema(StructType(
5254
params.head.map { p =>
53-
val Schema(dataType, nullable) = schemaFor(p.typeSignature)
55+
val Schema(dataType, nullable) =
56+
schemaFor(p.typeSignature.substituteTypes(formalTypeArgs, actualTypeArgs))
5457
StructField(p.name.toString, dataType, nullable)
5558
}), nullable = true)
5659
// Need to decide if we actually need a special type here.

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/ScalaReflectionSuite.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ case class ComplexData(
6060
mapField: Map[Int, String],
6161
structField: PrimitiveData)
6262

63+
case class GenericData[A](
64+
genericField: A)
65+
6366
class ScalaReflectionSuite extends FunSuite {
6467
import ScalaReflection._
6568

@@ -128,4 +131,21 @@ class ScalaReflectionSuite extends FunSuite {
128131
nullable = true))),
129132
nullable = true))
130133
}
134+
135+
test("generic data") {
136+
val schema = schemaFor[GenericData[Int]]
137+
assert(schema === Schema(
138+
StructType(Seq(
139+
StructField("genericField", IntegerType, nullable = false))),
140+
nullable = true))
141+
}
142+
143+
test("tuple data") {
144+
val schema = schemaFor[(Int, String)]
145+
assert(schema === Schema(
146+
StructType(Seq(
147+
StructField("_1", IntegerType, nullable = false),
148+
StructField("_2", StringType, nullable = true))),
149+
nullable = true))
150+
}
131151
}

0 commit comments

Comments
 (0)