diff --git a/doc/dijkstra/pgr_dijkstraVia.rst b/doc/dijkstra/pgr_dijkstraVia.rst index c3cd3dea539..5787794a95f 100644 --- a/doc/dijkstra/pgr_dijkstraVia.rst +++ b/doc/dijkstra/pgr_dijkstraVia.rst @@ -129,6 +129,21 @@ Result columns :start-after: result_via_start :end-before: result_via_end +``pgr_dijkstraVia`` additionally returns: + +.. list-table:: + :width: 81 + :widths: 12 14 60 + :header-rows: 1 + + * - Column + - Type + - Description + * - ``prev`` + - ``BIGINT`` + - Identifier of the previous node in the path. For the first row of a + path segment, equals ``start_vid``. + Additional Examples ------------------------------------------------------------------------------- diff --git a/docqueries/dijkstra/dijkstraVia.result b/docqueries/dijkstra/dijkstraVia.result index 972045ec134..0691a202bf9 100644 --- a/docqueries/dijkstra/dijkstraVia.result +++ b/docqueries/dijkstra/dijkstraVia.result @@ -9,40 +9,40 @@ SET SELECT * FROM pgr_dijkstraVia( 'SELECT id, source, target, cost, reverse_cost FROM edges order by id', ARRAY[5, 1, 8]); - seq | path_id | path_seq | start_vid | end_vid | node | edge | cost | agg_cost | route_agg_cost ------+---------+----------+-----------+---------+------+------+------+----------+---------------- - 1 | 1 | 1 | 5 | 1 | 5 | 1 | 1 | 0 | 0 - 2 | 1 | 2 | 5 | 1 | 6 | 4 | 1 | 1 | 1 - 3 | 1 | 3 | 5 | 1 | 7 | 7 | 1 | 2 | 2 - 4 | 1 | 4 | 5 | 1 | 3 | 6 | 1 | 3 | 3 - 5 | 1 | 5 | 5 | 1 | 1 | -1 | 0 | 4 | 4 - 6 | 2 | 1 | 1 | 8 | 1 | 6 | 1 | 0 | 4 - 7 | 2 | 2 | 1 | 8 | 3 | 7 | 1 | 1 | 5 - 8 | 2 | 3 | 1 | 8 | 7 | 10 | 1 | 2 | 6 - 9 | 2 | 4 | 1 | 8 | 8 | -2 | 0 | 3 | 7 + seq | path_id | path_seq | start_vid | end_vid | prev | node | edge | cost | agg_cost | route_agg_cost +-----+---------+----------+-----------+---------+------+------+------+------+----------+---------------- + 1 | 1 | 1 | 5 | 1 | 5 | 5 | 1 | 1 | 0 | 0 + 2 | 1 | 2 | 5 | 1 | 5 | 6 | 4 | 1 | 1 | 1 + 3 | 1 | 3 | 5 | 1 | 6 | 7 | 7 | 1 | 2 | 2 + 4 | 1 | 4 | 5 | 1 | 7 | 3 | 6 | 1 | 3 | 3 + 5 | 1 | 5 | 5 | 1 | 3 | 1 | -1 | 0 | 4 | 4 + 6 | 2 | 1 | 1 | 8 | 1 | 1 | 6 | 1 | 0 | 4 + 7 | 2 | 2 | 1 | 8 | 1 | 3 | 7 | 1 | 1 | 5 + 8 | 2 | 3 | 1 | 8 | 3 | 7 | 10 | 1 | 2 | 6 + 9 | 2 | 4 | 1 | 8 | 7 | 8 | -2 | 0 | 3 | 7 (9 rows) /* -- q1 */ SELECT * FROM pgr_dijkstraVia( 'SELECT id, source, target, cost, reverse_cost FROM edges order by id', ARRAY[5, 7, 1, 8, 15]); - seq | path_id | path_seq | start_vid | end_vid | node | edge | cost | agg_cost | route_agg_cost ------+---------+----------+-----------+---------+------+------+------+----------+---------------- - 1 | 1 | 1 | 5 | 7 | 5 | 1 | 1 | 0 | 0 - 2 | 1 | 2 | 5 | 7 | 6 | 4 | 1 | 1 | 1 - 3 | 1 | 3 | 5 | 7 | 7 | -1 | 0 | 2 | 2 - 4 | 2 | 1 | 7 | 1 | 7 | 7 | 1 | 0 | 2 - 5 | 2 | 2 | 7 | 1 | 3 | 6 | 1 | 1 | 3 - 6 | 2 | 3 | 7 | 1 | 1 | -1 | 0 | 2 | 4 - 7 | 3 | 1 | 1 | 8 | 1 | 6 | 1 | 0 | 4 - 8 | 3 | 2 | 1 | 8 | 3 | 7 | 1 | 1 | 5 - 9 | 3 | 3 | 1 | 8 | 7 | 10 | 1 | 2 | 6 - 10 | 3 | 4 | 1 | 8 | 8 | -1 | 0 | 3 | 7 - 11 | 4 | 1 | 8 | 15 | 8 | 12 | 1 | 0 | 7 - 12 | 4 | 2 | 8 | 15 | 12 | 13 | 1 | 1 | 8 - 13 | 4 | 3 | 8 | 15 | 17 | 15 | 1 | 2 | 9 - 14 | 4 | 4 | 8 | 15 | 16 | 16 | 1 | 3 | 10 - 15 | 4 | 5 | 8 | 15 | 15 | -2 | 0 | 4 | 11 + seq | path_id | path_seq | start_vid | end_vid | prev | node | edge | cost | agg_cost | route_agg_cost +-----+---------+----------+-----------+---------+------+------+------+------+----------+---------------- + 1 | 1 | 1 | 5 | 7 | 5 | 5 | 1 | 1 | 0 | 0 + 2 | 1 | 2 | 5 | 7 | 5 | 6 | 4 | 1 | 1 | 1 + 3 | 1 | 3 | 5 | 7 | 6 | 7 | -1 | 0 | 2 | 2 + 4 | 2 | 1 | 7 | 1 | 7 | 7 | 7 | 1 | 0 | 2 + 5 | 2 | 2 | 7 | 1 | 7 | 3 | 6 | 1 | 1 | 3 + 6 | 2 | 3 | 7 | 1 | 3 | 1 | -1 | 0 | 2 | 4 + 7 | 3 | 1 | 1 | 8 | 1 | 1 | 6 | 1 | 0 | 4 + 8 | 3 | 2 | 1 | 8 | 1 | 3 | 7 | 1 | 1 | 5 + 9 | 3 | 3 | 1 | 8 | 3 | 7 | 10 | 1 | 2 | 6 + 10 | 3 | 4 | 1 | 8 | 7 | 8 | -1 | 0 | 3 | 7 + 11 | 4 | 1 | 8 | 15 | 8 | 8 | 12 | 1 | 0 | 7 + 12 | 4 | 2 | 8 | 15 | 8 | 12 | 13 | 1 | 1 | 8 + 13 | 4 | 3 | 8 | 15 | 12 | 17 | 15 | 1 | 2 | 9 + 14 | 4 | 4 | 8 | 15 | 17 | 16 | 16 | 1 | 3 | 10 + 15 | 4 | 5 | 8 | 15 | 16 | 15 | -2 | 0 | 4 | 11 (15 rows) /* -- q2 */ diff --git a/include/c_types/routes_t.h b/include/c_types/routes_t.h index 452ba73e5ce..ec579ef61e5 100644 --- a/include/c_types/routes_t.h +++ b/include/c_types/routes_t.h @@ -43,6 +43,8 @@ struct Routes_t{ int path_seq; int64_t start_vid; int64_t end_vid; + /* Currently only populated meaningfully for pgr_dijkstraVia (see issue #3111) */ + int64_t prev; int64_t node; int64_t edge; double cost; diff --git a/sql/dijkstra/_dijkstraVia.sql b/sql/dijkstra/_dijkstraVia.sql index 8a81b8ea44a..58d861f8ff0 100644 --- a/sql/dijkstra/_dijkstraVia.sql +++ b/sql/dijkstra/_dijkstraVia.sql @@ -29,7 +29,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -- pgr_dijkstraVia ------------------ ---v3.0 +--v4.1 CREATE FUNCTION _pgr_dijkstraVia( edges_sql TEXT, via_vids ANYARRAY, @@ -43,6 +43,7 @@ CREATE FUNCTION _pgr_dijkstraVia( OUT path_seq INTEGER, OUT start_vid BIGINT, OUT end_vid BIGINT, + OUT prev BIGINT, OUT node BIGINT, OUT edge BIGINT, OUT cost FLOAT, diff --git a/sql/dijkstra/dijkstraVia.sql b/sql/dijkstra/dijkstraVia.sql index e495592afe6..170010ca160 100644 --- a/sql/dijkstra/dijkstraVia.sql +++ b/sql/dijkstra/dijkstraVia.sql @@ -25,7 +25,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ ---v3.0 +--v4.1 CREATE FUNCTION pgr_dijkstraVia( TEXT, -- edges_sql (required) ANYARRAY, -- via_vids (required) @@ -40,6 +40,7 @@ CREATE FUNCTION pgr_dijkstraVia( OUT path_seq INTEGER, OUT start_vid BIGINT, OUT end_vid BIGINT, + OUT prev BIGINT, OUT node BIGINT, OUT edge BIGINT, OUT cost FLOAT, @@ -47,7 +48,7 @@ CREATE FUNCTION pgr_dijkstraVia( OUT route_agg_cost FLOAT) RETURNS SETOF RECORD AS $BODY$ - SELECT seq, path_id, path_seq, start_vid, end_vid, node, edge, cost, agg_cost, route_agg_cost + SELECT seq, path_id, path_seq, start_vid, end_vid, prev, node, edge, cost, agg_cost, route_agg_cost FROM _pgr_dijkstraVia(_pgr_get_statement($1), $2, $3 , $4, $5); $BODY$ LANGUAGE SQL VOLATILE STRICT diff --git a/sql/scripts/build-extension-update-files.pl b/sql/scripts/build-extension-update-files.pl index f71d06e57c4..8b8243a817d 100755 --- a/sql/scripts/build-extension-update-files.pl +++ b/sql/scripts/build-extension-update-files.pl @@ -330,6 +330,12 @@ sub generate_upgrade_script { push @commands, drop_special_case_function("pgr_bddijkstra(text,anyarray,bigint,boolean)"); push @commands, drop_special_case_function("pgr_bddijkstra(text,bigint,anyarray,boolean)"); } + + # updating to 4.1+ (OUT parameters changed on pgr_dijkstraVia) + if ($old_minor < 4.1) { + push @commands, drop_special_case_function("_pgr_dijkstravia(text,anyarray,boolean,boolean,boolean)"); + push @commands, drop_special_case_function("pgr_dijkstravia(text,anyarray,boolean,boolean,boolean)"); + } } if ($old_mayor == 2) { diff --git a/src/cpp_common/to_postgres.cpp b/src/cpp_common/to_postgres.cpp index a8bf58998a1..028c32f6085 100644 --- a/src/cpp_common/to_postgres.cpp +++ b/src/cpp_common/to_postgres.cpp @@ -58,6 +58,7 @@ get_path( double &route_agg_cost, size_t &sequence) { int path_seq = 0; + int64_t prev_node = path.start_id(); for (const auto e : path) { (*tuples)[sequence] = { route_id, @@ -65,12 +66,14 @@ get_path( path_seq, path.start_id(), path.end_id(), + prev_node, e.node, e.edge, e.cost, e.agg_cost, route_agg_cost}; route_agg_cost += path[static_cast(path_seq)].cost; + prev_node = e.node; path_seq++; ++sequence; } diff --git a/src/dijkstra/dijkstraVia.c b/src/dijkstra/dijkstraVia.c index a6e74e509b1..bee3afbc35e 100644 --- a/src/dijkstra/dijkstraVia.c +++ b/src/dijkstra/dijkstraVia.c @@ -126,7 +126,7 @@ _pgr_dijkstravia(PG_FUNCTION_ARGS) { bool* nulls; size_t call_cntr = funcctx->call_cntr; - size_t numb_out = 10; + size_t numb_out = 11; values = palloc(numb_out * sizeof(Datum)); nulls = palloc(numb_out * sizeof(bool)); size_t i; @@ -139,11 +139,12 @@ _pgr_dijkstravia(PG_FUNCTION_ARGS) { values[2] = Int32GetDatum(result_tuples[call_cntr].path_seq + 1); values[3] = Int64GetDatum(result_tuples[call_cntr].start_vid); values[4] = Int64GetDatum(result_tuples[call_cntr].end_vid); - values[5] = Int64GetDatum(result_tuples[call_cntr].node); - values[6] = Int64GetDatum(result_tuples[call_cntr].edge); - values[7] = Float8GetDatum(result_tuples[call_cntr].cost); - values[8] = Float8GetDatum(result_tuples[call_cntr].agg_cost); - values[9] = Float8GetDatum(result_tuples[call_cntr].route_agg_cost); + values[5] = Int64GetDatum(result_tuples[call_cntr].prev); + values[6] = Int64GetDatum(result_tuples[call_cntr].node); + values[7] = Int64GetDatum(result_tuples[call_cntr].edge); + values[8] = Float8GetDatum(result_tuples[call_cntr].cost); + values[9] = Float8GetDatum(result_tuples[call_cntr].agg_cost); + values[10] = Float8GetDatum(result_tuples[call_cntr].route_agg_cost); tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/tools/testers/types_check.pg b/tools/testers/types_check.pg index 100f47232b1..128939c69f1 100644 --- a/tools/testers/types_check.pg +++ b/tools/testers/types_check.pg @@ -415,6 +415,11 @@ DECLARE typs TEXT; BEGIN + IF fn ILIKE '%dijkstravia%' THEN + return_params_names := ARRAY['seq','path_id','path_seq','start_vid','end_vid','prev','node','edge','cost','agg_cost','route_agg_cost']; + return_params_types := '{int4,int4,int4,int8,int8,int8,int8,int8,float8,float8,float8}'::TEXT[]; + END IF; + IF fn ilike '%withpoints%' AND NOT min_version('4.0.0') THEN RETURN QUERY SELECT skip(1, fn || ': New signatures on 4.0.0'); RETURN; diff --git a/tools/testers/via_compare.pg b/tools/testers/via_compare.pg index 38d0a8e5028..783a878c8bb 100644 --- a/tools/testers/via_compare.pg +++ b/tools/testers/via_compare.pg @@ -45,7 +45,7 @@ BEGIN all_found := (l=1); allow_u := (m=1); - dijkstraVia_sql := 'SELECT * FROM pgr_dijkstraVia( ' + dijkstraVia_sql := 'SELECT seq, path_id, path_seq, start_vid, end_vid, node, edge, cost, agg_cost, route_agg_cost FROM pgr_dijkstraVia( ' || with_reverse_cost || ', ARRAY[1, ' || i || ', ' || j || '], ' || flag || ',' || all_found || ',' || allow_u || ')'; @@ -57,7 +57,7 @@ BEGIN msg := '-1- ' || directed || ', with reverse_cost: from 1 to ' || i || ' to ' || j || msg_end; RETURN query SELECT set_eq(trsp_sql, dijkstraVia_sql, msg); - dijkstraVia_sql := 'SELECT * FROM pgr_dijkstraVia( ' + dijkstraVia_sql := 'SELECT seq, path_id, path_seq, start_vid, end_vid, node, edge, cost, agg_cost, route_agg_cost FROM pgr_dijkstraVia( ' || no_reverse_cost || ', ARRAY[1, ' || i || ', ' || j || '], ' || flag || ',' || all_found || ',' || allow_u || ')'; @@ -124,7 +124,7 @@ BEGIN all_found := (l=1); allow_u := (m=1); - dijkstraVia_sql := 'SELECT * FROM pgr_dijkstraVia( ' + dijkstraVia_sql := 'SELECT seq, path_id, path_seq, start_vid, end_vid, node, edge, cost, agg_cost, route_agg_cost FROM pgr_dijkstraVia( ' || with_reverse_cost || ', ARRAY[1, ' || i || ', ' || j || '], ' || flag || ',' || all_found || ',' || allow_u || ')'; @@ -136,7 +136,7 @@ BEGIN msg := '-1- ' || directed || ', with reverse_cost: from 1 to ' || i || ' to ' || j || msg_end || withPoints_sql; RETURN query SELECT set_eq(withPoints_sql, dijkstraVia_sql, msg); - dijkstraVia_sql := 'SELECT * FROM pgr_dijkstraVia( ' + dijkstraVia_sql := 'SELECT seq, path_id, path_seq, start_vid, end_vid, node, edge, cost, agg_cost, route_agg_cost FROM pgr_dijkstraVia( ' || no_reverse_cost || ', ARRAY[1, ' || i || ', ' || j || '], ' || flag || ',' || all_found || ',' || allow_u || ')';