aboutsummaryrefslogtreecommitdiff
path: root/sql/catalyst
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
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')
-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
-rw-r--r--sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ArithmeticExpressionSuite.scala107
-rw-r--r--sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ConditionalExpressionSuite.scala107
4 files changed, 228 insertions, 229 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")}""")
- }
-}
-
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ArithmeticExpressionSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ArithmeticExpressionSuite.scala
index 321d820b70..687387507e 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ArithmeticExpressionSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ArithmeticExpressionSuite.scala
@@ -17,7 +17,11 @@
package org.apache.spark.sql.catalyst.expressions
+import java.sql.{Date, Timestamp}
+
import org.apache.spark.SparkFunSuite
+import org.apache.spark.sql.catalyst.InternalRow
+import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.TypeCheckFailure
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.types._
@@ -211,4 +215,107 @@ class ArithmeticExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(Pmod(positiveInt, negativeInt), positiveInt)
checkEvaluation(Pmod(positiveLong, negativeLong), positiveLong)
}
+
+ test("function least") {
+ val row = create_row(1, 2, "a", "b", "c")
+ val c1 = 'a.int.at(0)
+ val c2 = 'a.int.at(1)
+ val c3 = 'a.string.at(2)
+ val c4 = 'a.string.at(3)
+ val c5 = 'a.string.at(4)
+ checkEvaluation(Least(Seq(c4, c3, c5)), "a", row)
+ checkEvaluation(Least(Seq(c1, c2)), 1, row)
+ checkEvaluation(Least(Seq(c1, c2, Literal(-1))), -1, row)
+ checkEvaluation(Least(Seq(c4, c5, c3, c3, Literal("a"))), "a", row)
+
+ val nullLiteral = Literal.create(null, IntegerType)
+ checkEvaluation(Least(Seq(nullLiteral, nullLiteral)), null)
+ checkEvaluation(Least(Seq(Literal(null), Literal(null))), null, InternalRow.empty)
+ checkEvaluation(Least(Seq(Literal(-1.0), Literal(2.5))), -1.0, InternalRow.empty)
+ checkEvaluation(Least(Seq(Literal(-1), Literal(2))), -1, InternalRow.empty)
+ checkEvaluation(
+ Least(Seq(Literal((-1.0).toFloat), Literal(2.5.toFloat))), (-1.0).toFloat, InternalRow.empty)
+ checkEvaluation(
+ Least(Seq(Literal(Long.MaxValue), Literal(Long.MinValue))), Long.MinValue, InternalRow.empty)
+ checkEvaluation(Least(Seq(Literal(1.toByte), Literal(2.toByte))), 1.toByte, InternalRow.empty)
+ checkEvaluation(
+ Least(Seq(Literal(1.toShort), Literal(2.toByte.toShort))), 1.toShort, InternalRow.empty)
+ checkEvaluation(Least(Seq(Literal("abc"), Literal("aaaa"))), "aaaa", InternalRow.empty)
+ checkEvaluation(Least(Seq(Literal(true), Literal(false))), false, InternalRow.empty)
+ checkEvaluation(
+ Least(Seq(
+ Literal(BigDecimal("1234567890987654321123456")),
+ Literal(BigDecimal("1234567890987654321123458")))),
+ BigDecimal("1234567890987654321123456"), InternalRow.empty)
+ checkEvaluation(
+ Least(Seq(Literal(Date.valueOf("2015-01-01")), Literal(Date.valueOf("2015-07-01")))),
+ Date.valueOf("2015-01-01"), InternalRow.empty)
+ checkEvaluation(
+ Least(Seq(
+ Literal(Timestamp.valueOf("2015-07-01 08:00:00")),
+ Literal(Timestamp.valueOf("2015-07-01 10:00:00")))),
+ Timestamp.valueOf("2015-07-01 08:00:00"), InternalRow.empty)
+
+ // Type checking error
+ assert(
+ Least(Seq(Literal(1), Literal("1"))).checkInputDataTypes() ==
+ TypeCheckFailure("The expressions should all have the same type, " +
+ "got LEAST(int, string)."))
+
+ DataTypeTestUtils.ordered.foreach { dt =>
+ checkConsistencyBetweenInterpretedAndCodegen(Least, dt, 2)
+ }
+ }
+
+ test("function greatest") {
+ val row = create_row(1, 2, "a", "b", "c")
+ val c1 = 'a.int.at(0)
+ val c2 = 'a.int.at(1)
+ val c3 = 'a.string.at(2)
+ val c4 = 'a.string.at(3)
+ val c5 = 'a.string.at(4)
+ checkEvaluation(Greatest(Seq(c4, c5, c3)), "c", row)
+ checkEvaluation(Greatest(Seq(c2, c1)), 2, row)
+ checkEvaluation(Greatest(Seq(c1, c2, Literal(2))), 2, row)
+ checkEvaluation(Greatest(Seq(c4, c5, c3, Literal("ccc"))), "ccc", row)
+
+ val nullLiteral = Literal.create(null, IntegerType)
+ checkEvaluation(Greatest(Seq(nullLiteral, nullLiteral)), null)
+ checkEvaluation(Greatest(Seq(Literal(null), Literal(null))), null, InternalRow.empty)
+ checkEvaluation(Greatest(Seq(Literal(-1.0), Literal(2.5))), 2.5, InternalRow.empty)
+ checkEvaluation(Greatest(Seq(Literal(-1), Literal(2))), 2, InternalRow.empty)
+ checkEvaluation(
+ Greatest(Seq(Literal((-1.0).toFloat), Literal(2.5.toFloat))), 2.5.toFloat, InternalRow.empty)
+ checkEvaluation(Greatest(
+ Seq(Literal(Long.MaxValue), Literal(Long.MinValue))), Long.MaxValue, InternalRow.empty)
+ checkEvaluation(
+ Greatest(Seq(Literal(1.toByte), Literal(2.toByte))), 2.toByte, InternalRow.empty)
+ checkEvaluation(
+ Greatest(Seq(Literal(1.toShort), Literal(2.toByte.toShort))), 2.toShort, InternalRow.empty)
+ checkEvaluation(Greatest(Seq(Literal("abc"), Literal("aaaa"))), "abc", InternalRow.empty)
+ checkEvaluation(Greatest(Seq(Literal(true), Literal(false))), true, InternalRow.empty)
+ checkEvaluation(
+ Greatest(Seq(
+ Literal(BigDecimal("1234567890987654321123456")),
+ Literal(BigDecimal("1234567890987654321123458")))),
+ BigDecimal("1234567890987654321123458"), InternalRow.empty)
+ checkEvaluation(Greatest(
+ Seq(Literal(Date.valueOf("2015-01-01")), Literal(Date.valueOf("2015-07-01")))),
+ Date.valueOf("2015-07-01"), InternalRow.empty)
+ checkEvaluation(
+ Greatest(Seq(
+ Literal(Timestamp.valueOf("2015-07-01 08:00:00")),
+ Literal(Timestamp.valueOf("2015-07-01 10:00:00")))),
+ Timestamp.valueOf("2015-07-01 10:00:00"), InternalRow.empty)
+
+ // Type checking error
+ assert(
+ Greatest(Seq(Literal(1), Literal("1"))).checkInputDataTypes() ==
+ TypeCheckFailure("The expressions should all have the same type, " +
+ "got GREATEST(int, string)."))
+
+ DataTypeTestUtils.ordered.foreach { dt =>
+ checkConsistencyBetweenInterpretedAndCodegen(Greatest, dt, 2)
+ }
+ }
}
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ConditionalExpressionSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ConditionalExpressionSuite.scala
index 36185b8c63..b04ea418fb 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ConditionalExpressionSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ConditionalExpressionSuite.scala
@@ -17,11 +17,7 @@
package org.apache.spark.sql.catalyst.expressions
-import java.sql.{Date, Timestamp}
-
import org.apache.spark.SparkFunSuite
-import org.apache.spark.sql.catalyst.InternalRow
-import org.apache.spark.sql.catalyst.analysis.TypeCheckResult.TypeCheckFailure
import org.apache.spark.sql.catalyst.dsl.expressions._
import org.apache.spark.sql.types._
@@ -141,107 +137,4 @@ class ConditionalExpressionSuite extends SparkFunSuite with ExpressionEvalHelper
checkEvaluation(CaseKeyWhen(c6, Seq(c5, c2, c4, c3)), null, row)
checkEvaluation(CaseKeyWhen(literalNull, Seq(c2, c5, c1, c6)), null, row)
}
-
- test("function least") {
- val row = create_row(1, 2, "a", "b", "c")
- val c1 = 'a.int.at(0)
- val c2 = 'a.int.at(1)
- val c3 = 'a.string.at(2)
- val c4 = 'a.string.at(3)
- val c5 = 'a.string.at(4)
- checkEvaluation(Least(Seq(c4, c3, c5)), "a", row)
- checkEvaluation(Least(Seq(c1, c2)), 1, row)
- checkEvaluation(Least(Seq(c1, c2, Literal(-1))), -1, row)
- checkEvaluation(Least(Seq(c4, c5, c3, c3, Literal("a"))), "a", row)
-
- val nullLiteral = Literal.create(null, IntegerType)
- checkEvaluation(Least(Seq(nullLiteral, nullLiteral)), null)
- checkEvaluation(Least(Seq(Literal(null), Literal(null))), null, InternalRow.empty)
- checkEvaluation(Least(Seq(Literal(-1.0), Literal(2.5))), -1.0, InternalRow.empty)
- checkEvaluation(Least(Seq(Literal(-1), Literal(2))), -1, InternalRow.empty)
- checkEvaluation(
- Least(Seq(Literal((-1.0).toFloat), Literal(2.5.toFloat))), (-1.0).toFloat, InternalRow.empty)
- checkEvaluation(
- Least(Seq(Literal(Long.MaxValue), Literal(Long.MinValue))), Long.MinValue, InternalRow.empty)
- checkEvaluation(Least(Seq(Literal(1.toByte), Literal(2.toByte))), 1.toByte, InternalRow.empty)
- checkEvaluation(
- Least(Seq(Literal(1.toShort), Literal(2.toByte.toShort))), 1.toShort, InternalRow.empty)
- checkEvaluation(Least(Seq(Literal("abc"), Literal("aaaa"))), "aaaa", InternalRow.empty)
- checkEvaluation(Least(Seq(Literal(true), Literal(false))), false, InternalRow.empty)
- checkEvaluation(
- Least(Seq(
- Literal(BigDecimal("1234567890987654321123456")),
- Literal(BigDecimal("1234567890987654321123458")))),
- BigDecimal("1234567890987654321123456"), InternalRow.empty)
- checkEvaluation(
- Least(Seq(Literal(Date.valueOf("2015-01-01")), Literal(Date.valueOf("2015-07-01")))),
- Date.valueOf("2015-01-01"), InternalRow.empty)
- checkEvaluation(
- Least(Seq(
- Literal(Timestamp.valueOf("2015-07-01 08:00:00")),
- Literal(Timestamp.valueOf("2015-07-01 10:00:00")))),
- Timestamp.valueOf("2015-07-01 08:00:00"), InternalRow.empty)
-
- // Type checking error
- assert(
- Least(Seq(Literal(1), Literal("1"))).checkInputDataTypes() ==
- TypeCheckFailure("The expressions should all have the same type, " +
- "got LEAST(int, string)."))
-
- DataTypeTestUtils.ordered.foreach { dt =>
- checkConsistencyBetweenInterpretedAndCodegen(Least, dt, 2)
- }
- }
-
- test("function greatest") {
- val row = create_row(1, 2, "a", "b", "c")
- val c1 = 'a.int.at(0)
- val c2 = 'a.int.at(1)
- val c3 = 'a.string.at(2)
- val c4 = 'a.string.at(3)
- val c5 = 'a.string.at(4)
- checkEvaluation(Greatest(Seq(c4, c5, c3)), "c", row)
- checkEvaluation(Greatest(Seq(c2, c1)), 2, row)
- checkEvaluation(Greatest(Seq(c1, c2, Literal(2))), 2, row)
- checkEvaluation(Greatest(Seq(c4, c5, c3, Literal("ccc"))), "ccc", row)
-
- val nullLiteral = Literal.create(null, IntegerType)
- checkEvaluation(Greatest(Seq(nullLiteral, nullLiteral)), null)
- checkEvaluation(Greatest(Seq(Literal(null), Literal(null))), null, InternalRow.empty)
- checkEvaluation(Greatest(Seq(Literal(-1.0), Literal(2.5))), 2.5, InternalRow.empty)
- checkEvaluation(Greatest(Seq(Literal(-1), Literal(2))), 2, InternalRow.empty)
- checkEvaluation(
- Greatest(Seq(Literal((-1.0).toFloat), Literal(2.5.toFloat))), 2.5.toFloat, InternalRow.empty)
- checkEvaluation(Greatest(
- Seq(Literal(Long.MaxValue), Literal(Long.MinValue))), Long.MaxValue, InternalRow.empty)
- checkEvaluation(
- Greatest(Seq(Literal(1.toByte), Literal(2.toByte))), 2.toByte, InternalRow.empty)
- checkEvaluation(
- Greatest(Seq(Literal(1.toShort), Literal(2.toByte.toShort))), 2.toShort, InternalRow.empty)
- checkEvaluation(Greatest(Seq(Literal("abc"), Literal("aaaa"))), "abc", InternalRow.empty)
- checkEvaluation(Greatest(Seq(Literal(true), Literal(false))), true, InternalRow.empty)
- checkEvaluation(
- Greatest(Seq(
- Literal(BigDecimal("1234567890987654321123456")),
- Literal(BigDecimal("1234567890987654321123458")))),
- BigDecimal("1234567890987654321123458"), InternalRow.empty)
- checkEvaluation(Greatest(
- Seq(Literal(Date.valueOf("2015-01-01")), Literal(Date.valueOf("2015-07-01")))),
- Date.valueOf("2015-07-01"), InternalRow.empty)
- checkEvaluation(
- Greatest(Seq(
- Literal(Timestamp.valueOf("2015-07-01 08:00:00")),
- Literal(Timestamp.valueOf("2015-07-01 10:00:00")))),
- Timestamp.valueOf("2015-07-01 10:00:00"), InternalRow.empty)
-
- // Type checking error
- assert(
- Greatest(Seq(Literal(1), Literal("1"))).checkInputDataTypes() ==
- TypeCheckFailure("The expressions should all have the same type, " +
- "got GREATEST(int, string)."))
-
- DataTypeTestUtils.ordered.foreach { dt =>
- checkConsistencyBetweenInterpretedAndCodegen(Greatest, dt, 2)
- }
- }
}