Skip to content
Snippets Groups Projects
Commit d24e7364 authored by Dongjoon Hyun's avatar Dongjoon Hyun Committed by Reynold Xin
Browse files

[SPARK-18200][GRAPHX] Support zero as an initial capacity in OpenHashSet

## What changes were proposed in this pull request?

[SPARK-18200](https://issues.apache.org/jira/browse/SPARK-18200) reports Apache Spark 2.x raises `java.lang.IllegalArgumentException: requirement failed: Invalid initial capacity` while running `triangleCount`. The root cause is that `VertexSet`, a type alias of `OpenHashSet`, does not allow zero as a initial size. This PR loosens the restriction to allow zero.

## How was this patch tested?

Pass the Jenkins test with a new test case in `OpenHashSetSuite`.

Author: Dongjoon Hyun <dongjoon@apache.org>

Closes #15741 from dongjoon-hyun/SPARK-18200.
parent 9ddec863
No related branches found
No related tags found
No related merge requests found
......@@ -48,7 +48,7 @@ class OpenHashSet[@specialized(Long, Int) T: ClassTag](
require(initialCapacity <= OpenHashSet.MAX_CAPACITY,
s"Can't make capacity bigger than ${OpenHashSet.MAX_CAPACITY} elements")
require(initialCapacity >= 1, "Invalid initial capacity")
require(initialCapacity >= 0, "Invalid initial capacity")
require(loadFactor < 1.0, "Load factor must be less than 1.0")
require(loadFactor > 0.0, "Load factor must be greater than 0.0")
......@@ -271,8 +271,12 @@ class OpenHashSet[@specialized(Long, Int) T: ClassTag](
private def hashcode(h: Int): Int = Hashing.murmur3_32().hashInt(h).asInt()
private def nextPowerOf2(n: Int): Int = {
val highBit = Integer.highestOneBit(n)
if (highBit == n) n else highBit << 1
if (n == 0) {
2
} else {
val highBit = Integer.highestOneBit(n)
if (highBit == n) n else highBit << 1
}
}
}
......
......@@ -49,9 +49,6 @@ class OpenHashMapSuite extends SparkFunSuite with Matchers {
intercept[IllegalArgumentException] {
new OpenHashMap[String, Int](-1)
}
intercept[IllegalArgumentException] {
new OpenHashMap[String, String](0)
}
}
test("primitive value") {
......
......@@ -176,4 +176,9 @@ class OpenHashSetSuite extends SparkFunSuite with Matchers {
assert(set.size === 1000)
assert(set.capacity > 1000)
}
test("SPARK-18200 Support zero as an initial set size") {
val set = new OpenHashSet[Long](0)
assert(set.size === 0)
}
}
......@@ -49,9 +49,6 @@ class PrimitiveKeyOpenHashMapSuite extends SparkFunSuite with Matchers {
intercept[IllegalArgumentException] {
new PrimitiveKeyOpenHashMap[Int, Int](-1)
}
intercept[IllegalArgumentException] {
new PrimitiveKeyOpenHashMap[Int, Int](0)
}
}
test("basic operations") {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment