Skip to content

Commit 123b4fb

Browse files
committed
[SPARK-20289][SQL] Use StaticInvoke to box primitive types
## 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 <[email protected]> Closes #17604 from rxin/SPARK-20289.
1 parent cd91f96 commit 123b4fb

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,19 @@ object JavaTypeInference {
204204
typeToken.getRawType match {
205205
case c if !inferExternalType(c).isInstanceOf[ObjectType] => getPath
206206

207-
case c if c == classOf[java.lang.Short] =>
208-
NewInstance(c, getPath :: Nil, ObjectType(c))
209-
case c if c == classOf[java.lang.Integer] =>
210-
NewInstance(c, getPath :: Nil, ObjectType(c))
211-
case c if c == classOf[java.lang.Long] =>
212-
NewInstance(c, getPath :: Nil, ObjectType(c))
213-
case c if c == classOf[java.lang.Double] =>
214-
NewInstance(c, getPath :: Nil, ObjectType(c))
215-
case c if c == classOf[java.lang.Byte] =>
216-
NewInstance(c, getPath :: Nil, ObjectType(c))
217-
case c if c == classOf[java.lang.Float] =>
218-
NewInstance(c, getPath :: Nil, ObjectType(c))
219-
case c if c == classOf[java.lang.Boolean] =>
220-
NewInstance(c, getPath :: Nil, ObjectType(c))
207+
case c if c == classOf[java.lang.Short] ||
208+
c == classOf[java.lang.Integer] ||
209+
c == classOf[java.lang.Long] ||
210+
c == classOf[java.lang.Double] ||
211+
c == classOf[java.lang.Float] ||
212+
c == classOf[java.lang.Byte] ||
213+
c == classOf[java.lang.Boolean] =>
214+
StaticInvoke(
215+
c,
216+
ObjectType(c),
217+
"valueOf",
218+
getPath :: Nil,
219+
propagateNull = true)
221220

222221
case c if c == classOf[java.sql.Date] =>
223222
StaticInvoke(

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -204,37 +204,37 @@ object ScalaReflection extends ScalaReflection {
204204
case t if t <:< localTypeOf[java.lang.Integer] =>
205205
val boxedType = classOf[java.lang.Integer]
206206
val objectType = ObjectType(boxedType)
207-
NewInstance(boxedType, getPath :: Nil, objectType)
207+
StaticInvoke(boxedType, objectType, "valueOf", getPath :: Nil, propagateNull = true)
208208

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

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

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

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

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

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

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

0 commit comments

Comments
 (0)