Skip to content

Hiroshima sql-function chapter#281

Open
cvvergara wants to merge 7 commits into
pgRouting:developfrom
cvvergara:hiroshima-sql-function
Open

Hiroshima sql-function chapter#281
cvvergara wants to merge 7 commits into
pgRouting:developfrom
cvvergara:hiroshima-sql-function

Conversation

@cvvergara

@cvvergara cvvergara commented Jun 30, 2026

Copy link
Copy Markdown
Member

Updates sql-function chapter

@pgRouting/admins

Summary by CodeRabbit

  • New Features

    • Updated the routing workshop examples to use revised route geometry, readable output, azimuth, and directionality outputs.
    • Added refreshed route comparison views and examples for the final routing workflow.
    • Simplified point-based routing so it now builds a reusable map view and generates testable query results.
  • Bug Fixes

    • Corrected route direction handling so displayed paths better match travel direction.
    • Updated waypoint coordinates and related workshop references to match the new route examples.

Align BYPRODUCTS with files actually generated by sql_function.sql,
in the order they appear. Add missing DEPENDS sql_function.sql.
Replaces old OUTPUT/DEPENDS pattern so psql always runs every
build, keeping DB objects in sync regardless of file timestamps.
@cvvergara cvvergara added this to the FOSS4G2026 milestone Jun 30, 2026
@coderabbitai

coderabbitai Bot commented Jun 30, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

This PR rewrites the Chapter 7 SQL function and withPoints workshop exercises. The wrk_dijkstra and wrk_withPoints functions drop generic edge-subset parameters in favor of hardcoded vehicle_net/vehicle_penalized_net networks, adding azimuth, readable, and geom outputs. Associated images.sql files are removed, CMake build commands and byproducts are updated, and documentation is rewritten to match new output columns and exercises.

Changes

SQL function workshop rewrite

Layer / File(s) Summary
New wrk_dijkstra function family
docs/scripts/basic/sql_function/sql_function.sql
Replaces the generic wrk_dijkstra(regclass, source, target) with wrk_dijkstra(source, target), wrk_dijkstra_fixed, and wrk_dijkstra_final over vehicle_net/vehicle_penalized_net, computing azimuth/readable/geom, plus new vw_initial, vw_fixed, vw_final views and azimuth-comparison helper queries.
Build wiring and image cleanup
docs/scripts/basic/sql_function/CMakeLists.txt, docs/scripts/basic/sql_function/images.sql, docs/basic/images/sql_function/CMakeLists.txt
CMake target now runs only sql_function.sql with new BYPRODUCTS; images.sql view definitions (using_vehicle, sql_route_geom) are removed; workshop image filenames updated to new route/directionality images.
sql_function.rst documentation rewrite
docs/basic/sql_function.rst
Rewrites function design tables, inputs/outputs, and exercises (additional information, route geometry, azimuth, directionality, final function) to match new column names and images.

withPoints workshop rewrite

Layer / File(s) Summary
New wrk_withPoints function and points_on_map view
docs/scripts/basic/withPoints/withPoints.sql
Adds points_on_map view; replaces wrk_withPoints (removing edges_subset) with a fixed-network implementation outputting azimuth/readable/geom; updates call sites and replaces example table with a view.
withPoints build wiring cleanup
docs/scripts/basic/withPoints/CMakeLists.txt, docs/scripts/basic/withPoints/images.sql
Reworks custom command to PRE_BUILD running only withPoints.sql; deletes images.sql and its views.
withPoints.rst documentation rewrite
docs/basic/withPoints.rst
Updates parameter/output documentation to reuse shared columns table, revises format() placeholder explanations, removes additional-information subsection, and condenses Exercise 4 instructions.

Workshop location config

Layer / File(s) Summary
CH7 waypoint reconfiguration
CMakeLists.txt
Reassigns CH7_PLACE_1/2, CH7_ID_1/2, and POINT1/2_LAT/LON values for the workshop area.

Sequence Diagram(s)

sequenceDiagram
  participant Script as sql_function.sql
  participant Vehicle as vehicle_net
  participant Penalized as vehicle_penalized_net
  participant Views as vw_initial / vw_fixed / vw_final

  Script->>Vehicle: wrk_dijkstra(source, target)
  Vehicle-->>Script: seq, id, azimuth, readable, geom
  Script->>Vehicle: wrk_dijkstra_fixed(source, target)
  Vehicle-->>Script: corrected geom, azimuth
  Script->>Penalized: wrk_dijkstra_final(source, target)
  Penalized-->>Script: final route + azimuth + geom
  Script->>Views: CREATE vw_initial, vw_fixed, vw_final
Loading
sequenceDiagram
  participant Script as withPoints.sql
  participant Map as points_on_map
  participant WithPoints as wrk_withPoints
  participant Penalized as vehicle_penalized_net
  participant Example as example view

  Script->>Map: SELECT * FROM points_on_map
  Script->>WithPoints: wrk_withPoints(lat1, lon1, lat2, lon2)
  WithPoints->>Penalized: pgr_withPoints via points_sql
  Penalized-->>WithPoints: route rows
  WithPoints-->>Script: seq, id, azimuth, readable, geom
  Script->>Example: CREATE OR REPLACE VIEW example AS SELECT *
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

  • pgRouting/workshop#259: Both PRs rewrite wrk_dijkstra implementation/signature in sql_function.sql and update related views in images.sql.
  • pgRouting/workshop#260: Both PRs touch the withPoints chapter's withPoints.sql function signature/output and CMakeLists.txt build wiring.
  • pgRouting/workshop#280: Both PRs update CMakeLists.txt workshop location configuration (CH7 place/ID/coordinate values).

Suggested reviewers

  • iosefa

A rabbit hops through routes anew,
Vehicle nets replacing the old crew,
Azimuth spins, geometry turns right,
Views named final, fixed, and bright,
Hop, hop — the workshop's freshly brewed! 🐇🗺️

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Title check ❓ Inconclusive The title is related to the SQL-function chapter update, but it is too generic to convey the main change. Rename it to summarize the actual update, e.g. "Revise SQL-function workshop chapter and routing examples".
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@docs/basic/sql_function.rst`:
- Line 160: The exercise numbering in the SQL function chapter is out of
sequence, with Exercise 5 appearing before Exercise 4, which breaks the
walkthrough flow. Update the exercise labels in the affected section so the
numbers increase sequentially in chapter order, and verify the related exercise
headings around the azimuth and subsequent step use the corrected numbering
consistently.

In `@docs/basic/withPoints.rst`:
- Around line 35-38: The chapter contract for wrk_withPoints is stale: it still
describes an edges table name even though the current implementation only
accepts the point coordinates and do_debug. Update the parameter list in
withPoints.rst to match the actual wrk_withPoints interface, removing the
edges-table input and aligning the surrounding prose with the accepted
lat1/lon1/lat2/lon2/do_debug arguments so readers see the correct arity.
- Line 104: Fix the user-facing typos in the withPoints.rst documentation text:
change “appropiate” to “appropriate” and reword “that is been generated” to a
correct phrase such as “that is generated” or “that is being generated”. Apply
the same wording cleanup to the repeated occurrence noted in the same document,
keeping the surrounding sentence meaning unchanged.

In `@docs/scripts/basic/withPoints/withPoints.sql`:
- Around line 71-99: The query in withPoints.sql is including the terminal
sentinel row from pgr_withPoints because it aliases edge to id and joins it
through results/additional, which leaves an extra blank segment. Update the
resuts_query/final_query flow to filter out the row where edge = -1 before the
LEFT JOIN to vehicle_net (or before building additional), so only real path
segments are returned and the example no longer includes NULL metadata.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e7209a1c-a500-4bb0-8cc3-ee86795b6fab

📥 Commits

Reviewing files that changed from the base of the PR and between 2bb50cb and e272b37.

⛔ Files ignored due to path filters (14)
  • docs/basic/images/sql_function/good_directionality.png is excluded by !**/*.png
  • docs/basic/images/sql_function/route_azimuth.png is excluded by !**/*.png
  • docs/basic/images/sql_function/route_final.png is excluded by !**/*.png
  • docs/basic/images/sql_function/route_names.png is excluded by !**/*.png
  • docs/basic/images/sql_function/route_readable.png is excluded by !**/*.png
  • docs/basic/images/sql_function/sql_azimuth_fixed.png is excluded by !**/*.png
  • docs/basic/images/sql_function/sql_route_geom.png is excluded by !**/*.png
  • docs/basic/images/sql_function/sql_route_geom_detail.png is excluded by !**/*.png
  • docs/basic/images/sql_function/sql_route_names.png is excluded by !**/*.png
  • docs/basic/images/sql_function/sql_route_readable.png is excluded by !**/*.png
  • docs/basic/images/sql_function/wrong_direccionality.png is excluded by !**/*.png
  • docs/basic/images/sql_function/wrong_directionality.png is excluded by !**/*.png
  • docs/basic/images/withPoints/points_on_map.png is excluded by !**/*.png
  • docs/basic/images/withPoints/points_routing.png is excluded by !**/*.png
📒 Files selected for processing (10)
  • CMakeLists.txt
  • docs/basic/images/sql_function/CMakeLists.txt
  • docs/basic/sql_function.rst
  • docs/basic/withPoints.rst
  • docs/scripts/basic/sql_function/CMakeLists.txt
  • docs/scripts/basic/sql_function/images.sql
  • docs/scripts/basic/sql_function/sql_function.sql
  • docs/scripts/basic/withPoints/CMakeLists.txt
  • docs/scripts/basic/withPoints/images.sql
  • docs/scripts/basic/withPoints/withPoints.sql
💤 Files with no reviewable changes (2)
  • docs/scripts/basic/sql_function/images.sql
  • docs/scripts/basic/withPoints/images.sql



Exercise 3: Route geometry (binary format)
Exercise 5: Get the azimuth

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Renumber the exercises to keep the chapter flow sequential.

Line 160 introduces Exercise 5, but Line 188 later introduces Exercise 4. That makes the walkthrough order confusing when readers follow the chapter step by step.

Also applies to: 188-188

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/basic/sql_function.rst` at line 160, The exercise numbering in the SQL
function chapter is out of sequence, with Exercise 5 appearing before Exercise
4, which breaks the walkthrough flow. Update the exercise labels in the affected
section so the numbers increase sequentially in chapter order, and verify the
related exercise headings around the azimuth and subsequent step use the
corrected numbering consistently.

Comment thread docs/basic/withPoints.rst
Comment on lines 35 to +38
* The function takes as input parameters:

- The edges table name
- Latitude/longitude of two point
- Latitude/longitude of two points

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Remove the stale edge-table input from the chapter contract.

Line 37 still says wrk_withPoints takes an edges table name, but the implementation now hardcodes the network and only accepts lat1/lon1/lat2/lon2/do_debug. This contradicts the parameter table below and will lead readers to call the function with the wrong arity.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/basic/withPoints.rst` around lines 35 - 38, The chapter contract for
wrk_withPoints is stale: it still describes an edges table name even though the
current implementation only accepts the point coordinates and do_debug. Update
the parameter list in withPoints.rst to match the actual wrk_withPoints
interface, removing the edges-table input and aligning the surrounding prose
with the accepted lat1/lon1/lat2/lon2/do_debug arguments so readers see the
correct arity.

Comment thread docs/basic/withPoints.rst

* Build the geometry of the points with the appropiate SRID. (lines **4** and
**10**)
* Build the geometry of the points with the appropiate SRID.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Fix the new user-facing typos.

appropiateappropriate, and that is been generated should be reworded (that is generated / that is being generated).

Also applies to: 262-262

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/basic/withPoints.rst` at line 104, Fix the user-facing typos in the
withPoints.rst documentation text: change “appropiate” to “appropriate” and
reword “that is been generated” to a correct phrase such as “that is generated”
or “that is being generated”. Apply the same wording cleanup to the repeated
occurrence noted in the same document, keeping the surrounding sentence meaning
unchanged.

Comment on lines 71 to +99
resuts_query := format(
$$SELECT seq, edge AS id, node, cost AS seconds
FROM pgr_withPoints(
'SELECT * FROM %1$I',
'%2$s',
'SELECT * FROM vehicle_penalized_net',
'%1$s',
-1, -2)
$$, edges_subset, closest_query);
$$, points_sql);

-- 2

additional_query := format(
$$SELECT
seq, id, seconds, name, length,
CASE
WHEN node = source THEN ST_AsText(geom)
ELSE ST_AsText(ST_Reverse(geom))
END AS readable,

CASE
WHEN node = source THEN geom
ELSE ST_Reverse(geom)
END AS geom
FROM results
LEFT JOIN %1$I USING (id)
ORDER BY seq
$$, edges_subset);
final_query := '
WITH
results AS (' || resuts_query || ' ),

additional AS (
SELECT
seq, id, seconds, name, length,
CASE
WHEN node = source THEN geom
ELSE ST_Reverse(geom)
END AS geom
FROM results
LEFT JOIN vehicle_net USING (id)
ORDER BY seq)

SELECT seq, id, seconds, name, length,
degrees(ST_azimuth(ST_StartPoint(geom), ST_EndPoint(geom))) AS azimuth,
ST_AsText(geom), geom
FROM additional ORDER BY seq;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Filter the terminal edge = -1 row before joining metadata.

pgr_withPoints appends a sentinel row with edge = -1. This code aliases it to id and left-joins vehicle_net, so the function returns an extra row with NULL name/length/geom. That also makes example include a blank “segment”.

Suggested fix
   resuts_query := format(
     $$SELECT seq, edge AS id, node, cost AS seconds
     FROM pgr_withPoints(
       'SELECT * FROM vehicle_penalized_net',
       '%1$s',
       -1, -2)
+    WHERE edge <> -1
     $$, points_sql);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
resuts_query := format(
$$SELECT seq, edge AS id, node, cost AS seconds
FROM pgr_withPoints(
'SELECT * FROM %1$I',
'%2$s',
'SELECT * FROM vehicle_penalized_net',
'%1$s',
-1, -2)
$$, edges_subset, closest_query);
$$, points_sql);
-- 2
additional_query := format(
$$SELECT
seq, id, seconds, name, length,
CASE
WHEN node = source THEN ST_AsText(geom)
ELSE ST_AsText(ST_Reverse(geom))
END AS readable,
CASE
WHEN node = source THEN geom
ELSE ST_Reverse(geom)
END AS geom
FROM results
LEFT JOIN %1$I USING (id)
ORDER BY seq
$$, edges_subset);
final_query := '
WITH
results AS (' || resuts_query || ' ),
additional AS (
SELECT
seq, id, seconds, name, length,
CASE
WHEN node = source THEN geom
ELSE ST_Reverse(geom)
END AS geom
FROM results
LEFT JOIN vehicle_net USING (id)
ORDER BY seq)
SELECT seq, id, seconds, name, length,
degrees(ST_azimuth(ST_StartPoint(geom), ST_EndPoint(geom))) AS azimuth,
ST_AsText(geom), geom
FROM additional ORDER BY seq;
resuts_query := format(
$$SELECT seq, edge AS id, node, cost AS seconds
FROM pgr_withPoints(
'SELECT * FROM vehicle_penalized_net',
'%1$s',
-1, -2)
WHERE edge <> -1
$$, points_sql);
-- 2
final_query := '
WITH
results AS (' || resuts_query || ' ),
additional AS (
SELECT
seq, id, seconds, name, length,
CASE
WHEN node = source THEN geom
ELSE ST_Reverse(geom)
END AS geom
FROM results
LEFT JOIN vehicle_net USING (id)
ORDER BY seq)
SELECT seq, id, seconds, name, length,
degrees(ST_azimuth(ST_StartPoint(geom), ST_EndPoint(geom))) AS azimuth,
ST_AsText(geom), geom
FROM additional ORDER BY seq;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@docs/scripts/basic/withPoints/withPoints.sql` around lines 71 - 99, The query
in withPoints.sql is including the terminal sentinel row from pgr_withPoints
because it aliases edge to id and joins it through results/additional, which
leaves an extra blank segment. Update the resuts_query/final_query flow to
filter out the row where edge = -1 before the LEFT JOIN to vehicle_net (or
before building additional), so only real path segments are returned and the
example no longer includes NULL metadata.

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