-
Notifications
You must be signed in to change notification settings - Fork 28.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-20253][SQL] Remove unnecessary nullchecks of a return value from Spark runtime routines in generated Java code #17569
Changes from 2 commits
4482e1c
ae5e232
fc6caac
41c96ab
a39803a
895a3be
3080ac2
510fb53
10cf4be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -251,19 +251,22 @@ object ScalaReflection extends ScalaReflection { | |
getPath :: Nil) | ||
|
||
case t if t <:< localTypeOf[java.lang.String] => | ||
Invoke(getPath, "toString", ObjectType(classOf[String])) | ||
Invoke(getPath, "toString", ObjectType(classOf[String]), returnNullable = false) | ||
|
||
case t if t <:< localTypeOf[java.math.BigDecimal] => | ||
Invoke(getPath, "toJavaBigDecimal", ObjectType(classOf[java.math.BigDecimal])) | ||
Invoke(getPath, "toJavaBigDecimal", ObjectType(classOf[java.math.BigDecimal]), | ||
returnNullable = false) | ||
|
||
case t if t <:< localTypeOf[BigDecimal] => | ||
Invoke(getPath, "toBigDecimal", ObjectType(classOf[BigDecimal])) | ||
Invoke(getPath, "toBigDecimal", ObjectType(classOf[BigDecimal]), returnNullable = false) | ||
|
||
case t if t <:< localTypeOf[java.math.BigInteger] => | ||
Invoke(getPath, "toJavaBigInteger", ObjectType(classOf[java.math.BigInteger])) | ||
Invoke(getPath, "toJavaBigInteger", ObjectType(classOf[java.math.BigInteger]), | ||
returnNullable = false) | ||
|
||
case t if t <:< localTypeOf[scala.math.BigInt] => | ||
Invoke(getPath, "toScalaBigInt", ObjectType(classOf[scala.math.BigInt])) | ||
Invoke(getPath, "toScalaBigInt", ObjectType(classOf[scala.math.BigInt]), | ||
returnNullable = false) | ||
|
||
case t if t <:< localTypeOf[Array[_]] => | ||
val TypeRef(_, _, Seq(elementType)) = t | ||
|
@@ -284,7 +287,7 @@ object ScalaReflection extends ScalaReflection { | |
val arrayCls = arrayClassFor(elementType) | ||
|
||
if (elementNullable) { | ||
Invoke(arrayData, "array", arrayCls) | ||
Invoke(arrayData, "array", arrayCls, returnNullable = false) | ||
} else { | ||
val primitiveMethod = elementType match { | ||
case t if t <:< definitions.IntTpe => "toIntArray" | ||
|
@@ -297,7 +300,7 @@ object ScalaReflection extends ScalaReflection { | |
case other => throw new IllegalStateException("expect primitive array element type " + | ||
"but got " + other) | ||
} | ||
Invoke(arrayData, primitiveMethod, arrayCls) | ||
Invoke(arrayData, primitiveMethod, arrayCls, returnNullable = false) | ||
} | ||
|
||
case t if t <:< localTypeOf[Seq[_]] => | ||
|
@@ -330,19 +333,21 @@ object ScalaReflection extends ScalaReflection { | |
Invoke( | ||
MapObjects( | ||
p => deserializerFor(keyType, Some(p), walkedTypePath), | ||
Invoke(getPath, "keyArray", ArrayType(schemaFor(keyType).dataType)), | ||
Invoke(getPath, "keyArray", ArrayType(schemaFor(keyType).dataType), | ||
returnNullable = false), | ||
schemaFor(keyType).dataType), | ||
"array", | ||
ObjectType(classOf[Array[Any]])) | ||
ObjectType(classOf[Array[Any]]), returnNullable = false) | ||
|
||
val valueData = | ||
Invoke( | ||
MapObjects( | ||
p => deserializerFor(valueType, Some(p), walkedTypePath), | ||
Invoke(getPath, "valueArray", ArrayType(schemaFor(valueType).dataType)), | ||
Invoke(getPath, "valueArray", ArrayType(schemaFor(valueType).dataType), | ||
returnNullable = false), | ||
schemaFor(valueType).dataType), | ||
"array", | ||
ObjectType(classOf[Array[Any]])) | ||
ObjectType(classOf[Array[Any]]), returnNullable = false) | ||
|
||
StaticInvoke( | ||
ArrayBasedMapData.getClass, | ||
|
@@ -356,7 +361,8 @@ object ScalaReflection extends ScalaReflection { | |
udt.userClass.getAnnotation(classOf[SQLUserDefinedType]).udt(), | ||
Nil, | ||
dataType = ObjectType(udt.userClass.getAnnotation(classOf[SQLUserDefinedType]).udt())) | ||
Invoke(obj, "deserialize", ObjectType(udt.userClass), getPath :: Nil) | ||
Invoke(obj, "deserialize", ObjectType(udt.userClass), getPath :: Nil, | ||
returnNullable = false) | ||
|
||
case t if UDTRegistration.exists(getClassNameFromType(t)) => | ||
val udt = UDTRegistration.getUDTFor(getClassNameFromType(t)).get.newInstance() | ||
|
@@ -365,7 +371,8 @@ object ScalaReflection extends ScalaReflection { | |
udt.getClass, | ||
Nil, | ||
dataType = ObjectType(udt.getClass)) | ||
Invoke(obj, "deserialize", ObjectType(udt.userClass), getPath :: Nil) | ||
Invoke(obj, "deserialize", ObjectType(udt.userClass), getPath :: Nil, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
returnNullable = false) | ||
|
||
case t if definedByConstructorParams(t) => | ||
val params = getConstructorParameters(t) | ||
|
@@ -577,7 +584,7 @@ object ScalaReflection extends ScalaReflection { | |
udt.userClass.getAnnotation(classOf[SQLUserDefinedType]).udt(), | ||
Nil, | ||
dataType = ObjectType(udt.userClass.getAnnotation(classOf[SQLUserDefinedType]).udt())) | ||
Invoke(obj, "serialize", udt, inputObject :: Nil) | ||
Invoke(obj, "serialize", udt, inputObject :: Nil, returnNullable = false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
|
||
case t if UDTRegistration.exists(getClassNameFromType(t)) => | ||
val udt = UDTRegistration.getUDTFor(getClassNameFromType(t)).get.newInstance() | ||
|
@@ -586,7 +593,7 @@ object ScalaReflection extends ScalaReflection { | |
udt.getClass, | ||
Nil, | ||
dataType = ObjectType(udt.getClass)) | ||
Invoke(obj, "serialize", udt, inputObject :: Nil) | ||
Invoke(obj, "serialize", udt, inputObject :: Nil, returnNullable = false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sam here |
||
|
||
case t if definedByConstructorParams(t) => | ||
if (seenTypeSet.contains(t)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,7 +89,7 @@ object RowEncoder { | |
udtClass, | ||
Nil, | ||
dataType = ObjectType(udtClass), false) | ||
Invoke(obj, "serialize", udt, inputObject :: Nil) | ||
Invoke(obj, "serialize", udt, inputObject :: Nil, returnNullable = false) | ||
|
||
case TimestampType => | ||
StaticInvoke( | ||
|
@@ -136,16 +136,18 @@ object RowEncoder { | |
case t @ MapType(kt, vt, valueNullable) => | ||
val keys = | ||
Invoke( | ||
Invoke(inputObject, "keysIterator", ObjectType(classOf[scala.collection.Iterator[_]])), | ||
Invoke(inputObject, "keysIterator", ObjectType(classOf[scala.collection.Iterator[_]]), | ||
returnNullable = false), | ||
"toSeq", | ||
ObjectType(classOf[scala.collection.Seq[_]])) | ||
ObjectType(classOf[scala.collection.Seq[_]]), returnNullable = false) | ||
val convertedKeys = serializerFor(keys, ArrayType(kt, false)) | ||
|
||
val values = | ||
Invoke( | ||
Invoke(inputObject, "valuesIterator", ObjectType(classOf[scala.collection.Iterator[_]])), | ||
Invoke(inputObject, "valuesIterator", ObjectType(classOf[scala.collection.Iterator[_]]), | ||
returnNullable = false), | ||
"toSeq", | ||
ObjectType(classOf[scala.collection.Seq[_]])) | ||
ObjectType(classOf[scala.collection.Seq[_]]), returnNullable = false) | ||
val convertedValues = serializerFor(values, ArrayType(vt, valueNullable)) | ||
|
||
NewInstance( | ||
|
@@ -245,7 +247,7 @@ object RowEncoder { | |
udtClass, | ||
Nil, | ||
dataType = ObjectType(udtClass)) | ||
Invoke(obj, "deserialize", ObjectType(udt.userClass), input :: Nil) | ||
Invoke(obj, "deserialize", ObjectType(udt.userClass), input :: Nil, returnNullable = false) | ||
|
||
case TimestampType => | ||
StaticInvoke( | ||
|
@@ -262,17 +264,18 @@ object RowEncoder { | |
input :: Nil) | ||
|
||
case _: DecimalType => | ||
Invoke(input, "toJavaBigDecimal", ObjectType(classOf[java.math.BigDecimal])) | ||
Invoke(input, "toJavaBigDecimal", ObjectType(classOf[java.math.BigDecimal]), | ||
returnNullable = false) | ||
|
||
case StringType => | ||
Invoke(input, "toString", ObjectType(classOf[String])) | ||
Invoke(input, "toString", ObjectType(classOf[String]), returnNullable = false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we check how many places we set There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is statistics for 59 call sites of 18: What do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok let's keep the default value unchanged |
||
|
||
case ArrayType(et, nullable) => | ||
val arrayData = | ||
Invoke( | ||
MapObjects(deserializerFor(_), input, et), | ||
"array", | ||
ObjectType(classOf[Array[_]])) | ||
ObjectType(classOf[Array[_]]), returnNullable = false) | ||
StaticInvoke( | ||
scala.collection.mutable.WrappedArray.getClass, | ||
ObjectType(classOf[Seq[_]]), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -225,20 +225,28 @@ case class Invoke( | |
getFuncResult(ev.value, s"${obj.value}.$functionName($argString)") | ||
} else { | ||
val funcResult = ctx.freshName("funcResult") | ||
s""" | ||
Object $funcResult = null; | ||
${getFuncResult(funcResult, s"${obj.value}.$functionName($argString)")} | ||
if ($funcResult == null) { | ||
${ev.isNull} = true; | ||
} else { | ||
if (!returnNullable) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since we have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, yes we can. |
||
s""" | ||
Object $funcResult = null; | ||
${getFuncResult(funcResult, s"${obj.value}.$functionName($argString)")} | ||
${ev.value} = (${ctx.boxedType(javaType)}) $funcResult; | ||
} | ||
""" | ||
""" | ||
} else { | ||
s""" | ||
Object $funcResult = null; | ||
${getFuncResult(funcResult, s"${obj.value}.$functionName($argString)")} | ||
if ($funcResult == null) { | ||
${ev.isNull} = true; | ||
} else { | ||
${ev.value} = (${ctx.boxedType(javaType)}) $funcResult; | ||
} | ||
""" | ||
} | ||
} | ||
|
||
// If the function can return null, we do an extra check to make sure our null bit is still set | ||
// correctly. | ||
val postNullCheck = if (ctx.defaultValue(dataType) == "null") { | ||
val postNullCheck = if (ctx.defaultValue(dataType) == "null" && returnNullable) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually, can we embed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, done |
||
s"${ev.isNull} = ${ev.value} == null;" | ||
} else { | ||
"" | ||
|
@@ -608,7 +616,7 @@ case class MapObjects private( | |
$convertedArray = $arrayConstructor; | ||
""", | ||
genValue => s"$convertedArray[$loopIndex] = $genValue;", | ||
s"new ${classOf[GenericArrayData].getName}($convertedArray);" | ||
s"new ${classOf[GenericArrayData].getName}($convertedArray); /*###*/" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, sorry. It was my debug code... |
||
) | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
deserialize
is totally implemented by users, can we guarantee not return null?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. It is UDT. I have checked
deserialized
only in Spark runtime.