Github user yhuai commented on a diff in the pull request:
https://github.com/apache/spark/pull/9718#discussion_r44866037
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/ordering.scala
---
@@ -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
- }
--- End diff --
These lines have been moved to `compareValue`.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]