package io.crate.operator.aggregation;

import io.crate.operator.Input;
import io.crate.planner.symbol.Aggregation;

import java.util.List;

public class AggregationCollector {

    private final Input[] inputs;
    private final Aggregation aggregation;
    private AggregationState aggregationState;
    private AggregationFunction aggregationFunction;

    public AggregationCollector(Aggregation a, AggregationFunction aggregationFunction, List<Input> inputs){
        // TODO: implement othe start end steps
        assert(a.fromStep()== Aggregation.Step.ITER);
        assert(a.toStep()== Aggregation.Step.FINAL);
        this.inputs = (Input[]) inputs.toArray();
        this.aggregationFunction = aggregationFunction;
        this.aggregation = a;
    }

    public void startCollect(){
        aggregationState = aggregationFunction.newState();
    }

    public boolean nextRow() {
        return aggregationFunction.iterate(aggregationState, inputs);
    }


    public Object finishCollect() {
        aggregationState.terminatePartial();
        return aggregationState.value();
    }
}

@@ -441,19 +441,16 @@
 on, 
-List%3C
 Input
-%3E
+...
  inp
@@ -636,34 +636,14 @@
 s = 
-(Input%5B%5D) inputs.toArray()
+inputs
 ;%0A  

use array for inputs