org.apache.flink.table.functions.AggregateFunction Java Examples

The following examples show how to use org.apache.flink.table.functions.AggregateFunction. 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: AggFunctionTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
protected ACC accumulateValues(List<T> values)
		throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
	AggregateFunction<T, ACC> aggregator = getAggregator();
	ACC accumulator = getAggregator().createAccumulator();
	Method accumulateFunc = getAccumulateFunc();
	for (T value : values) {
		if (accumulateFunc.getParameterCount() == 1) {
			accumulateFunc.invoke(aggregator, (Object) accumulator);
		} else if (accumulateFunc.getParameterCount() == 2) {
			accumulateFunc.invoke(aggregator, (Object) accumulator, (Object) value);
		} else {
			throw new TableException("Unsupported now");
		}
	}
	return accumulator;
}
 
Example #2
Source File: ExecutionContext.java    From flink with Apache License 2.0 5 votes vote down vote up
private void registerFunctions() {
	if (tableEnv instanceof StreamTableEnvironment) {
		StreamTableEnvironment streamTableEnvironment = (StreamTableEnvironment) tableEnv;
		functions.forEach((k, v) -> {
			if (v instanceof ScalarFunction) {
				streamTableEnvironment.registerFunction(k, (ScalarFunction) v);
			} else if (v instanceof AggregateFunction) {
				streamTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
			} else if (v instanceof TableFunction) {
				streamTableEnvironment.registerFunction(k, (TableFunction<?>) v);
			} else {
				throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
			}
		});
	} else {
		BatchTableEnvironment batchTableEnvironment = (BatchTableEnvironment) tableEnv;
		functions.forEach((k, v) -> {
			if (v instanceof ScalarFunction) {
				batchTableEnvironment.registerFunction(k, (ScalarFunction) v);
			} else if (v instanceof AggregateFunction) {
				batchTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
			} else if (v instanceof TableFunction) {
				batchTableEnvironment.registerFunction(k, (TableFunction<?>) v);
			} else {
				throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
			}
		});
	}
}
 
Example #3
Source File: ListAggWsWithRetractAggFunctionTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected ListAggWsWithRetractAccumulator accumulateValues(List<BinaryString> values)
		throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
	AggregateFunction<BinaryString, ListAggWsWithRetractAccumulator> aggregator = getAggregator();
	ListAggWsWithRetractAccumulator accumulator = getAggregator().createAccumulator();
	Method accumulateFunc = getAccumulateFunc();
	Preconditions.checkArgument(values.size() % 2 == 0,
			"number of values must be an integer multiple of 2.");
	for (int i = 0; i < values.size(); i += 2) {
		BinaryString value = values.get(i + 1);
		BinaryString delimiter = values.get(i);
		accumulateFunc.invoke(aggregator, accumulator, delimiter, value);
	}
	return accumulator;
}
 
Example #4
Source File: HiveAggSqlFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
public HiveAggSqlFunction(String name, String displayName,
		AggregateFunction aggregateFunction, FlinkTypeFactory typeFactory) {
	super(name, displayName, aggregateFunction, fromLegacyInfoToDataType(new GenericTypeInfo<>(Object.class)),
			fromLegacyInfoToDataType(new GenericTypeInfo<>(Object.class)), typeFactory, false,
			new Some<>(createReturnTypeInference(aggregateFunction, typeFactory)));
	this.aggregateFunction = aggregateFunction;
}
 
Example #5
Source File: HiveAggSqlFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public AggregateFunction makeFunction(Object[] constantArguments, LogicalType[] argTypes) {
	AggregateFunction clone;
	try {
		clone = InstantiationUtil.clone(aggregateFunction);
	} catch (IOException | ClassNotFoundException e) {
		throw new RuntimeException(e);
	}
	return (AggregateFunction) invokeSetArgs(clone, constantArguments, argTypes);
}
 
Example #6
Source File: HiveAggSqlFunction.java    From flink with Apache License 2.0 5 votes vote down vote up
private static SqlReturnTypeInference createReturnTypeInference(
		AggregateFunction function, FlinkTypeFactory typeFactory) {
	return opBinding -> {
		List<RelDataType> sqlTypes = opBinding.collectOperandTypes();
		LogicalType[] parameters = UserDefinedFunctionUtils.getOperandTypeArray(opBinding);

		Object[] constantArguments = new Object[sqlTypes.size()];
		// Can not touch the literals, Calcite make them in previous RelNode.
		// In here, all inputs are input refs.
		return invokeGetResultType(function, constantArguments, parameters, typeFactory);
	};
}
 
Example #7
Source File: AggFunctionTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
protected void retractValues(ACC accumulator, List<T> values)
		throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
	AggregateFunction<T, ACC> aggregator = getAggregator();
	Method retractFunc = getRetractFunc();
	for (T value : values) {
		if (retractFunc.getParameterCount() == 1) {
			retractFunc.invoke(aggregator, (Object) accumulator);
		} else if (retractFunc.getParameterCount() == 2) {
			retractFunc.invoke(aggregator, (Object) accumulator, (Object) value);
		} else {
			throw new TableException("Unsupported now");
		}
	}
}
 
Example #8
Source File: AggFunctionTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMergeReservedAccumulator() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
	AggregateFunction<T, ACC> aggregator = getAggregator();
	boolean hasMerge = UserDefinedFunctionUtils.ifMethodExistInFunction("merge", aggregator);
	boolean hasRetract = UserDefinedFunctionUtils.ifMethodExistInFunction("retract", aggregator);
	if (!hasMerge || !hasRetract) {
		// this test only verify AggregateFunctions which has merge() and retract() method
		return;
	}

	Method mergeFunc = aggregator.getClass().getMethod("merge", getAccClass(), Iterable.class);
	List<List<T>> inputValueSets = getInputValueSets();
	int size = getInputValueSets().size();

	// iterate over input sets
	for (int i = 0; i < size; ++i) {
		List<T> inputValues = inputValueSets.get(i);
		List<ACC> accumulators = new ArrayList<>();
		List<ACC> reversedAccumulators = new ArrayList<>();
		// prepare accumulators
		accumulators.add(accumulateValues(inputValues));
		// prepare reversed accumulators
		ACC retractedAcc = aggregator.createAccumulator();
		retractValues(retractedAcc, inputValues);
		reversedAccumulators.add(retractedAcc);
		// prepare accumulator only contain two elements
		ACC accWithSubset = accumulateValues(inputValues.subList(0, 2));
		T expectedValue = aggregator.getValue(accWithSubset);

		// merge
		ACC acc = aggregator.createAccumulator();
		mergeFunc.invoke(aggregator, acc, accumulators);
		mergeFunc.invoke(aggregator, acc, reversedAccumulators);
		mergeFunc.invoke(aggregator, accWithSubset, Collections.singleton(acc));

		// getValue
		T result = aggregator.getValue(accWithSubset);
		validateResult(expectedValue, result);
	}
}
 
Example #9
Source File: TypeInferenceExtractor.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts a type inference from a {@link AggregateFunction}.
 */
public static TypeInference forAggregateFunction(
		DataTypeFactory typeFactory,
		Class<? extends AggregateFunction<?, ?>> function) {
	final FunctionMappingExtractor mappingExtractor = new FunctionMappingExtractor(
		typeFactory,
		function,
		UserDefinedFunctionHelper.AGGREGATE_ACCUMULATE,
		createParameterSignatureExtraction(1),
		createGenericResultExtraction(AggregateFunction.class, 1),
		createGenericResultExtraction(AggregateFunction.class, 0),
		createParameterWithAccumulatorVerification());
	return extractTypeInference(mappingExtractor);
}
 
Example #10
Source File: AggFunctionTestBase.java    From flink with Apache License 2.0 5 votes vote down vote up
protected void retractValues(ACC accumulator, List<T> values)
		throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
	AggregateFunction<T, ACC> aggregator = getAggregator();
	Method retractFunc = getRetractFunc();
	for (T value : values) {
		if (retractFunc.getParameterCount() == 1) {
			retractFunc.invoke(aggregator, (Object) accumulator);
		} else if (retractFunc.getParameterCount() == 2) {
			retractFunc.invoke(aggregator, (Object) accumulator, (Object) value);
		} else {
			throw new TableException("Unsupported now");
		}
	}
}
 
Example #11
Source File: MaxWithRetractAggFunctionTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Double, MaxWithRetractAccumulator<Double>> getAggregator() {
	return new DoubleMaxWithRetractAggFunction();
}
 
Example #12
Source File: LastValueWithRetractAggFunctionWithoutOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Boolean, GenericRowData> getAggregator() {
	return new BooleanLastValueWithRetractAggFunction();
}
 
Example #13
Source File: LastValueWithRetractAggFunctionWithOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Long, GenericRow> getAggregator() {
	return new LongLastValueWithRetractAggFunction();
}
 
Example #14
Source File: FirstValueWithRetractAggFunctionWithOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Boolean, GenericRowData> getAggregator() {
	return new BooleanFirstValueWithRetractAggFunction();
}
 
Example #15
Source File: LastValueWithRetractAggFunctionWithoutOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Short, GenericRow> getAggregator() {
	return new ShortLastValueWithRetractAggFunction();
}
 
Example #16
Source File: FirstValueWithRetractAggFunctionWithoutOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<BinaryString, GenericRow> getAggregator() {
	return new StringFirstValueWithRetractAggFunction();
}
 
Example #17
Source File: FirstValueWithRetractAggFunctionWithoutOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<DecimalData, GenericRowData> getAggregator() {
	return new DecimalFirstValueWithRetractAggFunction(DecimalDataTypeInfo.of(precision, scale));
}
 
Example #18
Source File: FirstValueWithRetractAggFunctionWithOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Byte, GenericRow> getAggregator() {
	return new ByteFirstValueWithRetractAggFunction();
}
 
Example #19
Source File: FirstValueWithRetractAggFunctionWithOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Integer, GenericRow> getAggregator() {
	return new IntFirstValueWithRetractAggFunction();
}
 
Example #20
Source File: FirstValueAggFunctionWithOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<StringData, GenericRowData> getAggregator() {
	return new StringFirstValueAggFunction();
}
 
Example #21
Source File: LastValueWithRetractAggFunctionWithOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Short, GenericRow> getAggregator() {
	return new ShortLastValueWithRetractAggFunction();
}
 
Example #22
Source File: FirstValueWithRetractAggFunctionWithoutOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Byte, GenericRowData> getAggregator() {
	return new ByteFirstValueWithRetractAggFunction();
}
 
Example #23
Source File: LastValueWithRetractAggFunctionWithOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Integer, GenericRowData> getAggregator() {
	return new IntLastValueWithRetractAggFunction();
}
 
Example #24
Source File: MinWithRetractAggFunctionTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Time, MinWithRetractAccumulator<Time>> getAggregator() {
	return new TimeMinWithRetractAggFunction();
}
 
Example #25
Source File: FirstValueAggFunctionWithoutOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<DecimalData, GenericRowData> getAggregator() {
	return new DecimalFirstValueAggFunction(DecimalDataTypeInfo.of(precision, scale));
}
 
Example #26
Source File: ExecutionContext.java    From flink with Apache License 2.0 4 votes vote down vote up
private void registerFunctions(Map<String, FunctionDefinition> functions) {
	if (tableEnv instanceof StreamTableEnvironment) {
		StreamTableEnvironment streamTableEnvironment = (StreamTableEnvironment) tableEnv;
		functions.forEach((k, v) -> {
			// Blink planner uses FLIP-65 functions for scalar and table functions
			// aggregate functions still use the old type inference
			if (environment.getExecution().isBlinkPlanner()) {
				if (v instanceof ScalarFunction || v instanceof TableFunction) {
					streamTableEnvironment.createTemporarySystemFunction(k, (UserDefinedFunction) v);
				} else if (v instanceof AggregateFunction) {
					streamTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
				} else {
					throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
				}
			}
			// legacy
			else {
				if (v instanceof ScalarFunction) {
					streamTableEnvironment.registerFunction(k, (ScalarFunction) v);
				} else if (v instanceof AggregateFunction) {
					streamTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
				} else if (v instanceof TableFunction) {
					streamTableEnvironment.registerFunction(k, (TableFunction<?>) v);
				} else {
					throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
				}
			}
		});
	} else {
		BatchTableEnvironment batchTableEnvironment = (BatchTableEnvironment) tableEnv;
		functions.forEach((k, v) -> {
			if (v instanceof ScalarFunction) {
				batchTableEnvironment.registerFunction(k, (ScalarFunction) v);
			} else if (v instanceof AggregateFunction) {
				batchTableEnvironment.registerFunction(k, (AggregateFunction<?, ?>) v);
			} else if (v instanceof TableFunction) {
				batchTableEnvironment.registerFunction(k, (TableFunction<?>) v);
			} else {
				throw new SqlExecutionException("Unsupported function type: " + v.getClass().getName());
			}
		});
	}
}
 
Example #27
Source File: MinWithRetractAggFunctionTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<BinaryString, MinWithRetractAccumulator<BinaryString>> getAggregator() {
	return new StringMinWithRetractAggFunction();
}
 
Example #28
Source File: FirstValueAggFunctionWithoutOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Long, GenericRowData> getAggregator() {
	return new LongFirstValueAggFunction();
}
 
Example #29
Source File: LastValueWithRetractAggFunctionWithoutOrderTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Integer, GenericRowData> getAggregator() {
	return new IntLastValueWithRetractAggFunction();
}
 
Example #30
Source File: MinWithRetractAggFunctionTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Override
protected AggregateFunction<Double, MinWithRetractAccumulator<Double>> getAggregator() {
	return new DoubleMinWithRetractAggFunction();
}