@Override public ObjectInspector init(Mode m, ObjectInspector[] parameters) throws HiveException { assert (parameters.length == 1); super.init(m, parameters); result = new LongWritable(0); inputOI = (PrimitiveObjectInspector) parameters[0]; outputOI = (PrimitiveObjectInspector)ObjectInspectorUtils.getStandardObjectInspector(inputOI, ObjectInspectorCopyOption.JAVA); return PrimitiveObjectInspectorFactory.writableLongObjectInspector; }
/** * Check if the input object is eligible to contribute to the sum. If it's null * or the same value as the previous one for the case of SUM(DISTINCT). Then * skip it. * @param input the input object * @return True if sumDistinct is false or the non-null input is different from the previous object */ protected boolean isEligibleValue(SumAgg agg, Object input) { if (input == null) { return false; } if (isWindowingDistinct()) { HashSet<ObjectInspectorObject> uniqueObjs = agg.uniqueObjects; ObjectInspectorObject obj = input instanceof ObjectInspectorObject ? (ObjectInspectorObject)input : new ObjectInspectorObject( ObjectInspectorUtils.copyToStandardObject(input, inputOI, ObjectInspectorCopyOption.JAVA), outputOI); if (!uniqueObjs.contains(obj)) { uniqueObjs.add(obj); return true; } return false; } return true; } }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Object iterate(int currentRow, LeadLagInfo leadLagInfo) throws HiveException { // Currently sum(distinct) not supported in PartitionEvaluator if (((GenericUDAFSumEvaluator)wrappedEvaluator).isWindowingDistinct()) { return super.iterate(currentRow, leadLagInfo); } Range currentRange = getRange(winFrame, currentRow, partition); ResultType result; if (currentRow == 0 || // Reset for the new partition sumAgg.prevRange == null || currentRange.getSize() <= currentRange.getDiff(sumAgg.prevRange)) { result = (ResultType)calcFunctionValue(currentRange.iterator(), leadLagInfo); sumAgg.prevRange = currentRange; sumAgg.empty = false; sumAgg.prevSum = result; } else { // Given the previous range and the current range, calculate the new sum // from the previous sum and the difference to save the computation. Range r1 = new Range(sumAgg.prevRange.start, currentRange.start, partition); Range r2 = new Range(sumAgg.prevRange.end, currentRange.end, partition); ResultType sum1 = (ResultType)calcFunctionValue(r1.iterator(), leadLagInfo); ResultType sum2 = (ResultType)calcFunctionValue(r2.iterator(), leadLagInfo); result = typeOperation.add(typeOperation.minus(sumAgg.prevSum, sum1), sum2); sumAgg.prevRange = currentRange; sumAgg.prevSum = result; } return result; } }
/** * Check if the input object is eligible to contribute to the sum. If it's null * or the same value as the previous one for the case of SUM(DISTINCT). Then * skip it. * @param input the input object * @return True if sumDistinct is false or the non-null input is different from the previous object */ protected boolean isEligibleValue(SumAgg agg, Object input) { if (input == null) { return false; } if (isWindowingDistinct()) { if (agg.uniqueObjects == null) { agg.uniqueObjects = new HashSet<ObjectInspectorObject>(); } HashSet<ObjectInspectorObject> uniqueObjs = agg.uniqueObjects; ObjectInspectorObject obj = input instanceof ObjectInspectorObject ? (ObjectInspectorObject)input : new ObjectInspectorObject( ObjectInspectorUtils.copyToStandardObject(input, inputOI, ObjectInspectorCopyOption.JAVA), outputOI); if (!uniqueObjs.contains(obj)) { uniqueObjs.add(obj); return true; } return false; } return true; } }
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override public Object iterate(int currentRow, LeadLagInfo leadLagInfo) throws HiveException { // Currently sum(distinct) not supported in PartitionEvaluator if (((GenericUDAFSumEvaluator)wrappedEvaluator).isWindowingDistinct()) { return super.iterate(currentRow, leadLagInfo); } Range currentRange = getRange(winFrame, currentRow, partition, nullsLast); ResultType result; if (currentRow == 0 || // Reset for the new partition sumAgg.prevRange == null || currentRange.getSize() <= currentRange.getDiff(sumAgg.prevRange)) { result = (ResultType)calcFunctionValue(currentRange.iterator(), leadLagInfo); sumAgg.prevRange = currentRange; sumAgg.empty = false; sumAgg.prevSum = result; } else { // Given the previous range and the current range, calculate the new sum // from the previous sum and the difference to save the computation. Range r1 = new Range(sumAgg.prevRange.start, currentRange.start, partition); Range r2 = new Range(sumAgg.prevRange.end, currentRange.end, partition); ResultType sum1 = (ResultType)calcFunctionValue(r1.iterator(), leadLagInfo); ResultType sum2 = (ResultType)calcFunctionValue(r2.iterator(), leadLagInfo); result = typeOperation.add(typeOperation.minus(sumAgg.prevSum, sum1), sum2); sumAgg.prevRange = currentRange; sumAgg.prevSum = result; } return result; } }
@Override public ObjectInspector init(Mode m, ObjectInspector[] parameters) throws HiveException { assert (parameters.length == 1); super.init(m, parameters); result = new HiveDecimalWritable(0); inputOI = (PrimitiveObjectInspector) parameters[0]; // The output precision is 10 greater than the input which should cover at least // 10b rows. The scale is the same as the input. DecimalTypeInfo outputTypeInfo = null; if (mode == Mode.PARTIAL1 || mode == Mode.COMPLETE) { int precision = Math.min(HiveDecimal.MAX_PRECISION, inputOI.precision() + 10); outputTypeInfo = TypeInfoFactory.getDecimalTypeInfo(precision, inputOI.scale()); } else { outputTypeInfo = (DecimalTypeInfo) inputOI.getTypeInfo(); } ObjectInspector oi = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(outputTypeInfo); outputOI = (PrimitiveObjectInspector) ObjectInspectorUtils.getStandardObjectInspector( oi, ObjectInspectorCopyOption.JAVA); return oi; }
@Override public ObjectInspector init(Mode m, ObjectInspector[] parameters) throws HiveException { assert (parameters.length == 1); super.init(m, parameters); result = new HiveDecimalWritable(0); inputOI = (PrimitiveObjectInspector) parameters[0]; final DecimalTypeInfo outputTypeInfo; if (mode == Mode.PARTIAL1 || mode == Mode.COMPLETE) { outputTypeInfo = getOutputDecimalTypeInfoForSum(inputOI.precision(), inputOI.scale(), mode); } else { // No change. outputTypeInfo = (DecimalTypeInfo) inputOI.getTypeInfo(); } ObjectInspector oi = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(outputTypeInfo); outputOI = (PrimitiveObjectInspector) ObjectInspectorUtils.getStandardObjectInspector( oi, ObjectInspectorCopyOption.JAVA); return oi; }
@Override public ObjectInspector init(Mode m, ObjectInspector[] parameters) throws HiveException { assert (parameters.length == 1); super.init(m, parameters); result = new DoubleWritable(0); inputOI = (PrimitiveObjectInspector) parameters[0]; outputOI = (PrimitiveObjectInspector)ObjectInspectorUtils.getStandardObjectInspector(inputOI, ObjectInspectorCopyOption.JAVA); return PrimitiveObjectInspectorFactory.writableDoubleObjectInspector; }
@Override public Object terminatePartial(AggregationBuffer agg) throws HiveException { if (isWindowingDistinct()) { throw new HiveException("Distinct windowing UDAF doesn't support merge and terminatePartial"); } else { return terminate(agg); } }
@Override public GenericUDAFEvaluator getEvaluator(GenericUDAFParameterInfo info) throws SemanticException { TypeInfo[] parameters = info.getParameters(); GenericUDAFSumEvaluator eval = (GenericUDAFSumEvaluator) getEvaluator(parameters); eval.setWindowing(info.isWindowing()); eval.setSumDistinct(info.isDistinct()); return eval; }
@Override public ObjectInspector init(Mode m, ObjectInspector[] parameters) throws HiveException { assert (parameters.length == 1); super.init(m, parameters); result = new LongWritable(0); inputOI = (PrimitiveObjectInspector) parameters[0]; outputOI = (PrimitiveObjectInspector)ObjectInspectorUtils.getStandardObjectInspector(inputOI, ObjectInspectorCopyOption.JAVA); return PrimitiveObjectInspectorFactory.writableLongObjectInspector; }
@Override public GenericUDAFEvaluator getEvaluator(GenericUDAFParameterInfo info) throws SemanticException { TypeInfo[] parameters = info.getParameters(); GenericUDAFSumEvaluator eval = (GenericUDAFSumEvaluator) getEvaluator(parameters); eval.setWindowing(info.isWindowing()); eval.setSumDistinct(info.isDistinct()); return eval; }
@Override public ObjectInspector init(Mode m, ObjectInspector[] parameters) throws HiveException { assert (parameters.length == 1); super.init(m, parameters); result = new DoubleWritable(0); inputOI = (PrimitiveObjectInspector) parameters[0]; outputOI = (PrimitiveObjectInspector)ObjectInspectorUtils.getStandardObjectInspector(inputOI, ObjectInspectorCopyOption.JAVA); return PrimitiveObjectInspectorFactory.writableDoubleObjectInspector; }
@Override public Object terminatePartial(AggregationBuffer agg) throws HiveException { if (isWindowingDistinct()) { throw new HiveException("Distinct windowing UDAF doesn't support merge and terminatePartial"); } else { return terminate(agg); } }