Skip to content

Commit

Permalink
Modified ScalaReflection.schemaFor to take primary constructor of Pro…
Browse files Browse the repository at this point in the history
…duct when there are multiple constructors. Added test to suite which failed before but works now.
  • Loading branch information
jkbradley committed Dec 9, 2014
1 parent ab2abcb commit 796b2e4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,19 @@ trait ScalaReflection {
case t if t <:< typeOf[Product] =>
val formalTypeArgs = t.typeSymbol.asClass.typeParams
val TypeRef(_, _, actualTypeArgs) = t
val params = t.member(nme.CONSTRUCTOR).asMethod.paramss
val constructorSymbol = t.member(nme.CONSTRUCTOR)
val params = if (constructorSymbol.isMethod) {
constructorSymbol.asMethod.paramss
} else {
// Find the primary constructor, and use its parameter ordering.
val primaryConstructorSymbol: Option[Symbol] = constructorSymbol.asTerm.alternatives.find(
s => s.isMethod && s.asMethod.isPrimaryConstructor)
if (primaryConstructorSymbol.isEmpty) {
sys.error("Internal SQL error: Product object did not have a primary constructor.")
} else {
primaryConstructorSymbol.get.asMethod.paramss
}
}
Schema(StructType(
params.head.map { p =>
val Schema(dataType, nullable) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ case class ComplexData(
case class GenericData[A](
genericField: A)

case class MultipleConstructorsData(a: Int, b: String, c: Double) {
def this(b: String, a: Int) = this(a, b, c = 1.0)
}

class ScalaReflectionSuite extends FunSuite {
import ScalaReflection._

Expand Down Expand Up @@ -253,4 +257,14 @@ class ScalaReflectionSuite extends FunSuite {
Row(1, 1, 1, 1, 1, 1, true))
assert(convertToCatalyst(data, dataType) === convertedData)
}

test("infer schema from case class with multiple constructors") {
val dataType = schemaFor[MultipleConstructorsData].dataType
dataType match {
case s: StructType =>
// Schema should have order: a: Int, b: String, c: Double
assert(s.fieldNames === Seq("a", "b", "c"))
assert(s.fields.map(_.dataType) === Seq(IntegerType, StringType, DoubleType))
}
}
}

0 comments on commit 796b2e4

Please sign in to comment.