Skip to content

Commit ac86eed

Browse files
committed
initial commit
1 parent fbbe37e commit ac86eed

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/TypeUtils.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ object TypeUtils {
7070

7171
def compareBinary(x: Array[Byte], y: Array[Byte]): Int = {
7272
for (i <- 0 until x.length; if i < y.length) {
73-
val res = x(i).compareTo(y(i))
73+
val v1 = x(i) & 0xff
74+
val v2 = y(i) & 0xff
75+
val res = v1 - v2
7476
if (res != 0) return res
7577
}
7678
x.length - y.length

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/OrderingSuite.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,22 @@ class OrderingSuite extends SparkFunSuite with ExpressionEvalHelper {
137137
// verify that we can support up to 5000 ordering comparisons, which should be sufficient
138138
GenerateOrdering.generate(Array.fill(5000)(sortOrder))
139139
}
140+
141+
test("SPARK-21344: BinaryType comparison does signed byte array comparison") {
142+
val b1 = Array[Byte](1) // 0x01
143+
val b2 = Array[Byte](-1) // 0xff
144+
145+
val rowOrdering = InterpretedOrdering.forSchema(Seq(BinaryType, BinaryType))
146+
val genOrdering = GenerateOrdering.generate(
147+
BoundReference(0, BinaryType, nullable = true).asc ::
148+
BoundReference(1, BinaryType, nullable = true).asc :: Nil)
149+
val rowType = StructType(
150+
StructField("b1", BinaryType, nullable = true) ::
151+
StructField("b2", BinaryType, nullable = true) :: Nil)
152+
val toCatalyst = CatalystTypeConverters.createToCatalystConverter(rowType)
153+
val rowB1 = toCatalyst(Row(b1)).asInstanceOf[InternalRow]
154+
val rowB2 = toCatalyst(Row(b2)).asInstanceOf[InternalRow]
155+
assert(rowOrdering.compare(rowB1, rowB2) < 0)
156+
assert(genOrdering.compare(rowB1, rowB2) < 0)
157+
}
140158
}

0 commit comments

Comments
 (0)