Skip to content

Commit fa6de40

Browse files
zhpenggrxin
authored andcommitted
bugfix: overflow of graphx Edge compare function
Author: Zhen Peng <[email protected]> Closes #769 from zhpengg/bugfix-graphx-edge-compare and squashes the following commits: 8a978ff [Zhen Peng] add ut for graphx Edge.lexicographicOrdering.compare 413c258 [Zhen Peng] there maybe a overflow for two Long's substraction
1 parent e304eb9 commit fa6de40

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

graphx/src/main/scala/org/apache/spark/graphx/Edge.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ case class Edge[@specialized(Char, Int, Boolean, Byte, Long, Float, Double) ED]
5656

5757
object Edge {
5858
private[graphx] def lexicographicOrdering[ED] = new Ordering[Edge[ED]] {
59-
override def compare(a: Edge[ED], b: Edge[ED]): Int =
60-
(if (a.srcId != b.srcId) a.srcId - b.srcId else a.dstId - b.dstId).toInt
59+
override def compare(a: Edge[ED], b: Edge[ED]): Int = {
60+
if (a.srcId == b.srcId) {
61+
if (a.dstId == b.dstId) 0
62+
else if (a.dstId < b.dstId) -1
63+
else 1
64+
} else if (a.srcId < b.srcId) -1
65+
else 1
66+
}
6167
}
6268
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.spark.graphx
19+
20+
import org.scalatest.FunSuite
21+
22+
class EdgeSuite extends FunSuite {
23+
test ("compare") {
24+
// decending order
25+
val testEdges: Array[Edge[Int]] = Array(
26+
Edge(0x7FEDCBA987654321L, -0x7FEDCBA987654321L, 1),
27+
Edge(0x2345L, 0x1234L, 1),
28+
Edge(0x1234L, 0x5678L, 1),
29+
Edge(0x1234L, 0x2345L, 1),
30+
Edge(-0x7FEDCBA987654321L, 0x7FEDCBA987654321L, 1)
31+
)
32+
// to ascending order
33+
val sortedEdges = testEdges.sorted(Edge.lexicographicOrdering[Int])
34+
35+
for (i <- 0 until testEdges.length) {
36+
assert(sortedEdges(i) == testEdges(testEdges.length - i - 1))
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)