Search code examples
cassandrauser-defined-functionscqluser-defined-types

Cassandra: aggregate function INITCOND with complex user defined type


What can be put as INITCOND for user defined aggregate functions in Cassandra? I have only seen examples with simple types (eg. tuple).

I have the following type for the state object in the aggregation function:

create type avg_type_1 (
    accum tuple<text,int,double>,   // source, count, sum
    avg_map map<text,double>        // source, average
);

When I omit INITCOND I get a Java NullPointerException.


Solution

  • The following works for the UDT in the question:

    INITCOND ((null, 0, 0.0), null)
    

    The accum field (tuple): the first element (source, String) is set to null, the second element (count, int) is set to 0 (zero), and the third and final element (sum, double) is set to 0.0 (zero).

    The avg_map field (map): set to null (no map yet).

    The fields can also be referred to by name, as the following (from describe ...) shows.

    INITCOND {accum: (null, 0, 0.0), avg_map: null};
    

    For named fields, curly brackets "{}" are used (as they are represented as a map).

    Lastly, here is an example to also initialize the map.

    INITCOND {accum: (null, 0, 0.0), avg_map: {'i1': 23.5, 'i2': 1.2}};