Java Code Examples for org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator#AggregationBuffer

The following examples show how to use org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator#AggregationBuffer . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HiveGenericUDAFTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testUDAFCount() throws Exception {
	Object[] constantArgs = new Object[] {
		null
	};

	DataType[] argTypes = new DataType[] {
		DataTypes.DOUBLE()
	};

	HiveGenericUDAF udf = init(GenericUDAFCount.class, constantArgs, argTypes);

	GenericUDAFEvaluator.AggregationBuffer acc = udf.createAccumulator();

	udf.accumulate(acc, 0.5d);
	udf.accumulate(acc, 0.3d);
	udf.accumulate(acc, 5.3d);

	udf.merge(acc, Arrays.asList());

	assertEquals(3L, udf.getValue(acc));
}
 
Example 2
Source File: HiveGenericUDAFTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testUDAFMin() throws Exception {
	Object[] constantArgs = new Object[] {
		null
	};

	DataType[] argTypes = new DataType[] {
		DataTypes.BIGINT()
	};

	HiveGenericUDAF udf = init(GenericUDAFMin.class, constantArgs, argTypes);

	GenericUDAFEvaluator.AggregationBuffer acc = udf.createAccumulator();

	udf.accumulate(acc, 2L);
	udf.accumulate(acc, 3L);
	udf.accumulate(acc, 1L);

	udf.merge(acc, Arrays.asList());

	assertEquals(1L, udf.getValue(acc));
}
 
Example 3
Source File: HiveGenericUDAFTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testUDAFMin() throws Exception {
	Object[] constantArgs = new Object[] {
		null
	};

	DataType[] argTypes = new DataType[] {
		DataTypes.BIGINT()
	};

	HiveGenericUDAF udf = init(GenericUDAFMin.class, constantArgs, argTypes);

	GenericUDAFEvaluator.AggregationBuffer acc = udf.createAccumulator();

	udf.accumulate(acc, 2L);
	udf.accumulate(acc, 3L);
	udf.accumulate(acc, 1L);

	udf.merge(acc, Arrays.asList());

	assertEquals(1L, udf.getValue(acc));
}
 
Example 4
Source File: HiveGenericUDAFTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testUDAFCount() throws Exception {
	Object[] constantArgs = new Object[] {
		null
	};

	DataType[] argTypes = new DataType[] {
		DataTypes.DOUBLE()
	};

	HiveGenericUDAF udf = init(GenericUDAFCount.class, constantArgs, argTypes);

	GenericUDAFEvaluator.AggregationBuffer acc = udf.createAccumulator();

	udf.accumulate(acc, 0.5d);
	udf.accumulate(acc, 0.3d);
	udf.accumulate(acc, 5.3d);

	udf.merge(acc, Arrays.asList());

	assertEquals(3L, udf.getValue(acc));
}
 
Example 5
Source File: HiveGenericUDAF.java    From flink with Apache License 2.0 5 votes vote down vote up
public void accumulate(GenericUDAFEvaluator.AggregationBuffer acc, Object... inputs) throws HiveException {
	if (!allIdentityConverter) {
		for (int i = 0; i < inputs.length; i++) {
			inputs[i] = conversions[i].toHiveObject(inputs[i]);
		}
	}

	partialEvaluator.iterate(acc, inputs);
}
 
Example 6
Source File: HiveGenericUDAF.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Object getValue(GenericUDAFEvaluator.AggregationBuffer accumulator) {
	try {
		return HiveInspectors.toFlinkObject(finalResultObjectInspector, finalEvaluator.terminate(accumulator), hiveShim);
	} catch (HiveException e) {
		throw new FlinkHiveUDFException(
			String.format("Failed to get final result on %s", hiveFunctionWrapper.getClassName()), e);
	}
}
 
Example 7
Source File: HiveGenericUDAF.java    From flink with Apache License 2.0 5 votes vote down vote up
public void merge(
		GenericUDAFEvaluator.AggregationBuffer accumulator,
		Iterable<GenericUDAFEvaluator.AggregationBuffer> its) throws HiveException {

	for (GenericUDAFEvaluator.AggregationBuffer buffer : its) {
		finalEvaluator.merge(
			accumulator, partialEvaluator.terminatePartial(buffer));
	}
}
 
Example 8
Source File: HiveGenericUDAF.java    From flink with Apache License 2.0 5 votes vote down vote up
public void accumulate(GenericUDAFEvaluator.AggregationBuffer acc, Object... inputs) throws HiveException {
	if (!allIdentityConverter) {
		for (int i = 0; i < inputs.length; i++) {
			inputs[i] = conversions[i].toHiveObject(inputs[i]);
		}
	}

	partialEvaluator.iterate(acc, inputs);
}
 
Example 9
Source File: HiveGenericUDAF.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * This is invoked without calling open() in Blink, so we need to call init() for getNewAggregationBuffer().
 * TODO: re-evaluate how this will fit into Flink's new type inference and udf systemß
 */
@Override
public GenericUDAFEvaluator.AggregationBuffer createAccumulator() {
	try {
		if (!initialized) {
			init();
		}
		return partialEvaluator.getNewAggregationBuffer();
	} catch (Exception e) {
		throw new FlinkHiveUDFException(
			String.format("Failed to create accumulator for %s", hiveFunctionWrapper.getClassName()), e);
	}
}
 
Example 10
Source File: HiveGenericUDAF.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Object getValue(GenericUDAFEvaluator.AggregationBuffer accumulator) {
	try {
		return HiveInspectors.toFlinkObject(finalResultObjectInspector, finalEvaluator.terminate(accumulator));
	} catch (HiveException e) {
		throw new FlinkHiveUDFException(
			String.format("Failed to get final result on %s", hiveFunctionWrapper.getClassName()), e);
	}
}
 
Example 11
Source File: HiveGenericUDAF.java    From flink with Apache License 2.0 5 votes vote down vote up
public void merge(
		GenericUDAFEvaluator.AggregationBuffer accumulator,
		Iterable<GenericUDAFEvaluator.AggregationBuffer> its) throws HiveException {

	for (GenericUDAFEvaluator.AggregationBuffer buffer : its) {
		finalEvaluator.merge(
			accumulator, partialEvaluator.terminatePartial(buffer));
	}
}
 
Example 12
Source File: HiveGenericUDAF.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * This is invoked without calling open() in Blink, so we need to call init() for getNewAggregationBuffer().
 * TODO: re-evaluate how this will fit into Flink's new type inference and udf systemß
 */
@Override
public GenericUDAFEvaluator.AggregationBuffer createAccumulator() {
	try {
		if (!initialized) {
			init();
		}
		return partialEvaluator.getNewAggregationBuffer();
	} catch (Exception e) {
		throw new FlinkHiveUDFException(
			String.format("Failed to create accumulator for %s", hiveFunctionWrapper.getClassName()), e);
	}
}
 
Example 13
Source File: HiveUDAFParticipator.java    From marble with Apache License 2.0 4 votes vote down vote up
public void setAggregationBuffer(
    final GenericUDAFEvaluator.AggregationBuffer aggregationBuffer) {
  this.aggregationBuffer = aggregationBuffer;
}
 
Example 14
Source File: HiveUDAFParticipator.java    From marble with Apache License 2.0 4 votes vote down vote up
public GenericUDAFEvaluator.AggregationBuffer getAggregationBuffer() {
  return aggregationBuffer;
}
 
Example 15
Source File: HiveGenericUDAFTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testUDAFSum() throws Exception {
	Object[] constantArgs = new Object[] {
		null
	};

	DataType[] argTypes = new DataType[] {
		DataTypes.DOUBLE()
	};

	HiveGenericUDAF udf = init(GenericUDAFSum.class, constantArgs, argTypes);

	GenericUDAFEvaluator.AggregationBuffer acc = udf.createAccumulator();

	udf.accumulate(acc, 0.5d);
	udf.accumulate(acc, 0.3d);
	udf.accumulate(acc, 5.3d);

	udf.merge(acc, Arrays.asList());

	assertEquals(6.1d, udf.getValue(acc));

	constantArgs = new Object[] {
		null
	};

	argTypes = new DataType[] {
		DataTypes.DECIMAL(5, 3)
	};

	udf = init(GenericUDAFSum.class, constantArgs, argTypes);

	acc = udf.createAccumulator();

	udf.accumulate(acc, BigDecimal.valueOf(10.111));
	udf.accumulate(acc, BigDecimal.valueOf(3.222));
	udf.accumulate(acc, BigDecimal.valueOf(5.333));

	udf.merge(acc, Arrays.asList());

	assertEquals(BigDecimal.valueOf(18.666), udf.getValue(acc));
}
 
Example 16
Source File: HiveGenericUDAF.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public TypeInformation<GenericUDAFEvaluator.AggregationBuffer> getAccumulatorType() {
	return new GenericTypeInfo<>(GenericUDAFEvaluator.AggregationBuffer.class);
}
 
Example 17
Source File: HiveGenericUDAF.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
public TypeInformation<GenericUDAFEvaluator.AggregationBuffer> getAccumulatorType() {
	return new GenericTypeInfo<>(GenericUDAFEvaluator.AggregationBuffer.class);
}
 
Example 18
Source File: HiveGenericUDAFTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testUDAFSum() throws Exception {
	Object[] constantArgs = new Object[] {
		null
	};

	DataType[] argTypes = new DataType[] {
		DataTypes.DOUBLE()
	};

	HiveGenericUDAF udf = init(GenericUDAFSum.class, constantArgs, argTypes);

	GenericUDAFEvaluator.AggregationBuffer acc = udf.createAccumulator();

	udf.accumulate(acc, 0.5d);
	udf.accumulate(acc, 0.3d);
	udf.accumulate(acc, 5.3d);

	udf.merge(acc, Arrays.asList());

	assertEquals(6.1d, udf.getValue(acc));

	constantArgs = new Object[] {
		null
	};

	argTypes = new DataType[] {
		DataTypes.DECIMAL(5, 3)
	};

	udf = init(GenericUDAFSum.class, constantArgs, argTypes);

	acc = udf.createAccumulator();

	udf.accumulate(acc, BigDecimal.valueOf(10.111));
	udf.accumulate(acc, BigDecimal.valueOf(3.222));
	udf.accumulate(acc, BigDecimal.valueOf(5.333));

	udf.merge(acc, Arrays.asList());

	assertEquals(BigDecimal.valueOf(18.666), udf.getValue(acc));
}