-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-23917][SQL] Add array_max function #21024
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
93c5b8a
[SPARK-23917][SQL] Add array_max function
mgaido91 a296bc0
fix scalastyle
mgaido91 c8c1d03
add missing space
mgaido91 d017ccf
add orderable type check
mgaido91 e082f00
fix indentation
mgaido91 a4fd616
address comment
mgaido91 a78c752
Merge branch 'master' of github.com:apache/spark into SPARK-23917
mgaido91 8dfd263
update after codegen changes
mgaido91 e739a0a
fix nullable
mgaido91 1cde795
address comment
mgaido91 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ import java.util.Comparator | |
| 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.{ArrayData, GenericArrayData, MapData} | ||
| import org.apache.spark.sql.catalyst.util.{ArrayData, GenericArrayData, MapData, TypeUtils} | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| /** | ||
|
|
@@ -287,3 +287,69 @@ case class ArrayContains(left: Expression, right: Expression) | |
|
|
||
| override def prettyName: String = "array_contains" | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Returns the maximum value in the array. | ||
| */ | ||
| @ExpressionDescription( | ||
| usage = "_FUNC_(array) - Returns the maximum value in the array. NULL elements are skipped.", | ||
| examples = """ | ||
| Examples: | ||
| > SELECT _FUNC_(array(1, 20, null, 3)); | ||
| 20 | ||
| """, since = "2.4.0") | ||
| case class ArrayMax(child: Expression) extends UnaryExpression with ImplicitCastInputTypes { | ||
|
|
||
| override def nullable: Boolean = true | ||
|
|
||
| override def inputTypes: Seq[AbstractDataType] = Seq(ArrayType) | ||
|
|
||
| private lazy val ordering = TypeUtils.getInterpretedOrdering(dataType) | ||
|
|
||
| override def checkInputDataTypes(): TypeCheckResult = { | ||
| val typeCheckResult = super.checkInputDataTypes() | ||
| if (typeCheckResult.isSuccess) { | ||
| TypeUtils.checkForOrderingExpr(dataType, s"function $prettyName") | ||
| } else { | ||
| typeCheckResult | ||
| } | ||
| } | ||
|
|
||
| override protected def doGenCode(ctx: CodegenContext, ev: ExprCode): ExprCode = { | ||
| val childGen = child.genCode(ctx) | ||
| val javaType = CodeGenerator.javaType(dataType) | ||
| val i = ctx.freshName("i") | ||
| val item = ExprCode("", | ||
| isNull = JavaCode.isNullExpression(s"${childGen.value}.isNullAt($i)"), | ||
| value = JavaCode.expression(CodeGenerator.getValue(childGen.value, dataType, i), dataType)) | ||
| ev.copy(code = | ||
| s""" | ||
| |${childGen.code} | ||
| |boolean ${ev.isNull} = true; | ||
| |$javaType ${ev.value} = ${CodeGenerator.defaultValue(dataType)}; | ||
| |if (!${childGen.isNull}) { | ||
| | for (int $i = 0; $i < ${childGen.value}.numElements(); $i ++) { | ||
| | ${ctx.reassignIfGreater(dataType, ev, item)} | ||
| | } | ||
| |} | ||
| """.stripMargin) | ||
| } | ||
|
|
||
| override protected def nullSafeEval(input: Any): Any = { | ||
| var max: Any = null | ||
| input.asInstanceOf[ArrayData].foreach(dataType, (_, item) => | ||
| if (item != null && (max == null || ordering.gt(item, max))) { | ||
| max = item | ||
| } | ||
| ) | ||
| max | ||
| } | ||
|
|
||
| override def dataType: DataType = child.dataType match { | ||
| case ArrayType(dt, _) => dt | ||
|
Member
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 should also check if
Contributor
Author
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. I added the check in the |
||
| case _ => throw new IllegalStateException(s"$prettyName accepts only arrays.") | ||
| } | ||
|
|
||
| override def prettyName: String = "array_max" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we need to use
MINvalue for each data type instead of default value?If we perform this operation against (-10, -100, -1000), I think that we would get
-1as a result.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.
nvm,
isNullis used for assigning the initial value.