From 6ffe34a560829ac0e1f85b92f958ab394b1dda7a Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Thu, 30 Jul 2015 12:31:08 +0100 Subject: [PATCH 1/2] Check that SparseVector size is at least as big as the number of indices/values provided. And add tests for constructor checks. --- .../org/apache/spark/mllib/linalg/Vectors.scala | 2 ++ .../apache/spark/mllib/linalg/VectorsSuite.scala | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala b/mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala index 0cb28d78bec05..23c2c16d68d9a 100644 --- a/mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala +++ b/mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala @@ -637,6 +637,8 @@ class SparseVector( require(indices.length == values.length, "Sparse vectors require that the dimension of the" + s" indices match the dimension of the values. You provided ${indices.length} indices and " + s" ${values.length} values.") + require(indices.length <= size, s"You provided ${indices.length} indices and values, " + + s"which exceeds the specified vector size ${size}.") override def toString: String = s"($size,${indices.mkString("[", ",", "]")},${values.mkString("[", ",", "]")})" diff --git a/mllib/src/test/scala/org/apache/spark/mllib/linalg/VectorsSuite.scala b/mllib/src/test/scala/org/apache/spark/mllib/linalg/VectorsSuite.scala index 03be4119bdaca..627bed4cf8252 100644 --- a/mllib/src/test/scala/org/apache/spark/mllib/linalg/VectorsSuite.scala +++ b/mllib/src/test/scala/org/apache/spark/mllib/linalg/VectorsSuite.scala @@ -57,6 +57,21 @@ class VectorsSuite extends SparkFunSuite with Logging { assert(vec.values === values) } + test("sparse vector construction with mismatched indices/values array") { + intercept[IllegalArgumentException] { + Vectors.sparse(4, Array(1,2,3), Array(3.0, 5.0, 7.0, 9.0)) + } + intercept[IllegalArgumentException] { + Vectors.sparse(4, Array(1,2,3), Array(3.0, 5.0)) + } + } + + test("sparse vector construction with too many indices vs size") { + intercept[IllegalArgumentException] { + Vectors.sparse(3, Array(1,2,3,4), Array(3.0, 5.0, 7.0, 9.0)) + } + } + test("dense to array") { val vec = Vectors.dense(arr).asInstanceOf[DenseVector] assert(vec.toArray.eq(arr)) From e8dc31e899148989027b3a72e47a803a368a9881 Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Thu, 30 Jul 2015 16:34:49 +0100 Subject: [PATCH 2/2] Fix scalastyle --- .../scala/org/apache/spark/mllib/linalg/VectorsSuite.scala | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mllib/src/test/scala/org/apache/spark/mllib/linalg/VectorsSuite.scala b/mllib/src/test/scala/org/apache/spark/mllib/linalg/VectorsSuite.scala index 627bed4cf8252..1c37ea5123e82 100644 --- a/mllib/src/test/scala/org/apache/spark/mllib/linalg/VectorsSuite.scala +++ b/mllib/src/test/scala/org/apache/spark/mllib/linalg/VectorsSuite.scala @@ -59,16 +59,16 @@ class VectorsSuite extends SparkFunSuite with Logging { test("sparse vector construction with mismatched indices/values array") { intercept[IllegalArgumentException] { - Vectors.sparse(4, Array(1,2,3), Array(3.0, 5.0, 7.0, 9.0)) + Vectors.sparse(4, Array(1, 2, 3), Array(3.0, 5.0, 7.0, 9.0)) } intercept[IllegalArgumentException] { - Vectors.sparse(4, Array(1,2,3), Array(3.0, 5.0)) + Vectors.sparse(4, Array(1, 2, 3), Array(3.0, 5.0)) } } test("sparse vector construction with too many indices vs size") { intercept[IllegalArgumentException] { - Vectors.sparse(3, Array(1,2,3,4), Array(3.0, 5.0, 7.0, 9.0)) + Vectors.sparse(3, Array(1, 2, 3, 4), Array(3.0, 5.0, 7.0, 9.0)) } }