aboutsummaryrefslogtreecommitdiff
path: root/sql/catalyst/src/main
diff options
context:
space:
mode:
authorWenchen Fan <wenchen@databricks.com>2016-08-02 11:08:32 -0700
committerReynold Xin <rxin@databricks.com>2016-08-02 11:08:32 -0700
commita9beeaaaeb52e9c940fe86a3d70801655401623c (patch)
tree4d40d19b603bb5709d8f792c18dad29519856a80 /sql/catalyst/src/main
parentcbdff49357d6ce8d41b76b44628d90ead193eb5f (diff)
downloadspark-a9beeaaaeb52e9c940fe86a3d70801655401623c.tar.gz
spark-a9beeaaaeb52e9c940fe86a3d70801655401623c.tar.bz2
spark-a9beeaaaeb52e9c940fe86a3d70801655401623c.zip
[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 <wenchen@databricks.com> Closes #14460 from cloud-fan/move.
Diffstat (limited to 'sql/catalyst/src/main')
-rw-r--r--sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/arithmetic.scala121
-rw-r--r--sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/conditionalExpressions.scala122
2 files changed, 121 insertions, 122 deletions
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")}""")
- }
-}
-