Skip to content

Commit 1cf26f3

Browse files
committed
Supported Sorting of BinaryType
BinaryType is derived from NativeType and added Ordering support.
1 parent 3888ee2 commit 1cf26f3

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/types/dataTypes.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,18 @@ case object StringType extends NativeType with PrimitiveType {
157157
def simpleString: String = "string"
158158
}
159159

160-
case object BinaryType extends DataType with PrimitiveType {
160+
case object BinaryType extends NativeType with PrimitiveType {
161161
private[sql] type JvmType = Array[Byte]
162+
@transient private[sql] lazy val tag = ScalaReflectionLock.synchronized { typeTag[JvmType] }
163+
private[sql] val ordering = new Ordering[JvmType] {
164+
def compare(x: Array[Byte], y: Array[Byte]): Int = {
165+
for (i <- 0 until x.length; if i < y.length) {
166+
val res = x(i).compareTo(y(i))
167+
if (res != 0) return res
168+
}
169+
return x.length - y.length
170+
}
171+
}
162172
def simpleString: String = "binary"
163173
}
164174

sql/core/src/test/scala/org/apache/spark/sql/SQLQuerySuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,14 @@ class SQLQuerySuite extends QueryTest with BeforeAndAfterAll {
190190
sql("SELECT * FROM testData2 ORDER BY a DESC, b ASC"),
191191
Seq((3,1), (3,2), (2,1), (2,2), (1,1), (1,2)))
192192

193+
checkAnswer(
194+
sql("SELECT b FROM binaryData ORDER BY a ASC"),
195+
(1 to 5).map(Row(_)).toSeq)
196+
197+
checkAnswer(
198+
sql("SELECT b FROM binaryData ORDER BY a DESC"),
199+
(1 to 5).map(Row(_)).toSeq.reverse)
200+
193201
checkAnswer(
194202
sql("SELECT * FROM arrayData ORDER BY data[0] ASC"),
195203
arrayData.collect().sortBy(_.data(0)).toSeq)

sql/core/src/test/scala/org/apache/spark/sql/TestData.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ object TestData {
5454
TestData2(3, 2) :: Nil)
5555
testData2.registerTempTable("testData2")
5656

57+
case class BinaryData(a: Array[Byte], b: Int)
58+
val binaryData: SchemaRDD =
59+
TestSQLContext.sparkContext.parallelize(
60+
BinaryData("12".getBytes(), 1) ::
61+
BinaryData("22".getBytes(), 5) ::
62+
BinaryData("122".getBytes(), 3) ::
63+
BinaryData("121".getBytes(), 2) ::
64+
BinaryData("123".getBytes(), 4) :: Nil)
65+
binaryData.registerTempTable("binaryData")
66+
5767
// TODO: There is no way to express null primitives as case classes currently...
5868
val testData3 =
5969
logical.LocalRelation('a.int, 'b.int).loadData(

0 commit comments

Comments
 (0)