Skip to content
Merged
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
5 changes: 4 additions & 1 deletion python/graphframes/graphframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ def connectedComponents(
broadcastThreshold: int = 1000000,
useLabelsAsComponents: bool = False,
use_local_checkpoints: bool = False,
max_iter: int = 2 ^ 31 - 2,
max_iter: int = 2**31 - 2,
storage_level: StorageLevel = StorageLevel.MEMORY_AND_DISK_DESER,
) -> DataFrame:
"""
Expand All @@ -685,6 +685,9 @@ def connectedComponents(
a persistent checkpointDir; from the other side, local
checkpoints are less reliable and require executors to have
big enough local disks.
:param max_iter: maximum number of Pregel supersteps, only used when the algorithm is
"graphx" (default: 2 ** 31 - 2, that is effectively unlimited). The
other algorithms run until convergence and ignore this value.
:param storage_level: storage level for both intermediate and final dataframes.

:return: DataFrame with new vertices column "component"
Expand Down
23 changes: 23 additions & 0 deletions python/tests/test_graphframes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#


import inspect
from dataclasses import dataclass

import pytest
Expand Down Expand Up @@ -644,6 +645,28 @@ def test_connected_components_example(spark: SparkSession) -> None:
_ = cc.unpersist()


def test_connected_components_graphx_default_max_iter_is_unlimited(spark: SparkSession) -> None:
"""Regression: the default `2 ^ 31 - 2` is XOR and evaluated to 31, truncating GraphX."""
n = 50
v = spark.createDataFrame([(i,) for i in range(n)], ["id"])
e = spark.createDataFrame([(i, i + 1) for i in range(n - 1)], ["src", "dst"])
g = GraphFrame(v, e)

# max_iter is deliberately not passed: this exercises the default. The path graph has
# diameter n - 1 = 49, so a default capped at 31 supersteps splits it into 19 components.
result = g.connectedComponents(algorithm="graphx")
assert result.count() == n
assert result.select("component").distinct().count() == 1

_ = result.unpersist()


def test_connected_components_max_iter_default_value() -> None:
"""Guard the default itself so a `**` -> `^` regression fails without starting Spark."""
default = inspect.signature(GraphFrame.connectedComponents).parameters["max_iter"].default
assert default == 2**31 - 2


@pytest.mark.parametrize("args", PREGEL_ARGUMENTS, ids=PREGEL_IDS)
def test_shortest_paths(spark: SparkSession, args: PregelArguments) -> None:
edges = [(1, 2), (1, 5), (2, 3), (2, 5), (3, 4), (4, 5), (4, 6)]
Expand Down
Loading