1818package org .apache .spark .sql .catalyst .expressions
1919
2020import org .apache .spark .sql .catalyst .InternalRow
21- import org .apache .spark .sql .catalyst .util .ArrayData
2221import org .apache .spark .sql .types ._
2322
2423
@@ -30,76 +29,39 @@ class InterpretedOrdering(ordering: Seq[SortOrder]) extends Ordering[InternalRow
3029 def this (ordering : Seq [SortOrder ], inputSchema : Seq [Attribute ]) =
3130 this (ordering.map(BindReferences .bindReference(_, inputSchema)))
3231
33- private def compareValue (
34- left : Any ,
35- right : Any ,
36- dataType : DataType ,
37- direction : SortDirection ): Int = {
38- if (left == null && right == null ) {
39- return 0
40- } else if (left == null ) {
41- return if (direction == Ascending ) - 1 else 1
42- } else if (right == null ) {
43- return if (direction == Ascending ) 1 else - 1
44- } else {
45- dataType match {
46- case dt : AtomicType if direction == Ascending =>
47- return dt.ordering.asInstanceOf [Ordering [Any ]].compare(left, right)
48- case dt : AtomicType if direction == Descending =>
49- return dt.ordering.asInstanceOf [Ordering [Any ]].reverse.compare(left, right)
50- case s : StructType if direction == Ascending =>
51- return s.interpretedOrdering.asInstanceOf [Ordering [Any ]].compare(left, right)
52- case s : StructType if direction == Descending =>
53- return s.interpretedOrdering.asInstanceOf [Ordering [Any ]].reverse.compare(left, right)
54- case a : ArrayType =>
55- val leftArray = left.asInstanceOf [ArrayData ]
56- val rightArray = right.asInstanceOf [ArrayData ]
57- val minLength = scala.math.min(leftArray.numElements(), rightArray.numElements())
58- var i = 0
59- while (i < minLength) {
60- val isNullLeft = leftArray.isNullAt(i)
61- val isNullRight = rightArray.isNullAt(i)
62- if (isNullLeft && isNullRight) {
63- // Do nothing.
64- } else if (isNullLeft) {
65- return if (direction == Ascending ) - 1 else 1
66- } else if (isNullRight) {
67- return if (direction == Ascending ) 1 else - 1
68- } else {
69- val comp =
70- compareValue(
71- leftArray.get(i, a.elementType),
72- rightArray.get(i, a.elementType),
73- a.elementType,
74- direction)
75- if (comp != 0 ) {
76- return comp
77- }
78- }
79- i += 1
80- }
81- if (leftArray.numElements() < rightArray.numElements()) {
82- return if (direction == Ascending ) - 1 else 1
83- } else if (leftArray.numElements() > rightArray.numElements()) {
84- return if (direction == Ascending ) 1 else - 1
85- } else {
86- return 0
87- }
88- case other =>
89- throw new IllegalArgumentException (s " Type $other does not support ordered operations " )
90- }
91- }
92- }
93-
9432 def compare (a : InternalRow , b : InternalRow ): Int = {
9533 var i = 0
9634 while (i < ordering.size) {
9735 val order = ordering(i)
9836 val left = order.child.eval(a)
9937 val right = order.child.eval(b)
100- val comparison = compareValue(left, right, order.dataType, order.direction)
101- if (comparison != 0 ) {
102- return comparison
38+
39+ if (left == null && right == null ) {
40+ // Both null, continue looking.
41+ } else if (left == null ) {
42+ return if (order.direction == Ascending ) - 1 else 1
43+ } else if (right == null ) {
44+ return if (order.direction == Ascending ) 1 else - 1
45+ } else {
46+ val comparison = order.dataType match {
47+ case dt : AtomicType if order.direction == Ascending =>
48+ dt.ordering.asInstanceOf [Ordering [Any ]].compare(left, right)
49+ case dt : AtomicType if order.direction == Descending =>
50+ dt.ordering.asInstanceOf [Ordering [Any ]].reverse.compare(left, right)
51+ case a : ArrayType if order.direction == Ascending =>
52+ a.interpretedOrdering.asInstanceOf [Ordering [Any ]].compare(left, right)
53+ case a : ArrayType if order.direction == Descending =>
54+ a.interpretedOrdering.asInstanceOf [Ordering [Any ]].reverse.compare(left, right)
55+ case s : StructType if order.direction == Ascending =>
56+ s.interpretedOrdering.asInstanceOf [Ordering [Any ]].compare(left, right)
57+ case s : StructType if order.direction == Descending =>
58+ s.interpretedOrdering.asInstanceOf [Ordering [Any ]].reverse.compare(left, right)
59+ case other =>
60+ throw new IllegalArgumentException (s " Type $other does not support ordered operations " )
61+ }
62+ if (comparison != 0 ) {
63+ return comparison
64+ }
10365 }
10466 i += 1
10567 }
0 commit comments