Skip to content

Commit

Permalink
[SPARK-20289][SQL] Use StaticInvoke to box primitive types
Browse files Browse the repository at this point in the history
## What changes were proposed in this pull request?
Dataset typed API currently uses NewInstance to box primitive types (i.e. calling the constructor). Instead, it'd be slightly more idiomatic in Java to use PrimitiveType.valueOf, which can be invoked using StaticInvoke expression.

## How was this patch tested?
The change should be covered by existing tests for Dataset encoders.

Author: Reynold Xin <rxin@databricks.com>

Closes #17604 from rxin/SPARK-20289.
  • Loading branch information
rxin committed Apr 11, 2017
1 parent cd91f96 commit 123b4fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,20 +204,19 @@ object JavaTypeInference {
typeToken.getRawType match {
case c if !inferExternalType(c).isInstanceOf[ObjectType] => getPath

case c if c == classOf[java.lang.Short] =>
NewInstance(c, getPath :: Nil, ObjectType(c))
case c if c == classOf[java.lang.Integer] =>
NewInstance(c, getPath :: Nil, ObjectType(c))
case c if c == classOf[java.lang.Long] =>
NewInstance(c, getPath :: Nil, ObjectType(c))
case c if c == classOf[java.lang.Double] =>
NewInstance(c, getPath :: Nil, ObjectType(c))
case c if c == classOf[java.lang.Byte] =>
NewInstance(c, getPath :: Nil, ObjectType(c))
case c if c == classOf[java.lang.Float] =>
NewInstance(c, getPath :: Nil, ObjectType(c))
case c if c == classOf[java.lang.Boolean] =>
NewInstance(c, getPath :: Nil, ObjectType(c))
case c if c == classOf[java.lang.Short] ||
c == classOf[java.lang.Integer] ||
c == classOf[java.lang.Long] ||
c == classOf[java.lang.Double] ||
c == classOf[java.lang.Float] ||
c == classOf[java.lang.Byte] ||
c == classOf[java.lang.Boolean] =>
StaticInvoke(
c,
ObjectType(c),
"valueOf",
getPath :: Nil,
propagateNull = true)

case c if c == classOf[java.sql.Date] =>
StaticInvoke(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,37 +204,37 @@ object ScalaReflection extends ScalaReflection {
case t if t <:< localTypeOf[java.lang.Integer] =>
val boxedType = classOf[java.lang.Integer]
val objectType = ObjectType(boxedType)
NewInstance(boxedType, getPath :: Nil, objectType)
StaticInvoke(boxedType, objectType, "valueOf", getPath :: Nil, propagateNull = true)

case t if t <:< localTypeOf[java.lang.Long] =>
val boxedType = classOf[java.lang.Long]
val objectType = ObjectType(boxedType)
NewInstance(boxedType, getPath :: Nil, objectType)
StaticInvoke(boxedType, objectType, "valueOf", getPath :: Nil, propagateNull = true)

case t if t <:< localTypeOf[java.lang.Double] =>
val boxedType = classOf[java.lang.Double]
val objectType = ObjectType(boxedType)
NewInstance(boxedType, getPath :: Nil, objectType)
StaticInvoke(boxedType, objectType, "valueOf", getPath :: Nil, propagateNull = true)

case t if t <:< localTypeOf[java.lang.Float] =>
val boxedType = classOf[java.lang.Float]
val objectType = ObjectType(boxedType)
NewInstance(boxedType, getPath :: Nil, objectType)
StaticInvoke(boxedType, objectType, "valueOf", getPath :: Nil, propagateNull = true)

case t if t <:< localTypeOf[java.lang.Short] =>
val boxedType = classOf[java.lang.Short]
val objectType = ObjectType(boxedType)
NewInstance(boxedType, getPath :: Nil, objectType)
StaticInvoke(boxedType, objectType, "valueOf", getPath :: Nil, propagateNull = true)

case t if t <:< localTypeOf[java.lang.Byte] =>
val boxedType = classOf[java.lang.Byte]
val objectType = ObjectType(boxedType)
NewInstance(boxedType, getPath :: Nil, objectType)
StaticInvoke(boxedType, objectType, "valueOf", getPath :: Nil, propagateNull = true)

case t if t <:< localTypeOf[java.lang.Boolean] =>
val boxedType = classOf[java.lang.Boolean]
val objectType = ObjectType(boxedType)
NewInstance(boxedType, getPath :: Nil, objectType)
StaticInvoke(boxedType, objectType, "valueOf", getPath :: Nil, propagateNull = true)

case t if t <:< localTypeOf[java.sql.Date] =>
StaticInvoke(
Expand Down

0 comments on commit 123b4fb

Please sign in to comment.