-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-8203] [SPARK-8204] [SQL] conditional function: least/greatest #6851
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
Changes from 1 commit
ec625b0
c1f6824
7a6bdbb
0f1bff2
86fb049
22e8f3d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -312,3 +312,63 @@ case class CaseKeyWhen(key: Expression, branches: Seq[Expression]) extends CaseW | |
| }.mkString | ||
| } | ||
| } | ||
|
|
||
| case class Least(children: Expression*) | ||
| extends Expression { | ||
|
|
||
| override def nullable: Boolean = children.forall(_.nullable) | ||
|
|
||
| override def checkInputDataTypes(): TypeCheckResult = { | ||
| if (children.map(_.dataType).distinct.size > 1) { | ||
| TypeCheckResult.TypeCheckFailure( | ||
| s"differing types in Least (${children.map(_.dataType)}).") | ||
| } else { | ||
| TypeCheckResult.TypeCheckSuccess | ||
| } | ||
| } | ||
|
|
||
| override def dataType: DataType = children.head.dataType | ||
|
|
||
| override def eval(input: InternalRow): Any = { | ||
| val cmp = GreaterThan | ||
|
Contributor
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 make it as the property of the class? We don't want to create new instance for every row, right? And don't forget to mark it as
Contributor
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, I just notice it's impossible, but after checking the code in |
||
| children.foldLeft[Expression](null)((r, c) => { | ||
| if (c != null) { | ||
| if (r == null || cmp.apply(r, c).eval(input).asInstanceOf[Boolean]) c else r | ||
| } else { | ||
| r | ||
| } | ||
| }).eval(input) | ||
| } | ||
|
|
||
| override def toString: String = s"LEAST(${children.mkString(", ")})" | ||
| } | ||
|
|
||
| case class Greatest(children: Expression*) | ||
| extends Expression { | ||
|
|
||
| override def nullable: Boolean = children.forall(_.nullable) | ||
|
|
||
| override def checkInputDataTypes(): TypeCheckResult = { | ||
| if (children.map(_.dataType).distinct.size > 1) { | ||
| TypeCheckResult.TypeCheckFailure( | ||
| s"differing types in Greatest (${children.map(_.dataType)}).") | ||
| } else { | ||
| TypeCheckResult.TypeCheckSuccess | ||
| } | ||
| } | ||
|
|
||
| override def dataType: DataType = children.head.dataType | ||
|
|
||
| override def eval(input: InternalRow): Any = { | ||
| val cmp = LessThan | ||
| children.foldLeft[Expression](null)((r, c) => { | ||
| if (c != null) { | ||
| if (r == null || cmp.apply(r, c).eval(input).asInstanceOf[Boolean]) c else r | ||
| } else { | ||
| r | ||
| } | ||
| }).eval(input) | ||
| } | ||
|
|
||
| override def toString: String = s"LEAST(${children.mkString(", ")})" | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,4 +134,21 @@ class ConditionalExpressionSuite extends SparkFunSuite with ExpressionEvalHelper | |
| checkEvaluation(CaseKeyWhen(literalNull, Seq(c2, c5, c1, c6)), "c", row) | ||
| } | ||
|
|
||
| test("greatest/least") { | ||
| val row = create_row(1, 2, "a", "b", "c") | ||
|
Contributor
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. we need to test this for all data types. |
||
| 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(c4, c5, c3), "c", row) | ||
| checkEvaluation(Greatest(c2, c1), 2, row) | ||
| checkEvaluation(Least(c4, c3, c5), "a", row) | ||
| checkEvaluation(Least(c1, c2), 1, row) | ||
| checkEvaluation(Greatest(c1, c2, Literal(2)), 2, row) | ||
| checkEvaluation(Greatest(c4, c5, c3, Literal("ccc")), "ccc", row) | ||
| checkEvaluation(Least(c1, c2, Literal(-1)), -1, row) | ||
| checkEvaluation(Least(c4, c5, c3, c3, Literal("a")), "a", row) | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -599,7 +599,7 @@ object functions { | |
| /** | ||
| * Creates a new row for each element in the given array or map column. | ||
| */ | ||
| def explode(e: Column): Column = Explode(e.expr) | ||
| def explode(e: Column): Column = Explode(e.expr) | ||
|
|
||
| /** | ||
| * Converts a string exprsesion to lower case. | ||
|
|
@@ -1073,11 +1073,30 @@ object functions { | |
| def floor(columnName: String): Column = floor(Column(columnName)) | ||
|
|
||
| /** | ||
| * Computes hex value of the given column | ||
| * Returns the greatest value of the list of values. | ||
|
Contributor
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. should also document semantics regarding null values |
||
| * | ||
| * @group math_funcs | ||
| * @group normal_funcs | ||
| * @since 1.5.0 | ||
| */ | ||
| @scala.annotation.varargs | ||
| def greatest(exprs: Column*): Column = Greatest(exprs.map(_.expr): _*) | ||
|
Contributor
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. Should we check that |
||
|
|
||
| /** | ||
| * Returns the greatest value of the list of column names. | ||
| * | ||
| * @group normal_funcs | ||
| * @since 1.5.0 | ||
| */ | ||
| @scala.annotation.varargs | ||
| def greatest(columnName: String, columnNames: String*): Column = | ||
| greatest((columnName +: columnNames).map(Column.apply): _*) | ||
|
|
||
| /** | ||
| * Computes hex value of the given column | ||
|
Contributor
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. Nit:
|
||
| * | ||
| * @group math_funcs | ||
| * @since 1.5.0 | ||
| */ | ||
| def hex(column: Column): Column = Hex(column.expr) | ||
|
|
||
| /** | ||
|
|
@@ -1171,6 +1190,25 @@ object functions { | |
| */ | ||
| def hypot(l: Double, rightName: String): Column = hypot(l, Column(rightName)) | ||
|
|
||
| /** | ||
| * Returns the least value of the list of values. | ||
| * | ||
| * @group normal_funcs | ||
| * @since 1.5.0 | ||
| */ | ||
| @scala.annotation.varargs | ||
| def least(exprs: Column*): Column = Least(exprs.map(_.expr): _*) | ||
|
|
||
| /** | ||
| * Returns the least value of the list of column names. | ||
| * | ||
| * @group normal_funcs | ||
| * @since 1.5.0 | ||
| */ | ||
| @scala.annotation.varargs | ||
| def least(columnName: String, columnNames: String*): Column = | ||
| least((columnName +: columnNames).map(Column.apply): _*) | ||
|
|
||
| /** | ||
| * Computes the natural logarithm of the given value. | ||
| * | ||
|
|
||
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.
shouldn't least be nullable if any is nullable?
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.
hive 1.2.0 only return null when all arguments are 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.
If any child is not nullable, the result will not be nullable.