From a9beeaaaeb52e9c940fe86a3d70801655401623c Mon Sep 17 00:00:00 2001 From: Wenchen Fan Date: Tue, 2 Aug 2016 11:08:32 -0700 Subject: [SPARK-16855][SQL] move Greatest and Least from conditionalExpressions.scala to arithmetic.scala ## What changes were proposed in this pull request? `Greatest` and `Least` are not conditional expressions, but arithmetic expressions. ## How was this patch tested? N/A Author: Wenchen Fan Closes #14460 from cloud-fan/move. --- .../sql/catalyst/expressions/arithmetic.scala | 121 ++++++++++++++++++++ .../expressions/conditionalExpressions.scala | 122 --------------------- 2 files changed, 121 insertions(+), 122 deletions(-) (limited to 'sql/catalyst/src/main') diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala index 77d40a5079..4aebef92b9 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala @@ -18,6 +18,7 @@ package org.apache.spark.sql.catalyst.expressions import org.apache.spark.sql.catalyst.InternalRow +import org.apache.spark.sql.catalyst.analysis.TypeCheckResult import org.apache.spark.sql.catalyst.expressions.codegen._ import org.apache.spark.sql.catalyst.util.TypeUtils import org.apache.spark.sql.types._ @@ -460,3 +461,123 @@ case class Pmod(left: Expression, right: Expression) extends BinaryArithmetic wi override def sql: String = s"$prettyName(${left.sql}, ${right.sql})" } + +/** + * A function that returns the least value of all parameters, skipping null values. + * It takes at least 2 parameters, and returns null iff all parameters are null. + */ +@ExpressionDescription( + usage = "_FUNC_(n1, ...) - Returns the least value of all parameters, skipping null values.") +case class Least(children: Seq[Expression]) extends Expression { + + override def nullable: Boolean = children.forall(_.nullable) + override def foldable: Boolean = children.forall(_.foldable) + + private lazy val ordering = TypeUtils.getInterpretedOrdering(dataType) + + override def checkInputDataTypes(): TypeCheckResult = { + if (children.length <= 1) { + TypeCheckResult.TypeCheckFailure(s"LEAST requires at least 2 arguments") + } else if (children.map(_.dataType).distinct.count(_ != NullType) > 1) { + TypeCheckResult.TypeCheckFailure( + s"The expressions should all have the same type," + + s" got LEAST(${children.map(_.dataType.simpleString).mkString(", ")}).") + } else { + TypeUtils.checkForOrderingExpr(dataType, "function " + prettyName) + } + } + + override def dataType: DataType = children.head.dataType + + override def eval(input: InternalRow): Any = { + children.foldLeft[Any](null)((r, c) => { + val evalc = c.eval(input) + if (evalc != null) { + if (r == null || ordering.lt(evalc, r)) evalc else r + } else { + r + } + }) + } + + override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { + val evalChildren = children.map(_.genCode(ctx)) + val first = evalChildren(0) + val rest = evalChildren.drop(1) + def updateEval(eval: ExprCode): String = { + s""" + ${eval.code} + if (!${eval.isNull} && (${ev.isNull} || + ${ctx.genGreater(dataType, ev.value, eval.value)})) { + ${ev.isNull} = false; + ${ev.value} = ${eval.value}; + } + """ + } + ev.copy(code = s""" + ${first.code} + boolean ${ev.isNull} = ${first.isNull}; + ${ctx.javaType(dataType)} ${ev.value} = ${first.value}; + ${rest.map(updateEval).mkString("\n")}""") + } +} + +/** + * A function that returns the greatest value of all parameters, skipping null values. + * It takes at least 2 parameters, and returns null iff all parameters are null. + */ +@ExpressionDescription( + usage = "_FUNC_(n1, ...) - Returns the greatest value of all parameters, skipping null values.") +case class Greatest(children: Seq[Expression]) extends Expression { + + override def nullable: Boolean = children.forall(_.nullable) + override def foldable: Boolean = children.forall(_.foldable) + + private lazy val ordering = TypeUtils.getInterpretedOrdering(dataType) + + override def checkInputDataTypes(): TypeCheckResult = { + if (children.length <= 1) { + TypeCheckResult.TypeCheckFailure(s"GREATEST requires at least 2 arguments") + } else if (children.map(_.dataType).distinct.count(_ != NullType) > 1) { + TypeCheckResult.TypeCheckFailure( + s"The expressions should all have the same type," + + s" got GREATEST(${children.map(_.dataType.simpleString).mkString(", ")}).") + } else { + TypeUtils.checkForOrderingExpr(dataType, "function " + prettyName) + } + } + + override def dataType: DataType = children.head.dataType + + override def eval(input: InternalRow): Any = { + children.foldLeft[Any](null)((r, c) => { + val evalc = c.eval(input) + if (evalc != null) { + if (r == null || ordering.gt(evalc, r)) evalc else r + } else { + r + } + }) + } + + override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { + val evalChildren = children.map(_.genCode(ctx)) + val first = evalChildren(0) + val rest = evalChildren.drop(1) + def updateEval(eval: ExprCode): String = { + s""" + ${eval.code} + if (!${eval.isNull} && (${ev.isNull} || + ${ctx.genGreater(dataType, eval.value, ev.value)})) { + ${ev.isNull} = false; + ${ev.value} = ${eval.value}; + } + """ + } + ev.copy(code = s""" + ${first.code} + boolean ${ev.isNull} = ${first.isNull}; + ${ctx.javaType(dataType)} ${ev.value} = ${first.value}; + ${rest.map(updateEval).mkString("\n")}""") + } +} diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala index 5f2585fc40..1dd70bcfcf 100644 --- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala +++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala @@ -20,7 +20,6 @@ package org.apache.spark.sql.catalyst.expressions import org.apache.spark.sql.catalyst.InternalRow import org.apache.spark.sql.catalyst.analysis.TypeCheckResult import org.apache.spark.sql.catalyst.expressions.codegen._ -import org.apache.spark.sql.catalyst.util.TypeUtils import org.apache.spark.sql.types._ // scalastyle:off line.size.limit @@ -279,124 +278,3 @@ object CaseKeyWhen { CaseWhen(cases, elseValue) } } - -/** - * A function that returns the least value of all parameters, skipping null values. - * It takes at least 2 parameters, and returns null iff all parameters are null. - */ -@ExpressionDescription( - usage = "_FUNC_(n1, ...) - Returns the least value of all parameters, skipping null values.") -case class Least(children: Seq[Expression]) extends Expression { - - override def nullable: Boolean = children.forall(_.nullable) - override def foldable: Boolean = children.forall(_.foldable) - - private lazy val ordering = TypeUtils.getInterpretedOrdering(dataType) - - override def checkInputDataTypes(): TypeCheckResult = { - if (children.length <= 1) { - TypeCheckResult.TypeCheckFailure(s"LEAST requires at least 2 arguments") - } else if (children.map(_.dataType).distinct.count(_ != NullType) > 1) { - TypeCheckResult.TypeCheckFailure( - s"The expressions should all have the same type," + - s" got LEAST(${children.map(_.dataType.simpleString).mkString(", ")}).") - } else { - TypeUtils.checkForOrderingExpr(dataType, "function " + prettyName) - } - } - - override def dataType: DataType = children.head.dataType - - override def eval(input: InternalRow): Any = { - children.foldLeft[Any](null)((r, c) => { - val evalc = c.eval(input) - if (evalc != null) { - if (r == null || ordering.lt(evalc, r)) evalc else r - } else { - r - } - }) - } - - override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { - val evalChildren = children.map(_.genCode(ctx)) - val first = evalChildren(0) - val rest = evalChildren.drop(1) - def updateEval(eval: ExprCode): String = { - s""" - ${eval.code} - if (!${eval.isNull} && (${ev.isNull} || - ${ctx.genGreater(dataType, ev.value, eval.value)})) { - ${ev.isNull} = false; - ${ev.value} = ${eval.value}; - } - """ - } - ev.copy(code = s""" - ${first.code} - boolean ${ev.isNull} = ${first.isNull}; - ${ctx.javaType(dataType)} ${ev.value} = ${first.value}; - ${rest.map(updateEval).mkString("\n")}""") - } -} - -/** - * A function that returns the greatest value of all parameters, skipping null values. - * It takes at least 2 parameters, and returns null iff all parameters are null. - */ -@ExpressionDescription( - usage = "_FUNC_(n1, ...) - Returns the greatest value of all parameters, skipping null values.") -case class Greatest(children: Seq[Expression]) extends Expression { - - override def nullable: Boolean = children.forall(_.nullable) - override def foldable: Boolean = children.forall(_.foldable) - - private lazy val ordering = TypeUtils.getInterpretedOrdering(dataType) - - override def checkInputDataTypes(): TypeCheckResult = { - if (children.length <= 1) { - TypeCheckResult.TypeCheckFailure(s"GREATEST requires at least 2 arguments") - } else if (children.map(_.dataType).distinct.count(_ != NullType) > 1) { - TypeCheckResult.TypeCheckFailure( - s"The expressions should all have the same type," + - s" got GREATEST(${children.map(_.dataType.simpleString).mkString(", ")}).") - } else { - TypeUtils.checkForOrderingExpr(dataType, "function " + prettyName) - } - } - - override def dataType: DataType = children.head.dataType - - override def eval(input: InternalRow): Any = { - children.foldLeft[Any](null)((r, c) => { - val evalc = c.eval(input) - if (evalc != null) { - if (r == null || ordering.gt(evalc, r)) evalc else r - } else { - r - } - }) - } - - override def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { - val evalChildren = children.map(_.genCode(ctx)) - val first = evalChildren(0) - val rest = evalChildren.drop(1) - def updateEval(eval: ExprCode): String = { - s""" - ${eval.code} - if (!${eval.isNull} && (${ev.isNull} || - ${ctx.genGreater(dataType, eval.value, ev.value)})) { - ${ev.isNull} = false; - ${ev.value} = ${eval.value}; - } - """ - } - ev.copy(code = s""" - ${first.code} - boolean ${ev.isNull} = ${first.isNull}; - ${ctx.javaType(dataType)} ${ev.value} = ${first.value}; - ${rest.map(updateEval).mkString("\n")}""") - } -} - -- cgit v1.2.3