Search in sources :

Example 1 with AggregateFunctionDefinition

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));
    }
}
Also used : GenericUDAFResolver2(org.apache.hadoop.hive.ql.udf.generic.GenericUDAFResolver2) TableException(org.apache.flink.table.api.TableException) GenericUDAFEvaluator(org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator) HiveGenericUDAF(org.apache.flink.table.functions.hive.HiveGenericUDAF) TableFunctionDefinition(org.apache.flink.table.functions.TableFunctionDefinition) GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) HiveGenericUDF(org.apache.flink.table.functions.hive.HiveGenericUDF) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) HiveGenericUDTF(org.apache.flink.table.functions.hive.HiveGenericUDTF) HiveSimpleUDF(org.apache.flink.table.functions.hive.HiveSimpleUDF) Row(org.apache.flink.types.Row) HiveGenericUDF(org.apache.flink.table.functions.hive.HiveGenericUDF) HiveFunctionWrapper(org.apache.flink.table.functions.hive.HiveFunctionWrapper)

Example 2 with AggregateFunctionDefinition

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);
}
Also used : TypeInference(org.apache.flink.table.types.inference.TypeInference) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) DataTypeFactory(org.apache.flink.table.catalog.DataTypeFactory)

Example 3 with AggregateFunctionDefinition

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);
}
Also used : TableException(org.apache.flink.table.api.TableException) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) TableAggregateFunction(org.apache.flink.table.functions.TableAggregateFunction) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) AggregateFunction(org.apache.flink.table.functions.AggregateFunction) TableAggregateFunction(org.apache.flink.table.functions.TableAggregateFunction) ImperativeAggregateFunction(org.apache.flink.table.functions.ImperativeAggregateFunction) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) TableFunctionDefinition(org.apache.flink.table.functions.TableFunctionDefinition) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition)

Example 4 with AggregateFunctionDefinition

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);
    }
}
Also used : ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) TableFunctionDefinition(org.apache.flink.table.functions.TableFunctionDefinition) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableAggregateFunctionDefinition(org.apache.flink.table.functions.TableAggregateFunctionDefinition) TableFunctionDefinition(org.apache.flink.table.functions.TableFunctionDefinition) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition)

Example 5 with AggregateFunctionDefinition

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);
}
Also used : FunctionIdentifier(org.apache.flink.table.functions.FunctionIdentifier) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) DeferredTypeFlinkTableFunction(org.apache.flink.table.planner.plan.schema.DeferredTypeFlinkTableFunction) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableFunctionDefinition(org.apache.flink.table.functions.TableFunctionDefinition) DataType(org.apache.flink.table.types.DataType) TypeConversions.fromLegacyInfoToDataType(org.apache.flink.table.types.utils.TypeConversions.fromLegacyInfoToDataType) BuiltInFunctionDefinition(org.apache.flink.table.functions.BuiltInFunctionDefinition) AggregateFunctionDefinition(org.apache.flink.table.functions.AggregateFunctionDefinition) TableFunctionDefinition(org.apache.flink.table.functions.TableFunctionDefinition) ScalarFunctionDefinition(org.apache.flink.table.functions.ScalarFunctionDefinition) FunctionDefinition(org.apache.flink.table.functions.FunctionDefinition) Row(org.apache.flink.types.Row) HiveAggSqlFunction(org.apache.flink.table.planner.functions.utils.HiveAggSqlFunction) HiveTableSqlFunction(org.apache.flink.table.planner.functions.utils.HiveTableSqlFunction)

Aggregations

AggregateFunctionDefinition (org.apache.flink.table.functions.AggregateFunctionDefinition)5 FunctionDefinition (org.apache.flink.table.functions.FunctionDefinition)4 TableFunctionDefinition (org.apache.flink.table.functions.TableFunctionDefinition)4 ScalarFunctionDefinition (org.apache.flink.table.functions.ScalarFunctionDefinition)3 TableAggregateFunctionDefinition (org.apache.flink.table.functions.TableAggregateFunctionDefinition)3 TableException (org.apache.flink.table.api.TableException)2 BuiltInFunctionDefinition (org.apache.flink.table.functions.BuiltInFunctionDefinition)2 Row (org.apache.flink.types.Row)2 DataTypeFactory (org.apache.flink.table.catalog.DataTypeFactory)1 AggregateFunction (org.apache.flink.table.functions.AggregateFunction)1 FunctionIdentifier (org.apache.flink.table.functions.FunctionIdentifier)1 ImperativeAggregateFunction (org.apache.flink.table.functions.ImperativeAggregateFunction)1 TableAggregateFunction (org.apache.flink.table.functions.TableAggregateFunction)1 HiveFunctionWrapper (org.apache.flink.table.functions.hive.HiveFunctionWrapper)1 HiveGenericUDAF (org.apache.flink.table.functions.hive.HiveGenericUDAF)1 HiveGenericUDF (org.apache.flink.table.functions.hive.HiveGenericUDF)1 HiveGenericUDTF (org.apache.flink.table.functions.hive.HiveGenericUDTF)1 HiveSimpleUDF (org.apache.flink.table.functions.hive.HiveSimpleUDF)1 HiveAggSqlFunction (org.apache.flink.table.planner.functions.utils.HiveAggSqlFunction)1 HiveTableSqlFunction (org.apache.flink.table.planner.functions.utils.HiveTableSqlFunction)1