Skip to content

add reversed implementation to Pregel API#813

Open
slavlotski wants to merge 2 commits intographframes:mainfrom
slavlotski:feat--GraphFrames.as_reversed
Open

add reversed implementation to Pregel API#813
slavlotski wants to merge 2 commits intographframes:mainfrom
slavlotski:feat--GraphFrames.as_reversed

Conversation

@slavlotski
Copy link
Copy Markdown

What changes were proposed in this pull request?

Adds asReversed() method to GraphFrame that reverses the direction of all edges in the graph. For every directed edge (src, dst), the resulting graph contains an edge (dst, src) with
the same attributes. Vertices remain unchanged.

Implemented across all three layers:

  • Scala API: asReversed() in GraphFrame.scala
  • PySpark Classic: delegates to JVM via self._jvm_graph.asReversed()
  • PySpark Connect: pure DataFrame column swap (no protobuf needed)
  • Python wrapper: as_reversed() delegates to self._impl.asReversed()

Tests added for both Scala (GraphFrameSuite) and Python (test_graphframes.py), covering edges with and without attributes.

Closes #796

Why are the changes needed?

Edge reversal is a common graph operation needed for:

  1. Reusing forward-only Pregel algorithms in the backward direction without rewriting them
  2. Supporting upcoming optimizations to Pregel and future AggregateNeighbors, both of which depend on edge directionality
  3. Computing reverse PageRank, backward reachability, and other direction-dependent analyses

Currently users must manually swap src/dst columns — this provides an API for it.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an edge-reversal helper to GraphFrames so users can easily run direction-dependent algorithms (e.g., Pregel) on the transposed graph without manually swapping src/dst.

Changes:

  • Add asReversed() to the Scala GraphFrame API (reverse all edges, preserve edge attributes).
  • Expose reversal in Python: classic delegates to JVM; connect performs a pure-DataFrame src/dst swap; public wrapper adds as_reversed().
  • Add Scala + Python tests for reversing edges with/without attributes.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
core/src/main/scala/org/graphframes/GraphFrame.scala Adds Scala asReversed() implementation to flip edge direction while preserving attributes.
core/src/test/scala/org/graphframes/GraphFrameSuite.scala Adds unit tests validating edge reversal behavior (with and without edge attributes).
python/graphframes/classic/graphframe.py Adds classic (JVM-backed) asReversed() delegating to Scala.
python/graphframes/connect/graphframes_client.py Adds connect asReversed() implemented via DataFrame column swapping.
python/graphframes/graphframe.py Adds public Python wrapper as_reversed() delegating to the underlying impl.
python/tests/test_graphframes.py Adds Python tests for as_reversed() (with and without edge attributes).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +1022 to +1023
return GraphFrame._from_impl(self._impl.asReversed()

Copy link

Copilot AI Mar 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as_reversed currently has a syntax error: the call to _from_impl(self._impl.asReversed() is missing the closing parentheses, which will prevent the module from importing and will break all users/tests. Close the call (and remove trailing whitespace) so it returns a new GraphFrame built from the reversed impl result.

Suggested change
return GraphFrame._from_impl(self._impl.asReversed()
return GraphFrame._from_impl(self._impl.asReversed())

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: GraphFrames.as_reversed

2 participants