import java.util.ArrayList; import com.asterdata.ncluster.aggregator.DecomposableAggregatorFunction; import com.asterdata.ncluster.aggregator.DecomposableAggregatorRuntimeContract; import com.asterdata.ncluster.sqlmr.OutputInfo; import com.asterdata.ncluster.sqlmr.data.ColumnDefinition; import com.asterdata.ncluster.sqlmr.data.SqlType; import com.asterdata.ncluster.sqlmr.data.ValueHolder; import com.asterdata.ncluster.util.ImmutableList; import com.asterdata.ncluster.sqlmr.data.RowHolder; import com.asterdata.ncluster.sqlmr.data.RowView; /** * An aggregator that computes a maximum of integer values. * * Both the final aggregated value and the partially aggregated value are of * integer type. * * The max of no input is NULL, as in the SQL standard max aggregate function. */ public class int_max implements DecomposableAggregatorFunction { /** Partial max */ private RowHolder max_; /** * Complete the aggregator contract returning the row schema (int) partial * schema (int), and final schema (int). This aggregator takes no arguments. */ public int_max(DecomposableAggregatorRuntimeContract contract) { // Allocate a row to hold the aggregated value and initialize it. this.max_ = new RowHolder(SqlType.integer()); this.max_.setNullAt(0); // Partial schema ArrayListSqlType partialSchema = new ArrayListSqlType(); partialSchema.add(SqlType.integer()); contract.setPartialResultSchema(ImmutableList.elementsOf(partialSchema)); // Final (output) type ArrayListColumnDefinition outputColumns = new ArrayListColumnDefinition(); outputColumns.add(new ColumnDefinition("result", SqlType.integer())); contract.setOutputInfo(new OutputInfo(outputColumns)); contract.complete(); } /** * Aggregate the first attribute (integer) of the input row. */ public void aggregateRow(RowView row) { if (!row.isNullAt(0)) { if (max_.isNullAt(0)) { max_.setIntAt(0, row.getIntAt(0)); } else { max_.setIntAt(0, Math.max(row.getIntAt(0), max_.getIntAt(0))); } } } /** * Aggregate the first attribute (integer) of the input row. */ public void aggregatePartialRow(RowView partialRow) { this.aggregateRow(partialRow); } /** * Return max as final value. */ public ValueHolder getFinalValue() { ValueHolder retVal = new ValueHolder(this.max_.getColumnTypes().get(0)); this.max_.getValueAt(0, retVal); return retVal; } /** * Return max as partial aggregate. */ public RowView getPartialRow() { return this.max_.clone(); } /** * Reset the aggregate. */ public void reset() { this.max_.setNullAt(0); } }