-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-11738] [SQL] Making ArrayType orderable #9718
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 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d49b218
Make arrays orderable.
yhuai 519b593
Allow array type in grouping expression.
yhuai f5f074d
Array column is allowed in grouping expressions.
yhuai f43a7f9
Add interpretedOrdering to ArrayType.
yhuai 7228093
Fix ExpressionTypeCheckingSuite.scala
yhuai aadfeab
Address comments.
yhuai 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -267,6 +267,55 @@ class CodeGenContext { | |
| case dt: DataType if isPrimitiveType(dt) => s"($c1 > $c2 ? 1 : $c1 < $c2 ? -1 : 0)" | ||
| case BinaryType => s"org.apache.spark.sql.catalyst.util.TypeUtils.compareBinary($c1, $c2)" | ||
| case NullType => "0" | ||
| case array: ArrayType => | ||
| val elementType = array.elementType | ||
| val elementA = freshName("elementA") | ||
| val isNullA = freshName("isNullA") | ||
| val elementB = freshName("elementB") | ||
| val isNullB = freshName("isNullB") | ||
| val compareFunc = freshName("compareArray") | ||
| val i = freshName("i") | ||
| val minLength = freshName("minLength") | ||
| val funcCode: String = | ||
| s""" | ||
| public int $compareFunc(ArrayData a, ArrayData b) { | ||
| int lengthA = a.numElements(); | ||
| int lengthB = b.numElements(); | ||
| int $minLength = (lengthA > lengthB) ? lengthB : lengthA; | ||
| boolean $isNullA; | ||
| boolean $isNullB; | ||
| ${javaType(elementType)} $elementA; | ||
| ${javaType(elementType)} $elementB; | ||
| for (int $i = 0; $i < $minLength; $i++) { | ||
|
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.
|
||
| $isNullA = a.isNullAt($i); | ||
| $isNullB = b.isNullAt($i); | ||
|
|
||
| if ($isNullA && $isNullB) { | ||
| // Nothing | ||
| } else if ($isNullA) { | ||
| return -1; | ||
| } else if ($isNullB) { | ||
| return 1; | ||
| } else { | ||
| $elementA = ${getValue("a", elementType, i)}; | ||
| $elementB = ${getValue("b", elementType, i)}; | ||
| int comp = ${genComp(elementType, elementA, elementB)}; | ||
| if (comp != 0) { | ||
| return comp; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (lengthA < lengthB) { | ||
| return -1; | ||
| } else if (lengthA > lengthB) { | ||
| return 1; | ||
| } | ||
| return 0; | ||
| } | ||
| """ | ||
| addNewFunction(compareFunc, funcCode) | ||
| s"this.$compareFunc($c1, $c2)" | ||
| case schema: StructType => | ||
| val comparisons = GenerateOrdering.genComparisons(this, schema) | ||
| val compareFunc = freshName("compareStruct") | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| package org.apache.spark.sql.catalyst.expressions | ||
|
|
||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.util.ArrayData | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
|
|
||
|
|
@@ -29,35 +30,76 @@ class InterpretedOrdering(ordering: Seq[SortOrder]) extends Ordering[InternalRow | |
| def this(ordering: Seq[SortOrder], inputSchema: Seq[Attribute]) = | ||
| this(ordering.map(BindReferences.bindReference(_, inputSchema))) | ||
|
|
||
| private def compareValue( | ||
| left: Any, | ||
| right: Any, | ||
| dataType: DataType, | ||
| direction: SortDirection): Int = { | ||
| if (left == null && right == null) { | ||
| return 0 | ||
| } else if (left == null) { | ||
| return if (direction == Ascending) -1 else 1 | ||
| } else if (right == null) { | ||
| return if (direction == Ascending) 1 else -1 | ||
| } else { | ||
| dataType match { | ||
| case dt: AtomicType if direction == Ascending => | ||
| return dt.ordering.asInstanceOf[Ordering[Any]].compare(left, right) | ||
| case dt: AtomicType if direction == Descending => | ||
| return dt.ordering.asInstanceOf[Ordering[Any]].reverse.compare(left, right) | ||
| case s: StructType if direction == Ascending => | ||
| return s.interpretedOrdering.asInstanceOf[Ordering[Any]].compare(left, right) | ||
| case s: StructType if direction == Descending => | ||
| return s.interpretedOrdering.asInstanceOf[Ordering[Any]].reverse.compare(left, right) | ||
| case a: ArrayType => | ||
| val leftArray = left.asInstanceOf[ArrayData] | ||
| val rightArray = right.asInstanceOf[ArrayData] | ||
| val minLength = scala.math.min(leftArray.numElements(), rightArray.numElements()) | ||
| var i = 0 | ||
| while (i < minLength) { | ||
| val isNullLeft = leftArray.isNullAt(i) | ||
|
||
| val isNullRight = rightArray.isNullAt(i) | ||
| if (isNullLeft && isNullRight) { | ||
| // Do nothing. | ||
| } else if (isNullLeft) { | ||
| return if (direction == Ascending) -1 else 1 | ||
| } else if (isNullRight) { | ||
| return if (direction == Ascending) 1 else -1 | ||
| } else { | ||
| val comp = | ||
| compareValue( | ||
| leftArray.get(i, a.elementType), | ||
| rightArray.get(i, a.elementType), | ||
| a.elementType, | ||
| direction) | ||
| if (comp != 0) { | ||
| return comp | ||
| } | ||
| } | ||
| i += 1 | ||
| } | ||
| if (leftArray.numElements() < rightArray.numElements()) { | ||
| return if (direction == Ascending) -1 else 1 | ||
| } else if (leftArray.numElements() > rightArray.numElements()) { | ||
| return if (direction == Ascending) 1 else -1 | ||
| } else { | ||
| return 0 | ||
| } | ||
| case other => | ||
| throw new IllegalArgumentException(s"Type $other does not support ordered operations") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| def compare(a: InternalRow, b: InternalRow): Int = { | ||
| var i = 0 | ||
| while (i < ordering.size) { | ||
| val order = ordering(i) | ||
| val left = order.child.eval(a) | ||
| val right = order.child.eval(b) | ||
|
|
||
| if (left == null && right == null) { | ||
| // Both null, continue looking. | ||
| } else if (left == null) { | ||
| return if (order.direction == Ascending) -1 else 1 | ||
| } else if (right == null) { | ||
| return if (order.direction == Ascending) 1 else -1 | ||
| } else { | ||
| val comparison = order.dataType match { | ||
| case dt: AtomicType if order.direction == Ascending => | ||
| dt.ordering.asInstanceOf[Ordering[Any]].compare(left, right) | ||
| case dt: AtomicType if order.direction == Descending => | ||
| dt.ordering.asInstanceOf[Ordering[Any]].reverse.compare(left, right) | ||
| case s: StructType if order.direction == Ascending => | ||
| s.interpretedOrdering.asInstanceOf[Ordering[Any]].compare(left, right) | ||
| case s: StructType if order.direction == Descending => | ||
| s.interpretedOrdering.asInstanceOf[Ordering[Any]].reverse.compare(left, right) | ||
| case other => | ||
| throw new IllegalArgumentException(s"Type $other does not support ordered operations") | ||
| } | ||
| if (comparison != 0) { | ||
| return comparison | ||
| } | ||
| val comparison = compareValue(left, right, order.dataType, order.direction) | ||
| if (comparison != 0) { | ||
| return comparison | ||
| } | ||
| i += 1 | ||
| } | ||
|
|
@@ -86,6 +128,8 @@ object RowOrdering { | |
| case NullType => true | ||
| case dt: AtomicType => true | ||
| case struct: StructType => struct.fields.forall(f => isOrderable(f.dataType)) | ||
| case array: ArrayType => isOrderable(array.elementType) | ||
| case udt: UserDefinedType[_] => isOrderable(udt.sqlType) | ||
| case _ => false | ||
| } | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
These could be defined in the loop (let compiler to optimize them easily)