skip to Main Content

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


  1. Something like this would work for generating a random probability:

    float random_prob = (float) (rand() % 100) / 100.0;
    printf ("%.2fn", random_prob);
    

    I didn’t find the function PG_GETARG_FLOAT4 declaration, but you could use the function DatumGetFloat4(arg) to retrieve the float argument, like this:

    set_prob = DatumGetFloat4(PG_GETARG_DATUM(2));
    

    You could also try this:

    float prob = (float) PG_GETARG_FLOAT8(2);
    
    Login or Signup to reply.
  2. Try using PG_GETARG_FLOAT8 in place of of PG_GETARG_FLOAT4 for the retrieval. Modify the code code like this:

    float 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_FLOAT8(2);
    

    Also, for the random probability, try %f in place of %.2f. The update is as folows:

    printf("%fn", random_prob);
    

    Hope this will help!!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search