Cycling user-defined aggregate team_average

Creates a Java user-defined aggregate function that calculates the average of a target column.

Creates a Java user-defined aggregate function that calculates the average of a target column.

SOURCE '0_create_keyspace.cql';

// START-dropagg
DROP AGGREGATE IF EXISTS cycling.avgState;
//END-dropagg

DROP TABLE IF EXISTS cycling.test_avg;

// START-state
CREATE OR REPLACE FUNCTION cycling.avgState (
  state tuple<int, bigint>,
  val int
) 
  CALLED ON NULL INPUT 
  RETURNS tuple<int, bigint> 
  LANGUAGE java AS 
    $$
      if (val != null) { 
        state.setInt(0, state.getInt(0) + 1); 
        state.setLong(1, state.getLong(1) + val.intValue()); 
      } 
      return state; 
    $$
;
// END-state

// START-test
CREATE TABLE IF NOT EXISTS cycling.test_avg (
  id int PRIMARY KEY,
  state frozen<tuple<int, bigint>>,
  val int
);

INSERT INTO cycling.test_avg (
  id, state, val
) VALUES (
  1, (6, 9949), 51
);

INSERT INTO cycling.test_avg (
  id, state, val
) VALUES (
  2, (79, 10000), 9999
);

SELECT state, avgstate(state, val), val
FROM cycling.test_avg;
// END-test

// START-final
CREATE OR REPLACE FUNCTION cycling.avgFinal (
  state tuple<int,bigint>
) 
  CALLED ON NULL INPUT 
  RETURNS double 
  LANGUAGE java AS 
    $$
      double r = 0; 
      if (state.getInt(0) == 0) return null; 
      r = state.getLong(1); 
      r /= state.getInt(0); 
      return Double.valueOf(r);
    $$ 
;
// END-final

// START-agg
CREATE OR REPLACE AGGREGATE cycling.average (
  int
) 
  SFUNC avgState 
  STYPE tuple<int,bigint> 
  FINALFUNC avgFinal 
  INITCOND (0, 0)
;
// END-agg