Skip to content

Fix pgr_bellmanFord neg-edges empty check#3072

Open
sakirr05 wants to merge 1 commit intopgRouting:developfrom
sakirr05:fix/bellman-ford-neg-empty-check
Open

Fix pgr_bellmanFord neg-edges empty check#3072
sakirr05 wants to merge 1 commit intopgRouting:developfrom
sakirr05:fix/bellman-ford-neg-empty-check

Conversation

@sakirr05
Copy link

@sakirr05 sakirr05 commented Feb 19, 2026

Fix wrong empty-check in Bellman-Ford neg-edges driver

The neg-edges driver was returning "No edges found" whenever the main edges_sql had any rows. The bug is in the condition that decides when to treat the graph as empty.

What was wrong

In bellman_ford_neg_driver.cpp we had:

if (edges.size() + neg_edges.empty()) {
    *notice_msg = to_pg_msg("No edges found");
    return;
}

edges.size() is a size_t and neg_edges.empty() is a bool (0 or 1). So we're doing integer + bool. For example with 3 positive edges and 2 negative edges we get 3 + 0 = 3 (truthy), so we always hit this early return when there are any positive edges. The only time we didn't was when edges was empty and neg_edges.empty() was true (0 + 1 = 1), i.e. both empty.

Fix

We only want to return "No edges found" when both edge sets are empty:

if (edges.empty() && neg_edges.empty()) {
    *notice_msg = to_pg_msg("No edges found");
    return;
}

Example (before vs after)

Before the fix, a call with both positive and negative edges returns no rows:

-- Positive edges: 1→2 (cost 1), 2→3 (cost 2)
-- Negative edge: 3→4 (cost -0.5)
-- Path 1→4 should have cost 2.5

SELECT * FROM pgr_bellmanFord(
  'SELECT 1 AS id, 1 AS source, 2 AS target, 1.0::float AS cost
   UNION ALL SELECT 2, 2, 3, 2.0',
  'SELECT 3 AS id, 3 AS source, 4 AS target, -0.5::float AS cost',
  1, 4, true
);
-- Before: 0 rows (wrong)
-- After:  path with agg_cost 2.5

What's in this PR

  • Fix the condition in src/bellman_ford/bellman_ford_neg_driver.cpp (line 119).

Summary by CodeRabbit

  • Bug Fixes
    • Corrected edge detection logic in the Bellman-Ford algorithm to properly identify and handle graphs with no edges, ensuring accurate processing and appropriate status reporting.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 19, 2026

No actionable comments were generated in the recent review. 🎉


Walkthrough

The no-edges detection condition in the Bellman-Ford negative driver was modified. The logic now treats the case as "no edges" only when both the standard edge list and negative edge list are simultaneously empty, affecting behavior when no edges exist.

Changes

Cohort / File(s) Summary
No-Edges Detection Logic
src/bellman_ford/bellman_ford_neg_driver.cpp
Updated condition from edges.size() + neg_edges.empty() to edges.empty() && neg_edges.empty(), changing when the function short-circuits and assigns notice/log messages for the no-edges case.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 One line changed, logic made clear,
Where edges and negatives both disappear,
Now both must be empty to trigger the call,
A condition refined, precise overall! ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: fixing an incorrect empty-check condition in the Bellman-Ford negative-edges driver that was preventing the function from working correctly.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

1 participant

Comments