/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.hadoop.hive.ql.exec.vector.expressions.aggregates.gen; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable; import org.apache.hadoop.hive.common.type.HiveDecimal; import org.apache.hadoop.hive.ql.exec.Description; import org.apache.hadoop.hive.ql.exec.vector.expressions.VectorExpression; import org.apache.hadoop.hive.ql.exec.vector.expressions.aggregates.VectorAggregateExpression; import org.apache.hadoop.hive.ql.exec.vector.VectorAggregationBufferRow; import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch; import org.apache.hadoop.hive.ql.exec.vector.DecimalColumnVector; import org.apache.hadoop.hive.ql.metadata.HiveException; import org.apache.hadoop.hive.ql.plan.AggregationDesc; import org.apache.hadoop.hive.ql.util.JavaDataModel; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.hive.serde2.io.DoubleWritable; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector; import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory; import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory; /** * . Vectorized implementation for VARIANCE aggregates. */ @Description(name = "", value = "") public class extends VectorAggregateExpression { private static final long serialVersionUID = 1L; /** /* class for storing the current aggregate value. */ private static final class Aggregation implements AggregationBuffer { private static final long serialVersionUID = 1L; transient private double sum; transient private long count; transient private double variance; /** * Value is explicitly (re)initialized in reset() */ transient private boolean isNull = true; public Aggregation() { } public void init() { isNull = false; sum = 0f; count = 0; variance = 0f; } @Override public int getVariableSize() { throw new UnsupportedOperationException(); } @Override public void reset () { isNull = true; sum = 0f; count = 0; variance = 0f; } public void updateValueWithCheckAndInit(HiveDecimalWritable value, short scale) { if (this.isNull) { this.init(); } double dval = value.getHiveDecimal().doubleValue(); this.sum += dval; this.count += 1; if(this.count > 1) { double t = this.count*dval - this.sum; this.variance += (t*t) / ((double)this.count*(this.count-1)); } } public void updateValueNoCheck(HiveDecimalWritable value, short scale) { double dval = value.getHiveDecimal().doubleValue(); this.sum += dval; this.count += 1; double t = this.count*dval - this.sum; this.variance += (t*t) / ((double)this.count*(this.count-1)); } } private VectorExpression inputExpression; transient private LongWritable resultCount; transient private DoubleWritable resultSum; transient private DoubleWritable resultVariance; transient private Object[] partialResult; transient private ObjectInspector soi; public (VectorExpression inputExpression) { this(); this.inputExpression = inputExpression; } public () { super(); partialResult = new Object[3]; resultCount = new LongWritable(); resultSum = new DoubleWritable(); resultVariance = new DoubleWritable(); partialResult[0] = resultCount; partialResult[1] = resultSum; partialResult[2] = resultVariance; initPartialResultInspector(); } private void initPartialResultInspector() { List foi = new ArrayList(); foi.add(PrimitiveObjectInspectorFactory.writableLongObjectInspector); foi.add(PrimitiveObjectInspectorFactory.writableDoubleObjectInspector); foi.add(PrimitiveObjectInspectorFactory.writableDoubleObjectInspector); List fname = new ArrayList(); fname.add("count"); fname.add("sum"); fname.add("variance"); soi = ObjectInspectorFactory.getStandardStructObjectInspector(fname, foi); } private Aggregation getCurrentAggregationBuffer( VectorAggregationBufferRow[] aggregationBufferSets, int aggregateIndex, int row) { VectorAggregationBufferRow mySet = aggregationBufferSets[row]; Aggregation myagg = (Aggregation) mySet.getAggregationBuffer(aggregateIndex); return myagg; } @Override public void aggregateInputSelection( VectorAggregationBufferRow[] aggregationBufferSets, int aggregateIndex, VectorizedRowBatch batch) throws HiveException { inputExpression.evaluate(batch); DecimalColumnVector inputVector = (DecimalColumnVector)batch. cols[this.inputExpression.getOutputColumn()]; int batchSize = batch.size; if (batchSize == 0) { return; } HiveDecimalWritable[] vector = inputVector.vector; if (inputVector.isRepeating) { if (inputVector.noNulls || !inputVector.isNull[0]) { iterateRepeatingNoNullsWithAggregationSelection( aggregationBufferSets, aggregateIndex, vector[0], inputVector.scale, batchSize); } } else if (!batch.selectedInUse && inputVector.noNulls) { iterateNoSelectionNoNullsWithAggregationSelection( aggregationBufferSets, aggregateIndex, vector, inputVector.scale, batchSize); } else if (!batch.selectedInUse) { iterateNoSelectionHasNullsWithAggregationSelection( aggregationBufferSets, aggregateIndex, vector, inputVector.scale, batchSize, inputVector.isNull); } else if (inputVector.noNulls){ iterateSelectionNoNullsWithAggregationSelection( aggregationBufferSets, aggregateIndex, vector, inputVector.scale, batchSize, batch.selected); } else { iterateSelectionHasNullsWithAggregationSelection( aggregationBufferSets, aggregateIndex, vector, inputVector.scale, batchSize, inputVector.isNull, batch.selected); } } private void iterateRepeatingNoNullsWithAggregationSelection( VectorAggregationBufferRow[] aggregationBufferSets, int aggregateIndex, HiveDecimalWritable value, short scale, int batchSize) { for (int i=0; i 1 check in the loop for (int i=1; i 1 check in the loop // for (int i=1; i< batchSize; ++i) { value = vector[selected[i]]; myagg.updateValueNoCheck(value, scale); } } private void iterateNoSelectionHasNulls( Aggregation myagg, HiveDecimalWritable[] vector, short scale, int batchSize, boolean[] isNull) { for(int i=0;i 1 check for (int i=1; i