anchor.diff:      %% TODO ditto float and double
       {[double], true, fun(_,_) -> V end};
   compile_select_col_stateless(_, {return_state, N}) when is_integer(N) ->
  -    % FIXME calculate type
  -    {[sint64], true, fun(Row,_) -> lists:nth(N, Row) end};
  +    %% FIXME calculate type
  +    {[sint64], true, fun(Row,_) -> pull_from_row(N, Row) end};
  +compile_select_col_stateless(_, {finalise_aggregation, FnName, N}) ->
  +    FinaliseFn =
  +        fun(Row,_) ->
  +            ColValue = pull_from_row(N, Row),
  +            riak_ql_window_agg_fns:finalise(FnName, ColValue)
  +        end,
  +    {[sint64], true, FinaliseFn};
   compile_select_col_stateless(#ddl_v1{ fields = Fields }, {identifier, ColumnName}) ->
       {Index, Type} = col_index_and_type_of(Fields, to_column_name_binary(ColumnName)),
  -    {[Type], true, fun(Row,_) -> lists:nth(Index, Row) end};
  +    {[Type], true, fun(Row,_) -> pull_from_row(Index, Row) end};
   compile_select_col_stateless(DDL, {Op, A, B}) ->
       {Ta, IsValida, Arg_a} = compile_select_col_stateless(DDL, A),
       {Tb, IsValidb, Arg_b} = compile_select_col_stateless(DDL, B),
  
positive.code:         when Tag == identifier; Tag == sint64; Tag == integer; Tag == float;
               Tag == binary;     Tag == varchar; Tag == boolean; Tag == negate ->
      {Node, Fns};
  extract_stateful_functions2({{window_agg_fn, _}, _} = Function, FinaliserLen, Fns1) ->
      Fns2 = [Function | Fns1],
      {{return_state, FinaliserLen + length(Fns2)}, Fns2}.
  
  %% TODO rewrite so that all the operators are just functions
  %% then we can eliminate the double code for creating return types
  
negative.code:     CountFn1 = {{window_agg_fn, 'COUNT'}, [{identifier, [<<"col1">>]}]},
      CountFn2 = {{window_agg_fn, 'COUNT'}, [{identifier, [<<"col2">>]}]},
      ?assertEqual(
          {{'+', {finalise_aggregation, 'COUNT', 1}, {finalise_aggregation, 'COUNT', 2}}, [CountFn1,CountFn2]},
          extract_stateful_functions(Select, 0)
      ).
  
  