Skip to content

[SPARK-17027][ML] Avoid integer overflow in PolynomialExpansion.getPolySize #14614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package org.apache.spark.ml.feature

import scala.collection.mutable

import org.apache.commons.math3.util.CombinatoricsUtils

import org.apache.spark.annotation.Since
import org.apache.spark.ml.UnaryTransformer
import org.apache.spark.ml.linalg._
Expand Down Expand Up @@ -84,12 +86,12 @@ class PolynomialExpansion @Since("1.4.0") (@Since("1.4.0") override val uid: Str
@Since("1.6.0")
object PolynomialExpansion extends DefaultParamsReadable[PolynomialExpansion] {

private def choose(n: Int, k: Int): Int = {
Range(n, n - k, -1).product / Range(k, 1, -1).product
private def getPolySize(numFeatures: Int, degree: Int): Int = {
val n = CombinatoricsUtils.binomialCoefficient(numFeatures + degree, degree)
require(n <= Integer.MAX_VALUE)
n.toInt
}

private def getPolySize(numFeatures: Int, degree: Int): Int = choose(numFeatures + degree, degree)

private def expandDense(
values: Array[Double],
lastIdx: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,29 @@ class PolynomialExpansionSuite
.setDegree(3)
testDefaultReadWrite(t)
}

test("SPARK-17027. Integer overflow in PolynomialExpansion.getPolySize") {
val data: Array[(Vector, Int, Int)] = Array(
(Vectors.dense(1.0, 2.0, 3.0, 4.0, 5.0), 3002, 4367),
(Vectors.sparse(5, Seq((0, 1.0), (4, 5.0))), 3002, 4367),
(Vectors.dense(1.0, 2.0, 3.0, 4.0, 5.0, 6.0), 8007, 12375)
)

val df = spark.createDataFrame(data)
.toDF("features", "expectedPoly10size", "expectedPoly11size")

val t = new PolynomialExpansion()
.setInputCol("features")
.setOutputCol("polyFeatures")

for (i <- Seq(10, 11)) {
val transformed = t.setDegree(i)
.transform(df)
.select(s"expectedPoly${i}size", "polyFeatures")
.rdd.map { case Row(expected: Int, v: Vector) => expected == v.size }

assert(transformed.collect.forall(identity))
}
}
}