Richard Herron
Richard Herron

Reputation: 10102

Initialize values in struct in C function used as a SQLite aggregator

I have a program to calculate a weighted product (I adapted from another program I found). But I can't figure out how to initialize the total_data part of the product_state struct to one so that I don't always end up with a product of zero. Right now it's keeping the default zero value so that all of the values I pass in from SQLite get zeroed out.

I took some C++ in college, but never deep into structs, and coming back to this is outside my wheelhouse. Thanks!

/* product.c */

#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1;

#include <stdlib.h>

typedef struct product_state_s {
   double   total_data;  /* sum of (data * weight) values */
   double   total_wt;    /* sum of weight values */
} product_state;

static void product_step( sqlite3_context *ctx, int num_values, sqlite3_value **values )
{
    double         row_wt = 1.0;
    int            type;
    product_state   *st = (product_state*)sqlite3_aggregate_context( ctx,
                                               sizeof( product_state ) );
    if ( st == NULL ) {
        sqlite3_result_error_nomem( ctx );
        return;
    }

    /* Extract weight, if we have a weight and it looks like a number */
    if ( num_values == 2 ) {
        type = sqlite3_value_numeric_type( values[1] );
        if ( ( type == SQLITE_FLOAT )||( type == SQLITE_INTEGER ) ) {
            row_wt = sqlite3_value_double( values[1] );
        }
    }

    /* Extract data, if we were given something that looks like a number. */
    type = sqlite3_value_numeric_type( values[0] );
    if ( ( type == SQLITE_FLOAT )||( type == SQLITE_INTEGER ) ) {
        st->total_data *= row_wt * sqlite3_value_double( values[0] );
        st->total_wt   += row_wt;
    }
}

static void product_final( sqlite3_context *ctx )
{
    double         result = 0.0;
    product_state   *st = (product_state*)sqlite3_aggregate_context( ctx,
                                               sizeof( product_state ) );
    if ( st == NULL ) {
        sqlite3_result_error_nomem( ctx );
        return;
    }

    if ( st->total_wt != 0.0 ) {
        result = st->total_data / st->total_wt;
    }
    sqlite3_result_double( ctx, result );
}

int product_init( sqlite3 *db, char **error, const sqlite3_api_routines *api )
{
    SQLITE_EXTENSION_INIT2(api);

    sqlite3_create_function( db, "product", 1, SQLITE_UTF8,
            NULL, NULL, product_step, product_final );
    sqlite3_create_function( db, "product", 2, SQLITE_UTF8,
            NULL, NULL, product_step, product_final );

    return SQLITE_OK;
}

Upvotes: 1

Views: 338

Answers (1)

Jeremy W. Sherman
Jeremy W. Sherman

Reputation: 36143

In product_step, after your test for st == NULL, try adding this:

if (0.0 == st->total_data) st->total_data = 1.0;

In general, you should be very wary of comparing a floating point value for equality. Floating point math is limited in its precision. However, a straightforward comparison should work fine here, provided none of the values in the row are zero: the first time through, the buffer returned by sqlite3_aggregate_context has been zeroed by sqlite, so the value should compare equal to 0.0.

If a row value being zero is possible (and it likely is), you should add state to the struct to tell whether initialization has been done:

typedef struct product_state_s {
   double   total_data;  /* sum of (data * weight) values */
   double   total_wt;    /* sum of weight values */
   bool     did_init;
} product_state;

and, later, instead of testing whether st->total_data is zero, instead:

if (!did_init) {
    st->total_data = 1.0;
    st->did_init = true;
}

Upvotes: 1

Related Questions