File tree Expand file tree Collapse file tree 2 files changed +32
-4
lines changed
main/scala/org/apache/spark/mllib/util
test/scala/org/apache/spark/mllib/util Expand file tree Collapse file tree 2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -82,8 +82,19 @@ object MLUtils {
82
82
val value = indexAndValue(1 ).toDouble
83
83
(index, value)
84
84
}.unzip
85
- require(indices.size == 0 || indices(0 ) >= 0 ,
86
- " indices should be one-based in LIBSVM format" )
85
+
86
+ // check if indices is one-based and in ascending order
87
+ var previous = - 1
88
+ var i = 0
89
+ val indicesLength = indices.size
90
+ while (i < indicesLength) {
91
+ if (indices(i) <= previous) {
92
+ throw new IllegalArgumentException (" indices should be one-based and in ascending order" )
93
+ }
94
+ previous = indices(i)
95
+ i += 1
96
+ }
97
+
87
98
(label, indices.toArray, values.toArray)
88
99
}
89
100
Original file line number Diff line number Diff line change @@ -110,7 +110,7 @@ class MLUtilsSuite extends FunSuite with MLlibTestSparkContext {
110
110
Utils .deleteRecursively(tempDir)
111
111
}
112
112
113
- test(" loadLibSVMFile throws SparkException when passing a zero-based vector " ) {
113
+ test(" loadLibSVMFile throws IllegalArgumentException when indices is zero-based" ) {
114
114
val lines =
115
115
"""
116
116
|0
@@ -122,7 +122,24 @@ class MLUtilsSuite extends FunSuite with MLlibTestSparkContext {
122
122
val path = tempDir.toURI.toString
123
123
124
124
intercept[SparkException ] {
125
- val pointsWithoutNumFeatures = loadLibSVMFile(sc, path).collect()
125
+ loadLibSVMFile(sc, path).collect()
126
+ }
127
+ Utils .deleteRecursively(tempDir)
128
+ }
129
+
130
+ test(" loadLibSVMFile throws IllegalArgumentException when indices is not in ascending order" ) {
131
+ val lines =
132
+ """
133
+ |0
134
+ |0 3:4.0 2:5.0 6:6.0
135
+ """ .stripMargin
136
+ val tempDir = Utils .createTempDir()
137
+ val file = new File (tempDir.getPath, " part-00000" )
138
+ Files .write(lines, file, Charsets .US_ASCII )
139
+ val path = tempDir.toURI.toString
140
+
141
+ intercept[SparkException ] {
142
+ loadLibSVMFile(sc, path).collect()
126
143
}
127
144
Utils .deleteRecursively(tempDir)
128
145
}
You can’t perform that action at this time.
0 commit comments