33 #include "utils/array.h"
53 char *combinations_sql,
60 size_t *result_count) {
65 size_t size_start_vidsArr = 0;
66 int64_t* start_vidsArr = NULL;
68 size_t size_end_vidsArr = 0;
69 int64_t* end_vidsArr = NULL;
71 size_t total_combinations = 0;
75 start_vidsArr = (int64_t*)
77 end_vidsArr = (int64_t*)
79 }
else if (combinations_sql) {
81 if (total_combinations == 0) {
89 (*result_tuples) = NULL;
94 size_t total_positive_edges = 0;
96 pgr_get_edges(edges_sql, &positive_edges, &total_positive_edges);
98 "Total positive weighted edges in query: %ld",
99 total_positive_edges);
102 size_t total_negative_edges = 0;
104 pgr_get_edges(neg_edges_sql, &negative_edges, &total_negative_edges);
106 "Total negative weighted edges in query: %ld",
107 total_negative_edges);
109 size_t total_edges = total_positive_edges + total_negative_edges;
111 if (total_edges == 0) {
112 if (end_vidsArr) pfree(end_vidsArr);
113 if (start_vidsArr) pfree(start_vidsArr);
118 PGR_DBG(
"Starting processing");
119 clock_t start_t = clock();
120 char *log_msg = NULL;
121 char *notice_msg = NULL;
122 char *err_msg = NULL;
125 total_positive_edges,
127 total_negative_edges,
144 time_msg(
" processing pgr_bellman_ford", start_t, clock());
145 PGR_DBG(
"Returning %ld tuples", *result_count);
148 if (*result_tuples) pfree(*result_tuples);
153 if (positive_edges) pfree(positive_edges);
154 if (negative_edges) pfree(negative_edges);
155 if (log_msg) pfree(log_msg);
156 if (notice_msg) pfree(notice_msg);
157 if (err_msg) pfree(err_msg);
159 if (end_vidsArr) pfree(end_vidsArr);
160 if (start_vidsArr) pfree(start_vidsArr);
166 FuncCallContext *funcctx;
167 TupleDesc tuple_desc;
171 size_t result_count = 0;
174 if (SRF_IS_FIRSTCALL()) {
175 MemoryContext oldcontext;
176 funcctx = SRF_FIRSTCALL_INIT();
177 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
181 if (PG_NARGS() == 6) {
186 text_to_cstring(PG_GETARG_TEXT_P(0)),
187 text_to_cstring(PG_GETARG_TEXT_P(1)),
189 PG_GETARG_ARRAYTYPE_P(2),
190 PG_GETARG_ARRAYTYPE_P(3),
195 }
else if (PG_NARGS() == 5) {
200 text_to_cstring(PG_GETARG_TEXT_P(0)),
201 text_to_cstring(PG_GETARG_TEXT_P(1)),
202 text_to_cstring(PG_GETARG_TEXT_P(2)),
214 #if PGSQL_VERSION > 95
215 funcctx->max_calls = result_count;
217 funcctx->max_calls = (uint32_t)result_count;
219 funcctx->user_fctx = result_tuples;
220 if (get_call_result_type(fcinfo, NULL, &tuple_desc)
221 != TYPEFUNC_COMPOSITE) {
223 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
224 errmsg(
"function returning record called in context "
225 "that cannot accept type record")));
228 funcctx->tuple_desc = tuple_desc;
229 MemoryContextSwitchTo(oldcontext);
232 funcctx = SRF_PERCALL_SETUP();
233 tuple_desc = funcctx->tuple_desc;
236 if (funcctx->call_cntr < funcctx->max_calls) {
255 values = palloc(numb *
sizeof(Datum));
256 nulls = palloc(numb *
sizeof(
bool));
260 for (i = 0; i < numb; ++i) {
264 values[0] = Int32GetDatum(funcctx->call_cntr + 1);
265 values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].
seq);
266 values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].
start_id);
267 values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].
end_id);
268 values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].
node);
269 values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].
edge);
270 values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].
cost);
271 values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].
agg_cost);
275 tuple = heap_form_tuple(tuple_desc, values, nulls);
276 result = HeapTupleGetDatum(tuple);
277 SRF_RETURN_NEXT(funcctx, result);
285 SRF_RETURN_DONE(funcctx);