package com.splicemachine.derby.impl.sql.execute.operations.distinctscalar;

import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.iapi.reference.SQLState;
import org.apache.derby.iapi.sql.execute.ExecRow;
import org.apache.log4j.Logger;
import com.splicemachine.derby.impl.sql.execute.operations.WarningCollector;
import com.splicemachine.derby.impl.sql.execute.operations.framework.BufferedAggregator;
import com.splicemachine.derby.impl.sql.execute.operations.framework.SpliceGenericAggregator;
import com.splicemachine.derby.utils.StandardSupplier;

public class DistinctScalarBufferedAggregator implements BufferedAggregator {
    private static final Logger LOG = Logger.getLogger(DistinctScalarBufferedAggregator.class);
    protected final SpliceGenericAggregator[] aggregates;
    private final StandardSupplier<ExecRow> emptyRowSupplier;
    private final WarningCollector warningCollector;
    private ExecRow currentRow;
    private boolean isSorted;
    private boolean alreadyInitialized;

    protected DistinctScalarBufferedAggregator(SpliceGenericAggregator[] aggregates,
                                 StandardSupplier<ExecRow> emptyRowSupplier,
                                 WarningCollector warningCollector, boolean isSorted, boolean alreadyInitialized) {
        this.aggregates= aggregates;
        this.emptyRowSupplier = emptyRowSupplier;
        this.warningCollector = warningCollector;
        this.isSorted = isSorted;
        this.alreadyInitialized = alreadyInitialized;
    }
    /**
     * Initialize Aggregator
     */
    public void initialize(ExecRow row) throws StandardException{
    	System.out.println("initialize: " + row);
        this.currentRow = row.getClone();
        if (!alreadyInitialized) { // In the case we are reading from temp an already initialized aggregations.
	        for(SpliceGenericAggregator aggregator:aggregates){
	        	if (!aggregator.isDistinct() || isSorted) { // Only Initialize the distinct aggregates unless we are already sorted...
		            aggregator.initialize(row);
		            aggregator.accumulate(row,row);
	        	}
	        }
        }
    	System.out.println("after initialize: " + row);
    }
    /**
     * Merge Non Distinct Aggregates
     * 
     */
    public void merge(ExecRow newRow) throws StandardException{
    	System.out.println("merge: currentRow " + currentRow + " with newRow " + newRow);
    	for(SpliceGenericAggregator aggregator:aggregates) {
        	if (!aggregator.isDistinct() || isSorted) // Merge Non Distinct Aggregates and Distinct Aggregates if Source is Sorted
        		aggregator.merge(currentRow, newRow);
        }
    	System.out.println("After Merge " + currentRow);
    }

    public boolean isInitialized() {
        return currentRow!=null;
    }

    public ExecRow finish() throws StandardException{
        if(currentRow==null)
            currentRow = emptyRowSupplier.get();
        boolean eliminatedNulls = false;
        System.out.println("Finish " + currentRow);
        for(SpliceGenericAggregator aggregate:aggregates){
            if(aggregate.finish(currentRow))
                eliminatedNulls=true;
        }
        System.out.println("After Finish " + currentRow);
        if(eliminatedNulls)
            warningCollector.addWarning(SQLState.LANG_NULL_ELIMINATED_IN_SET_FUNCTION);
        //once finished, return this to an unitialized state so it can be reused
        ExecRow toReturn = currentRow;
        currentRow= null;
        return toReturn;
    }
}
@@ -1645,55 +1645,8 @@
 on%7B%0A
-    %09System.out.println(%22initialize: %22 + row);%0A
     
@@ -1679,24 +1679,24 @@
 getClone();%0A
+
         if (
@@ -2110,61 +2110,8 @@
   %7D%0A
-    %09System.out.println(%22after initialize: %22 + row);%0A
     
@@ -2241,95 +2241,8 @@
 on%7B%0A
-    %09System.out.println(%22merge: currentRow %22 + currentRow + %22 with newRow %22 + newRow);%0A
     
@@ -2485,62 +2485,8 @@
   %7D%0A
-    %09System.out.println(%22After Merge %22 + currentRow);%0A
     
@@ -2742,60 +2742,8 @@
 se;%0A
-        System.out.println(%22Finish %22 + currentRow);%0A
     
@@ -2880,16 +2880,16 @@
 s=true;%0A
+
         
@@ -2894,66 +2894,8 @@
   %7D%0A
-        System.out.println(%22After Finish %22 + currentRow);%0A
     

Remove System.outs