I have written a function for Apache AGE to create a specific kind of graph where the creation of edges is based on a probability, which is one argument of the function. The problem is that, when calling PG_GETARG_FLOAT4
it always returns 0
instead of the passed floating point number. I’m using GDB to help me debug it and printing set_prob
shows:
/* --------- Apache AGE Code --------*/
float4 set_prob;
/* (more code in between) */
/* Get the probability for each edge to exist. */
if (PG_ARGISNULL(2))
{
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Probability cannot be NULL.")));
}
set_prob = PG_GETARG_FLOAT4(2);
/* -------------- GDB -------------- */
p set_prob
0
Also, generating a random probability also return 0
.
/* --------- Apache AGE Code --------*/
/* Generate a random float number between 0 and 1. */
random_prob = (float) ((rand() % 100) / 100);
/* -------------- GDB -------------- */
printf "%.2fn", random_prob
0.00
This is how the created function declared and called:
CREATE FUNCTION ag_catalog.age_create_erdos_renyi_graph(graph_name name,
n int,
p float,
vertex_label_name name = NULL,
edge_label_name name = NULL,
bidirectional bool = true)
RETURNS void
LANGUAGE c
CALLED ON NULL INPUT
PARALLEL SAFE
AS 'MODULE_PATHNAME';
-- How I'm calling it.
SELECT ag_catalog.age_create_erdos_renyi_graph('test', 5, 0.5, NULL, NULL, NULL);
What is the proper way to retrieve the float argument and to generate the random number in this case?
2
Answers
Something like this would work for generating a random probability:
I didn’t find the function
PG_GETARG_FLOAT4
declaration, but you could use the functionDatumGetFloat4(arg)
to retrieve the float argument, like this:You could also try this:
Try using PG_GETARG_FLOAT8 in place of of PG_GETARG_FLOAT4 for the retrieval. Modify the code code like this:
Also, for the random probability, try %f in place of %.2f. The update is as folows:
Hope this will help!!