Repository: spark Updated Branches: refs/heads/master b67b35f76 -> cbd11d235
[SPARK-19072][SQL] codegen of Literal should not output boxed value ## What changes were proposed in this pull request? In https://github.com/apache/spark/pull/16402 we made a mistake that, when double/float is infinity, the `Literal` codegen will output boxed value and cause wrong result. This PR fixes this by special handling infinity to not output boxed value. ## How was this patch tested? new regression test Author: Wenchen Fan <wenc...@databricks.com> Closes #16469 from cloud-fan/literal. Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/cbd11d23 Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/cbd11d23 Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/cbd11d23 Branch: refs/heads/master Commit: cbd11d235752d0ab30cfdbf2351cb3e68a123606 Parents: b67b35f Author: Wenchen Fan <wenc...@databricks.com> Authored: Tue Jan 3 22:40:14 2017 -0800 Committer: Yin Huai <yh...@databricks.com> Committed: Tue Jan 3 22:40:14 2017 -0800 ---------------------------------------------------------------------- .../sql/catalyst/expressions/literals.scala | 30 +++++++++++++------- .../catalyst/expressions/PredicateSuite.scala | 5 ++++ 2 files changed, 24 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/spark/blob/cbd11d23/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala ---------------------------------------------------------------------- diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala index ab45c41..cb0c4d3 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/literals.scala @@ -266,33 +266,41 @@ case class Literal (value: Any, dataType: DataType) extends LeafExpression { override def eval(input: InternalRow): Any = value override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { + val javaType = ctx.javaType(dataType) // change the isNull and primitive to consts, to inline them if (value == null) { ev.isNull = "true" - ev.copy(s"final ${ctx.javaType(dataType)} ${ev.value} = ${ctx.defaultValue(dataType)};") + ev.copy(s"final $javaType ${ev.value} = ${ctx.defaultValue(dataType)};") } else { ev.isNull = "false" - ev.value = dataType match { - case BooleanType | IntegerType | DateType => value.toString + dataType match { + case BooleanType | IntegerType | DateType => + ev.copy(code = "", value = value.toString) case FloatType => val v = value.asInstanceOf[Float] if (v.isNaN || v.isInfinite) { - ctx.addReferenceMinorObj(v) + val boxedValue = ctx.addReferenceMinorObj(v) + val code = s"final $javaType ${ev.value} = ($javaType) $boxedValue;" + ev.copy(code = code) } else { - s"${value}f" + ev.copy(code = "", value = s"${value}f") } case DoubleType => val v = value.asInstanceOf[Double] if (v.isNaN || v.isInfinite) { - ctx.addReferenceMinorObj(v) + val boxedValue = ctx.addReferenceMinorObj(v) + val code = s"final $javaType ${ev.value} = ($javaType) $boxedValue;" + ev.copy(code = code) } else { - s"${value}D" + ev.copy(code = "", value = s"${value}D") } - case ByteType | ShortType => s"(${ctx.javaType(dataType)})$value" - case TimestampType | LongType => s"${value}L" - case other => ctx.addReferenceMinorObj(value, ctx.javaType(dataType)) + case ByteType | ShortType => + ev.copy(code = "", value = s"($javaType)$value") + case TimestampType | LongType => + ev.copy(code = "", value = s"${value}L") + case other => + ev.copy(code = "", value = ctx.addReferenceMinorObj(value, ctx.javaType(dataType))) } - ev.copy("") } } http://git-wip-us.apache.org/repos/asf/spark/blob/cbd11d23/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/PredicateSuite.scala ---------------------------------------------------------------------- diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/PredicateSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/PredicateSuite.scala index 6fc3de1..6fe295c 100644 --- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/PredicateSuite.scala +++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/PredicateSuite.scala @@ -324,4 +324,9 @@ class PredicateSuite extends SparkFunSuite with ExpressionEvalHelper { Literal.create(struct, structType), Literal.create(unsafeStruct, structType)), true) } + + test("EqualTo double/float infinity") { + val infinity = Literal(Double.PositiveInfinity) + checkEvaluation(EqualTo(infinity, infinity), true) + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org For additional commands, e-mail: commits-h...@spark.apache.org