From bf84fe3d9f8d7da9872b10ee264b04096e30ad1c Mon Sep 17 00:00:00 2001 From: Pingan Ren Date: Tue, 20 Feb 2024 19:14:25 +0100 Subject: [PATCH] Bug fix: src == dst search result is always 0 --- .../functions/scalar/iterativelength.cpp | 9 +++++-- .../functions/scalar/shortest_path.cpp | 20 ++++++++------ test/sql/path-finding/shortest_path.test | 26 +++++++++++++------ .../sql/path-finding/shortest_path_bound.test | 9 +++++++ test/sql/path-finding/subpath_match.test | 1 + 5 files changed, 47 insertions(+), 18 deletions(-) diff --git a/duckpgq/src/duckpgq/functions/scalar/iterativelength.cpp b/duckpgq/src/duckpgq/functions/scalar/iterativelength.cpp index abbdc124..61f56808 100644 --- a/duckpgq/src/duckpgq/functions/scalar/iterativelength.cpp +++ b/duckpgq/src/duckpgq/functions/scalar/iterativelength.cpp @@ -133,8 +133,13 @@ static void IterativeLengthFunction(DataChunk &args, ExpressionState &state, result_validity.SetInvalid(search_num); result_data[search_num] = (int64_t)-1; /* no path */ } else if (src_data[src_pos] == dst_data[dst_pos]) { - result_data[search_num] = - (int64_t)0; // path of length 0 does not require a search + // result_data[search_num] = + // (int64_t)0; // path of length 0 does not require a search + result_data[search_num] = (int64_t)-1; /* no path */ + visit1[src_data[src_pos]][lane] = true; + lane_to_num[lane] = search_num; // active lane + active++; + break; } else { result_data[search_num] = (int64_t)-1; /* initialize to no path */ seen[src_data[src_pos]][lane] = true; diff --git a/duckpgq/src/duckpgq/functions/scalar/shortest_path.cpp b/duckpgq/src/duckpgq/functions/scalar/shortest_path.cpp index f11df13b..aca4bdab 100644 --- a/duckpgq/src/duckpgq/functions/scalar/shortest_path.cpp +++ b/duckpgq/src/duckpgq/functions/scalar/shortest_path.cpp @@ -161,14 +161,18 @@ static void ShortestPathFunction(DataChunk &args, ExpressionState &state, if (!vdata_src.validity.RowIsValid(src_pos)) { result_validity.SetInvalid(search_num); } else if (src_data[src_pos] == dst_data[dst_pos]) { - unique_ptr output = - make_uniq(LogicalType::LIST(LogicalType::BIGINT)); - ListVector::PushBack(*output, src_data[src_pos]); - ListVector::Append(result, ListVector::GetEntry(*output), - ListVector::GetListSize(*output)); - result_data[search_num].length = ListVector::GetListSize(*output); - result_data[search_num].offset = total_len; - total_len += result_data[search_num].length; + // unique_ptr output = + // make_uniq(LogicalType::LIST(LogicalType::BIGINT)); + // ListVector::PushBack(*output, src_data[src_pos]); + // ListVector::Append(result, ListVector::GetEntry(*output), + // ListVector::GetListSize(*output)); + // result_data[search_num].length = ListVector::GetListSize(*output); + // result_data[search_num].offset = total_len; + // total_len += result_data[search_num].length; + visit1[src_data[src_pos]][lane] = true; + lane_to_num[lane] = search_num; // active lane + active++; + break; } else { visit1[src_data[src_pos]][lane] = true; seen[src_data[src_pos]][lane] = true; diff --git a/test/sql/path-finding/shortest_path.test b/test/sql/path-finding/shortest_path.test index ccfb8572..7bfa70a8 100644 --- a/test/sql/path-finding/shortest_path.test +++ b/test/sql/path-finding/shortest_path.test @@ -50,6 +50,7 @@ query III COLUMNS (element_id(p), a.name as name, b.name as b_name) ) study; ---- +[0, 2, 3, 3, 0] Daniel Daniel [0, 0, 1, 4, 2] Daniel Gabor [0, 1, 2, 6, 3] Daniel Peter @@ -61,18 +62,22 @@ query IIII ) study order by study.name, study.b_name; ---- +2 [0, 2, 3, 3, 0] Daniel Daniel 2 [0, 0, 1, 4, 2] Daniel Gabor 2 [0, 1, 2, 6, 3] Daniel Peter 2 [4, 7, 3, 3, 0] David Daniel 3 [4, 7, 3, 3, 0, 1, 2] David Gabor 3 [4, 7, 3, 3, 0, 0, 1] David Tavneet 2 [2, 6, 3, 3, 0] Gabor Daniel +3 [2, 6, 3, 3, 0, 1, 2] Gabor Gabor 3 [2, 6, 3, 3, 0, 0, 1] Gabor Tavneet 2 [3, 3, 0, 1, 2] Peter Gabor +2 [3, 3, 0, 2, 3] Peter Peter 2 [3, 3, 0, 0, 1] Peter Tavneet 2 [1, 5, 3, 3, 0] Tavneet Daniel 3 [1, 5, 3, 3, 0, 1, 2] Tavneet Gabor 2 [1, 4, 2, 6, 3] Tavneet Peter +3 [1, 5, 3, 3, 0, 0, 1] Tavneet Tavneet statement error @@ -113,17 +118,22 @@ WITH cte1 AS ( JOIN student c on c.id = k.dst ) SELECT shortestpath(0, (select count(*) from student), a.rowid, b.rowid, 2, 3) as path, a.name as a_name, b.name as b_name FROM student a, student b, (select count(cte1.temp) * 0 as temp from cte1) __x - WHERE __x.temp * 0 + iterativelength(0, (select count(*) from student), a.rowid, b.rowid, 2, 3); + WHERE __x.temp * 0 + iterativelength(0, (select count(*) from student), a.rowid, b.rowid, 2, 3) + ORDER BY a.name, b.name; ---- +[0, 2, 3, 3, 0] Daniel Daniel [0, 0, 1, 4, 2] Daniel Gabor [0, 1, 2, 6, 3] Daniel Peter -[1, 5, 3, 3, 0] Tavneet Daniel -[1, 5, 3, 3, 0, 1, 2] Tavneet Gabor -[1, 4, 2, 6, 3] Tavneet Peter +[4, 7, 3, 3, 0] David Daniel +[4, 7, 3, 3, 0, 1, 2] David Gabor +[4, 7, 3, 3, 0, 0, 1] David Tavneet [2, 6, 3, 3, 0] Gabor Daniel +[2, 6, 3, 3, 0, 1, 2] Gabor Gabor [2, 6, 3, 3, 0, 0, 1] Gabor Tavneet -[3, 3, 0, 0, 1] Peter Tavneet [3, 3, 0, 1, 2] Peter Gabor -[4, 7, 3, 3, 0] David Daniel -[4, 7, 3, 3, 0, 0, 1] David Tavneet -[4, 7, 3, 3, 0, 1, 2] David Gabor +[3, 3, 0, 2, 3] Peter Peter +[3, 3, 0, 0, 1] Peter Tavneet +[1, 5, 3, 3, 0] Tavneet Daniel +[1, 5, 3, 3, 0, 1, 2] Tavneet Gabor +[1, 4, 2, 6, 3] Tavneet Peter +[1, 5, 3, 3, 0, 0, 1] Tavneet Tavneet \ No newline at end of file diff --git a/test/sql/path-finding/shortest_path_bound.test b/test/sql/path-finding/shortest_path_bound.test index 6d713239..013f6c50 100644 --- a/test/sql/path-finding/shortest_path_bound.test +++ b/test/sql/path-finding/shortest_path_bound.test @@ -196,6 +196,7 @@ WITH cte1 AS ( FROM Point3 a, Point3 b, (select count(cte1.temp) * 0 as temp from cte1) __x WHERE a.id = 0 and __x.temp * 0 + iterativelength(0, (select count(*) from Point3), a.rowid, b.rowid, 2, 3); ---- +0 0 2 # Graph to test shortest path bound with a cycle # (1) <- (0) <-> (2) @@ -225,7 +226,9 @@ query III COLUMNS (a.id, b.id, vertices(p)) ) tmp; ---- +0 0 [0, 2, 0] 2 1 [2, 0, 1] +2 2 [2, 0, 2] # Description: Test algorithm's capability to ignore isolated nodes. @@ -256,6 +259,8 @@ query III COLUMNS (a.id, b.id, vertices(p)) ) tmp; ---- +0 0 [0, 2, 0] +2 2 [2, 0, 2] # Description: Test shortest paths in a graph with cycles. # Graph Structure: @@ -288,13 +293,17 @@ query III ) tmp order by tmp.id1, tmp.id2; ---- +0 0 [0, 2, 0] 0 1 [0, 2, 3, 1] 0 3 [0, 2, 3] +1 1 [1, 0, 2, 3, 1] 1 2 [1, 0, 2] 1 3 [1, 0, 2, 3] 2 0 [2, 3, 1, 0] 2 1 [2, 3, 1] +2 2 [2, 3, 2] 3 0 [3, 2, 0] 3 2 [3, 1, 0, 2] +3 3 [3, 2, 3] diff --git a/test/sql/path-finding/subpath_match.test b/test/sql/path-finding/subpath_match.test index 1e8d7527..97ae2384 100644 --- a/test/sql/path-finding/subpath_match.test +++ b/test/sql/path-finding/subpath_match.test @@ -110,6 +110,7 @@ FROM GRAPH_TABLE (pg Peter Daniel Peter Tavneet Peter Gabor +Peter Peter statement error -SELECT study.a_name, study.b_name