Skip to content

Commit 7de5706

Browse files
committed
Make ScalaReflection be able to handle Generic case classes.
1 parent 441cdcc commit 7de5706

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ object ScalaReflection {
4545
val TypeRef(_, _, Seq(optType)) = t
4646
schemaFor(optType)
4747
case t if t <:< typeOf[Product] =>
48-
val params = t.member("<init>": TermName).asMethod.paramss
48+
val formalTypeArgs = t.typeSymbol.asClass.typeParams
49+
val TypeRef(_, _, actualTypeArgs) = t
50+
val params = t.member(nme.CONSTRUCTOR).asMethod.paramss
4951
StructType(
5052
params.head.map(p =>
51-
StructField(p.name.toString, schemaFor(p.typeSignature), nullable = true)))
53+
StructField(
54+
p.name.toString,
55+
schemaFor(p.typeSignature.substituteTypes(formalTypeArgs, actualTypeArgs)),
56+
nullable = true)))
5257
// Need to decide if we actually need a special type here.
5358
case t if t <:< typeOf[Array[Byte]] => BinaryType
5459
case t if t <:< typeOf[Array[_]] =>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.sql.catalyst
19+
20+
import java.sql.Timestamp
21+
22+
import org.scalatest.FunSuite
23+
24+
import org.apache.spark.sql.catalyst.expressions._
25+
import org.apache.spark.sql.catalyst.types._
26+
27+
case class GenericData[A](
28+
genericField: A)
29+
30+
class ScalaReflectionSuite extends FunSuite {
31+
32+
test("generic data") {
33+
val schema = ScalaReflection.schemaFor[GenericData[Int]]
34+
assert(schema ===
35+
StructType(Seq(
36+
StructField("genericField", IntegerType, nullable = true))))
37+
}
38+
39+
test("tuple data") {
40+
val schema = ScalaReflection.schemaFor[(Int, String)]
41+
assert(schema ===
42+
StructType(Seq(
43+
StructField("_1", IntegerType, nullable = true),
44+
StructField("_2", StringType, nullable = true))))
45+
}
46+
}

0 commit comments

Comments
 (0)