Skip to content

Commit a71c6d1

Browse files
zsxwingrxin
authored andcommitted
SPARK-1628: Add missing hashCode methods in Partitioner subclasses
JIRA: https://issues.apache.org/jira/browse/SPARK-1628 Added `hashCode` in HashPartitioner, RangePartitioner, PythonPartitioner and PageRankUtils.CustomPartitioner. Author: zsxwing <[email protected]> Closes apache#549 from zsxwing/SPARK-1628 and squashes the following commits: 2620936 [zsxwing] SPARK-1628: Add missing hashCode methods in Partitioner subclasses
1 parent ee96e94 commit a71c6d1

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

core/src/main/scala/org/apache/spark/Partitioner.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class HashPartitioner(partitions: Int) extends Partitioner {
8383
case _ =>
8484
false
8585
}
86+
87+
override def hashCode: Int = numPartitions
8688
}
8789

8890
/**
@@ -119,7 +121,7 @@ class RangePartitioner[K : Ordering : ClassTag, V](
119121
}
120122
}
121123

122-
def numPartitions = partitions
124+
def numPartitions = rangeBounds.length + 1
123125

124126
private val binarySearch: ((Array[K], K) => Int) = CollectionsUtils.makeBinarySearch[K]
125127

@@ -155,4 +157,17 @@ class RangePartitioner[K : Ordering : ClassTag, V](
155157
case _ =>
156158
false
157159
}
160+
161+
162+
override def hashCode(): Int = {
163+
val prime = 31
164+
var result = 1
165+
var i = 0
166+
while (i < rangeBounds.length) {
167+
result = prime * result + rangeBounds(i).hashCode
168+
i += 1
169+
}
170+
result = prime * result + ascending.hashCode
171+
result
172+
}
158173
}

core/src/main/scala/org/apache/spark/api/python/PythonPartitioner.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ private[spark] class PythonPartitioner(
5050
case _ =>
5151
false
5252
}
53+
54+
override def hashCode: Int = 31 * numPartitions + pyPartitionFunctionId.hashCode
5355
}

examples/src/main/scala/org/apache/spark/examples/bagel/PageRankUtils.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,6 @@ class CustomPartitioner(partitions: Int) extends Partitioner {
124124
c.numPartitions == numPartitions
125125
case _ => false
126126
}
127+
128+
override def hashCode: Int = numPartitions
127129
}

0 commit comments

Comments
 (0)