Skip to content

Commit

Permalink
Bug fix: src == dst search result is always 0
Browse files Browse the repository at this point in the history
  • Loading branch information
SiberiaWolfP committed Feb 20, 2024
1 parent 9ef7574 commit bf84fe3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
9 changes: 7 additions & 2 deletions duckpgq/src/duckpgq/functions/scalar/iterativelength.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 12 additions & 8 deletions duckpgq/src/duckpgq/functions/scalar/shortest_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector> output =
make_uniq<Vector>(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<Vector> output =
// make_uniq<Vector>(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;
Expand Down
26 changes: 18 additions & 8 deletions test/sql/path-finding/shortest_path.test
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions test/sql/path-finding/shortest_path_bound.test
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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]


1 change: 1 addition & 0 deletions test/sql/path-finding/subpath_match.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit bf84fe3

Please sign in to comment.