From 30603cd0c9e818a9e8a84425e740b11f5318ba29 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Mon, 18 May 2020 20:10:07 +0200 Subject: [PATCH 01/33] adding the new signature --- sql/dijkstra/_dijkstra.sql | 23 ++++++++++++++++++++++ sql/dijkstra/dijkstra.sql | 39 +++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/sql/dijkstra/_dijkstra.sql b/sql/dijkstra/_dijkstra.sql index 01bbc27be54..9215e64e17d 100644 --- a/sql/dijkstra/_dijkstra.sql +++ b/sql/dijkstra/_dijkstra.sql @@ -51,7 +51,30 @@ RETURNS SETOF RECORD AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE STRICT; + +CREATE OR REPLACE FUNCTION _pgr_dijkstra( + edges_sql TEXT, + combinations_sql TEXT, + directed BOOLEAN DEFAULT true, + only_cost BOOLEAN DEFAULT false, + normal BOOLEAN DEFAULT true, + + OUT seq INTEGER, + OUT path_seq INTEGER, + OUT start_vid BIGINT, + OUT end_vid BIGINT, + OUT node BIGINT, + OUT edge BIGINT, + OUT cost FLOAT, + OUT agg_cost FLOAT) +RETURNS SETOF RECORD AS +'MODULE_PATHNAME' +LANGUAGE C VOLATILE STRICT; + -- COMMENTS COMMENT ON FUNCTION _pgr_dijkstra(TEXT, ANYARRAY, ANYARRAY, BOOLEAN, BOOLEAN, BOOLEAN, BIGINT) IS 'pgRouting internal function'; + +COMMENT ON FUNCTION _pgr_dijkstra(TEXT, TEXT, BOOLEAN, BOOLEAN, BOOLEAN) +IS 'pgRouting internal function'; diff --git a/sql/dijkstra/dijkstra.sql b/sql/dijkstra/dijkstra.sql index c15eeb6c551..3b15ed8246c 100644 --- a/sql/dijkstra/dijkstra.sql +++ b/sql/dijkstra/dijkstra.sql @@ -51,7 +51,6 @@ LANGUAGE sql VOLATILE STRICT COST 100 ROWS 1000; - -- ONE to MANY CREATE OR REPLACE FUNCTION pgr_dijkstra( TEXT, -- edges_sql (required) @@ -76,7 +75,6 @@ LANGUAGE sql VOLATILE STRICT COST 100 ROWS 1000; - -- MANY to ONE CREATE OR REPLACE FUNCTION pgr_dijkstra( TEXT, -- edges_sql (required) @@ -101,7 +99,6 @@ LANGUAGE sql VOLATILE STRICT COST 100 ROWS 1000; - -- MANY to MANY CREATE OR REPLACE FUNCTION pgr_dijkstra( TEXT, -- edges_sql (required) @@ -127,6 +124,31 @@ LANGUAGE sql VOLATILE STRICT COST 100 ROWS 1000; +-- Combinations SQL signature +CREATE OR REPLACE FUNCTION pgr_dijkstra( + TEXT, -- edges_sql (required) + TEXT, -- combinations_sql (required) + + directed BOOLEAN DEFAULT true, + + OUT seq INTEGER, + OUT path_seq INTEGER, + OUT start_vid BIGINT, + OUT end_vid BIGINT, + OUT node BIGINT, + OUT edge BIGINT, + OUT cost FLOAT, + OUT agg_cost FLOAT) +RETURNS SETOF RECORD AS +$BODY$ + SELECT a.seq, a.path_seq, a.start_vid, a.end_vid, a.node, a.edge, a.cost, a.agg_cost + FROM _pgr_dijkstra(_pgr_get_statement($1), _pgr_get_statement($2), $3, false, true) AS a; +$BODY$ +LANGUAGE sql VOLATILE STRICT +COST 100 +ROWS 1000; + + -- COMMENTS COMMENT ON FUNCTION pgr_dijkstra(TEXT, BIGINT, BIGINT, BOOLEAN) @@ -176,3 +198,14 @@ IS 'pgr_dijkstra(Many to Many) - Documentation: - ${PGROUTING_DOC_LINK}/pgr_dijkstra.html '; + +COMMENT ON FUNCTION pgr_dijkstra(TEXT, TEXT, BOOLEAN) +IS 'pgr_dijkstra(One to One) +- Parameters: + - Edges SQL with columns: id, source, target, cost [,reverse_cost] + - Combinations SQL with columns: source, target +- Optional Parameters + - directed := true +- Documentation: + - ${PGROUTING_DOC_LINK}/pgr_dijkstra.html +'; From a9fe2b0dd95c2d75b38b2c8d3c29de583d3bb0de Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Mon, 18 May 2020 23:30:44 +0200 Subject: [PATCH 02/33] adding cobinations sql sugnature --- src/dijkstra/dijkstra.c | 161 ++++++++++++++++++++++++++++++++++------ 1 file changed, 139 insertions(+), 22 deletions(-) diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index a18bc515803..7e839433543 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -39,6 +39,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/time_msg.h" #include "c_common/edges_input.h" #include "c_common/arrays_input.h" +#include "c_common/combinations_input.h" #include "drivers/dijkstra/dijkstra_driver.h" PG_MODULE_MAGIC; @@ -135,6 +136,102 @@ process( pgr_SPI_finish(); } + + +static +void +process_combinations( + char* edges_sql, + char* combinations_sql, + bool directed, + bool only_cost, + bool normal, + General_path_element_t **result_tuples, + size_t *result_count) { + pgr_SPI_connect(); + + int64_t* start_vidsArr = NULL; + size_t size_start_vidsArr = 0; + + int64_t* end_vidsArr = NULL; + size_t size_end_vidsArr = 0; + + pgr_edge_t *edges = NULL; + size_t total_edges = 0; + + pgr_combination_t *combinations = NULL; + size_t total_combinations = 0; + + if (normal) { + pgr_get_edges(edges_sql, &edges, &total_edges); + } else { + pgr_get_edges_reversed(edges_sql, &edges, &total_edges); + } + //end_vidsArr = (int64_t*) + // pgr_get_bigIntArray(&size_end_vidsArr, starts); + //start_vidsArr = (int64_t*) + // pgr_get_bigIntArray(&size_start_vidsArr, ends); + + + if (total_edges == 0) { + if (end_vidsArr) pfree(end_vidsArr); + if (start_vidsArr) pfree(start_vidsArr); + pgr_SPI_finish(); + return; + } else { + pgr_get_combinations(combinations_sql, &combinations, &total_combinations); + + } + + PGR_DBG("Starting timer"); + clock_t start_t = clock(); + char* log_msg = NULL; + char* notice_msg = NULL; + char* err_msg = NULL; + do_pgr_many_to_many_dijkstra( + edges, total_edges, + start_vidsArr, size_start_vidsArr, + end_vidsArr, size_end_vidsArr, + + directed, + only_cost, + normal, + 3, + + result_tuples, + result_count, + + &log_msg, + ¬ice_msg, + &err_msg); + + if (only_cost) { + time_msg("processing pgr_dijkstraCost", start_t, clock()); + } else { + time_msg("processing pgr_dijkstra", start_t, clock()); + } + + + if (err_msg && (*result_tuples)) { + pfree(*result_tuples); + (*result_tuples) = NULL; + (*result_count) = 0; + } + + pgr_global_report(log_msg, notice_msg, err_msg); + + if (log_msg) pfree(log_msg); + if (notice_msg) pfree(notice_msg); + if (err_msg) pfree(err_msg); + if (edges) pfree(edges); + if (start_vidsArr) pfree(start_vidsArr); + if (end_vidsArr) pfree(end_vidsArr); + pgr_SPI_finish(); +} + + + + PGDLLEXPORT Datum _pgr_dijkstra(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; @@ -150,29 +247,49 @@ _pgr_dijkstra(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); + if(PG_NARGS() == 7) { + /**********************************************************************/ + // pgr_dijkstra( + // sql TEXT, + // start_vids ANYARRAY, + // end_vids ANYARRAY, + // directed BOOLEAN default true, + // only_cost BOOLEAN default false + // normal BOOLEAN default true + + process( + text_to_cstring(PG_GETARG_TEXT_P(0)), + PG_GETARG_ARRAYTYPE_P(1), + PG_GETARG_ARRAYTYPE_P(2), + PG_GETARG_BOOL(3), + PG_GETARG_BOOL(4), + PG_GETARG_BOOL(5), + PG_GETARG_INT64(6), + &result_tuples, + &result_count); + + /**********************************************************************/ + } + else if(PG_NARGS() == 5){ + /**********************************************************************/ + // pgr_dijkstra( + // edge_sql TEXT, + // combinations_sql TEXT, + // directed BOOLEAN default true, + // only_cost BOOLEAN default false + + process_combinations( + text_to_cstring(PG_GETARG_TEXT_P(0)), + text_to_cstring(PG_GETARG_TEXT_P(1)), + PG_GETARG_BOOL(2), + PG_GETARG_BOOL(3), + PG_GETARG_BOOL(4), + &result_tuples, + &result_count); + + /**********************************************************************/ - /**********************************************************************/ - // pgr_dijkstra( - // sql TEXT, - // start_vids ANYARRAY, - // end_vids ANYARRAY, - // directed BOOLEAN default true, - // only_cost BOOLEAN default false - // normal BOOLEAN default true - - process( - text_to_cstring(PG_GETARG_TEXT_P(0)), - PG_GETARG_ARRAYTYPE_P(1), - PG_GETARG_ARRAYTYPE_P(2), - PG_GETARG_BOOL(3), - PG_GETARG_BOOL(4), - PG_GETARG_BOOL(5), - PG_GETARG_INT64(6), - &result_tuples, - &result_count); - - /**********************************************************************/ - + } #if PGSQL_VERSION > 95 funcctx->max_calls = result_count; #else From bc29a964f26937eb1055530bd2596207e9d083ae Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 19 May 2020 03:37:20 +0200 Subject: [PATCH 03/33] pgr_dijkstra combinations_qry signature, first running solution --- include/c_common/combinations_input.h | 50 ++++++++ include/c_types/pgr_combination_t.h | 42 +++++++ include/c_types/pgr_edge_t.h | 2 +- src/common/combinations_input.c | 174 ++++++++++++++++++++++++++ 4 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 include/c_common/combinations_input.h create mode 100644 include/c_types/pgr_combination_t.h create mode 100644 src/common/combinations_input.c diff --git a/include/c_common/combinations_input.h b/include/c_common/combinations_input.h new file mode 100644 index 00000000000..4d68952cf18 --- /dev/null +++ b/include/c_common/combinations_input.h @@ -0,0 +1,50 @@ +/*PGR-GNU***************************************************************** +File: combinations_input.h + +Copyright (c) 2015 Celia Virginia Vergara Castillo +vicky_vergara@hotmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ +#ifndef INCLUDE_C_COMMON_COMBINATIONS_INPUT_H_ +#define INCLUDE_C_COMMON_COMBINATIONS_INPUT_H_ +#pragma once + +/* for size-t */ +#include +#include "c_types/pgr_combination_t.h" + + +/*! @brief combinations_sql + +~~~~{.c} +SELECT source, target +FROM combinations_table; +~~~~ + + +@param[in] combinations_sql +@param[out] combinations +@param[out] combinations_edges +*/ +void pgr_get_combinations( + char *combinations_sql, + pgr_combination_t **combinations, + size_t *total_combinations); + +#endif // INCLUDE_C_COMMON_COMBINATIONS_INPUT_H_ diff --git a/include/c_types/pgr_combination_t.h b/include/c_types/pgr_combination_t.h new file mode 100644 index 00000000000..a941c898af8 --- /dev/null +++ b/include/c_types/pgr_combination_t.h @@ -0,0 +1,42 @@ +/*PGR-GNU***************************************************************** +File: pgr_combination_t.h + +Copyright (c) 2017 Celia Virginia Vergara Castillo +Mail: vicky_vergara@hotmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ +/*! @file */ + +#ifndef INCLUDE_C_TYPES_PGR_COMBINATION_T_H_ +#define INCLUDE_C_TYPES_PGR_COMBINATION_T_H_ +#pragma once + +/* for int64_t */ +#ifdef __cplusplus +# include +#else +# include +#endif + +typedef struct { + int64_t source; + int64_t target; +} pgr_combination_t; + +#endif // INCLUDE_C_TYPES_PGR_COMBINATION_T_H_ diff --git a/include/c_types/pgr_edge_t.h b/include/c_types/pgr_edge_t.h index 9acce633756..b8bebc83281 100644 --- a/include/c_types/pgr_edge_t.h +++ b/include/c_types/pgr_edge_t.h @@ -42,4 +42,4 @@ typedef struct { double reverse_cost; } pgr_edge_t; -#endif // INCLUDE_C_TYPES_PGR_EDGE_T_H_ +#endif // INCLUDE_C_TYPES_PGR_COMBINATION_T_H_ diff --git a/src/common/combinations_input.c b/src/common/combinations_input.c new file mode 100644 index 00000000000..33d56d2eecc --- /dev/null +++ b/src/common/combinations_input.c @@ -0,0 +1,174 @@ +/*PGR-GNU***************************************************************** +File: combinations_input.c + +Copyright (c) 2015 Celia Virginia Vergara Castillo +vicky_vergara@hotmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#include "c_common/edges_input.h" + +#include +#include +#include +/* for size-t */ +#ifdef __cplusplus +# include +#else +# include +#endif + +#include "c_types/column_info_t.h" + +#include "c_common/debug_macro.h" +#include "c_common/get_check_data.h" +#include "c_common/time_msg.h" + +static +void fetch_basic_edge( + HeapTuple *tuple, + TupleDesc *tupdesc, + Column_info_t info[5], + int64_t *default_id, + pgr_basic_edge_t *edge, + size_t *valid_edges) { + if (column_found(info[0].colNumber)) { + edge->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]); + } else { + edge->id = *default_id; + ++(*default_id); + } + + edge->source = pgr_SPI_getBigInt(tuple, tupdesc, info[1]); + edge->target = pgr_SPI_getBigInt(tuple, tupdesc, info[2]); + edge->going = pgr_SPI_getFloat8(tuple, tupdesc, info[3]) > 0 ? + true : false; + edge->coming = (column_found(info[4].colNumber) + && pgr_SPI_getFloat8(tuple, tupdesc, info[4]) > 0) ? + true : false; + + (*valid_edges)++; +} + +static +void fetch_edge( + HeapTuple *tuple, + TupleDesc *tupdesc, + Column_info_t info[2], + pgr_combination_t *combination, + size_t *valid_combinations) { + combination->source = pgr_SPI_getBigInt(tuple, tupdesc, info[0]); + combination->target = pgr_SPI_getBigInt(tuple, tupdesc, info[1]); + + *valid_edges = *valid_edges + 1; +} + + + +static +void +get_combinations_2_columns( + char *sql, + pgr_combinations_t **combinations, + size_t *totalTuples) { + clock_t start_t = clock(); + + const int tuple_limit = 1000000; + + size_t total_tuples; + size_t valid_edges; + + Column_info_t info[2]; + + int i; + for (i = 0; i < 2; ++i) { + info[i].colNumber = -1; + info[i].type = 0; + info[i].strict = true; + info[i].eType = ANY_INTEGER; + } + info[0].name = "source"; + info[1].name = "target"; + + void *SPIplan; + SPIplan = pgr_SPI_prepare(sql); + + Portal SPIportal; + SPIportal = pgr_SPI_cursor_open(SPIplan); + + + bool moredata = true; + (*totalTuples) = total_tuples = valid_combinations = 0; + + + int64_t default_id = 0; + while (moredata == true) { + SPI_cursor_fetch(SPIportal, true, tuple_limit); + if (total_tuples == 0) + pgr_fetch_column_info(info, 2); + + size_t ntuples = SPI_processed; + total_tuples += ntuples; + + if (ntuples > 0) { + if ((*combinations) == NULL) + (*combinations) = (pgr_combination_t *) + palloc0(total_tuples * sizeof(pgr_combination_t)); + else + (*combinations) = (pgr_combination_t *) + repalloc((*combinations), total_tuples * sizeof(pgr_combination_t)); + + if ((*combinations) == NULL) { + elog(ERROR, "Out of memory"); + } + + size_t t; + SPITupleTable *tuptable = SPI_tuptable; + TupleDesc tupdesc = SPI_tuptable->tupdesc; + for (t = 0; t < ntuples; t++) { + HeapTuple tuple = tuptable->vals[t]; + fetch_combination(&tuple, &tupdesc, info, + &(*combinations)[total_tuples - ntuples + t], + &valid_combinations); + } + SPI_freetuptable(tuptable); + } else { + moredata = false; + } + } + + SPI_cursor_close(SPIportal); + + if (total_tuples == 0 || valid_combinations == 0) { + PGR_DBG("No combinations found"); + } + + (*totalTuples) = total_tuples; + PGR_DBG("Reading %ld combinations", total_tuples); + time_msg("reading combinations", start_t, clock()); +} + +/* select source, target */ +void +pgr_get_combinations( + char *combinations_sql, + pgr_combination_t **combinations, + size_t *total_combinations) { + get_combinations_2_columns(combinations_sql, combinations, total_combinations); +} From 40d7e68270ef0e17f882084c519e6d8e1f2d6897 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 19 May 2020 03:39:20 +0200 Subject: [PATCH 04/33] pgr_dijkstra combinations_qry signature, first running solution --- include/c_types/pgr_combination_t.h | 1 + include/dijkstra/pgr_dijkstra.hpp | 42 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/c_types/pgr_combination_t.h b/include/c_types/pgr_combination_t.h index a941c898af8..09446b73e47 100644 --- a/include/c_types/pgr_combination_t.h +++ b/include/c_types/pgr_combination_t.h @@ -39,4 +39,5 @@ typedef struct { int64_t target; } pgr_combination_t; + #endif // INCLUDE_C_TYPES_PGR_COMBINATION_T_H_ diff --git a/include/dijkstra/pgr_dijkstra.hpp b/include/dijkstra/pgr_dijkstra.hpp index 4343713cefb..fa5acc581cc 100644 --- a/include/dijkstra/pgr_dijkstra.hpp +++ b/include/dijkstra/pgr_dijkstra.hpp @@ -30,6 +30,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "c_types/pgr_combination_t.h" #if BOOST_VERSION_OK #include @@ -299,6 +300,47 @@ class Pgr_dijkstra { return paths; } + // preparation for parallel arrays + std::deque dijkstra( + G &graph, + const std::vector< pgr_combination_t > &combinations, + bool only_cost, + size_t n_goals = std::numeric_limits::max()) { + // a call to 1 to many is faster for each of the sources + std::deque paths; + + // group targets per distinct source + std::map > vertex_map; + for (const pgr_combination_t &comb : combinations){ + std::map< int64_t , std::vector >::iterator it= vertex_map.find(comb.source); + if (it != vertex_map.end()) { + it->second.push_back(comb.target); + } else { + std::vector targets{comb.target}; + vertex_map[comb.source] = targets; + } + } + + for (const auto &start_ends : vertex_map) { + auto r_paths = dijkstra( + graph, + start_ends.first, start_ends.second, + only_cost, n_goals); + paths.insert(paths.begin(), r_paths.begin(), r_paths.end()); + } + + std::sort(paths.begin(), paths.end(), + [](const Path &e1, const Path &e2)->bool { + return e1.end_id() < e2.end_id(); + }); + std::stable_sort(paths.begin(), paths.end(), + [](const Path &e1, const Path &e2)->bool { + return e1.start_id() < e2.start_id(); + }); + + return paths; + } + //@} private: From 7ffeca8000e6a9e2602a2108edcad5e09e6b07ae Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 19 May 2020 03:40:19 +0200 Subject: [PATCH 05/33] pgr_dijkstra combinations_qry signature, first running solution --- include/drivers/dijkstra/dijkstra_driver.h | 21 ++++ src/common/combinations_input.c | 39 +----- src/dijkstra/dijkstra.c | 31 ++--- src/dijkstra/dijkstra_driver.cpp | 134 +++++++++++++++++++++ 4 files changed, 170 insertions(+), 55 deletions(-) diff --git a/include/drivers/dijkstra/dijkstra_driver.h b/include/drivers/dijkstra/dijkstra_driver.h index b1ea059e0d5..cd1768a0082 100644 --- a/include/drivers/dijkstra/dijkstra_driver.h +++ b/include/drivers/dijkstra/dijkstra_driver.h @@ -39,6 +39,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #endif #include "c_types/pgr_edge_t.h" +#include "c_types/pgr_combination_t.h" #include "c_types/general_path_element_t.h" @@ -70,6 +71,26 @@ extern "C" { char** notice_msg, char** err_msg); + + // CREATE OR REPLACE FUNCTION pgr_dijkstra( + // sql text, + // combinations_sql text, + // directed boolean default true, + void do_pgr_parallel_dijkstra( + pgr_edge_t *data_edges, + size_t total_tuples, + pgr_combination_t *combinations, + size_t total_combinations, + bool directed, + bool only_cost, + bool normal, + + General_path_element_t **return_tuples, + size_t *return_count, + + char** log_msg, + char** notice_msg, + char** err_msg); #ifdef __cplusplus } #endif diff --git a/src/common/combinations_input.c b/src/common/combinations_input.c index 33d56d2eecc..093ac0fb243 100644 --- a/src/common/combinations_input.c +++ b/src/common/combinations_input.c @@ -22,7 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#include "c_common/edges_input.h" +#include "c_common/combinations_input.h" #include #include @@ -35,39 +35,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #endif #include "c_types/column_info_t.h" - +#include "c_types/pgr_combination_t.h" #include "c_common/debug_macro.h" #include "c_common/get_check_data.h" #include "c_common/time_msg.h" -static -void fetch_basic_edge( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info[5], - int64_t *default_id, - pgr_basic_edge_t *edge, - size_t *valid_edges) { - if (column_found(info[0].colNumber)) { - edge->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]); - } else { - edge->id = *default_id; - ++(*default_id); - } - - edge->source = pgr_SPI_getBigInt(tuple, tupdesc, info[1]); - edge->target = pgr_SPI_getBigInt(tuple, tupdesc, info[2]); - edge->going = pgr_SPI_getFloat8(tuple, tupdesc, info[3]) > 0 ? - true : false; - edge->coming = (column_found(info[4].colNumber) - && pgr_SPI_getFloat8(tuple, tupdesc, info[4]) > 0) ? - true : false; - - (*valid_edges)++; -} static -void fetch_edge( +void fetch_combination( HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[2], @@ -76,7 +51,7 @@ void fetch_edge( combination->source = pgr_SPI_getBigInt(tuple, tupdesc, info[0]); combination->target = pgr_SPI_getBigInt(tuple, tupdesc, info[1]); - *valid_edges = *valid_edges + 1; + *valid_combinations = *valid_combinations + 1; } @@ -85,14 +60,14 @@ static void get_combinations_2_columns( char *sql, - pgr_combinations_t **combinations, + pgr_combination_t **combinations, size_t *totalTuples) { clock_t start_t = clock(); const int tuple_limit = 1000000; size_t total_tuples; - size_t valid_edges; + size_t valid_combinations; Column_info_t info[2]; @@ -116,8 +91,6 @@ get_combinations_2_columns( bool moredata = true; (*totalTuples) = total_tuples = valid_combinations = 0; - - int64_t default_id = 0; while (moredata == true) { SPI_cursor_fetch(SPIportal, true, tuple_limit); if (total_tuples == 0) diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index 7e839433543..cd6717594a3 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -150,12 +150,6 @@ process_combinations( size_t *result_count) { pgr_SPI_connect(); - int64_t* start_vidsArr = NULL; - size_t size_start_vidsArr = 0; - - int64_t* end_vidsArr = NULL; - size_t size_end_vidsArr = 0; - pgr_edge_t *edges = NULL; size_t total_edges = 0; @@ -167,20 +161,17 @@ process_combinations( } else { pgr_get_edges_reversed(edges_sql, &edges, &total_edges); } - //end_vidsArr = (int64_t*) - // pgr_get_bigIntArray(&size_end_vidsArr, starts); - //start_vidsArr = (int64_t*) - // pgr_get_bigIntArray(&size_start_vidsArr, ends); - - if (total_edges == 0) { - if (end_vidsArr) pfree(end_vidsArr); - if (start_vidsArr) pfree(start_vidsArr); + if (total_edges == 0 ) { pgr_SPI_finish(); return; } else { pgr_get_combinations(combinations_sql, &combinations, &total_combinations); - + if(total_combinations == 0){ + if (edges) pfree(edges); + pgr_SPI_finish(); + return; + } } PGR_DBG("Starting timer"); @@ -188,15 +179,12 @@ process_combinations( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - do_pgr_many_to_many_dijkstra( + do_pgr_parallel_dijkstra( edges, total_edges, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, - + combinations, total_combinations, directed, only_cost, normal, - 3, result_tuples, result_count, @@ -224,8 +212,7 @@ process_combinations( if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); if (edges) pfree(edges); - if (start_vidsArr) pfree(start_vidsArr); - if (end_vidsArr) pfree(end_vidsArr); + if (combinations) pfree(combinations); pgr_SPI_finish(); } diff --git a/src/dijkstra/dijkstra_driver.cpp b/src/dijkstra/dijkstra_driver.cpp index c71cf4d994a..d8647edf96f 100644 --- a/src/dijkstra/dijkstra_driver.cpp +++ b/src/dijkstra/dijkstra_driver.cpp @@ -34,6 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include #include +#include #include "dijkstra/pgr_dijkstra.hpp" @@ -74,6 +75,35 @@ pgr_dijkstra( } +bool cmp_combination(const pgr_combination_t &a, const pgr_combination_t &b){ + return (a.source < b.source); +} + +template < class G > +std::deque< Path > +pgr_dijkstra( + G &graph, + std::vector < pgr_combination_t > combinations, + bool only_cost, + bool normal) { + std::sort(combinations.begin(), combinations.end(), cmp_combination ); + + pgrouting::Pgr_dijkstra< G > fn_dijkstra; + auto paths = fn_dijkstra.dijkstra( + graph, + combinations, + only_cost); + + if (!normal) { + for (auto &path : paths) { + path.reverse(); + } + } + return paths; + +} + + // CREATE OR REPLACE FUNCTION pgr_dijkstra( // sql text, @@ -181,3 +211,107 @@ do_pgr_many_to_many_dijkstra( *log_msg = pgr_msg(log.str().c_str()); } } + + +// CREATE OR REPLACE FUNCTION pgr_dijkstra( +// sql text, +// combinations sql text, +// directed boolean default true, +void +do_pgr_parallel_dijkstra( + pgr_edge_t *data_edges, + size_t total_edges, + pgr_combination_t *combinations, + size_t total_combinations, + bool directed, + bool only_cost, + bool normal, + + General_path_element_t **return_tuples, + size_t *return_count, + char ** log_msg, + char ** notice_msg, + char ** err_msg) { + std::ostringstream log; + std::ostringstream err; + std::ostringstream notice; + + try { + pgassert(total_edges != 0); + pgassert(total_combinations != 0); + pgassert(!(*log_msg)); + pgassert(!(*notice_msg)); + pgassert(!(*err_msg)); + pgassert(!(*return_tuples)); + pgassert(*return_count == 0); + + graphType gType = directed? DIRECTED: UNDIRECTED; + + + log << "Inserting vertices into a c++ vector structure"; + std::vector + combinations_vector(combinations, combinations + total_combinations); + + std::deque< Path >paths; + if (directed) { + log << "\nWorking with directed Graph"; + pgrouting::DirectedGraph digraph(gType); + digraph.insert_edges(data_edges, total_edges); + paths = pgr_dijkstra( + digraph, + combinations_vector, + only_cost, normal); + } else { + log << "\nWorking with Undirected Graph"; + pgrouting::UndirectedGraph undigraph(gType); + undigraph.insert_edges(data_edges, total_edges); + paths = pgr_dijkstra( + undigraph, + combinations_vector, + only_cost, normal); + } + + size_t count(0); + count = count_tuples(paths); + + if (count == 0) { + (*return_tuples) = NULL; + (*return_count) = 0; + notice << + "No paths found"; + *log_msg = pgr_msg(notice.str().c_str()); + return; + } + + (*return_tuples) = pgr_alloc(count, (*return_tuples)); + log << "\nConverting a set of paths into the tuples"; + (*return_count) = (collapse_paths(return_tuples, paths)); + + *log_msg = log.str().empty()? + *log_msg : + pgr_msg(log.str().c_str()); + *notice_msg = notice.str().empty()? + *notice_msg : + pgr_msg(notice.str().c_str()); + } catch (AssertFailedException &except) { + (*return_tuples) = pgr_free(*return_tuples); + (*return_count) = 0; + err << except.what(); + *err_msg = pgr_msg(err.str().c_str()); + *log_msg = pgr_msg(log.str().c_str()); + } catch (std::exception &except) { + (*return_tuples) = pgr_free(*return_tuples); + (*return_count) = 0; + err << except.what(); + *err_msg = pgr_msg(err.str().c_str()); + *log_msg = pgr_msg(log.str().c_str()); + } catch(...) { + (*return_tuples) = pgr_free(*return_tuples); + (*return_count) = 0; + err << "Caught unknown exception!"; + *err_msg = pgr_msg(err.str().c_str()); + *log_msg = pgr_msg(log.str().c_str()); + } +} + + From fd8de6375196ada74087c4e4fcbf7a2904f54c17 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 19 May 2020 04:05:52 +0200 Subject: [PATCH 06/33] adding files to CMakeLists --- src/common/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index be65f75dea5..9b2a073b29e 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -8,6 +8,7 @@ ADD_LIBRARY(common OBJECT matrixRows_input.c get_check_data.c edges_input.c + combinations_input.c orders_input.c orders_input.c vehicles_input.c From 5dc69ebbbc99329d4d0c812cc8d6109b66d7c432 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Thu, 21 May 2020 16:25:10 +0200 Subject: [PATCH 07/33] removing unnecessary sorting, and clearing collections asap --- include/dijkstra/pgr_dijkstra.hpp | 4 ++-- include/drivers/dijkstra/dijkstra_driver.h | 2 +- src/dijkstra/dijkstra.c | 2 +- src/dijkstra/dijkstra_driver.cpp | 13 ++++--------- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/include/dijkstra/pgr_dijkstra.hpp b/include/dijkstra/pgr_dijkstra.hpp index fa5acc581cc..3cf2a61db03 100644 --- a/include/dijkstra/pgr_dijkstra.hpp +++ b/include/dijkstra/pgr_dijkstra.hpp @@ -322,13 +322,13 @@ class Pgr_dijkstra { } for (const auto &start_ends : vertex_map) { - auto r_paths = dijkstra( + auto r_paths = dijkstra( graph, start_ends.first, start_ends.second, only_cost, n_goals); paths.insert(paths.begin(), r_paths.begin(), r_paths.end()); } - + vertex_map.clear(); std::sort(paths.begin(), paths.end(), [](const Path &e1, const Path &e2)->bool { return e1.end_id() < e2.end_id(); diff --git a/include/drivers/dijkstra/dijkstra_driver.h b/include/drivers/dijkstra/dijkstra_driver.h index cd1768a0082..60b34fc7be1 100644 --- a/include/drivers/dijkstra/dijkstra_driver.h +++ b/include/drivers/dijkstra/dijkstra_driver.h @@ -76,7 +76,7 @@ extern "C" { // sql text, // combinations_sql text, // directed boolean default true, - void do_pgr_parallel_dijkstra( + void do_pgr_combinations_dijkstra( pgr_edge_t *data_edges, size_t total_tuples, pgr_combination_t *combinations, diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index cd6717594a3..7cc67d4ba24 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -179,7 +179,7 @@ process_combinations( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - do_pgr_parallel_dijkstra( + do_pgr_combinations_dijkstra( edges, total_edges, combinations, total_combinations, directed, diff --git a/src/dijkstra/dijkstra_driver.cpp b/src/dijkstra/dijkstra_driver.cpp index d8647edf96f..df32c588287 100644 --- a/src/dijkstra/dijkstra_driver.cpp +++ b/src/dijkstra/dijkstra_driver.cpp @@ -75,18 +75,13 @@ pgr_dijkstra( } -bool cmp_combination(const pgr_combination_t &a, const pgr_combination_t &b){ - return (a.source < b.source); -} - template < class G > std::deque< Path > pgr_dijkstra( G &graph, - std::vector < pgr_combination_t > combinations, + std::vector < pgr_combination_t > &combinations, bool only_cost, bool normal) { - std::sort(combinations.begin(), combinations.end(), cmp_combination ); pgrouting::Pgr_dijkstra< G > fn_dijkstra; auto paths = fn_dijkstra.dijkstra( @@ -218,7 +213,7 @@ do_pgr_many_to_many_dijkstra( // combinations sql text, // directed boolean default true, void -do_pgr_parallel_dijkstra( +do_pgr_combinations_dijkstra( pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, @@ -248,7 +243,7 @@ do_pgr_parallel_dijkstra( graphType gType = directed? DIRECTED: UNDIRECTED; - log << "Inserting vertices into a c++ vector structure"; + log << "Inserting combinations into a c++ vector structure"; std::vector combinations_vector(combinations, combinations + total_combinations); @@ -270,7 +265,7 @@ do_pgr_parallel_dijkstra( combinations_vector, only_cost, normal); } - + combinations_vector.clear(); size_t count(0); count = count_tuples(paths); From 93ed004e9fcff0264a7e9e07496a125cc211be37 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 26 May 2020 16:48:40 +0200 Subject: [PATCH 08/33] adding developer names --- doc/src/pgRouting-introduction.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/src/pgRouting-introduction.rst b/doc/src/pgRouting-introduction.rst index b77f104ebee..4b6d41e782b 100644 --- a/doc/src/pgRouting-introduction.rst +++ b/doc/src/pgRouting-introduction.rst @@ -53,10 +53,12 @@ Cayetano Benavent, Gudesa Venkata Sai Akhil, Hang Wu, Maoguang Wang, Martha Vergara, +Mahmoud SAKR, Esteban Zimanyi Regina Obe, Rohith Reddy, Sourabh Garg, Virginia Vergara + And all the people that give us a little of their time making comments, finding issues, making pull requests etc. in any of our products: osm2pgrouting, pgRouting, pgRoutingLayer. @@ -87,6 +89,7 @@ Gerald Fenoy, Gudesa Venkata Sai Akhil, Hang Wu, Jay Mahadeokar, Jinfu Leng, Kai Behncke, Kishore Kumar, Ko Nagase, +Mahmoud SAKR, Esteban Zimanyi Manikata Kondeti, Mario Basa, Martin Wiesenhaan, Maxim Dubinin, Maoguang Wang, Mohamed Zia, Mukul Priya, Razequl Islam, Regina Obe, Rohith Reddy, From 1d66f37e6a1a2a0dd18c84825cc2f4e2dbdd0824 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 26 May 2020 22:30:54 +0200 Subject: [PATCH 09/33] adding copyright notice --- include/c_types/pgr_combination_t.h | 10 ++++++++-- include/dijkstra/pgr_dijkstra.hpp | 4 ++++ include/drivers/dijkstra/dijkstra_driver.h | 4 ++++ sql/dijkstra/_dijkstra.sql | 4 ++++ sql/dijkstra/dijkstra.sql | 4 ++++ src/common/combinations_input.c | 9 +++++++-- src/dijkstra/dijkstra.c | 4 ++++ src/dijkstra/dijkstra_driver.cpp | 4 ++++ 8 files changed, 39 insertions(+), 4 deletions(-) diff --git a/include/c_types/pgr_combination_t.h b/include/c_types/pgr_combination_t.h index 09446b73e47..0bb4ce39576 100644 --- a/include/c_types/pgr_combination_t.h +++ b/include/c_types/pgr_combination_t.h @@ -1,8 +1,14 @@ /*PGR-GNU***************************************************************** File: pgr_combination_t.h -Copyright (c) 2017 Celia Virginia Vergara Castillo -Mail: vicky_vergara@hotmail.com + +Generated with Template by: +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Function's developer: +Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com ------ diff --git a/include/dijkstra/pgr_dijkstra.hpp b/include/dijkstra/pgr_dijkstra.hpp index 3cf2a61db03..eee32a6e0fb 100644 --- a/include/dijkstra/pgr_dijkstra.hpp +++ b/include/dijkstra/pgr_dijkstra.hpp @@ -6,6 +6,10 @@ Mail: project@pgrouting.org Copyright (c) 2015 Celia Virginia Vergara Castillo vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify diff --git a/include/drivers/dijkstra/dijkstra_driver.h b/include/drivers/dijkstra/dijkstra_driver.h index 60b34fc7be1..1414b2542b0 100644 --- a/include/drivers/dijkstra/dijkstra_driver.h +++ b/include/drivers/dijkstra/dijkstra_driver.h @@ -10,6 +10,10 @@ Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo Mail: vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify diff --git a/sql/dijkstra/_dijkstra.sql b/sql/dijkstra/_dijkstra.sql index 9215e64e17d..7de0b047c41 100644 --- a/sql/dijkstra/_dijkstra.sql +++ b/sql/dijkstra/_dijkstra.sql @@ -6,6 +6,10 @@ Mail: project@pgrouting.org Copyright (c) 2015 Celia Virginia Vergara Castillo mail: vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify diff --git a/sql/dijkstra/dijkstra.sql b/sql/dijkstra/dijkstra.sql index 3b15ed8246c..e6918a4f81a 100644 --- a/sql/dijkstra/dijkstra.sql +++ b/sql/dijkstra/dijkstra.sql @@ -6,6 +6,10 @@ Mail: project@pgrouting.org Copyright (c) 2015 Celia Virginia Vergara Castillo mail: vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify diff --git a/src/common/combinations_input.c b/src/common/combinations_input.c index 093ac0fb243..9fc1f26215c 100644 --- a/src/common/combinations_input.c +++ b/src/common/combinations_input.c @@ -1,8 +1,13 @@ /*PGR-GNU***************************************************************** File: combinations_input.c -Copyright (c) 2015 Celia Virginia Vergara Castillo -vicky_vergara@hotmail.com +Generated with Template by: +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Function's developer: +Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com ------ diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index 7cc67d4ba24..2a2f04291c1 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -10,6 +10,10 @@ Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo Mail: vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify diff --git a/src/dijkstra/dijkstra_driver.cpp b/src/dijkstra/dijkstra_driver.cpp index df32c588287..d5cade99532 100644 --- a/src/dijkstra/dijkstra_driver.cpp +++ b/src/dijkstra/dijkstra_driver.cpp @@ -9,6 +9,10 @@ Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo Mail: vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify From 096e4fea93a99328d35c08d1ce4b94114d8d3258 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 26 May 2020 22:32:37 +0200 Subject: [PATCH 10/33] documenting pgr_dijkstra combinations_sql --- doc/bdDijkstra/pgr_bdDijkstra.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/bdDijkstra/pgr_bdDijkstra.rst b/doc/bdDijkstra/pgr_bdDijkstra.rst index a657a335528..79a6eb6f229 100644 --- a/doc/bdDijkstra/pgr_bdDijkstra.rst +++ b/doc/bdDijkstra/pgr_bdDijkstra.rst @@ -19,6 +19,11 @@ pgr_bdDijkstra .. rubric:: Availability: +* Version 3.1.? + + * New **Proposed** functions: + * pgr_bdDijkstra(combinations sql) + * Version 3.0.0 * **Official** function @@ -75,6 +80,7 @@ Signatures pgr_bdDijkstra(edges_sql, start_vid, end_vids [, directed]) pgr_bdDijkstra(edges_sql, start_vids, end_vid [, directed]) pgr_bdDijkstra(edges_sql, start_vids, end_vids [, directed]) + pgr_bdDijkstra(edges_sql, combinations_sql [, directed]) RETURNS SET OF (seq, path_seq [, start_vid] [, end_vid], node, edge, cost, agg_cost) OR EMPTY SET From f8676537a2174d1b39eae90ad6b822e8481b6909 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Wed, 27 May 2020 01:23:30 +0200 Subject: [PATCH 11/33] documenting the combinations signature --- doc/dijkstra/pgr_dijkstra.rst | 55 +++++++++++++++++++++++++++------- doc/src/pgRouting-concepts.rst | 21 +++++++++++++ doc/src/sampledata.rst | 8 +++++ tools/testers/sampledata.sql | 24 +++++++++++++++ 4 files changed, 97 insertions(+), 11 deletions(-) diff --git a/doc/dijkstra/pgr_dijkstra.rst b/doc/dijkstra/pgr_dijkstra.rst index 20a4afb70a3..bd228cec5fa 100644 --- a/doc/dijkstra/pgr_dijkstra.rst +++ b/doc/dijkstra/pgr_dijkstra.rst @@ -20,6 +20,12 @@ In particular, the Dijkstra algorithm implemented by Boost.Graph. .. rubric:: Availability +* Version 3.1.? + + * New **Proposed** functions: + + * pgr_bdDijkstra(combinations sql) + * Version 3.0.0 * **Official** functions @@ -96,6 +102,7 @@ Signatures pgr_dijkstra(edges_sql, start_vid, end_vids [, directed]) pgr_dijkstra(edges_sql, start_vids, end_vid [, directed]) pgr_dijkstra(edges_sql, start_vids, end_vids [, directed]) + pgr_dijkstra(edges_sql, combinations_sql [, directed]) RETURNS SET OF (seq, path_seq [, start_vid] [, end_vid], node, edge, cost, agg_cost) OR EMPTY SET @@ -188,22 +195,39 @@ Many to Many :start-after: -- q5 :end-before: -- q6 +Combinations SQL +............................................................................... + +.. code-block:: none + + pgr_dijkstra(TEXT edges_sql, TEXT combination_sql, BOOLEAN directed:=true); + RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost) + OR EMPTY SET + +:Example: Three (source, target) vertex combinaitons: (from :math:`1` to :math:`2`), (form :math:`1` to :math:`17` -no route-), and (form :math:`2` to :math:`12`) on an **undirected** graph + + +.. literalinclude:: doc-pgr_dijkstra.queries + :start-after: -- q6 + :end-before: -- q7 + Parameters ------------------------------------------------------------------------------- .. pgr_dijkstra_parameters_start -============== ================== ======== ================================================= -Parameter Type Default Description -============== ================== ======== ================================================= -**edges_sql** ``TEXT`` Inner SQL query as described below. -**start_vid** ``BIGINT`` Identifier of the starting vertex of the path. -**start_vids** ``ARRAY[BIGINT]`` Array of identifiers of starting vertices. -**end_vid** ``BIGINT`` Identifier of the ending vertex of the path. -**end_vids** ``ARRAY[BIGINT]`` Array of identifiers of ending vertices. -**directed** ``BOOLEAN`` ``true`` - When ``true`` Graph is considered `Directed` - - When ``false`` the graph is considered as `Undirected`. -============== ================== ======== ================================================= +====================== ================== ======== ================================================= +Parameter Type Default Description +====================== ================== ======== ================================================= +**edges_sql** ``TEXT`` Inner SQL query as described below. +**start_vid** ``BIGINT`` Identifier of the starting vertex of the path. +**start_vids** ``ARRAY[BIGINT]`` Array of identifiers of starting vertices. +**end_vid** ``BIGINT`` Identifier of the ending vertex of the path. +**end_vids** ``ARRAY[BIGINT]`` Array of identifiers of ending vertices. +**combinations_sql** ``TEXT`` Inner SQL query producing pairs of starting and ending vertices as described below. +**directed** ``BOOLEAN`` ``true`` - When ``true`` Graph is considered `Directed` + - When ``false`` the graph is considered as `Undirected`. +====================== ================== ======== ================================================= .. pgr_dijkstra_parameters_end @@ -216,6 +240,15 @@ Inner query :start-after: basic_edges_sql_start :end-before: basic_edges_sql_end +Combinations query +------------------------------------------------------------------------------- + +.. rubric::combinations_sql + +.. include:: pgRouting-concepts.rst + :start-after: basic_combinations_sql_start + :end-before: basic_combinations_sql_end + Return Columns ------------------------------------------------------------------------------- diff --git a/doc/src/pgRouting-concepts.rst b/doc/src/pgRouting-concepts.rst index 5cbe84bf228..60f06f3dbf2 100644 --- a/doc/src/pgRouting-concepts.rst +++ b/doc/src/pgRouting-concepts.rst @@ -365,6 +365,25 @@ Where: .. points_sql_end +Description of the combinations_sql query for dijkstra like functions +............................................................................... + +.. basic_combinations_sql_start + +================= =================== ======== ================================================= +Column Type Default Description +================= =================== ======== ================================================= +**source** ``ANY-INTEGER`` Identifier of the first end point vertex of the edge. +**target** ``ANY-INTEGER`` Identifier of the second end point vertex of the edge. + +================= =================== ======== ================================================= + +Where: + +:ANY-INTEGER: SMALLINT, INTEGER, BIGINT + +.. basic_combinations_sql_end + .. _return_values: @@ -427,6 +446,7 @@ Column Type Description * `Many to One`_ * `Many to Many`_ + * `Combinations Query`_ **end_vid** ``BIGINT`` Identifier of the ending vertex. @@ -434,6 +454,7 @@ Column Type Description * `One to Many`_ * `Many to Many`_ + * `Combinations Query`_ **node** ``BIGINT`` Identifier of the node in the path from ``start_vid`` to ``end_vid``. **edge** ``BIGINT`` Identifier of the edge used to go from ``node`` to the next node in the path sequence. ``-1`` for the last node of the path. diff --git a/doc/src/sampledata.rst b/doc/src/sampledata.rst index 53cd1df6cf3..435ff50bcf7 100644 --- a/doc/src/sampledata.rst +++ b/doc/src/sampledata.rst @@ -43,6 +43,14 @@ To be able to execute the sample queries, run the following SQL commands to crea :start-after: --EDGE TABLE TOPOLOGY start :end-before: --EDGE TABLE TOPOLOGY end +.. rubric:: Combinations of start and end vertices + +- Used to test the combinations_sql signature in dijkstra-like functions. + +.. literalinclude:: ../../tools/testers/sampledata.sql + :start-after: --COMBINATIONS CREATE start + :end-before: --COMBINATIONS CREATE end + .. rubric:: Points of interest - When points outside of the graph. diff --git a/tools/testers/sampledata.sql b/tools/testers/sampledata.sql index ea2d15df908..13b16395d58 100644 --- a/tools/testers/sampledata.sql +++ b/tools/testers/sampledata.sql @@ -197,3 +197,27 @@ INSERT INTO orders --ORDERS TABLE END + +--COMBINATIONS CREATE start +CREATE TABLE combinations_table ( + source BIGINT, + target BIGINT +); + +INSERT INTO combinations_table ( + source, target) VALUES +(1, 2), +(1, 17), +(2, 12), +(2, 4), +(6, 13), +(13, 6), +(10, 4), +(4, 10), +(15, 4), +(6, 11), +(11,6), +(9, 12), +(12, 9), +(7, 17); +--COMBINATIONS CREATE end From 5532d80610399c395d7f17d9b455d3f7bd8b3153 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Wed, 27 May 2020 01:24:04 +0200 Subject: [PATCH 12/33] documenting the combinations signature --- docqueries/dijkstra/doc-pgr_dijkstra.result | 101 ++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/docqueries/dijkstra/doc-pgr_dijkstra.result b/docqueries/dijkstra/doc-pgr_dijkstra.result index 4a3741dd35e..869505daa94 100644 --- a/docqueries/dijkstra/doc-pgr_dijkstra.result +++ b/docqueries/dijkstra/doc-pgr_dijkstra.result @@ -82,6 +82,22 @@ SELECT * FROM pgr_dijkstra( (10 rows) -- q6 +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM combinations_table LIMIT 3', + FALSE +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 1 | 2 | 1 | 1 | 1 | 0 + 2 | 2 | 1 | 2 | 2 | -1 | 0 | 1 + 3 | 1 | 2 | 12 | 2 | 4 | 1 | 0 + 4 | 2 | 2 | 12 | 5 | 10 | 1 | 1 + 5 | 3 | 2 | 12 | 10 | 12 | 1 | 2 + 6 | 4 | 2 | 12 | 11 | 13 | 1 | 3 + 7 | 5 | 2 | 12 | 12 | -1 | 0 | 4 +(7 rows) + -- q7 SELECT * FROM pgr_dijkstra( 'SELECT id, source, target, cost, reverse_cost FROM edge_table', @@ -190,6 +206,25 @@ SELECT * FROM pgr_dijkstra( 18 | 5 | 11 | 5 | 5 | -1 | 0 | 4 (18 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)' +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 + 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 + 3 | 3 | 2 | 3 | 6 | 9 | 1 | 2 + 4 | 4 | 2 | 3 | 9 | 16 | 1 | 3 + 5 | 5 | 2 | 3 | 4 | 3 | 1 | 4 + 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 + 7 | 1 | 11 | 5 | 11 | 13 | 1 | 0 + 8 | 2 | 11 | 5 | 12 | 15 | 1 | 1 + 9 | 3 | 11 | 5 | 9 | 9 | 1 | 2 + 10 | 4 | 11 | 5 | 6 | 8 | 1 | 3 + 11 | 5 | 11 | 5 | 5 | -1 | 0 | 4 +(11 rows) + -- q8 -- q9 SELECT * FROM pgr_dijkstra( @@ -284,6 +319,21 @@ SELECT * FROM pgr_dijkstra( 10 | 3 | 11 | 5 | 5 | -1 | 0 | 2 (10 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)', + FALSE +); + + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 2 | 1 | 0 + 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 + 3 | 1 | 11 | 5 | 11 | 11 | 1 | 0 + 4 | 2 | 11 | 5 | 6 | 8 | 1 | 1 + 5 | 3 | 11 | 5 | 5 | -1 | 0 | 2 +(5 rows) + -- q10 -- q11 SELECT * FROM pgr_dijkstra( @@ -350,6 +400,14 @@ SELECT * FROM pgr_dijkstra( 2 | 2 | 2 | 5 | 5 | -1 | 0 | 1 (2 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)' +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- +(0 rows) + -- q12 -- q13 SELECT * FROM pgr_dijkstra( @@ -450,6 +508,22 @@ SELECT * FROM pgr_dijkstra( 12 | 3 | 11 | 5 | 5 | -1 | 0 | 2 (12 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)', + FALSE +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 + 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 + 3 | 3 | 2 | 3 | 6 | 5 | 1 | 2 + 4 | 4 | 2 | 3 | 3 | -1 | 0 | 3 + 5 | 1 | 11 | 5 | 11 | 11 | 1 | 0 + 6 | 2 | 11 | 5 | 6 | 8 | 1 | 1 + 7 | 3 | 11 | 5 | 5 | -1 | 0 | 2 +(7 rows) + -- q14 -- q15 SELECT * FROM pgr_dijkstra( @@ -539,6 +613,20 @@ SELECT * FROM pgr_dijkstra( 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 (6 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3)) AS combinations (source, target)' +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 + 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 + 3 | 3 | 2 | 3 | 6 | 9 | 1 | 2 + 4 | 4 | 2 | 3 | 9 | 16 | 1 | 3 + 5 | 5 | 2 | 3 | 4 | 3 | 1 | 4 + 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 +(6 rows) + -- q16 -- q17 SELECT * FROM pgr_dijkstra( @@ -585,6 +673,19 @@ SELECT * FROM pgr_dijkstra( 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 (2 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)', FALSE +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 2 | 1 | 0 + 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 + 3 | 1 | 11 | 5 | 11 | 11 | 1 | 0 + 4 | 2 | 11 | 5 | 6 | 8 | 1 | 1 + 5 | 3 | 11 | 5 | 5 | -1 | 0 | 2 +(5 rows) + -- q18 ROLLBACK; ROLLBACK From dd4308508b4a310ad995dd04af13c0ad2f1a9fbb Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Mon, 18 May 2020 20:10:07 +0200 Subject: [PATCH 13/33] adding the new signature --- sql/dijkstra/_dijkstra.sql | 23 ++++++++++++++++++++++ sql/dijkstra/dijkstra.sql | 39 +++++++++++++++++++++++++++++++++++--- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/sql/dijkstra/_dijkstra.sql b/sql/dijkstra/_dijkstra.sql index 01bbc27be54..9215e64e17d 100644 --- a/sql/dijkstra/_dijkstra.sql +++ b/sql/dijkstra/_dijkstra.sql @@ -51,7 +51,30 @@ RETURNS SETOF RECORD AS 'MODULE_PATHNAME' LANGUAGE C VOLATILE STRICT; + +CREATE OR REPLACE FUNCTION _pgr_dijkstra( + edges_sql TEXT, + combinations_sql TEXT, + directed BOOLEAN DEFAULT true, + only_cost BOOLEAN DEFAULT false, + normal BOOLEAN DEFAULT true, + + OUT seq INTEGER, + OUT path_seq INTEGER, + OUT start_vid BIGINT, + OUT end_vid BIGINT, + OUT node BIGINT, + OUT edge BIGINT, + OUT cost FLOAT, + OUT agg_cost FLOAT) +RETURNS SETOF RECORD AS +'MODULE_PATHNAME' +LANGUAGE C VOLATILE STRICT; + -- COMMENTS COMMENT ON FUNCTION _pgr_dijkstra(TEXT, ANYARRAY, ANYARRAY, BOOLEAN, BOOLEAN, BOOLEAN, BIGINT) IS 'pgRouting internal function'; + +COMMENT ON FUNCTION _pgr_dijkstra(TEXT, TEXT, BOOLEAN, BOOLEAN, BOOLEAN) +IS 'pgRouting internal function'; diff --git a/sql/dijkstra/dijkstra.sql b/sql/dijkstra/dijkstra.sql index c15eeb6c551..3b15ed8246c 100644 --- a/sql/dijkstra/dijkstra.sql +++ b/sql/dijkstra/dijkstra.sql @@ -51,7 +51,6 @@ LANGUAGE sql VOLATILE STRICT COST 100 ROWS 1000; - -- ONE to MANY CREATE OR REPLACE FUNCTION pgr_dijkstra( TEXT, -- edges_sql (required) @@ -76,7 +75,6 @@ LANGUAGE sql VOLATILE STRICT COST 100 ROWS 1000; - -- MANY to ONE CREATE OR REPLACE FUNCTION pgr_dijkstra( TEXT, -- edges_sql (required) @@ -101,7 +99,6 @@ LANGUAGE sql VOLATILE STRICT COST 100 ROWS 1000; - -- MANY to MANY CREATE OR REPLACE FUNCTION pgr_dijkstra( TEXT, -- edges_sql (required) @@ -127,6 +124,31 @@ LANGUAGE sql VOLATILE STRICT COST 100 ROWS 1000; +-- Combinations SQL signature +CREATE OR REPLACE FUNCTION pgr_dijkstra( + TEXT, -- edges_sql (required) + TEXT, -- combinations_sql (required) + + directed BOOLEAN DEFAULT true, + + OUT seq INTEGER, + OUT path_seq INTEGER, + OUT start_vid BIGINT, + OUT end_vid BIGINT, + OUT node BIGINT, + OUT edge BIGINT, + OUT cost FLOAT, + OUT agg_cost FLOAT) +RETURNS SETOF RECORD AS +$BODY$ + SELECT a.seq, a.path_seq, a.start_vid, a.end_vid, a.node, a.edge, a.cost, a.agg_cost + FROM _pgr_dijkstra(_pgr_get_statement($1), _pgr_get_statement($2), $3, false, true) AS a; +$BODY$ +LANGUAGE sql VOLATILE STRICT +COST 100 +ROWS 1000; + + -- COMMENTS COMMENT ON FUNCTION pgr_dijkstra(TEXT, BIGINT, BIGINT, BOOLEAN) @@ -176,3 +198,14 @@ IS 'pgr_dijkstra(Many to Many) - Documentation: - ${PGROUTING_DOC_LINK}/pgr_dijkstra.html '; + +COMMENT ON FUNCTION pgr_dijkstra(TEXT, TEXT, BOOLEAN) +IS 'pgr_dijkstra(One to One) +- Parameters: + - Edges SQL with columns: id, source, target, cost [,reverse_cost] + - Combinations SQL with columns: source, target +- Optional Parameters + - directed := true +- Documentation: + - ${PGROUTING_DOC_LINK}/pgr_dijkstra.html +'; From 22b6bfdb7b06abc2485a1351793619084dc7cb74 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Mon, 18 May 2020 23:30:44 +0200 Subject: [PATCH 14/33] adding cobinations sql sugnature --- src/dijkstra/dijkstra.c | 161 ++++++++++++++++++++++++++++++++++------ 1 file changed, 139 insertions(+), 22 deletions(-) diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index a18bc515803..7e839433543 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -39,6 +39,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/time_msg.h" #include "c_common/edges_input.h" #include "c_common/arrays_input.h" +#include "c_common/combinations_input.h" #include "drivers/dijkstra/dijkstra_driver.h" PG_MODULE_MAGIC; @@ -135,6 +136,102 @@ process( pgr_SPI_finish(); } + + +static +void +process_combinations( + char* edges_sql, + char* combinations_sql, + bool directed, + bool only_cost, + bool normal, + General_path_element_t **result_tuples, + size_t *result_count) { + pgr_SPI_connect(); + + int64_t* start_vidsArr = NULL; + size_t size_start_vidsArr = 0; + + int64_t* end_vidsArr = NULL; + size_t size_end_vidsArr = 0; + + pgr_edge_t *edges = NULL; + size_t total_edges = 0; + + pgr_combination_t *combinations = NULL; + size_t total_combinations = 0; + + if (normal) { + pgr_get_edges(edges_sql, &edges, &total_edges); + } else { + pgr_get_edges_reversed(edges_sql, &edges, &total_edges); + } + //end_vidsArr = (int64_t*) + // pgr_get_bigIntArray(&size_end_vidsArr, starts); + //start_vidsArr = (int64_t*) + // pgr_get_bigIntArray(&size_start_vidsArr, ends); + + + if (total_edges == 0) { + if (end_vidsArr) pfree(end_vidsArr); + if (start_vidsArr) pfree(start_vidsArr); + pgr_SPI_finish(); + return; + } else { + pgr_get_combinations(combinations_sql, &combinations, &total_combinations); + + } + + PGR_DBG("Starting timer"); + clock_t start_t = clock(); + char* log_msg = NULL; + char* notice_msg = NULL; + char* err_msg = NULL; + do_pgr_many_to_many_dijkstra( + edges, total_edges, + start_vidsArr, size_start_vidsArr, + end_vidsArr, size_end_vidsArr, + + directed, + only_cost, + normal, + 3, + + result_tuples, + result_count, + + &log_msg, + ¬ice_msg, + &err_msg); + + if (only_cost) { + time_msg("processing pgr_dijkstraCost", start_t, clock()); + } else { + time_msg("processing pgr_dijkstra", start_t, clock()); + } + + + if (err_msg && (*result_tuples)) { + pfree(*result_tuples); + (*result_tuples) = NULL; + (*result_count) = 0; + } + + pgr_global_report(log_msg, notice_msg, err_msg); + + if (log_msg) pfree(log_msg); + if (notice_msg) pfree(notice_msg); + if (err_msg) pfree(err_msg); + if (edges) pfree(edges); + if (start_vidsArr) pfree(start_vidsArr); + if (end_vidsArr) pfree(end_vidsArr); + pgr_SPI_finish(); +} + + + + PGDLLEXPORT Datum _pgr_dijkstra(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; @@ -150,29 +247,49 @@ _pgr_dijkstra(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); + if(PG_NARGS() == 7) { + /**********************************************************************/ + // pgr_dijkstra( + // sql TEXT, + // start_vids ANYARRAY, + // end_vids ANYARRAY, + // directed BOOLEAN default true, + // only_cost BOOLEAN default false + // normal BOOLEAN default true + + process( + text_to_cstring(PG_GETARG_TEXT_P(0)), + PG_GETARG_ARRAYTYPE_P(1), + PG_GETARG_ARRAYTYPE_P(2), + PG_GETARG_BOOL(3), + PG_GETARG_BOOL(4), + PG_GETARG_BOOL(5), + PG_GETARG_INT64(6), + &result_tuples, + &result_count); + + /**********************************************************************/ + } + else if(PG_NARGS() == 5){ + /**********************************************************************/ + // pgr_dijkstra( + // edge_sql TEXT, + // combinations_sql TEXT, + // directed BOOLEAN default true, + // only_cost BOOLEAN default false + + process_combinations( + text_to_cstring(PG_GETARG_TEXT_P(0)), + text_to_cstring(PG_GETARG_TEXT_P(1)), + PG_GETARG_BOOL(2), + PG_GETARG_BOOL(3), + PG_GETARG_BOOL(4), + &result_tuples, + &result_count); + + /**********************************************************************/ - /**********************************************************************/ - // pgr_dijkstra( - // sql TEXT, - // start_vids ANYARRAY, - // end_vids ANYARRAY, - // directed BOOLEAN default true, - // only_cost BOOLEAN default false - // normal BOOLEAN default true - - process( - text_to_cstring(PG_GETARG_TEXT_P(0)), - PG_GETARG_ARRAYTYPE_P(1), - PG_GETARG_ARRAYTYPE_P(2), - PG_GETARG_BOOL(3), - PG_GETARG_BOOL(4), - PG_GETARG_BOOL(5), - PG_GETARG_INT64(6), - &result_tuples, - &result_count); - - /**********************************************************************/ - + } #if PGSQL_VERSION > 95 funcctx->max_calls = result_count; #else From 1f4f6c4ed4e86c47f0d3ae026d62d371c8990f52 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 19 May 2020 03:37:20 +0200 Subject: [PATCH 15/33] pgr_dijkstra combinations_qry signature, first running solution --- include/c_common/combinations_input.h | 50 ++++++++ include/c_types/pgr_combination_t.h | 42 +++++++ include/c_types/pgr_edge_t.h | 2 +- src/common/combinations_input.c | 174 ++++++++++++++++++++++++++ 4 files changed, 267 insertions(+), 1 deletion(-) create mode 100644 include/c_common/combinations_input.h create mode 100644 include/c_types/pgr_combination_t.h create mode 100644 src/common/combinations_input.c diff --git a/include/c_common/combinations_input.h b/include/c_common/combinations_input.h new file mode 100644 index 00000000000..4d68952cf18 --- /dev/null +++ b/include/c_common/combinations_input.h @@ -0,0 +1,50 @@ +/*PGR-GNU***************************************************************** +File: combinations_input.h + +Copyright (c) 2015 Celia Virginia Vergara Castillo +vicky_vergara@hotmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ +#ifndef INCLUDE_C_COMMON_COMBINATIONS_INPUT_H_ +#define INCLUDE_C_COMMON_COMBINATIONS_INPUT_H_ +#pragma once + +/* for size-t */ +#include +#include "c_types/pgr_combination_t.h" + + +/*! @brief combinations_sql + +~~~~{.c} +SELECT source, target +FROM combinations_table; +~~~~ + + +@param[in] combinations_sql +@param[out] combinations +@param[out] combinations_edges +*/ +void pgr_get_combinations( + char *combinations_sql, + pgr_combination_t **combinations, + size_t *total_combinations); + +#endif // INCLUDE_C_COMMON_COMBINATIONS_INPUT_H_ diff --git a/include/c_types/pgr_combination_t.h b/include/c_types/pgr_combination_t.h new file mode 100644 index 00000000000..a941c898af8 --- /dev/null +++ b/include/c_types/pgr_combination_t.h @@ -0,0 +1,42 @@ +/*PGR-GNU***************************************************************** +File: pgr_combination_t.h + +Copyright (c) 2017 Celia Virginia Vergara Castillo +Mail: vicky_vergara@hotmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ +/*! @file */ + +#ifndef INCLUDE_C_TYPES_PGR_COMBINATION_T_H_ +#define INCLUDE_C_TYPES_PGR_COMBINATION_T_H_ +#pragma once + +/* for int64_t */ +#ifdef __cplusplus +# include +#else +# include +#endif + +typedef struct { + int64_t source; + int64_t target; +} pgr_combination_t; + +#endif // INCLUDE_C_TYPES_PGR_COMBINATION_T_H_ diff --git a/include/c_types/pgr_edge_t.h b/include/c_types/pgr_edge_t.h index 9acce633756..b8bebc83281 100644 --- a/include/c_types/pgr_edge_t.h +++ b/include/c_types/pgr_edge_t.h @@ -42,4 +42,4 @@ typedef struct { double reverse_cost; } pgr_edge_t; -#endif // INCLUDE_C_TYPES_PGR_EDGE_T_H_ +#endif // INCLUDE_C_TYPES_PGR_COMBINATION_T_H_ diff --git a/src/common/combinations_input.c b/src/common/combinations_input.c new file mode 100644 index 00000000000..33d56d2eecc --- /dev/null +++ b/src/common/combinations_input.c @@ -0,0 +1,174 @@ +/*PGR-GNU***************************************************************** +File: combinations_input.c + +Copyright (c) 2015 Celia Virginia Vergara Castillo +vicky_vergara@hotmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ + +#include "c_common/edges_input.h" + +#include +#include +#include +/* for size-t */ +#ifdef __cplusplus +# include +#else +# include +#endif + +#include "c_types/column_info_t.h" + +#include "c_common/debug_macro.h" +#include "c_common/get_check_data.h" +#include "c_common/time_msg.h" + +static +void fetch_basic_edge( + HeapTuple *tuple, + TupleDesc *tupdesc, + Column_info_t info[5], + int64_t *default_id, + pgr_basic_edge_t *edge, + size_t *valid_edges) { + if (column_found(info[0].colNumber)) { + edge->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]); + } else { + edge->id = *default_id; + ++(*default_id); + } + + edge->source = pgr_SPI_getBigInt(tuple, tupdesc, info[1]); + edge->target = pgr_SPI_getBigInt(tuple, tupdesc, info[2]); + edge->going = pgr_SPI_getFloat8(tuple, tupdesc, info[3]) > 0 ? + true : false; + edge->coming = (column_found(info[4].colNumber) + && pgr_SPI_getFloat8(tuple, tupdesc, info[4]) > 0) ? + true : false; + + (*valid_edges)++; +} + +static +void fetch_edge( + HeapTuple *tuple, + TupleDesc *tupdesc, + Column_info_t info[2], + pgr_combination_t *combination, + size_t *valid_combinations) { + combination->source = pgr_SPI_getBigInt(tuple, tupdesc, info[0]); + combination->target = pgr_SPI_getBigInt(tuple, tupdesc, info[1]); + + *valid_edges = *valid_edges + 1; +} + + + +static +void +get_combinations_2_columns( + char *sql, + pgr_combinations_t **combinations, + size_t *totalTuples) { + clock_t start_t = clock(); + + const int tuple_limit = 1000000; + + size_t total_tuples; + size_t valid_edges; + + Column_info_t info[2]; + + int i; + for (i = 0; i < 2; ++i) { + info[i].colNumber = -1; + info[i].type = 0; + info[i].strict = true; + info[i].eType = ANY_INTEGER; + } + info[0].name = "source"; + info[1].name = "target"; + + void *SPIplan; + SPIplan = pgr_SPI_prepare(sql); + + Portal SPIportal; + SPIportal = pgr_SPI_cursor_open(SPIplan); + + + bool moredata = true; + (*totalTuples) = total_tuples = valid_combinations = 0; + + + int64_t default_id = 0; + while (moredata == true) { + SPI_cursor_fetch(SPIportal, true, tuple_limit); + if (total_tuples == 0) + pgr_fetch_column_info(info, 2); + + size_t ntuples = SPI_processed; + total_tuples += ntuples; + + if (ntuples > 0) { + if ((*combinations) == NULL) + (*combinations) = (pgr_combination_t *) + palloc0(total_tuples * sizeof(pgr_combination_t)); + else + (*combinations) = (pgr_combination_t *) + repalloc((*combinations), total_tuples * sizeof(pgr_combination_t)); + + if ((*combinations) == NULL) { + elog(ERROR, "Out of memory"); + } + + size_t t; + SPITupleTable *tuptable = SPI_tuptable; + TupleDesc tupdesc = SPI_tuptable->tupdesc; + for (t = 0; t < ntuples; t++) { + HeapTuple tuple = tuptable->vals[t]; + fetch_combination(&tuple, &tupdesc, info, + &(*combinations)[total_tuples - ntuples + t], + &valid_combinations); + } + SPI_freetuptable(tuptable); + } else { + moredata = false; + } + } + + SPI_cursor_close(SPIportal); + + if (total_tuples == 0 || valid_combinations == 0) { + PGR_DBG("No combinations found"); + } + + (*totalTuples) = total_tuples; + PGR_DBG("Reading %ld combinations", total_tuples); + time_msg("reading combinations", start_t, clock()); +} + +/* select source, target */ +void +pgr_get_combinations( + char *combinations_sql, + pgr_combination_t **combinations, + size_t *total_combinations) { + get_combinations_2_columns(combinations_sql, combinations, total_combinations); +} From acb06336fda74dd19473a56c8f0adec90d828193 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 19 May 2020 03:39:20 +0200 Subject: [PATCH 16/33] pgr_dijkstra combinations_qry signature, first running solution --- include/c_types/pgr_combination_t.h | 1 + include/dijkstra/pgr_dijkstra.hpp | 42 +++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/c_types/pgr_combination_t.h b/include/c_types/pgr_combination_t.h index a941c898af8..09446b73e47 100644 --- a/include/c_types/pgr_combination_t.h +++ b/include/c_types/pgr_combination_t.h @@ -39,4 +39,5 @@ typedef struct { int64_t target; } pgr_combination_t; + #endif // INCLUDE_C_TYPES_PGR_COMBINATION_T_H_ diff --git a/include/dijkstra/pgr_dijkstra.hpp b/include/dijkstra/pgr_dijkstra.hpp index 4343713cefb..fa5acc581cc 100644 --- a/include/dijkstra/pgr_dijkstra.hpp +++ b/include/dijkstra/pgr_dijkstra.hpp @@ -30,6 +30,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include +#include "c_types/pgr_combination_t.h" #if BOOST_VERSION_OK #include @@ -299,6 +300,47 @@ class Pgr_dijkstra { return paths; } + // preparation for parallel arrays + std::deque dijkstra( + G &graph, + const std::vector< pgr_combination_t > &combinations, + bool only_cost, + size_t n_goals = std::numeric_limits::max()) { + // a call to 1 to many is faster for each of the sources + std::deque paths; + + // group targets per distinct source + std::map > vertex_map; + for (const pgr_combination_t &comb : combinations){ + std::map< int64_t , std::vector >::iterator it= vertex_map.find(comb.source); + if (it != vertex_map.end()) { + it->second.push_back(comb.target); + } else { + std::vector targets{comb.target}; + vertex_map[comb.source] = targets; + } + } + + for (const auto &start_ends : vertex_map) { + auto r_paths = dijkstra( + graph, + start_ends.first, start_ends.second, + only_cost, n_goals); + paths.insert(paths.begin(), r_paths.begin(), r_paths.end()); + } + + std::sort(paths.begin(), paths.end(), + [](const Path &e1, const Path &e2)->bool { + return e1.end_id() < e2.end_id(); + }); + std::stable_sort(paths.begin(), paths.end(), + [](const Path &e1, const Path &e2)->bool { + return e1.start_id() < e2.start_id(); + }); + + return paths; + } + //@} private: From e5469dae6040f6fcb8ad4ab251a4573e8551ff87 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 19 May 2020 03:40:19 +0200 Subject: [PATCH 17/33] pgr_dijkstra combinations_qry signature, first running solution --- include/drivers/dijkstra/dijkstra_driver.h | 21 ++++ src/common/combinations_input.c | 39 +----- src/dijkstra/dijkstra.c | 31 ++--- src/dijkstra/dijkstra_driver.cpp | 134 +++++++++++++++++++++ 4 files changed, 170 insertions(+), 55 deletions(-) diff --git a/include/drivers/dijkstra/dijkstra_driver.h b/include/drivers/dijkstra/dijkstra_driver.h index b1ea059e0d5..cd1768a0082 100644 --- a/include/drivers/dijkstra/dijkstra_driver.h +++ b/include/drivers/dijkstra/dijkstra_driver.h @@ -39,6 +39,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #endif #include "c_types/pgr_edge_t.h" +#include "c_types/pgr_combination_t.h" #include "c_types/general_path_element_t.h" @@ -70,6 +71,26 @@ extern "C" { char** notice_msg, char** err_msg); + + // CREATE OR REPLACE FUNCTION pgr_dijkstra( + // sql text, + // combinations_sql text, + // directed boolean default true, + void do_pgr_parallel_dijkstra( + pgr_edge_t *data_edges, + size_t total_tuples, + pgr_combination_t *combinations, + size_t total_combinations, + bool directed, + bool only_cost, + bool normal, + + General_path_element_t **return_tuples, + size_t *return_count, + + char** log_msg, + char** notice_msg, + char** err_msg); #ifdef __cplusplus } #endif diff --git a/src/common/combinations_input.c b/src/common/combinations_input.c index 33d56d2eecc..093ac0fb243 100644 --- a/src/common/combinations_input.c +++ b/src/common/combinations_input.c @@ -22,7 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ -#include "c_common/edges_input.h" +#include "c_common/combinations_input.h" #include #include @@ -35,39 +35,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #endif #include "c_types/column_info_t.h" - +#include "c_types/pgr_combination_t.h" #include "c_common/debug_macro.h" #include "c_common/get_check_data.h" #include "c_common/time_msg.h" -static -void fetch_basic_edge( - HeapTuple *tuple, - TupleDesc *tupdesc, - Column_info_t info[5], - int64_t *default_id, - pgr_basic_edge_t *edge, - size_t *valid_edges) { - if (column_found(info[0].colNumber)) { - edge->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]); - } else { - edge->id = *default_id; - ++(*default_id); - } - - edge->source = pgr_SPI_getBigInt(tuple, tupdesc, info[1]); - edge->target = pgr_SPI_getBigInt(tuple, tupdesc, info[2]); - edge->going = pgr_SPI_getFloat8(tuple, tupdesc, info[3]) > 0 ? - true : false; - edge->coming = (column_found(info[4].colNumber) - && pgr_SPI_getFloat8(tuple, tupdesc, info[4]) > 0) ? - true : false; - - (*valid_edges)++; -} static -void fetch_edge( +void fetch_combination( HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[2], @@ -76,7 +51,7 @@ void fetch_edge( combination->source = pgr_SPI_getBigInt(tuple, tupdesc, info[0]); combination->target = pgr_SPI_getBigInt(tuple, tupdesc, info[1]); - *valid_edges = *valid_edges + 1; + *valid_combinations = *valid_combinations + 1; } @@ -85,14 +60,14 @@ static void get_combinations_2_columns( char *sql, - pgr_combinations_t **combinations, + pgr_combination_t **combinations, size_t *totalTuples) { clock_t start_t = clock(); const int tuple_limit = 1000000; size_t total_tuples; - size_t valid_edges; + size_t valid_combinations; Column_info_t info[2]; @@ -116,8 +91,6 @@ get_combinations_2_columns( bool moredata = true; (*totalTuples) = total_tuples = valid_combinations = 0; - - int64_t default_id = 0; while (moredata == true) { SPI_cursor_fetch(SPIportal, true, tuple_limit); if (total_tuples == 0) diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index 7e839433543..cd6717594a3 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -150,12 +150,6 @@ process_combinations( size_t *result_count) { pgr_SPI_connect(); - int64_t* start_vidsArr = NULL; - size_t size_start_vidsArr = 0; - - int64_t* end_vidsArr = NULL; - size_t size_end_vidsArr = 0; - pgr_edge_t *edges = NULL; size_t total_edges = 0; @@ -167,20 +161,17 @@ process_combinations( } else { pgr_get_edges_reversed(edges_sql, &edges, &total_edges); } - //end_vidsArr = (int64_t*) - // pgr_get_bigIntArray(&size_end_vidsArr, starts); - //start_vidsArr = (int64_t*) - // pgr_get_bigIntArray(&size_start_vidsArr, ends); - - if (total_edges == 0) { - if (end_vidsArr) pfree(end_vidsArr); - if (start_vidsArr) pfree(start_vidsArr); + if (total_edges == 0 ) { pgr_SPI_finish(); return; } else { pgr_get_combinations(combinations_sql, &combinations, &total_combinations); - + if(total_combinations == 0){ + if (edges) pfree(edges); + pgr_SPI_finish(); + return; + } } PGR_DBG("Starting timer"); @@ -188,15 +179,12 @@ process_combinations( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - do_pgr_many_to_many_dijkstra( + do_pgr_parallel_dijkstra( edges, total_edges, - start_vidsArr, size_start_vidsArr, - end_vidsArr, size_end_vidsArr, - + combinations, total_combinations, directed, only_cost, normal, - 3, result_tuples, result_count, @@ -224,8 +212,7 @@ process_combinations( if (notice_msg) pfree(notice_msg); if (err_msg) pfree(err_msg); if (edges) pfree(edges); - if (start_vidsArr) pfree(start_vidsArr); - if (end_vidsArr) pfree(end_vidsArr); + if (combinations) pfree(combinations); pgr_SPI_finish(); } diff --git a/src/dijkstra/dijkstra_driver.cpp b/src/dijkstra/dijkstra_driver.cpp index c71cf4d994a..d8647edf96f 100644 --- a/src/dijkstra/dijkstra_driver.cpp +++ b/src/dijkstra/dijkstra_driver.cpp @@ -34,6 +34,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include #include +#include #include "dijkstra/pgr_dijkstra.hpp" @@ -74,6 +75,35 @@ pgr_dijkstra( } +bool cmp_combination(const pgr_combination_t &a, const pgr_combination_t &b){ + return (a.source < b.source); +} + +template < class G > +std::deque< Path > +pgr_dijkstra( + G &graph, + std::vector < pgr_combination_t > combinations, + bool only_cost, + bool normal) { + std::sort(combinations.begin(), combinations.end(), cmp_combination ); + + pgrouting::Pgr_dijkstra< G > fn_dijkstra; + auto paths = fn_dijkstra.dijkstra( + graph, + combinations, + only_cost); + + if (!normal) { + for (auto &path : paths) { + path.reverse(); + } + } + return paths; + +} + + // CREATE OR REPLACE FUNCTION pgr_dijkstra( // sql text, @@ -181,3 +211,107 @@ do_pgr_many_to_many_dijkstra( *log_msg = pgr_msg(log.str().c_str()); } } + + +// CREATE OR REPLACE FUNCTION pgr_dijkstra( +// sql text, +// combinations sql text, +// directed boolean default true, +void +do_pgr_parallel_dijkstra( + pgr_edge_t *data_edges, + size_t total_edges, + pgr_combination_t *combinations, + size_t total_combinations, + bool directed, + bool only_cost, + bool normal, + + General_path_element_t **return_tuples, + size_t *return_count, + char ** log_msg, + char ** notice_msg, + char ** err_msg) { + std::ostringstream log; + std::ostringstream err; + std::ostringstream notice; + + try { + pgassert(total_edges != 0); + pgassert(total_combinations != 0); + pgassert(!(*log_msg)); + pgassert(!(*notice_msg)); + pgassert(!(*err_msg)); + pgassert(!(*return_tuples)); + pgassert(*return_count == 0); + + graphType gType = directed? DIRECTED: UNDIRECTED; + + + log << "Inserting vertices into a c++ vector structure"; + std::vector + combinations_vector(combinations, combinations + total_combinations); + + std::deque< Path >paths; + if (directed) { + log << "\nWorking with directed Graph"; + pgrouting::DirectedGraph digraph(gType); + digraph.insert_edges(data_edges, total_edges); + paths = pgr_dijkstra( + digraph, + combinations_vector, + only_cost, normal); + } else { + log << "\nWorking with Undirected Graph"; + pgrouting::UndirectedGraph undigraph(gType); + undigraph.insert_edges(data_edges, total_edges); + paths = pgr_dijkstra( + undigraph, + combinations_vector, + only_cost, normal); + } + + size_t count(0); + count = count_tuples(paths); + + if (count == 0) { + (*return_tuples) = NULL; + (*return_count) = 0; + notice << + "No paths found"; + *log_msg = pgr_msg(notice.str().c_str()); + return; + } + + (*return_tuples) = pgr_alloc(count, (*return_tuples)); + log << "\nConverting a set of paths into the tuples"; + (*return_count) = (collapse_paths(return_tuples, paths)); + + *log_msg = log.str().empty()? + *log_msg : + pgr_msg(log.str().c_str()); + *notice_msg = notice.str().empty()? + *notice_msg : + pgr_msg(notice.str().c_str()); + } catch (AssertFailedException &except) { + (*return_tuples) = pgr_free(*return_tuples); + (*return_count) = 0; + err << except.what(); + *err_msg = pgr_msg(err.str().c_str()); + *log_msg = pgr_msg(log.str().c_str()); + } catch (std::exception &except) { + (*return_tuples) = pgr_free(*return_tuples); + (*return_count) = 0; + err << except.what(); + *err_msg = pgr_msg(err.str().c_str()); + *log_msg = pgr_msg(log.str().c_str()); + } catch(...) { + (*return_tuples) = pgr_free(*return_tuples); + (*return_count) = 0; + err << "Caught unknown exception!"; + *err_msg = pgr_msg(err.str().c_str()); + *log_msg = pgr_msg(log.str().c_str()); + } +} + + From 07b7ce64c804b260f9aaf3f6883f93a6ed632a29 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 19 May 2020 04:05:52 +0200 Subject: [PATCH 18/33] adding files to CMakeLists --- src/common/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index be65f75dea5..9b2a073b29e 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -8,6 +8,7 @@ ADD_LIBRARY(common OBJECT matrixRows_input.c get_check_data.c edges_input.c + combinations_input.c orders_input.c orders_input.c vehicles_input.c From 0430a5cae7e7b5f19ce1a8b1e4f7fef4a58248f5 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Thu, 21 May 2020 16:25:10 +0200 Subject: [PATCH 19/33] removing unnecessary sorting, and clearing collections asap --- include/dijkstra/pgr_dijkstra.hpp | 4 ++-- include/drivers/dijkstra/dijkstra_driver.h | 2 +- src/dijkstra/dijkstra.c | 2 +- src/dijkstra/dijkstra_driver.cpp | 13 ++++--------- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/include/dijkstra/pgr_dijkstra.hpp b/include/dijkstra/pgr_dijkstra.hpp index fa5acc581cc..3cf2a61db03 100644 --- a/include/dijkstra/pgr_dijkstra.hpp +++ b/include/dijkstra/pgr_dijkstra.hpp @@ -322,13 +322,13 @@ class Pgr_dijkstra { } for (const auto &start_ends : vertex_map) { - auto r_paths = dijkstra( + auto r_paths = dijkstra( graph, start_ends.first, start_ends.second, only_cost, n_goals); paths.insert(paths.begin(), r_paths.begin(), r_paths.end()); } - + vertex_map.clear(); std::sort(paths.begin(), paths.end(), [](const Path &e1, const Path &e2)->bool { return e1.end_id() < e2.end_id(); diff --git a/include/drivers/dijkstra/dijkstra_driver.h b/include/drivers/dijkstra/dijkstra_driver.h index cd1768a0082..60b34fc7be1 100644 --- a/include/drivers/dijkstra/dijkstra_driver.h +++ b/include/drivers/dijkstra/dijkstra_driver.h @@ -76,7 +76,7 @@ extern "C" { // sql text, // combinations_sql text, // directed boolean default true, - void do_pgr_parallel_dijkstra( + void do_pgr_combinations_dijkstra( pgr_edge_t *data_edges, size_t total_tuples, pgr_combination_t *combinations, diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index cd6717594a3..7cc67d4ba24 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -179,7 +179,7 @@ process_combinations( char* log_msg = NULL; char* notice_msg = NULL; char* err_msg = NULL; - do_pgr_parallel_dijkstra( + do_pgr_combinations_dijkstra( edges, total_edges, combinations, total_combinations, directed, diff --git a/src/dijkstra/dijkstra_driver.cpp b/src/dijkstra/dijkstra_driver.cpp index d8647edf96f..df32c588287 100644 --- a/src/dijkstra/dijkstra_driver.cpp +++ b/src/dijkstra/dijkstra_driver.cpp @@ -75,18 +75,13 @@ pgr_dijkstra( } -bool cmp_combination(const pgr_combination_t &a, const pgr_combination_t &b){ - return (a.source < b.source); -} - template < class G > std::deque< Path > pgr_dijkstra( G &graph, - std::vector < pgr_combination_t > combinations, + std::vector < pgr_combination_t > &combinations, bool only_cost, bool normal) { - std::sort(combinations.begin(), combinations.end(), cmp_combination ); pgrouting::Pgr_dijkstra< G > fn_dijkstra; auto paths = fn_dijkstra.dijkstra( @@ -218,7 +213,7 @@ do_pgr_many_to_many_dijkstra( // combinations sql text, // directed boolean default true, void -do_pgr_parallel_dijkstra( +do_pgr_combinations_dijkstra( pgr_edge_t *data_edges, size_t total_edges, pgr_combination_t *combinations, @@ -248,7 +243,7 @@ do_pgr_parallel_dijkstra( graphType gType = directed? DIRECTED: UNDIRECTED; - log << "Inserting vertices into a c++ vector structure"; + log << "Inserting combinations into a c++ vector structure"; std::vector combinations_vector(combinations, combinations + total_combinations); @@ -270,7 +265,7 @@ do_pgr_parallel_dijkstra( combinations_vector, only_cost, normal); } - + combinations_vector.clear(); size_t count(0); count = count_tuples(paths); From fc3f81ef6b0d796150e62c08c6a0ae8c9c0ad64e Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 26 May 2020 16:48:40 +0200 Subject: [PATCH 20/33] adding developer names --- doc/src/pgRouting-introduction.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/src/pgRouting-introduction.rst b/doc/src/pgRouting-introduction.rst index b77f104ebee..4b6d41e782b 100644 --- a/doc/src/pgRouting-introduction.rst +++ b/doc/src/pgRouting-introduction.rst @@ -53,10 +53,12 @@ Cayetano Benavent, Gudesa Venkata Sai Akhil, Hang Wu, Maoguang Wang, Martha Vergara, +Mahmoud SAKR, Esteban Zimanyi Regina Obe, Rohith Reddy, Sourabh Garg, Virginia Vergara + And all the people that give us a little of their time making comments, finding issues, making pull requests etc. in any of our products: osm2pgrouting, pgRouting, pgRoutingLayer. @@ -87,6 +89,7 @@ Gerald Fenoy, Gudesa Venkata Sai Akhil, Hang Wu, Jay Mahadeokar, Jinfu Leng, Kai Behncke, Kishore Kumar, Ko Nagase, +Mahmoud SAKR, Esteban Zimanyi Manikata Kondeti, Mario Basa, Martin Wiesenhaan, Maxim Dubinin, Maoguang Wang, Mohamed Zia, Mukul Priya, Razequl Islam, Regina Obe, Rohith Reddy, From d78c6ab99ecba6a4c33b6b09e42c26f73b68013d Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 26 May 2020 22:30:54 +0200 Subject: [PATCH 21/33] adding copyright notice --- include/c_types/pgr_combination_t.h | 10 ++++++++-- include/dijkstra/pgr_dijkstra.hpp | 4 ++++ include/drivers/dijkstra/dijkstra_driver.h | 4 ++++ sql/dijkstra/_dijkstra.sql | 4 ++++ sql/dijkstra/dijkstra.sql | 4 ++++ src/common/combinations_input.c | 9 +++++++-- src/dijkstra/dijkstra.c | 4 ++++ src/dijkstra/dijkstra_driver.cpp | 4 ++++ 8 files changed, 39 insertions(+), 4 deletions(-) diff --git a/include/c_types/pgr_combination_t.h b/include/c_types/pgr_combination_t.h index 09446b73e47..0bb4ce39576 100644 --- a/include/c_types/pgr_combination_t.h +++ b/include/c_types/pgr_combination_t.h @@ -1,8 +1,14 @@ /*PGR-GNU***************************************************************** File: pgr_combination_t.h -Copyright (c) 2017 Celia Virginia Vergara Castillo -Mail: vicky_vergara@hotmail.com + +Generated with Template by: +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Function's developer: +Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com ------ diff --git a/include/dijkstra/pgr_dijkstra.hpp b/include/dijkstra/pgr_dijkstra.hpp index 3cf2a61db03..eee32a6e0fb 100644 --- a/include/dijkstra/pgr_dijkstra.hpp +++ b/include/dijkstra/pgr_dijkstra.hpp @@ -6,6 +6,10 @@ Mail: project@pgrouting.org Copyright (c) 2015 Celia Virginia Vergara Castillo vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify diff --git a/include/drivers/dijkstra/dijkstra_driver.h b/include/drivers/dijkstra/dijkstra_driver.h index 60b34fc7be1..1414b2542b0 100644 --- a/include/drivers/dijkstra/dijkstra_driver.h +++ b/include/drivers/dijkstra/dijkstra_driver.h @@ -10,6 +10,10 @@ Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo Mail: vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify diff --git a/sql/dijkstra/_dijkstra.sql b/sql/dijkstra/_dijkstra.sql index 9215e64e17d..7de0b047c41 100644 --- a/sql/dijkstra/_dijkstra.sql +++ b/sql/dijkstra/_dijkstra.sql @@ -6,6 +6,10 @@ Mail: project@pgrouting.org Copyright (c) 2015 Celia Virginia Vergara Castillo mail: vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify diff --git a/sql/dijkstra/dijkstra.sql b/sql/dijkstra/dijkstra.sql index 3b15ed8246c..e6918a4f81a 100644 --- a/sql/dijkstra/dijkstra.sql +++ b/sql/dijkstra/dijkstra.sql @@ -6,6 +6,10 @@ Mail: project@pgrouting.org Copyright (c) 2015 Celia Virginia Vergara Castillo mail: vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify diff --git a/src/common/combinations_input.c b/src/common/combinations_input.c index 093ac0fb243..9fc1f26215c 100644 --- a/src/common/combinations_input.c +++ b/src/common/combinations_input.c @@ -1,8 +1,13 @@ /*PGR-GNU***************************************************************** File: combinations_input.c -Copyright (c) 2015 Celia Virginia Vergara Castillo -vicky_vergara@hotmail.com +Generated with Template by: +Copyright (c) 2015 pgRouting developers +Mail: project@pgrouting.org + +Function's developer: +Copyright (c) 2020 Mahmoud SAKR and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com ------ diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index 7cc67d4ba24..2a2f04291c1 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -10,6 +10,10 @@ Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo Mail: vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify diff --git a/src/dijkstra/dijkstra_driver.cpp b/src/dijkstra/dijkstra_driver.cpp index df32c588287..d5cade99532 100644 --- a/src/dijkstra/dijkstra_driver.cpp +++ b/src/dijkstra/dijkstra_driver.cpp @@ -9,6 +9,10 @@ Function's developer: Copyright (c) 2015 Celia Virginia Vergara Castillo Mail: vicky_vergara@hotmail.com +Copyright (c) 2020 The combinations_sql signature is added by Mahmoud SAKR +and Esteban ZIMANYI +mail: m_attia_sakr@yahoo.com, estebanzimanyi@gmail.com + ------ This program is free software; you can redistribute it and/or modify From ce17db4c9d0700d40af56bcb30efb65c5431a247 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Tue, 26 May 2020 22:32:37 +0200 Subject: [PATCH 22/33] documenting pgr_dijkstra combinations_sql --- doc/bdDijkstra/pgr_bdDijkstra.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/doc/bdDijkstra/pgr_bdDijkstra.rst b/doc/bdDijkstra/pgr_bdDijkstra.rst index a657a335528..79a6eb6f229 100644 --- a/doc/bdDijkstra/pgr_bdDijkstra.rst +++ b/doc/bdDijkstra/pgr_bdDijkstra.rst @@ -19,6 +19,11 @@ pgr_bdDijkstra .. rubric:: Availability: +* Version 3.1.? + + * New **Proposed** functions: + * pgr_bdDijkstra(combinations sql) + * Version 3.0.0 * **Official** function @@ -75,6 +80,7 @@ Signatures pgr_bdDijkstra(edges_sql, start_vid, end_vids [, directed]) pgr_bdDijkstra(edges_sql, start_vids, end_vid [, directed]) pgr_bdDijkstra(edges_sql, start_vids, end_vids [, directed]) + pgr_bdDijkstra(edges_sql, combinations_sql [, directed]) RETURNS SET OF (seq, path_seq [, start_vid] [, end_vid], node, edge, cost, agg_cost) OR EMPTY SET From c062d36705eaa172c6ce9e4c070dd3616c259958 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Wed, 27 May 2020 01:23:30 +0200 Subject: [PATCH 23/33] documenting the combinations signature --- doc/dijkstra/pgr_dijkstra.rst | 55 +++++++++++++++++++++++++++------- doc/src/pgRouting-concepts.rst | 21 +++++++++++++ doc/src/sampledata.rst | 8 +++++ tools/testers/sampledata.sql | 24 +++++++++++++++ 4 files changed, 97 insertions(+), 11 deletions(-) diff --git a/doc/dijkstra/pgr_dijkstra.rst b/doc/dijkstra/pgr_dijkstra.rst index 20a4afb70a3..bd228cec5fa 100644 --- a/doc/dijkstra/pgr_dijkstra.rst +++ b/doc/dijkstra/pgr_dijkstra.rst @@ -20,6 +20,12 @@ In particular, the Dijkstra algorithm implemented by Boost.Graph. .. rubric:: Availability +* Version 3.1.? + + * New **Proposed** functions: + + * pgr_bdDijkstra(combinations sql) + * Version 3.0.0 * **Official** functions @@ -96,6 +102,7 @@ Signatures pgr_dijkstra(edges_sql, start_vid, end_vids [, directed]) pgr_dijkstra(edges_sql, start_vids, end_vid [, directed]) pgr_dijkstra(edges_sql, start_vids, end_vids [, directed]) + pgr_dijkstra(edges_sql, combinations_sql [, directed]) RETURNS SET OF (seq, path_seq [, start_vid] [, end_vid], node, edge, cost, agg_cost) OR EMPTY SET @@ -188,22 +195,39 @@ Many to Many :start-after: -- q5 :end-before: -- q6 +Combinations SQL +............................................................................... + +.. code-block:: none + + pgr_dijkstra(TEXT edges_sql, TEXT combination_sql, BOOLEAN directed:=true); + RETURNS SET OF (seq, path_seq, start_vid, end_vid, node, edge, cost, agg_cost) + OR EMPTY SET + +:Example: Three (source, target) vertex combinaitons: (from :math:`1` to :math:`2`), (form :math:`1` to :math:`17` -no route-), and (form :math:`2` to :math:`12`) on an **undirected** graph + + +.. literalinclude:: doc-pgr_dijkstra.queries + :start-after: -- q6 + :end-before: -- q7 + Parameters ------------------------------------------------------------------------------- .. pgr_dijkstra_parameters_start -============== ================== ======== ================================================= -Parameter Type Default Description -============== ================== ======== ================================================= -**edges_sql** ``TEXT`` Inner SQL query as described below. -**start_vid** ``BIGINT`` Identifier of the starting vertex of the path. -**start_vids** ``ARRAY[BIGINT]`` Array of identifiers of starting vertices. -**end_vid** ``BIGINT`` Identifier of the ending vertex of the path. -**end_vids** ``ARRAY[BIGINT]`` Array of identifiers of ending vertices. -**directed** ``BOOLEAN`` ``true`` - When ``true`` Graph is considered `Directed` - - When ``false`` the graph is considered as `Undirected`. -============== ================== ======== ================================================= +====================== ================== ======== ================================================= +Parameter Type Default Description +====================== ================== ======== ================================================= +**edges_sql** ``TEXT`` Inner SQL query as described below. +**start_vid** ``BIGINT`` Identifier of the starting vertex of the path. +**start_vids** ``ARRAY[BIGINT]`` Array of identifiers of starting vertices. +**end_vid** ``BIGINT`` Identifier of the ending vertex of the path. +**end_vids** ``ARRAY[BIGINT]`` Array of identifiers of ending vertices. +**combinations_sql** ``TEXT`` Inner SQL query producing pairs of starting and ending vertices as described below. +**directed** ``BOOLEAN`` ``true`` - When ``true`` Graph is considered `Directed` + - When ``false`` the graph is considered as `Undirected`. +====================== ================== ======== ================================================= .. pgr_dijkstra_parameters_end @@ -216,6 +240,15 @@ Inner query :start-after: basic_edges_sql_start :end-before: basic_edges_sql_end +Combinations query +------------------------------------------------------------------------------- + +.. rubric::combinations_sql + +.. include:: pgRouting-concepts.rst + :start-after: basic_combinations_sql_start + :end-before: basic_combinations_sql_end + Return Columns ------------------------------------------------------------------------------- diff --git a/doc/src/pgRouting-concepts.rst b/doc/src/pgRouting-concepts.rst index 5cbe84bf228..60f06f3dbf2 100644 --- a/doc/src/pgRouting-concepts.rst +++ b/doc/src/pgRouting-concepts.rst @@ -365,6 +365,25 @@ Where: .. points_sql_end +Description of the combinations_sql query for dijkstra like functions +............................................................................... + +.. basic_combinations_sql_start + +================= =================== ======== ================================================= +Column Type Default Description +================= =================== ======== ================================================= +**source** ``ANY-INTEGER`` Identifier of the first end point vertex of the edge. +**target** ``ANY-INTEGER`` Identifier of the second end point vertex of the edge. + +================= =================== ======== ================================================= + +Where: + +:ANY-INTEGER: SMALLINT, INTEGER, BIGINT + +.. basic_combinations_sql_end + .. _return_values: @@ -427,6 +446,7 @@ Column Type Description * `Many to One`_ * `Many to Many`_ + * `Combinations Query`_ **end_vid** ``BIGINT`` Identifier of the ending vertex. @@ -434,6 +454,7 @@ Column Type Description * `One to Many`_ * `Many to Many`_ + * `Combinations Query`_ **node** ``BIGINT`` Identifier of the node in the path from ``start_vid`` to ``end_vid``. **edge** ``BIGINT`` Identifier of the edge used to go from ``node`` to the next node in the path sequence. ``-1`` for the last node of the path. diff --git a/doc/src/sampledata.rst b/doc/src/sampledata.rst index 53cd1df6cf3..435ff50bcf7 100644 --- a/doc/src/sampledata.rst +++ b/doc/src/sampledata.rst @@ -43,6 +43,14 @@ To be able to execute the sample queries, run the following SQL commands to crea :start-after: --EDGE TABLE TOPOLOGY start :end-before: --EDGE TABLE TOPOLOGY end +.. rubric:: Combinations of start and end vertices + +- Used to test the combinations_sql signature in dijkstra-like functions. + +.. literalinclude:: ../../tools/testers/sampledata.sql + :start-after: --COMBINATIONS CREATE start + :end-before: --COMBINATIONS CREATE end + .. rubric:: Points of interest - When points outside of the graph. diff --git a/tools/testers/sampledata.sql b/tools/testers/sampledata.sql index ea2d15df908..13b16395d58 100644 --- a/tools/testers/sampledata.sql +++ b/tools/testers/sampledata.sql @@ -197,3 +197,27 @@ INSERT INTO orders --ORDERS TABLE END + +--COMBINATIONS CREATE start +CREATE TABLE combinations_table ( + source BIGINT, + target BIGINT +); + +INSERT INTO combinations_table ( + source, target) VALUES +(1, 2), +(1, 17), +(2, 12), +(2, 4), +(6, 13), +(13, 6), +(10, 4), +(4, 10), +(15, 4), +(6, 11), +(11,6), +(9, 12), +(12, 9), +(7, 17); +--COMBINATIONS CREATE end From ca476e6c5df0e3bb975071a2b77271a5a965ac65 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Wed, 27 May 2020 01:24:04 +0200 Subject: [PATCH 24/33] documenting the combinations signature --- docqueries/dijkstra/doc-pgr_dijkstra.result | 101 ++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/docqueries/dijkstra/doc-pgr_dijkstra.result b/docqueries/dijkstra/doc-pgr_dijkstra.result index 4a3741dd35e..869505daa94 100644 --- a/docqueries/dijkstra/doc-pgr_dijkstra.result +++ b/docqueries/dijkstra/doc-pgr_dijkstra.result @@ -82,6 +82,22 @@ SELECT * FROM pgr_dijkstra( (10 rows) -- q6 +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM combinations_table LIMIT 3', + FALSE +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 1 | 2 | 1 | 1 | 1 | 0 + 2 | 2 | 1 | 2 | 2 | -1 | 0 | 1 + 3 | 1 | 2 | 12 | 2 | 4 | 1 | 0 + 4 | 2 | 2 | 12 | 5 | 10 | 1 | 1 + 5 | 3 | 2 | 12 | 10 | 12 | 1 | 2 + 6 | 4 | 2 | 12 | 11 | 13 | 1 | 3 + 7 | 5 | 2 | 12 | 12 | -1 | 0 | 4 +(7 rows) + -- q7 SELECT * FROM pgr_dijkstra( 'SELECT id, source, target, cost, reverse_cost FROM edge_table', @@ -190,6 +206,25 @@ SELECT * FROM pgr_dijkstra( 18 | 5 | 11 | 5 | 5 | -1 | 0 | 4 (18 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)' +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 + 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 + 3 | 3 | 2 | 3 | 6 | 9 | 1 | 2 + 4 | 4 | 2 | 3 | 9 | 16 | 1 | 3 + 5 | 5 | 2 | 3 | 4 | 3 | 1 | 4 + 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 + 7 | 1 | 11 | 5 | 11 | 13 | 1 | 0 + 8 | 2 | 11 | 5 | 12 | 15 | 1 | 1 + 9 | 3 | 11 | 5 | 9 | 9 | 1 | 2 + 10 | 4 | 11 | 5 | 6 | 8 | 1 | 3 + 11 | 5 | 11 | 5 | 5 | -1 | 0 | 4 +(11 rows) + -- q8 -- q9 SELECT * FROM pgr_dijkstra( @@ -284,6 +319,21 @@ SELECT * FROM pgr_dijkstra( 10 | 3 | 11 | 5 | 5 | -1 | 0 | 2 (10 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)', + FALSE +); + + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 2 | 1 | 0 + 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 + 3 | 1 | 11 | 5 | 11 | 11 | 1 | 0 + 4 | 2 | 11 | 5 | 6 | 8 | 1 | 1 + 5 | 3 | 11 | 5 | 5 | -1 | 0 | 2 +(5 rows) + -- q10 -- q11 SELECT * FROM pgr_dijkstra( @@ -350,6 +400,14 @@ SELECT * FROM pgr_dijkstra( 2 | 2 | 2 | 5 | 5 | -1 | 0 | 1 (2 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)' +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- +(0 rows) + -- q12 -- q13 SELECT * FROM pgr_dijkstra( @@ -450,6 +508,22 @@ SELECT * FROM pgr_dijkstra( 12 | 3 | 11 | 5 | 5 | -1 | 0 | 2 (12 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)', + FALSE +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 + 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 + 3 | 3 | 2 | 3 | 6 | 5 | 1 | 2 + 4 | 4 | 2 | 3 | 3 | -1 | 0 | 3 + 5 | 1 | 11 | 5 | 11 | 11 | 1 | 0 + 6 | 2 | 11 | 5 | 6 | 8 | 1 | 1 + 7 | 3 | 11 | 5 | 5 | -1 | 0 | 2 +(7 rows) + -- q14 -- q15 SELECT * FROM pgr_dijkstra( @@ -539,6 +613,20 @@ SELECT * FROM pgr_dijkstra( 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 (6 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3)) AS combinations (source, target)' +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 + 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 + 3 | 3 | 2 | 3 | 6 | 9 | 1 | 2 + 4 | 4 | 2 | 3 | 9 | 16 | 1 | 3 + 5 | 5 | 2 | 3 | 4 | 3 | 1 | 4 + 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 +(6 rows) + -- q16 -- q17 SELECT * FROM pgr_dijkstra( @@ -585,6 +673,19 @@ SELECT * FROM pgr_dijkstra( 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 (2 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)', FALSE +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 2 | 1 | 0 + 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 + 3 | 1 | 11 | 5 | 11 | 11 | 1 | 0 + 4 | 2 | 11 | 5 | 6 | 8 | 1 | 1 + 5 | 3 | 11 | 5 | 5 | -1 | 0 | 2 +(5 rows) + -- q18 ROLLBACK; ROLLBACK From b8fa4fb10c4284a83de62565f81786fbc1b81042 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Thu, 28 May 2020 12:35:04 +0200 Subject: [PATCH 25/33] adding pgr_dijkstra combinations_sql to release notes 3.1.0 --- doc/src/release_notes.rst | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/src/release_notes.rst b/doc/src/release_notes.rst index f0b59ead665..be18575a636 100644 --- a/doc/src/release_notes.rst +++ b/doc/src/release_notes.rst @@ -53,7 +53,9 @@ To see the full list of changes check the list of `Git commits Date: Sun, 31 May 2020 17:01:30 +0200 Subject: [PATCH 26/33] fixing documentation bug, that was wrongly inserted in bdDijkstra --- doc/bdDijkstra/pgr_bdDijkstra.rst | 6 ------ 1 file changed, 6 deletions(-) diff --git a/doc/bdDijkstra/pgr_bdDijkstra.rst b/doc/bdDijkstra/pgr_bdDijkstra.rst index 79a6eb6f229..a657a335528 100644 --- a/doc/bdDijkstra/pgr_bdDijkstra.rst +++ b/doc/bdDijkstra/pgr_bdDijkstra.rst @@ -19,11 +19,6 @@ pgr_bdDijkstra .. rubric:: Availability: -* Version 3.1.? - - * New **Proposed** functions: - * pgr_bdDijkstra(combinations sql) - * Version 3.0.0 * **Official** function @@ -80,7 +75,6 @@ Signatures pgr_bdDijkstra(edges_sql, start_vid, end_vids [, directed]) pgr_bdDijkstra(edges_sql, start_vids, end_vid [, directed]) pgr_bdDijkstra(edges_sql, start_vids, end_vids [, directed]) - pgr_bdDijkstra(edges_sql, combinations_sql [, directed]) RETURNS SET OF (seq, path_seq [, start_vid] [, end_vid], node, edge, cost, agg_cost) OR EMPTY SET From 82a42bd2178432778b1dd26e2188acf370aa1877 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Sun, 31 May 2020 17:17:22 +0200 Subject: [PATCH 27/33] adding queries for documentation of dijkstra combinations SQL --- docqueries/dijkstra/doc-pgr_dijkstra.result | 109 +++--------------- docqueries/dijkstra/doc-pgr_dijkstra.test.sql | 14 +++ 2 files changed, 27 insertions(+), 96 deletions(-) diff --git a/docqueries/dijkstra/doc-pgr_dijkstra.result b/docqueries/dijkstra/doc-pgr_dijkstra.result index 869505daa94..07846c14593 100644 --- a/docqueries/dijkstra/doc-pgr_dijkstra.result +++ b/docqueries/dijkstra/doc-pgr_dijkstra.result @@ -82,22 +82,6 @@ SELECT * FROM pgr_dijkstra( (10 rows) -- q6 -SELECT * FROM pgr_dijkstra( - 'SELECT id, source, target, cost, reverse_cost FROM edge_table', - 'SELECT * FROM combinations_table LIMIT 3', - FALSE -); - seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost ------+----------+-----------+---------+------+------+------+---------- - 1 | 1 | 1 | 2 | 1 | 1 | 1 | 0 - 2 | 2 | 1 | 2 | 2 | -1 | 0 | 1 - 3 | 1 | 2 | 12 | 2 | 4 | 1 | 0 - 4 | 2 | 2 | 12 | 5 | 10 | 1 | 1 - 5 | 3 | 2 | 12 | 10 | 12 | 1 | 2 - 6 | 4 | 2 | 12 | 11 | 13 | 1 | 3 - 7 | 5 | 2 | 12 | 12 | -1 | 0 | 4 -(7 rows) - -- q7 SELECT * FROM pgr_dijkstra( 'SELECT id, source, target, cost, reverse_cost FROM edge_table', @@ -206,25 +190,6 @@ SELECT * FROM pgr_dijkstra( 18 | 5 | 11 | 5 | 5 | -1 | 0 | 4 (18 rows) -SELECT * FROM pgr_dijkstra( - 'SELECT id, source, target, cost, reverse_cost FROM edge_table', - 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)' -); - seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost ------+----------+-----------+---------+------+------+------+---------- - 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 - 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 - 3 | 3 | 2 | 3 | 6 | 9 | 1 | 2 - 4 | 4 | 2 | 3 | 9 | 16 | 1 | 3 - 5 | 5 | 2 | 3 | 4 | 3 | 1 | 4 - 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 - 7 | 1 | 11 | 5 | 11 | 13 | 1 | 0 - 8 | 2 | 11 | 5 | 12 | 15 | 1 | 1 - 9 | 3 | 11 | 5 | 9 | 9 | 1 | 2 - 10 | 4 | 11 | 5 | 6 | 8 | 1 | 3 - 11 | 5 | 11 | 5 | 5 | -1 | 0 | 4 -(11 rows) - -- q8 -- q9 SELECT * FROM pgr_dijkstra( @@ -319,21 +284,6 @@ SELECT * FROM pgr_dijkstra( 10 | 3 | 11 | 5 | 5 | -1 | 0 | 2 (10 rows) -SELECT * FROM pgr_dijkstra( - 'SELECT id, source, target, cost, reverse_cost FROM edge_table', - 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)', - FALSE -); - - seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost ------+----------+-----------+---------+------+------+------+---------- - 1 | 1 | 2 | 3 | 2 | 2 | 1 | 0 - 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 - 3 | 1 | 11 | 5 | 11 | 11 | 1 | 0 - 4 | 2 | 11 | 5 | 6 | 8 | 1 | 1 - 5 | 3 | 11 | 5 | 5 | -1 | 0 | 2 -(5 rows) - -- q10 -- q11 SELECT * FROM pgr_dijkstra( @@ -400,14 +350,6 @@ SELECT * FROM pgr_dijkstra( 2 | 2 | 2 | 5 | 5 | -1 | 0 | 1 (2 rows) -SELECT * FROM pgr_dijkstra( - 'SELECT id, source, target, cost FROM edge_table', - 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)' -); - seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost ------+----------+-----------+---------+------+------+------+---------- -(0 rows) - -- q12 -- q13 SELECT * FROM pgr_dijkstra( @@ -508,22 +450,6 @@ SELECT * FROM pgr_dijkstra( 12 | 3 | 11 | 5 | 5 | -1 | 0 | 2 (12 rows) -SELECT * FROM pgr_dijkstra( - 'SELECT id, source, target, cost FROM edge_table', - 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)', - FALSE -); - seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost ------+----------+-----------+---------+------+------+------+---------- - 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 - 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 - 3 | 3 | 2 | 3 | 6 | 5 | 1 | 2 - 4 | 4 | 2 | 3 | 3 | -1 | 0 | 3 - 5 | 1 | 11 | 5 | 11 | 11 | 1 | 0 - 6 | 2 | 11 | 5 | 6 | 8 | 1 | 1 - 7 | 3 | 11 | 5 | 5 | -1 | 0 | 2 -(7 rows) - -- q14 -- q15 SELECT * FROM pgr_dijkstra( @@ -613,20 +539,6 @@ SELECT * FROM pgr_dijkstra( 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 (6 rows) -SELECT * FROM pgr_dijkstra( - 'SELECT id, source, target, cost, reverse_cost FROM edge_table', - 'SELECT * FROM (VALUES(2, 3)) AS combinations (source, target)' -); - seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost ------+----------+-----------+---------+------+------+------+---------- - 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 - 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 - 3 | 3 | 2 | 3 | 6 | 9 | 1 | 2 - 4 | 4 | 2 | 3 | 9 | 16 | 1 | 3 - 5 | 5 | 2 | 3 | 4 | 3 | 1 | 4 - 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 -(6 rows) - -- q16 -- q17 SELECT * FROM pgr_dijkstra( @@ -673,19 +585,24 @@ SELECT * FROM pgr_dijkstra( 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 (2 rows) +-- q18 +-- q19 SELECT * FROM pgr_dijkstra( 'SELECT id, source, target, cost, reverse_cost FROM edge_table', - 'SELECT * FROM (VALUES(2, 3), (11, 5)) AS combinations (source, target)', FALSE + 'SELECT * FROM combinations_table LIMIT 3', + FALSE ); seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost -----+----------+-----------+---------+------+------+------+---------- - 1 | 1 | 2 | 3 | 2 | 2 | 1 | 0 - 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 - 3 | 1 | 11 | 5 | 11 | 11 | 1 | 0 - 4 | 2 | 11 | 5 | 6 | 8 | 1 | 1 - 5 | 3 | 11 | 5 | 5 | -1 | 0 | 2 -(5 rows) + 1 | 1 | 1 | 2 | 1 | 1 | 1 | 0 + 2 | 2 | 1 | 2 | 2 | -1 | 0 | 1 + 3 | 1 | 2 | 12 | 2 | 4 | 1 | 0 + 4 | 2 | 2 | 12 | 5 | 10 | 1 | 1 + 5 | 3 | 2 | 12 | 10 | 12 | 1 | 2 + 6 | 4 | 2 | 12 | 11 | 13 | 1 | 3 + 7 | 5 | 2 | 12 | 12 | -1 | 0 | 4 +(7 rows) --- q18 +-- q20 ROLLBACK; ROLLBACK diff --git a/docqueries/dijkstra/doc-pgr_dijkstra.test.sql b/docqueries/dijkstra/doc-pgr_dijkstra.test.sql index 18adfb3759e..d17a1dbeec8 100644 --- a/docqueries/dijkstra/doc-pgr_dijkstra.test.sql +++ b/docqueries/dijkstra/doc-pgr_dijkstra.test.sql @@ -250,3 +250,17 @@ SELECT * FROM pgr_dijkstra( ); \echo -- q18 + +-- pgr_dijkstra combinations SQL +------------------------------------------------------------------------------- + +\echo -- q19 + +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM combinations_table LIMIT 3', + FALSE +); + +\echo -- q20 + From 057294121ab1a0f0734d6a1ac9095d7a95343e9c Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Sun, 31 May 2020 18:34:47 +0200 Subject: [PATCH 28/33] completing the documentation of dijkstra combinations SQL --- doc/dijkstra/pgr_dijkstra.rst | 9 +- docqueries/dijkstra/doc-pgr_dijkstra.result | 119 ++++++++++++++++-- docqueries/dijkstra/doc-pgr_dijkstra.test.sql | 31 ++++- tools/testers/sampledata.sql | 16 +-- 4 files changed, 147 insertions(+), 28 deletions(-) diff --git a/doc/dijkstra/pgr_dijkstra.rst b/doc/dijkstra/pgr_dijkstra.rst index bd228cec5fa..02ae93154df 100644 --- a/doc/dijkstra/pgr_dijkstra.rst +++ b/doc/dijkstra/pgr_dijkstra.rst @@ -20,7 +20,7 @@ In particular, the Dijkstra algorithm implemented by Boost.Graph. .. rubric:: Availability -* Version 3.1.? +* Version 3.1.0 * New **Proposed** functions: @@ -208,8 +208,8 @@ Combinations SQL .. literalinclude:: doc-pgr_dijkstra.queries - :start-after: -- q6 - :end-before: -- q7 + :start-after: -- q19 + :end-before: -- q20 Parameters ------------------------------------------------------------------------------- @@ -240,9 +240,6 @@ Inner query :start-after: basic_edges_sql_start :end-before: basic_edges_sql_end -Combinations query -------------------------------------------------------------------------------- - .. rubric::combinations_sql .. include:: pgRouting-concepts.rst diff --git a/docqueries/dijkstra/doc-pgr_dijkstra.result b/docqueries/dijkstra/doc-pgr_dijkstra.result index 07846c14593..bd70b2b2106 100644 --- a/docqueries/dijkstra/doc-pgr_dijkstra.result +++ b/docqueries/dijkstra/doc-pgr_dijkstra.result @@ -190,6 +190,32 @@ SELECT * FROM pgr_dijkstra( 18 | 5 | 11 | 5 | 5 | -1 | 0 | 4 (18 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES (2, 3), (2, 5), (11, 3), (11, 5)) AS combinations (source, target)' +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 + 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 + 3 | 3 | 2 | 3 | 6 | 9 | 1 | 2 + 4 | 4 | 2 | 3 | 9 | 16 | 1 | 3 + 5 | 5 | 2 | 3 | 4 | 3 | 1 | 4 + 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 + 7 | 1 | 2 | 5 | 2 | 4 | 1 | 0 + 8 | 2 | 2 | 5 | 5 | -1 | 0 | 1 + 9 | 1 | 11 | 3 | 11 | 13 | 1 | 0 + 10 | 2 | 11 | 3 | 12 | 15 | 1 | 1 + 11 | 3 | 11 | 3 | 9 | 16 | 1 | 2 + 12 | 4 | 11 | 3 | 4 | 3 | 1 | 3 + 13 | 5 | 11 | 3 | 3 | -1 | 0 | 4 + 14 | 1 | 11 | 5 | 11 | 13 | 1 | 0 + 15 | 2 | 11 | 5 | 12 | 15 | 1 | 1 + 16 | 3 | 11 | 5 | 9 | 9 | 1 | 2 + 17 | 4 | 11 | 5 | 6 | 8 | 1 | 3 + 18 | 5 | 11 | 5 | 5 | -1 | 0 | 4 +(18 rows) + -- q8 -- q9 SELECT * FROM pgr_dijkstra( @@ -284,6 +310,25 @@ SELECT * FROM pgr_dijkstra( 10 | 3 | 11 | 5 | 5 | -1 | 0 | 2 (10 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES (2, 3), (2, 5), (11, 3), (11, 5)) AS combinations (source, target)', + FALSE +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 2 | 1 | 0 + 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 + 3 | 1 | 2 | 5 | 2 | 4 | 1 | 0 + 4 | 2 | 2 | 5 | 5 | -1 | 0 | 1 + 5 | 1 | 11 | 3 | 11 | 11 | 1 | 0 + 6 | 2 | 11 | 3 | 6 | 5 | 1 | 1 + 7 | 3 | 11 | 3 | 3 | -1 | 0 | 2 + 8 | 1 | 11 | 5 | 11 | 11 | 1 | 0 + 9 | 2 | 11 | 5 | 6 | 8 | 1 | 1 + 10 | 3 | 11 | 5 | 5 | -1 | 0 | 2 +(10 rows) + -- q10 -- q11 SELECT * FROM pgr_dijkstra( @@ -350,6 +395,16 @@ SELECT * FROM pgr_dijkstra( 2 | 2 | 2 | 5 | 5 | -1 | 0 | 1 (2 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost FROM edge_table', + 'SELECT * FROM (VALUES (2, 3), (2, 5), (11, 3), (11, 5)) AS combinations (source, target)' +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 5 | 2 | 4 | 1 | 0 + 2 | 2 | 2 | 5 | 5 | -1 | 0 | 1 +(2 rows) + -- q12 -- q13 SELECT * FROM pgr_dijkstra( @@ -450,6 +505,27 @@ SELECT * FROM pgr_dijkstra( 12 | 3 | 11 | 5 | 5 | -1 | 0 | 2 (12 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost FROM edge_table', + 'SELECT * FROM (VALUES (2, 3), (2, 5), (11, 3), (11, 5)) AS combinations (source, target)', + FALSE +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 + 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 + 3 | 3 | 2 | 3 | 6 | 5 | 1 | 2 + 4 | 4 | 2 | 3 | 3 | -1 | 0 | 3 + 5 | 1 | 2 | 5 | 2 | 4 | 1 | 0 + 6 | 2 | 2 | 5 | 5 | -1 | 0 | 1 + 7 | 1 | 11 | 3 | 11 | 11 | 1 | 0 + 8 | 2 | 11 | 3 | 6 | 5 | 1 | 1 + 9 | 3 | 11 | 3 | 3 | -1 | 0 | 2 + 10 | 1 | 11 | 5 | 11 | 11 | 1 | 0 + 11 | 2 | 11 | 5 | 6 | 8 | 1 | 1 + 12 | 3 | 11 | 5 | 5 | -1 | 0 | 2 +(12 rows) + -- q14 -- q15 SELECT * FROM pgr_dijkstra( @@ -539,6 +615,20 @@ SELECT * FROM pgr_dijkstra( 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 (6 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3)) AS combinations (source, target)' +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 4 | 1 | 0 + 2 | 2 | 2 | 3 | 5 | 8 | 1 | 1 + 3 | 3 | 2 | 3 | 6 | 9 | 1 | 2 + 4 | 4 | 2 | 3 | 9 | 16 | 1 | 3 + 5 | 5 | 2 | 3 | 4 | 3 | 1 | 4 + 6 | 6 | 2 | 3 | 3 | -1 | 0 | 5 +(6 rows) + -- q16 -- q17 SELECT * FROM pgr_dijkstra( @@ -585,23 +675,38 @@ SELECT * FROM pgr_dijkstra( 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 (2 rows) +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3)) AS combinations (source, target)', + FALSE +); + seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost +-----+----------+-----------+---------+------+------+------+---------- + 1 | 1 | 2 | 3 | 2 | 2 | 1 | 0 + 2 | 2 | 2 | 3 | 3 | -1 | 0 | 1 +(2 rows) + -- q18 -- q19 SELECT * FROM pgr_dijkstra( 'SELECT id, source, target, cost, reverse_cost FROM edge_table', - 'SELECT * FROM combinations_table LIMIT 3', + 'SELECT * FROM combinations_table', FALSE ); seq | path_seq | start_vid | end_vid | node | edge | cost | agg_cost -----+----------+-----------+---------+------+------+------+---------- 1 | 1 | 1 | 2 | 1 | 1 | 1 | 0 2 | 2 | 1 | 2 | 2 | -1 | 0 | 1 - 3 | 1 | 2 | 12 | 2 | 4 | 1 | 0 - 4 | 2 | 2 | 12 | 5 | 10 | 1 | 1 - 5 | 3 | 2 | 12 | 10 | 12 | 1 | 2 - 6 | 4 | 2 | 12 | 11 | 13 | 1 | 3 - 7 | 5 | 2 | 12 | 12 | -1 | 0 | 4 -(7 rows) + 3 | 1 | 1 | 4 | 1 | 1 | 1 | 0 + 4 | 2 | 1 | 4 | 2 | 2 | 1 | 1 + 5 | 3 | 1 | 4 | 3 | 3 | 1 | 2 + 6 | 4 | 1 | 4 | 4 | -1 | 0 | 3 + 7 | 1 | 2 | 1 | 2 | 1 | 1 | 0 + 8 | 2 | 2 | 1 | 1 | -1 | 0 | 1 + 9 | 1 | 2 | 4 | 2 | 2 | 1 | 0 + 10 | 2 | 2 | 4 | 3 | 3 | 1 | 1 + 11 | 3 | 2 | 4 | 4 | -1 | 0 | 2 +(11 rows) -- q20 ROLLBACK; diff --git a/docqueries/dijkstra/doc-pgr_dijkstra.test.sql b/docqueries/dijkstra/doc-pgr_dijkstra.test.sql index d17a1dbeec8..2e8be245953 100644 --- a/docqueries/dijkstra/doc-pgr_dijkstra.test.sql +++ b/docqueries/dijkstra/doc-pgr_dijkstra.test.sql @@ -62,6 +62,10 @@ SELECT * FROM pgr_dijkstra( 'SELECT id, source, target, cost, reverse_cost FROM edge_table', ARRAY[2, 11], ARRAY[3,5] ); +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES (2, 3), (2, 5), (11, 3), (11, 5)) AS combinations (source, target)' +); \echo -- q8 @@ -105,6 +109,11 @@ SELECT * FROM pgr_dijkstra( ARRAY[2, 11], ARRAY[3,5], FALSE ); +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES (2, 3), (2, 5), (11, 3), (11, 5)) AS combinations (source, target)', + FALSE +); \echo -- q10 @@ -141,6 +150,10 @@ SELECT * FROM pgr_dijkstra( 'SELECT id, source, target, cost FROM edge_table', ARRAY[2, 11], ARRAY[3,5] ); +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost FROM edge_table', + 'SELECT * FROM (VALUES (2, 3), (2, 5), (11, 3), (11, 5)) AS combinations (source, target)' +); \echo -- q12 @@ -183,6 +196,11 @@ SELECT * FROM pgr_dijkstra( ARRAY[2, 11], ARRAY[3,5], FALSE ); +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost FROM edge_table', + 'SELECT * FROM (VALUES (2, 3), (2, 5), (11, 3), (11, 5)) AS combinations (source, target)', + FALSE +); \echo -- q14 @@ -219,7 +237,10 @@ SELECT * FROM pgr_dijkstra( 'SELECT id, source, target, cost, reverse_cost FROM edge_table', ARRAY[2], ARRAY[3] ); - +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3)) AS combinations (source, target)' +); \echo -- q16 @@ -248,7 +269,11 @@ SELECT * FROM pgr_dijkstra( ARRAY[2], ARRAY[3], FALSE ); - +SELECT * FROM pgr_dijkstra( + 'SELECT id, source, target, cost, reverse_cost FROM edge_table', + 'SELECT * FROM (VALUES(2, 3)) AS combinations (source, target)', + FALSE +); \echo -- q18 -- pgr_dijkstra combinations SQL @@ -258,7 +283,7 @@ SELECT * FROM pgr_dijkstra( SELECT * FROM pgr_dijkstra( 'SELECT id, source, target, cost, reverse_cost FROM edge_table', - 'SELECT * FROM combinations_table LIMIT 3', + 'SELECT * FROM combinations_table', FALSE ); diff --git a/tools/testers/sampledata.sql b/tools/testers/sampledata.sql index 13b16395d58..2eeb83e8686 100644 --- a/tools/testers/sampledata.sql +++ b/tools/testers/sampledata.sql @@ -207,17 +207,9 @@ CREATE TABLE combinations_table ( INSERT INTO combinations_table ( source, target) VALUES (1, 2), -(1, 17), -(2, 12), +(1, 4), +(2, 1), (2, 4), -(6, 13), -(13, 6), -(10, 4), -(4, 10), -(15, 4), -(6, 11), -(11,6), -(9, 12), -(12, 9), -(7, 17); +(2, 17); + --COMBINATIONS CREATE end From bb458861941962aca38e359bdd9ea6bdd188b3e4 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Sun, 31 May 2020 19:07:55 +0200 Subject: [PATCH 29/33] fixing the documentation of dijkstra combinations SQL --- NEWS | 8 ++++++++ doc/dijkstra/pgr_dijkstra.rst | 3 +++ doc/src/pgRouting-concepts.rst | 14 ++++++++++++-- sql/sigs/pgrouting--3.1.0.sig | 2 ++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 7926cb298c2..dd9d7184372 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,12 @@ +pgRouting 3.1.0 Release Notes +------------------------------------------------------------------------------- + +*New functions* + +* pgr_dijkstra(combinations) + + pgRouting 3.0.0 Release Notes ------------------------------------------------------------------------------- diff --git a/doc/dijkstra/pgr_dijkstra.rst b/doc/dijkstra/pgr_dijkstra.rst index 02ae93154df..b131d242df3 100644 --- a/doc/dijkstra/pgr_dijkstra.rst +++ b/doc/dijkstra/pgr_dijkstra.rst @@ -195,6 +195,9 @@ Many to Many :start-after: -- q5 :end-before: -- q6 +.. index:: + single: dijkstra(Combinations) + Combinations SQL ............................................................................... diff --git a/doc/src/pgRouting-concepts.rst b/doc/src/pgRouting-concepts.rst index 60f06f3dbf2..38d79f8c8fb 100644 --- a/doc/src/pgRouting-concepts.rst +++ b/doc/src/pgRouting-concepts.rst @@ -143,6 +143,7 @@ Across this documentation, to indicate which overload we use the following terms * `One to Many`_ * `Many to One`_ * `Many to Many`_ +* `Combinations SQL`_ Depending on the overload are the parameters used, keeping consistency across all functions. @@ -179,6 +180,15 @@ When routing from: * From **many** starting vertices * to **many** ending vertices +Combinations SQL +............................................................................... + +When routing from: + +* From **many** different starting vertices +* to **many** different ending vertices +* Every tuple specifies a pair of a start vertex and an end vertex +* Users can define the combinations as desired. @@ -446,7 +456,7 @@ Column Type Description * `Many to One`_ * `Many to Many`_ - * `Combinations Query`_ + * `Combinations SQL`_ **end_vid** ``BIGINT`` Identifier of the ending vertex. @@ -454,7 +464,7 @@ Column Type Description * `One to Many`_ * `Many to Many`_ - * `Combinations Query`_ + * `Combinations SQL`_ **node** ``BIGINT`` Identifier of the node in the path from ``start_vid`` to ``end_vid``. **edge** ``BIGINT`` Identifier of the edge used to go from ``node`` to the next node in the path sequence. ``-1`` for the last node of the path. diff --git a/sql/sigs/pgrouting--3.1.0.sig b/sql/sigs/pgrouting--3.1.0.sig index 8022f36a1bc..48df5a42eea 100644 --- a/sql/sigs/pgrouting--3.1.0.sig +++ b/sql/sigs/pgrouting--3.1.0.sig @@ -93,6 +93,8 @@ _pgr_dijkstra(text,anyarray,anyarray,boolean,boolean,boolean,bigint) pgr_dijkstra(text,anyarray,bigint,boolean) pgr_dijkstra(text,bigint,anyarray,boolean) pgr_dijkstra(text,bigint,bigint,boolean) +pgr_dijkstra(text,text,boolean) +_pgr_dijkstra(text,text,boolean,boolean,boolean) _pgr_dijkstravia(text,anyarray,boolean,boolean,boolean) pgr_dijkstravia(text,anyarray,boolean,boolean,boolean) _pgr_drivingdistance(text,anyarray,double precision,boolean,boolean) From d2936e3edb46e3cd90e1fd9bd7e4d55e4a6288ad Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Sun, 31 May 2020 19:19:33 +0200 Subject: [PATCH 30/33] constinue fixing the documentation of dijkstra combinations SQL --- doc/src/pgRouting-concepts.rst | 2 -- 1 file changed, 2 deletions(-) diff --git a/doc/src/pgRouting-concepts.rst b/doc/src/pgRouting-concepts.rst index 38d79f8c8fb..10a10477a21 100644 --- a/doc/src/pgRouting-concepts.rst +++ b/doc/src/pgRouting-concepts.rst @@ -456,7 +456,6 @@ Column Type Description * `Many to One`_ * `Many to Many`_ - * `Combinations SQL`_ **end_vid** ``BIGINT`` Identifier of the ending vertex. @@ -464,7 +463,6 @@ Column Type Description * `One to Many`_ * `Many to Many`_ - * `Combinations SQL`_ **node** ``BIGINT`` Identifier of the node in the path from ``start_vid`` to ``end_vid``. **edge** ``BIGINT`` Identifier of the edge used to go from ``node`` to the next node in the path sequence. ``-1`` for the last node of the path. From fbcc7bf35024f84c0ce0fdb20535a6832f080d15 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Sun, 31 May 2020 22:14:34 +0200 Subject: [PATCH 31/33] fixing code style as per code_checker.sh --- src/dijkstra/dijkstra.c | 10 ++++------ src/dijkstra/dijkstra_driver.cpp | 6 ++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/dijkstra/dijkstra.c b/src/dijkstra/dijkstra.c index 2a2f04291c1..59874f94e89 100644 --- a/src/dijkstra/dijkstra.c +++ b/src/dijkstra/dijkstra.c @@ -166,12 +166,12 @@ process_combinations( pgr_get_edges_reversed(edges_sql, &edges, &total_edges); } - if (total_edges == 0 ) { + if (total_edges == 0) { pgr_SPI_finish(); return; } else { pgr_get_combinations(combinations_sql, &combinations, &total_combinations); - if(total_combinations == 0){ + if (total_combinations == 0) { if (edges) pfree(edges); pgr_SPI_finish(); return; @@ -238,7 +238,7 @@ _pgr_dijkstra(PG_FUNCTION_ARGS) { funcctx = SRF_FIRSTCALL_INIT(); oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx); - if(PG_NARGS() == 7) { + if (PG_NARGS() == 7) { /**********************************************************************/ // pgr_dijkstra( // sql TEXT, @@ -260,8 +260,7 @@ _pgr_dijkstra(PG_FUNCTION_ARGS) { &result_count); /**********************************************************************/ - } - else if(PG_NARGS() == 5){ + } else if (PG_NARGS() == 5) { /**********************************************************************/ // pgr_dijkstra( // edge_sql TEXT, @@ -279,7 +278,6 @@ _pgr_dijkstra(PG_FUNCTION_ARGS) { &result_count); /**********************************************************************/ - } #if PGSQL_VERSION > 95 funcctx->max_calls = result_count; diff --git a/src/dijkstra/dijkstra_driver.cpp b/src/dijkstra/dijkstra_driver.cpp index d5cade99532..d3a4b6bf09a 100644 --- a/src/dijkstra/dijkstra_driver.cpp +++ b/src/dijkstra/dijkstra_driver.cpp @@ -32,13 +32,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ #include "drivers/dijkstra/dijkstra_driver.h" +#include #include #include #include #include #include -#include #include "dijkstra/pgr_dijkstra.hpp" @@ -86,7 +86,6 @@ pgr_dijkstra( std::vector < pgr_combination_t > &combinations, bool only_cost, bool normal) { - pgrouting::Pgr_dijkstra< G > fn_dijkstra; auto paths = fn_dijkstra.dijkstra( graph, @@ -99,7 +98,6 @@ pgr_dijkstra( } } return paths; - } @@ -269,7 +267,7 @@ do_pgr_combinations_dijkstra( combinations_vector, only_cost, normal); } - combinations_vector.clear(); + combinations_vector.clear(); size_t count(0); count = count_tuples(paths); From ba60ec37432d5b9aa559289be993b4da92384795 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Mon, 1 Jun 2020 00:16:30 +0200 Subject: [PATCH 32/33] adding pgtap tests for pgr_dijkstra combinations sql --- ...a_empty_combinations_empty_result.test.sql | 26 +++++++++ .../manyToMany_equiv_combinations.test.sql | 58 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 pgtap/dijkstra/dijkstra_empty_combinations_empty_result.test.sql create mode 100644 pgtap/dijkstra/manyToMany_equiv_combinations.test.sql diff --git a/pgtap/dijkstra/dijkstra_empty_combinations_empty_result.test.sql b/pgtap/dijkstra/dijkstra_empty_combinations_empty_result.test.sql new file mode 100644 index 00000000000..1769c23ef5e --- /dev/null +++ b/pgtap/dijkstra/dijkstra_empty_combinations_empty_result.test.sql @@ -0,0 +1,26 @@ + +\i setup.sql + +SELECT plan(1); + +create or REPLACE FUNCTION foo() +RETURNS SETOF TEXT AS +$BODY$ +BEGIN + + RETURN query SELECT is_empty( + 'SELECT path_seq, start_vid, end_vid, node, edge, cost, agg_cost FROM pgr_dijkstra( + ''SELECT id, source, target, cost, reverse_cost FROM edge_table'', + ''SELECT * FROM combinations_table WHERE source=-1'' ) ' + ); + RETURN; +END +$BODY$ +language plpgsql; + +select * from foo(); + +-- Finish the tests and clean up. +SELECT * FROM finish(); +ROLLBACK; + diff --git a/pgtap/dijkstra/manyToMany_equiv_combinations.test.sql b/pgtap/dijkstra/manyToMany_equiv_combinations.test.sql new file mode 100644 index 00000000000..f9a9929c8a6 --- /dev/null +++ b/pgtap/dijkstra/manyToMany_equiv_combinations.test.sql @@ -0,0 +1,58 @@ + +\i setup.sql + +SELECT plan(1); + +UPDATE edge_table SET cost = cost + 0.001 * id * id, reverse_cost = reverse_cost + 0.001 * id * id; + +create or REPLACE FUNCTION foo(cant INTEGER default 18 ) +RETURNS SETOF TEXT AS +$BODY$ +DECLARE +sql_Combinations TEXT; +sql_Many TEXT; +BEGIN + + sql_Combinations := ''; + sql_Many := ''; + FOR i IN 1.. cant LOOP + IF (i > 1) THEN + sql_Many := sql_Many ||', '; + END IF; + sql_Many := sql_Many || i ; + END LOOP; + + FOR i IN 1.. cant LOOP + FOR j IN 1..cant LOOP + IF NOT (i = j) THEN + sql_Combinations := sql_Combinations || '(' || i || ',' || j || '),' ; + END IF; + END LOOP; + END LOOP; + sql_Combinations := trim(trailing ',' from sql_Combinations); + + sql_Many := + ' SELECT path_seq, start_vid, end_vid, node, edge, cost, agg_cost FROM pgr_dijkstra( + ''SELECT id, source, target, cost, reverse_cost FROM edge_table'', ' + || ' ARRAY[' || sql_Many ||'], ARRAY[' || sql_Many || + '] ) '; + + sql_Combinations := + ' SELECT path_seq, start_vid, end_vid, node, edge, cost, agg_cost FROM pgr_dijkstra( + ''SELECT id, source, target, cost, reverse_cost FROM edge_table'', + ''SELECT * FROM (VALUES' || sql_Combinations ||') AS combinations (source, target)'' ) '; + + RETURN query SELECT set_eq( sql_Many, sql_Combinations ); + RETURN; +END +$BODY$ +language plpgsql; + + +select * from foo(); + + +-- Finish the tests and clean up. +SELECT * FROM finish(); +ROLLBACK; + From 14762d1e86036abae69ded750d23c2c7dd11ee59 Mon Sep 17 00:00:00 2001 From: mahmsakr Date: Mon, 1 Jun 2020 17:04:54 +0200 Subject: [PATCH 33/33] updating news, and fixing a documentation typo --- NEWS | 2 +- doc/dijkstra/pgr_dijkstra.rst | 2 +- doc/src/release_notes.rst | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index dd9d7184372..143bdae7e19 100644 --- a/NEWS +++ b/NEWS @@ -2,7 +2,7 @@ pgRouting 3.1.0 Release Notes ------------------------------------------------------------------------------- -*New functions* +*New proposed functions* * pgr_dijkstra(combinations) diff --git a/doc/dijkstra/pgr_dijkstra.rst b/doc/dijkstra/pgr_dijkstra.rst index b131d242df3..1d3206a6205 100644 --- a/doc/dijkstra/pgr_dijkstra.rst +++ b/doc/dijkstra/pgr_dijkstra.rst @@ -24,7 +24,7 @@ In particular, the Dijkstra algorithm implemented by Boost.Graph. * New **Proposed** functions: - * pgr_bdDijkstra(combinations sql) + * pgr_dijkstra(combinations sql) * Version 3.0.0 diff --git a/doc/src/release_notes.rst b/doc/src/release_notes.rst index be18575a636..ee9bfa31770 100644 --- a/doc/src/release_notes.rst +++ b/doc/src/release_notes.rst @@ -53,7 +53,7 @@ To see the full list of changes check the list of `Git commits