use of org.apache.flink.table.functions.AggregateFunctionDefinition in project flink by apache.
the class HiveFunctionDefinitionFactory method createFunctionDefinitionFromHiveFunction.
/**
* Create a FunctionDefinition from a Hive function's class name. Called directly by {@link
* org.apache.flink.table.module.hive.HiveModule}.
*/
public FunctionDefinition createFunctionDefinitionFromHiveFunction(String name, String functionClassName) {
Class clazz;
try {
clazz = Thread.currentThread().getContextClassLoader().loadClass(functionClassName);
LOG.info("Successfully loaded Hive udf '{}' with class '{}'", name, functionClassName);
} catch (ClassNotFoundException e) {
throw new TableException(String.format("Failed to initiate an instance of class %s.", functionClassName), e);
}
if (UDF.class.isAssignableFrom(clazz)) {
LOG.info("Transforming Hive function '{}' into a HiveSimpleUDF", name);
return new HiveSimpleUDF(new HiveFunctionWrapper<>(functionClassName), hiveShim);
} else if (GenericUDF.class.isAssignableFrom(clazz)) {
LOG.info("Transforming Hive function '{}' into a HiveGenericUDF", name);
return new HiveGenericUDF(new HiveFunctionWrapper<>(functionClassName), hiveShim);
} else if (GenericUDTF.class.isAssignableFrom(clazz)) {
LOG.info("Transforming Hive function '{}' into a HiveGenericUDTF", name);
HiveGenericUDTF udtf = new HiveGenericUDTF(new HiveFunctionWrapper<>(functionClassName), hiveShim);
return new TableFunctionDefinition(name, udtf, GenericTypeInfo.of(Row.class));
} else if (GenericUDAFResolver2.class.isAssignableFrom(clazz) || UDAF.class.isAssignableFrom(clazz)) {
HiveGenericUDAF udaf;
if (GenericUDAFResolver2.class.isAssignableFrom(clazz)) {
LOG.info("Transforming Hive function '{}' into a HiveGenericUDAF without UDAF bridging", name);
udaf = new HiveGenericUDAF(new HiveFunctionWrapper<>(functionClassName), false, hiveShim);
} else {
LOG.info("Transforming Hive function '{}' into a HiveGenericUDAF with UDAF bridging", name);
udaf = new HiveGenericUDAF(new HiveFunctionWrapper<>(functionClassName), true, hiveShim);
}
return new AggregateFunctionDefinition(name, udaf, GenericTypeInfo.of(Object.class), GenericTypeInfo.of(GenericUDAFEvaluator.AggregationBuffer.class));
} else {
throw new IllegalArgumentException(String.format("HiveFunctionDefinitionFactory cannot initiate FunctionDefinition for class %s", functionClassName));
}
}
use of org.apache.flink.table.functions.AggregateFunctionDefinition in project flink by apache.
the class SqlAggFunctionVisitor method createSqlAggFunction.
private SqlAggFunction createSqlAggFunction(CallExpression call) {
final FunctionDefinition definition = call.getFunctionDefinition();
// legacy
if (definition instanceof AggregateFunctionDefinition) {
return createLegacySqlAggregateFunction(call.getFunctionIdentifier().orElse(null), (AggregateFunctionDefinition) definition);
} else if (definition instanceof TableAggregateFunctionDefinition) {
return createLegacySqlTableAggregateFunction(call.getFunctionIdentifier().orElse(null), (TableAggregateFunctionDefinition) definition);
}
// new stack
final DataTypeFactory dataTypeFactory = ShortcutUtils.unwrapContext(relBuilder).getCatalogManager().getDataTypeFactory();
final TypeInference typeInference = definition.getTypeInference(dataTypeFactory);
return BridgingSqlAggFunction.of(dataTypeFactory, ShortcutUtils.unwrapTypeFactory(relBuilder), SqlKind.OTHER_FUNCTION, ContextResolvedFunction.fromCallExpression(call), typeInference);
}
use of org.apache.flink.table.functions.AggregateFunctionDefinition in project flink by apache.
the class FunctionCatalog method registerTempSystemAggregateFunction.
/**
* @deprecated Use {@link #registerTemporarySystemFunction(String, FunctionDefinition, boolean)}
* instead.
*/
@Deprecated
public <T, ACC> void registerTempSystemAggregateFunction(String name, ImperativeAggregateFunction<T, ACC> function, TypeInformation<T> resultType, TypeInformation<ACC> accType) {
UserDefinedFunctionHelper.prepareInstance(config, function);
final FunctionDefinition definition;
if (function instanceof AggregateFunction) {
definition = new AggregateFunctionDefinition(name, (AggregateFunction<?, ?>) function, resultType, accType);
} else if (function instanceof TableAggregateFunction) {
definition = new TableAggregateFunctionDefinition(name, (TableAggregateFunction<?, ?>) function, resultType, accType);
} else {
throw new TableException("Unknown function class: " + function.getClass());
}
registerTempSystemFunction(name, definition);
}
use of org.apache.flink.table.functions.AggregateFunctionDefinition in project flink by apache.
the class RexNodeJsonSerializer method serializeBridgingSqlFunction.
private static void serializeBridgingSqlFunction(String summaryName, ContextResolvedFunction resolvedFunction, JsonGenerator gen, SerializerProvider serializerProvider, boolean serializeCatalogObjects) throws IOException {
final FunctionDefinition definition = resolvedFunction.getDefinition();
if (definition instanceof ScalarFunctionDefinition || definition instanceof TableFunctionDefinition || definition instanceof TableAggregateFunctionDefinition || definition instanceof AggregateFunctionDefinition) {
throw legacyException(summaryName);
}
if (definition instanceof BuiltInFunctionDefinition) {
final BuiltInFunctionDefinition builtInFunction = (BuiltInFunctionDefinition) definition;
gen.writeStringField(FIELD_NAME_INTERNAL_NAME, builtInFunction.getQualifiedName());
} else if (resolvedFunction.isAnonymous()) {
serializeInlineFunction(summaryName, definition, gen);
} else if (resolvedFunction.isTemporary()) {
serializeTemporaryFunction(resolvedFunction, gen, serializerProvider);
} else {
assert resolvedFunction.isPermanent();
serializePermanentFunction(resolvedFunction, gen, serializerProvider, serializeCatalogObjects);
}
}
use of org.apache.flink.table.functions.AggregateFunctionDefinition in project flink by apache.
the class FunctionCatalogOperatorTable method convertToSqlFunction.
private Optional<SqlFunction> convertToSqlFunction(@Nullable SqlFunctionCategory category, ContextResolvedFunction resolvedFunction) {
final FunctionDefinition definition = resolvedFunction.getDefinition();
final FunctionIdentifier identifier = resolvedFunction.getIdentifier().orElse(null);
// legacy
if (definition instanceof AggregateFunctionDefinition) {
AggregateFunctionDefinition def = (AggregateFunctionDefinition) definition;
if (isHiveFunc(def.getAggregateFunction())) {
return Optional.of(new HiveAggSqlFunction(identifier, def.getAggregateFunction(), typeFactory));
} else {
return convertAggregateFunction(identifier, (AggregateFunctionDefinition) definition);
}
} else if (definition instanceof ScalarFunctionDefinition) {
ScalarFunctionDefinition def = (ScalarFunctionDefinition) definition;
return convertScalarFunction(identifier, def);
} else if (definition instanceof TableFunctionDefinition && category != null && category.isTableFunction()) {
TableFunctionDefinition def = (TableFunctionDefinition) definition;
if (isHiveFunc(def.getTableFunction())) {
DataType returnType = fromLegacyInfoToDataType(new GenericTypeInfo<>(Row.class));
return Optional.of(new HiveTableSqlFunction(identifier, def.getTableFunction(), returnType, typeFactory, new DeferredTypeFlinkTableFunction(def.getTableFunction(), returnType), HiveTableSqlFunction.operandTypeChecker(identifier.toString(), def.getTableFunction())));
} else {
return convertTableFunction(identifier, (TableFunctionDefinition) definition);
}
}
// new stack
return convertToBridgingSqlFunction(category, resolvedFunction);
}
Aggregations