Skip to content

Commit e48caf9

Browse files
author
xinyunh
committed
Fix errors in Float and Double comparators
1 parent fceb9dc commit e48caf9

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

src/main/scala/org/apache/spark/sql/hbase/util/comparators.scala

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,11 @@ object LongComparator {
148148

149149
class LongComparator(value: Array[Byte]) extends CustomComparator(value) {
150150
override def compareTo(value: HBaseRawType, offset: Int, length: Int): Int = {
151-
(StringBytesUtils.toLong(this.value, 0, this.value.length) -
152-
StringBytesUtils.toLong(value, offset, length)).toInt
151+
val r = StringBytesUtils.toLong(this.value, 0, this.value.length) -
152+
StringBytesUtils.toLong(value, offset, length)
153+
if (r > 0) 1
154+
else if (r == 0) 0
155+
else -1
153156
}
154157
}
155158

@@ -175,8 +178,11 @@ object DoubleComparator {
175178

176179
class DoubleComparator(value: Array[Byte]) extends CustomComparator(value) {
177180
override def compareTo(value: HBaseRawType, offset: Int, length: Int): Int = {
178-
(StringBytesUtils.toDouble(this.value, 0, this.value.length) -
179-
StringBytesUtils.toDouble(value, offset, length)).toInt
181+
val r = StringBytesUtils.toDouble(this.value, 0, this.value.length) -
182+
StringBytesUtils.toDouble(value, offset, length)
183+
if (r > 0) 1
184+
else if (r == 0) 0
185+
else -1
180186
}
181187
}
182188

@@ -202,8 +208,11 @@ object FloatComparator {
202208

203209
class FloatComparator(value: Array[Byte]) extends CustomComparator(value) {
204210
override def compareTo(value: HBaseRawType, offset: Int, length: Int): Int = {
205-
(StringBytesUtils.toFloat(this.value, 0, this.value.length) -
206-
StringBytesUtils.toFloat(value, offset, length)).toInt
211+
val r = StringBytesUtils.toFloat(this.value, 0, this.value.length) -
212+
StringBytesUtils.toFloat(value, offset, length)
213+
if (r > 0) 1
214+
else if (r == 0) 0
215+
else -1
207216
}
208217
}
209218

src/test/scala/org/apache/spark/sql/hbase/HBaseBasicOperationSuite.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class HBaseBasicOperationSuite extends TestBaseWithSplitData {
7777
| col5 LONG, col6 FLOAT, col7 DOUBLE,
7878
| PRIMARY KEY(col1))
7979
| MAPPED BY (ht2, COLS=[col2=cf1.cq11, col3=cf1.cq12, col4=cf1.cq13,
80-
| col5=cf2.cq21, col6=cf2.cq22, col7=cf2.cq23])""".stripMargin
80+
| col5=cf2.cq21, col6=cf2.cq22, col7=cf2.cq23]) In StringFormat""".stripMargin
8181
)
8282

8383
assert(sql( """SELECT * FROM tb1""").collect().length == 0)
@@ -86,15 +86,15 @@ class HBaseBasicOperationSuite extends TestBaseWithSplitData {
8686
sql( """INSERT INTO TABLE tb1 VALUES ("row3", true , 555 , 999 , 100000, 500.05, 10000.01)""")
8787
sql( """SELECT col1 FROM tb1 where col2<true order by col2""")
8888
.collect().zip(Seq("row1", "row2")).foreach{case (r,s) => assert(r.getString(0) == s)}
89-
sql( """SELECT col1 FROM tb1 where col3>500 order by col3""")
89+
sql( """SELECT * FROM tb1 where col3>500 order by col3""")
9090
.collect().zip(Seq("row3", "row1")).foreach{case (r,s) => assert(r.getString(0) == s)}
91-
sql( """SELECT col1 FROM tb1 where col4>5000 order by col4""")
91+
sql( """SELECT * FROM tb1 where col4>5000 order by col4""")
9292
.collect().zip(Seq("row1", "row2")).foreach{case (r,s) => assert(r.getString(0) == s)}
93-
sql( """SELECT col1 FROM tb1 where col5>50000 order by col5""")
93+
sql( """SELECT * FROM tb1 where col5>50000 order by col5""")
9494
.collect().zip(Seq("row3")).foreach{case (r,s) => assert(r.getString(0) == s)}
95-
sql( """SELECT col1 FROM tb1 where col6>500 order by col6""")
95+
sql( """SELECT * FROM tb1 where col6>500 order by col6""")
9696
.collect().zip(Seq("row3", "row2")).foreach{case (r,s) => assert(r.getString(0) == s)}
97-
sql( """SELECT col1 FROM tb1 where col7>5000 order by col7""")
97+
sql( """SELECT * FROM tb1 where col7>5000 order by col7""")
9898
.collect().zip(Seq("row2", "row3")).foreach{case (r,s) => assert(r.getString(0) == s)}
9999

100100
sql( """DROP TABLE tb1""")

0 commit comments

Comments
 (0)