Skip to content

New function pgr contraction dead end #2793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

cvvergara
Copy link
Member

@cvvergara cvvergara commented Mar 22, 2025

Fixes #2790 .

Changes proposed in this pull request:

  • New function: pgr_contractiondeadend(text,boolean,bigint[])
  • Added pgtap tests, docqueries, documentation
  • Entry on release_notes and news

@pgRouting/admins

Summary by CodeRabbit

  • New Features
    • Introduced a proposed graph contraction function, pgr_contractionDeadEnd, enhancing network routing capabilities.
  • Documentation
    • Added detailed documentation for the new pgr_contractionDeadEnd function, including usage examples and updates to related sections.
    • Updated release notes to include the new proposed function and its significance.
    • Introduced a new section for the "contraction-family" in the documentation.
  • Tests
    • Expanded automated test suites to validate the functionality of pgr_contractionDeadEnd under various scenarios.

@cvvergara cvvergara added this to the Release 3.8.0 milestone Mar 22, 2025
@cvvergara cvvergara self-assigned this Mar 22, 2025
Copy link
Contributor

coderabbitai bot commented Mar 22, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

The changes add a new proposed function, pgr_contractionDeadEnd, across both documentation and SQL code. Updates span release notes, version history, documentation files, test scripts, and SQL definitions. The new function is defined to perform dead end graph contraction with a signature accepting an edge query and options. Test scripts and translation files have been added or updated accordingly, along with modifications to existing documents to reference the new function while removing or adjusting details for the now deprecated explanation of dead end contraction.

Changes

Files Change Summary
NEWS.md, doc/src/release_notes.rst Added a new section for "New proposed functions" including the pgr_contractionDeadEnd function with reference to issue [#2790].
doc/_static/page_history.js Added a new entry for version '3.8' with the page pgr_contractionDeadEnd.
doc/contraction/CMakeLists.txt, docqueries/contraction/CMakeLists.txt, sql/contraction/CMakeLists.txt Updated LOCAL_FILES lists to include new documentation/query files (pgr_contractionDeadEnd.rst, contractionDeadEnd, deadEndContraction.sql).
doc/contraction/contraction-family.rst, doc/contraction/pgr_contractionDeadEnd.rst, doc/src/proposed.rst, doc/contraction/pgr_contraction.rst Updated documentation by introducing new sections and files for pgr_contractionDeadEnd and redirecting references from the old "Dead End Contraction" details.
docqueries/contraction/contractionDeadEnd.pg, docqueries/contraction/contractionDeadEnd.result Added new SQL scripts demonstrating usage of pgr_contractionDeadEnd, modifying the vertices table and creating a contracted_graph view.
locale/en/LC_MESSAGES/pgrouting_doc_strings.po, locale/pot/pgrouting_doc_strings.pot Updated translation message files for the new pgr_contractionDeadEnd function.
pgtap/contraction/contractionDeadEnd/** (multiple files) Introduced new SQL test scripts (edge cases, inner query, no crash tests, types check) to verify and validate the new function's behavior and types.
sql/contraction/deadEndContraction.sql, sql/sigs/pgrouting--3.8.sig Added the SQL function definition for pgr_contractionDeadEnd and updated the pgrouting signature file with the new function.
docqueries/contraction/test.conf Added new test file contractionDeadEnd.pg to the test configuration.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client Query
    participant DB as Database
    participant FUNC as pgr_contractionDeadEnd
    Client->>+DB: Execute SELECT pgr_contractionDeadEnd(Edges SQL, options)
    DB->>+FUNC: Process dead end contraction on provided edges
    FUNC-->>-DB: Return set (type, id, contracted_vertices, source, target, cost)
    DB-->>-Client: Return result set
Loading

Assessment against linked issues

Objective Addressed Explanation
pgr_contractionDeadEnd new function (#2790)

Possibly related PRs

Suggested labels

Tests, Documentation

Suggested reviewers

  • robe2
  • iosefa

Poem

I’m just a rabbit, hopping with delight,
In fields of code that glow so bright.
A new function now springs to life,
Contraction magic easing strife.
With pgr_contractionDeadEnd in view,
I nibble on updates fresh and new,
Happy hops in revision’s dew! 🐇✨

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f468d5 and ea8663b.

📒 Files selected for processing (22)
  • NEWS.md (1 hunks)
  • doc/_static/page_history.js (1 hunks)
  • doc/contraction/CMakeLists.txt (1 hunks)
  • doc/contraction/contraction-family.rst (1 hunks)
  • doc/contraction/pgr_contraction.rst (1 hunks)
  • doc/contraction/pgr_contractionDeadEnd.rst (1 hunks)
  • doc/src/proposed.rst (1 hunks)
  • doc/src/release_notes.rst (1 hunks)
  • docqueries/contraction/CMakeLists.txt (1 hunks)
  • docqueries/contraction/contractionDeadEnd.pg (1 hunks)
  • docqueries/contraction/contractionDeadEnd.result (1 hunks)
  • docqueries/contraction/test.conf (1 hunks)
  • locale/en/LC_MESSAGES/pgrouting_doc_strings.po (7 hunks)
  • locale/pot/pgrouting_doc_strings.pot (7 hunks)
  • pgtap/contraction/contractionDeadEnd/edge_cases/compare_dijkstra.pg (1 hunks)
  • pgtap/contraction/contractionDeadEnd/edge_cases/edge_cases.pg (1 hunks)
  • pgtap/contraction/contractionDeadEnd/inner_query.pg (1 hunks)
  • pgtap/contraction/contractionDeadEnd/no_crash_test.pg (1 hunks)
  • pgtap/contraction/contractionDeadEnd/types_check.pg (1 hunks)
  • sql/contraction/CMakeLists.txt (1 hunks)
  • sql/contraction/deadEndContraction.sql (1 hunks)
  • sql/sigs/pgrouting--3.8.sig (1 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@cvvergara cvvergara requested review from robe2 and iosefa March 22, 2025 14:52
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 19

🔭 Outside diff range comments (1)
doc/contraction/contraction-family.rst (1)

53-54: 🛠️ Refactor suggestion

Missing detailed explanation for Dead end contraction.

While you mention "Dead end contraction" as one of the two supported algorithms, the detailed explanation for it has been removed from the documentation. Since a dedicated function is now available, consider adding a brief explanation with a reference to the full documentation for pgr_contractionDeadEnd.

1. Dead end contraction
2. Linear contraction

+Dead end contraction identifies and contracts nodes that have only one adjacent edge, 
+reducing the graph size while preserving the connectivity. For detailed explanation, 
+see :doc:`pgr_contractionDeadEnd`.
+
Allowing the user to:
📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6f468d5 and 27680de.

📒 Files selected for processing (22)
  • NEWS.md (1 hunks)
  • doc/_static/page_history.js (1 hunks)
  • doc/contraction/CMakeLists.txt (1 hunks)
  • doc/contraction/contraction-family.rst (1 hunks)
  • doc/contraction/pgr_contraction.rst (1 hunks)
  • doc/contraction/pgr_contractionDeadEnd.rst (1 hunks)
  • doc/src/proposed.rst (1 hunks)
  • doc/src/release_notes.rst (1 hunks)
  • docqueries/contraction/CMakeLists.txt (1 hunks)
  • docqueries/contraction/contractionDeadEnd.pg (1 hunks)
  • docqueries/contraction/contractionDeadEnd.result (1 hunks)
  • docqueries/contraction/test.conf (1 hunks)
  • locale/en/LC_MESSAGES/pgrouting_doc_strings.po (7 hunks)
  • locale/pot/pgrouting_doc_strings.pot (7 hunks)
  • pgtap/contraction/contractionDeadEnd/edge_cases/compare_dijkstra.pg (1 hunks)
  • pgtap/contraction/contractionDeadEnd/edge_cases/edge_cases.pg (1 hunks)
  • pgtap/contraction/contractionDeadEnd/inner_query.pg (1 hunks)
  • pgtap/contraction/contractionDeadEnd/no_crash_test.pg (1 hunks)
  • pgtap/contraction/contractionDeadEnd/types_check.pg (1 hunks)
  • sql/contraction/CMakeLists.txt (1 hunks)
  • sql/contraction/deadEndContraction.sql (1 hunks)
  • sql/sigs/pgrouting--3.8.sig (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
sql/sigs/pgrouting--3.8.sig (2)
Learnt from: cvvergara
PR: pgRouting/pgrouting#2764
File: sql/sigs/pgrouting--3.8.sig:125-136
Timestamp: 2025-03-18T01:02:51.111Z
Learning: The file sql/sigs/pgrouting--*.sig is automatically generated by a command and cannot be manually modified. Comments about naming conventions or other improvements should be directed at the source code that generates these signatures, not at the signature file itself.
Learnt from: cvvergara
PR: pgRouting/pgrouting#2764
File: sql/sigs/pgrouting--3.8.sig:274-275
Timestamp: 2025-03-18T01:02:51.111Z
Learning: The file `sql/sigs/pgrouting--3.8.sig` is auto-generated by a command and should not be modified manually.
🪛 markdownlint-cli2 (0.17.2)
NEWS.md

40-40: Emphasis used instead of a heading
null

(MD036, no-emphasis-as-heading)

⏰ Context from checks skipped due to timeout of 90000ms (16)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Ubuntu Boost (86)
  • GitHub Check: Ubuntu Boost (80)
  • GitHub Check: Ubuntu Boost (79)
  • GitHub Check: Ubuntu Boost (77)
  • GitHub Check: Ubuntu Boost (78)
  • GitHub Check: Ubuntu clang
  • GitHub Check: Ubuntu Boost (83)
  • GitHub Check: Ubuntu Boost (84)
  • GitHub Check: Ubuntu Boost (76)
  • GitHub Check: Ubuntu Boost (75)
  • GitHub Check: Ubuntu Boost (56)
  • GitHub Check: Ubuntu Boost (68)
  • GitHub Check: Check queries
  • GitHub Check: macos (macos-14)
  • GitHub Check: macos (macos-latest)
🔇 Additional comments (51)
docqueries/contraction/test.conf (1)

7-7: Addition of test file looks good!

The inclusion of contractionDeadEnd.pg to the test configuration appropriately adds test coverage for the new pgr_contractionDeadEnd function.

doc/contraction/CMakeLists.txt (1)

5-5: Documentation file addition is appropriate

The addition of pgr_contractionDeadEnd.rst to the documentation build system properly integrates the documentation for the new function.

sql/contraction/CMakeLists.txt (1)

5-5: SQL implementation file added correctly

Adding deadEndContraction.sql to the SQL build system ensures the new function implementation is properly included in the build process.

docqueries/contraction/CMakeLists.txt (1)

4-4: Example queries file addition looks good

The addition of contractionDeadEnd to the documentation query build system appropriately provides example queries for the new function.

sql/sigs/pgrouting--3.8.sig (1)

89-89: New function signature looks good.

The new function pgr_contractiondeadend(text,boolean,bigint[]) has been properly added to the signature file in the correct alphabetical order.

Note: As indicated in the retrieved learnings, this file is automatically generated and should not be manually modified.

locale/en/LC_MESSAGES/pgrouting_doc_strings.po (8)

11-11: No issues found.
These metadata updates look fine and do not raise any concerns.


2772-2773: Reference to pgr_contractionDeadEnd looks consistent.
All good: the documentation string for :doc:\pgr_contractionDeadEnd`` is aligned with the newly introduced function.


2835-2853: Documentation entries for node adjacency seem fine.
The added lines defining “Node,” “Adjacent nodes,” and “Number of adjacent nodes” appear correct.


2869-2891: No issues found with these doc strings.
The clarifications about symmetrical properties and related examples are acceptable as given.


4109-4119: No issues found with the “New proposed functions” references.
References to “pgr_contractionDeadEnd” and issue #2790 are accurate and informative.


9292-9292: No issues with referencing Dead End Contraction doc.
The pointer to pgr_contractionDeadEnd is correct and clear.


9595-9749: Additions detailing pgr_contractionDeadEnd usage are well-structured.
No concerns here: the explanations and examples provide the necessary clarity on new function behavior.


9760-9761: 🧹 Nitpick (assertive)

Correct the spelling of “contrcted.”
Replace “contrcted” with “contracted” for clarity and correctness.

-After contracting :math:`2`, stop. Node :math:`1` has the information of nodes that were contrcted.
+After contracting :math:`2`, stop. Node :math:`1` has the information of nodes that were contracted.

Likely an incorrect or invalid review comment.

doc/src/release_notes.rst (1)

40-40: Formatting Note: Emphasis vs. Heading

A static analysis warning (MD036) flagged the use of emphasis instead of a proper heading on line 40. Given that this file is written in reStructuredText (RST) and that the formatting is chosen for consistency with pgRouting’s documentation style, this warning can be safely ignored. However, if you plan to convert or align the formatting with Markdown standards later, consider using a proper heading syntax.

locale/pot/pgrouting_doc_strings.pot (5)

11-11: Creation date update looks fine.
No issues found with this metadata change.


2557-2559: Addition of pgr_contractionDeadEnd reference is valid.
Ensure translations are added in the future when available.


2611-2628: New doc strings for node references appear correct.
All references to adjacency and node counts are consistent.


3685-3693: New references to proposed functions are clear.
Links to the GitHub issue (#2790) are correctly stated.


7978-7978: Reference to Dead End Contraction is appropriate.
Documentation cross-links align well with the new function.

doc/src/proposed.rst (1)

71-76: LGTM: Clean addition of contraction-family documentation.

The addition of the contraction-family section follows the same pattern as other family sections in this file, maintaining consistency with the existing documentation structure.

doc/contraction/pgr_contraction.rst (1)

78-78: Good reference redirection to the new specialized function.

The modification appropriately redirects readers to the new specialized pgr_contractionDeadEnd function documentation, which is a better approach than keeping the implementation details in the general pgr_contraction documentation.

doc/_static/page_history.js (1)

18-19: LGTM: Proper versioning for the new function.

The new entry is correctly added to the newpages array for version 3.8, following the established pattern of the file. This ensures that the documentation system properly associates the new function with the appropriate version.

sql/contraction/deadEndContraction.sql (3)

1-24: LGTM: License header looks good.

The license header follows the project's standard format and includes the appropriate copyright notice and GPL license text.


25-44: Function implementation is clean and follows project conventions.

The function signature and implementation look good:

  • Version marking (--v3.8) is included
  • Parameters are clearly documented with comments
  • Default values are appropriate
  • Implementation correctly calls the internal _pgr_contraction function with the dead end contraction method (ARRAY[1])

This specialized wrapper function will make it easier for users to perform specifically dead end contractions without having to understand the internals of the contraction methods.


45-54: Documentation comment is well structured.

The function comment provides clear information about the parameters and includes a link to the detailed documentation. The format follows the project's standards.

pgtap/contraction/contractionDeadEnd/types_check.pg (4)

21-21: Function plan and version check are well structured.

Good job setting up the plan count conditionally based on version compatibility.


33-35: Comprehensive function existence and signature checks.

The tests appropriately verify that the function exists with the correct signature and return type.


37-41: Thorough parameter name verification.

The test correctly verifies that the function has the expected parameter names in the right order.


43-44: Verify parameter types match function signature.

This test ensures the function accepts the correct parameter types.

doc/contraction/contraction-family.rst (2)

24-32: Correctly includes the new function in proposed section.

The documentation properly includes the warning about proposed functionality and lists the new pgr_contractionDeadEnd function.


38-38: Function added to documentation navigation.

The new function is correctly added to the hidden toctree for proper documentation navigation.

pgtap/contraction/contractionDeadEnd/inner_query.pg (3)

34-36: Good tests for different directed parameter values.

The tests correctly verify both directed and undirected graph functionality.


37-71: Comprehensive input validation tests.

The tests thoroughly verify the function's behavior with different array types and dimensions, ensuring it properly handles valid inputs and rejects invalid ones.


3-3: 🧹 Nitpick (assertive)

Update copyright year to current year.

The copyright year is set to 2025, but it should reflect the current year (2025) or when the code was actually created.

-Copyright (c) 2025  pgRouting developers
+Copyright (c) 2024  pgRouting developers

Likely an incorrect or invalid review comment.

docqueries/contraction/contractionDeadEnd.pg (3)

3-16: Well-structured basic usage examples.

The basic examples demonstrate proper usage of the function with both table and inline values.


17-42: Good examples with different graph structures.

These examples show how the function handles various graph structures including cul-de-sacs and directed edges.


78-99: Complex Dijkstra examples demonstrate practical usage.

These examples effectively show how to integrate the contraction results with Dijkstra algorithm for different scenarios, including handling cul-de-sac vertices.

pgtap/contraction/contractionDeadEnd/no_crash_test.pg (2)

31-55: Good approach for conditional version checks.
The structure of test_function() correctly accounts for min_version('3.8.0') and skips tests on earlier versions, ensuring forward-compatibility.


22-22:

❓ Verification inconclusive

Verify potential zero-cost edges.
Using sign(cost) and sign(reverse_cost) may unintentionally convert zero-cost edges to zero, possibly affecting edge traversal logic.

Please run the following script to find occurrences of zero-cost edges and confirm if they need special handling:


🏁 Script executed:

#!/bin/bash
# Searching for edges with zero cost.
rg -A 3 --line-number 'INSERT INTO edges|UPDATE edges|VALUES' .
rg "('cost'|sign\(cost\))"

Length of output: 67828


Action: Verify Handling of Zero‐Cost Edges

The update statement in this file (and similar ones across the test suite) applies the built‐in PostgreSQL function sign(), which maps positive values to 1, negative values to –1, and leaves zero unchanged. Our grep results indicate that some test inserts (for example, in the contraction tapfuncs data) explicitly include zero-cost edges. Please confirm whether retaining a cost of 0 for such edges is intended or if they should be adjusted (e.g. mapped to a nonzero default) so as not to inadvertently affect downstream edge traversal logic.

pgtap/contraction/contractionDeadEnd/edge_cases/compare_dijkstra.pg (2)

24-25: Check potential conflicts with newly added columns.
Adding is_new and contracted_vertices columns to edges and vertices might conflict with existing columns if they already exist. Consider adding a safeguard to handle potential name collisions or to drop them first if they exist.


101-107: Assess the integration of related calls.
create_contracted_graph, check_cg, and compare_dijkstra_contraction are chained correctly. Verify that the test suite captures all relevant states before and after calling each procedure to ensure consistency.

docqueries/contraction/contractionDeadEnd.result (1)

146-172: Validate correctness of path cost data.
The returned costs in these rows are critical for verifying correct shortest-path logic. Double-check actual edge values match expected results from the data definitions.

pgtap/contraction/contractionDeadEnd/edge_cases/edge_cases.pg (9)

22-22: Great approach using version-dependent test planning!

The conditional test plan count (128 for version >= 3.8.0, 1 otherwise) ensures appropriate test execution based on feature availability.


24-31: Well-structured test data table design.

The graphs table contains all necessary fields for testing the contraction functionality, with the dead_case field allowing for organized test case management.


33-63: Good use of descriptive comments for graph structures.

The comments provide clear visualization of the graph structures being tested, which enhances code readability and maintainability.


120-244: Comprehensive test coverage for different graph scenarios.

The edge_cases_sampledata function provides excellent coverage of different graph configurations with various forbidden vertices combinations. The systematic approach ensures all edge cases are properly tested.


269-280: Clear handling of directed vs undirected graph differences.

The code correctly handles the differences between directed and undirected graphs with appropriate conditional logic and test expectations.


291-295: Proper version checking for feature availability.

The code correctly checks if the current PostgreSQL version supports the new pgr_contractionDeadEnd function (>= 3.8.0) and skips tests appropriately if not supported.


297-305: Well-formed SQL prepared statements for function testing.

The prepared statements properly call the new pgr_contractionDeadEnd function with appropriate parameters, ensuring consistent test execution.


307-310: Thorough testing with both directed and undirected graphs.

Testing both directed (true) and undirected (false) scenarios ensures complete coverage of the function's behavior in all contexts.


316-319: Proper test cleanup with transaction rollback.

The code properly finalizes the tests and rolls back the transaction, ensuring test data doesn't persist in the database.

@cvvergara cvvergara merged commit 1f51838 into pgRouting:main Mar 22, 2025
26 checks passed
@cvvergara cvvergara deleted the issue-2790-new-function-pgr_contractionDeadEnd branch March 22, 2025 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create pgr_contractionDeadEnd new contraction function
2 participants