Skip to content

Commit 546569e

Browse files
Chenliang Xumengxr
authored andcommitted
[SPARK-14187][MLLIB] Fix incorrect use of binarySearch in SparseMatrix
## What changes were proposed in this pull request? Fix incorrect use of binarySearch in SparseMatrix ## How was this patch tested? Unit test added. Author: Chenliang Xu <chexu@groupon.com> Closes #11992 from luckyrandom/SPARK-14187. (cherry picked from commit c838829) Signed-off-by: Xiangrui Meng <meng@databricks.com>
1 parent fba84d1 commit 546569e

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ class SparseMatrix @Since("1.3.0") (
584584

585585
private[mllib] def update(i: Int, j: Int, v: Double): Unit = {
586586
val ind = index(i, j)
587-
if (ind == -1) {
587+
if (ind < 0) {
588588
throw new NoSuchElementException("The given row and column indices correspond to a zero " +
589589
"value. Only non-zero elements in Sparse Matrices can be updated.")
590590
} else {

mllib/src/test/scala/org/apache/spark/mllib/linalg/MatricesSuite.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,10 @@ class MatricesSuite extends SparkFunSuite {
150150
sparseMat.update(0, 0, 10.0)
151151
}
152152

153+
intercept[NoSuchElementException] {
154+
sparseMat.update(2, 1, 10.0)
155+
}
156+
153157
sparseMat.update(0, 1, 10.0)
154158
assert(sparseMat(0, 1) === 10.0)
155159
assert(sparseMat.values(2) === 10.0)

0 commit comments

Comments
 (0)