Search in sources :

Example 1 with HiveGenericUDAF

use of org.apache.flink.table.functions.hive.HiveGenericUDAF in project flink by apache.

the class HiveParserUtils method getGenericUDAFEvaluator.

// Returns the GenericUDAFEvaluator for the aggregation. This is called once for each GroupBy
// aggregation.
// TODO: Requiring a GenericUDAFEvaluator means we only support hive UDAFs. Need to avoid this
// to support flink UDAFs.
public static GenericUDAFEvaluator getGenericUDAFEvaluator(String aggName, ArrayList<ExprNodeDesc> aggParameters, HiveParserASTNode aggTree, boolean isDistinct, boolean isAllColumns, SqlOperatorTable opTable) throws SemanticException {
    ArrayList<ObjectInspector> originalParameterTypeInfos = getWritableObjectInspector(aggParameters);
    GenericUDAFEvaluator result = FunctionRegistry.getGenericUDAFEvaluator(aggName, originalParameterTypeInfos, isDistinct, isAllColumns);
    if (result == null) {
        // this happens for temp functions
        SqlOperator sqlOperator = getSqlOperator(aggName, opTable, SqlFunctionCategory.USER_DEFINED_FUNCTION);
        if (sqlOperator instanceof HiveAggSqlFunction) {
            HiveGenericUDAF hiveGenericUDAF = (HiveGenericUDAF) ((HiveAggSqlFunction) sqlOperator).makeFunction(new Object[0], new LogicalType[0]);
            result = hiveGenericUDAF.createEvaluator(originalParameterTypeInfos.toArray(new ObjectInspector[0]));
        }
    }
    if (null == result) {
        String reason = "Looking for UDAF Evaluator\"" + aggName + "\" with parameters " + originalParameterTypeInfos;
        throw new SemanticException(HiveParserErrorMsg.getMsg(ErrorMsg.INVALID_FUNCTION_SIGNATURE, aggTree.getChild(0), reason));
    }
    return result;
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) GenericUDAFEvaluator(org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator) SqlOperator(org.apache.calcite.sql.SqlOperator) HiveGenericUDAF(org.apache.flink.table.functions.hive.HiveGenericUDAF) LogicalType(org.apache.flink.table.types.logical.LogicalType) NlsString(org.apache.calcite.util.NlsString) HiveAggSqlFunction(org.apache.flink.table.planner.functions.utils.HiveAggSqlFunction) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException)

Example 2 with HiveGenericUDAF

use of org.apache.flink.table.functions.hive.HiveGenericUDAF 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)

Aggregations

HiveGenericUDAF (org.apache.flink.table.functions.hive.HiveGenericUDAF)2 GenericUDAFEvaluator (org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator)2 SqlOperator (org.apache.calcite.sql.SqlOperator)1 NlsString (org.apache.calcite.util.NlsString)1 TableException (org.apache.flink.table.api.TableException)1 AggregateFunctionDefinition (org.apache.flink.table.functions.AggregateFunctionDefinition)1 TableFunctionDefinition (org.apache.flink.table.functions.TableFunctionDefinition)1 HiveFunctionWrapper (org.apache.flink.table.functions.hive.HiveFunctionWrapper)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 LogicalType (org.apache.flink.table.types.logical.LogicalType)1 Row (org.apache.flink.types.Row)1 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)1 GenericUDAFResolver2 (org.apache.hadoop.hive.ql.udf.generic.GenericUDAFResolver2)1 GenericUDF (org.apache.hadoop.hive.ql.udf.generic.GenericUDF)1 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)1