When I call a custom aggregate function on an empty table T, e.g.,
`select myaggr(A) from T`, the sqlite3_aggregate_context() call in the
xFinal callback does not return zeroed out memory, but a block of
seemingly uninitialized memory. Is that expected? If so, how do I know
that the xStep callback has not been called?

The code looks like this:

typedef struct Aggr Aggr;
struct Aggr {
  Value value;
  uint32_t count;
};

static void
myaggrStep(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
  Aggr* sum = (Aggr*)sqlite3_aggregate_context(ctx, sizeof(Aggr*));

  if (sum == 0) return;

  if (sqlite3_value_type(argv[0]) == SQLITE_NULL)
    return;

  if (sum->count == 0) { // First invocation
    // Initialize aggr->value
    sum->count++;
  }
  else {
    // Update aggr->value
    sum->count++;
  }
}

static void myaggrFinal(sqlite3_context* ctx) {
  Aggr* aggr = (Aggr*)sqlite3_aggregate_context(ctx, sizeof(Aggr*));
  if (aggr == 0) return;
  if (aggr->count == 0) { // (*)
    // xStep was not called, set default value
  }
  // Set result using aggr->value
}

(*) The problem is that in the situation described above, aggr->count
here is not zero.

Using SQLite 3.21.0.

Thanks,
Life.

_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to