Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions include/cpp_common/get_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,53 @@ std::vector<Data_type> get_data(

while (moredata == true) {
SPI_cursor_fetch(SPIportal, true, tuple_limit);
long fetched = SPI_processed;
auto tuptable = SPI_tuptable;
auto tupdesc = SPI_tuptable->tupdesc;
if (total_tuples == 0) fetch_column_info(tupdesc, info);
auto tupdesc = tuptable ? tuptable->tupdesc : nullptr;

if (fetched <= 0) {
/*
* Zero-row batch: validate schema when a descriptor is
* available so typos still raise a clean error, then exit
* with whatever was collected so far (possibly empty).
*/
if (tupdesc && total_tuples == 0) {
try {
fetch_column_info(tupdesc, info);
} catch (...) {
if (tuptable) SPI_freetuptable(tuptable);
SPI_cursor_close(SPIportal);
throw;
}
}
if (tuptable) SPI_freetuptable(tuptable);
moredata = false;
break;
}

size_t ntuples = SPI_processed;
total_tuples += ntuples;
if (!tuptable || !tupdesc) {
if (tuptable) SPI_freetuptable(tuptable);
moredata = false;
break;
}

try {
if (total_tuples == 0) fetch_column_info(tupdesc, info);

size_t ntuples = static_cast<size_t>(fetched);
total_tuples += ntuples;

if (ntuples > 0) {
tuples.reserve(total_tuples);
for (size_t t = 0; t < ntuples; t++) {
tuples.push_back(func(tuptable->vals[t], tupdesc, info,
&default_id,
&valid_pgtuples, flag));
}
SPI_freetuptable(tuptable);
} else {
moredata = false;
} catch (...) {
SPI_freetuptable(tuptable);
SPI_cursor_close(SPIportal);
throw;
}
}

Expand Down
9 changes: 8 additions & 1 deletion pgtap/dijkstra/dijkstra/no_crash_test.pg
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ BEGIN;


UPDATE edges SET cost = sign(cost), reverse_cost = sign(reverse_cost);
SELECT CASE WHEN min_version('3.1.0') THEN plan(82) ELSE plan(69) END;
SELECT CASE WHEN min_version('3.1.0') THEN plan(84) ELSE plan(71) END;

SELECT no_crash_dijkstra('pgr_dijkstra');
SELECT throw_on_empty_edges_sql('pgr_dijkstra', ',1,2');
SELECT is_empty(
$$SELECT * FROM pgr_dijkstra('SELECT id, source, target, cost FROM edges WHERE false', 1, 2)$$,
'zero-row edges_sql returns empty set without crashing');
SELECT throws_ok(
$$SELECT * FROM pgr_dijkstra('SELECT id, source FROM edges WHERE false', 1, 2)$$,
'XX000', 'Column ''cost'' not Found',
'zero-row edges_sql with missing column raises a clean error');

SELECT finish();

Expand Down