Search in sources :

Example 1 with SettableUDF

use of org.apache.hadoop.hive.ql.udf.SettableUDF in project flink by apache.

the class HiveParserDMLHelper method createConversionCast.

private static RexNode createConversionCast(RexBuilder rexBuilder, RexNode srcRex, TypeInfo targetHiveType, RelDataType targetCalType, SqlFunctionConverter funcConverter) throws SemanticException {
    if (funcConverter == null) {
        return rexBuilder.makeCast(targetCalType, srcRex);
    }
    // hive implements CAST with UDFs
    String udfName = TypeInfoUtils.getBaseName(targetHiveType.getTypeName());
    FunctionInfo functionInfo;
    try {
        functionInfo = FunctionRegistry.getFunctionInfo(udfName);
    } catch (SemanticException e) {
        throw new SemanticException(String.format("Failed to get UDF %s for casting", udfName), e);
    }
    if (functionInfo == null || functionInfo.getGenericUDF() == null) {
        throw new SemanticException(String.format("Failed to get UDF %s for casting", udfName));
    }
    if (functionInfo.getGenericUDF() instanceof SettableUDF) {
        // that currently. Therefore just use calcite cast for these types.
        return rexBuilder.makeCast(targetCalType, srcRex);
    } else {
        RexCall cast = (RexCall) rexBuilder.makeCall(HiveParserSqlFunctionConverter.getCalciteOperator(udfName, functionInfo.getGenericUDF(), Collections.singletonList(srcRex.getType()), targetCalType), srcRex);
        if (!funcConverter.hasOverloadedOp(cast.getOperator(), SqlFunctionCategory.USER_DEFINED_FUNCTION)) {
            // in which case fall back to calcite cast
            return rexBuilder.makeCast(targetCalType, srcRex);
        }
        return cast.accept(funcConverter);
    }
}
Also used : RexCall(org.apache.calcite.rex.RexCall) FunctionInfo(org.apache.hadoop.hive.ql.exec.FunctionInfo) SettableUDF(org.apache.hadoop.hive.ql.udf.SettableUDF) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException)

Example 2 with SettableUDF

use of org.apache.hadoop.hive.ql.udf.SettableUDF in project hive by apache.

the class FunctionRegistry method cloneGenericUDF.

/**
 * Create a copy of an existing GenericUDF.
 */
public static GenericUDF cloneGenericUDF(GenericUDF genericUDF) {
    if (null == genericUDF) {
        return null;
    }
    GenericUDF clonedUDF;
    if (genericUDF instanceof GenericUDFBridge) {
        GenericUDFBridge bridge = (GenericUDFBridge) genericUDF;
        clonedUDF = new GenericUDFBridge(bridge.getUdfName(), bridge.isOperator(), bridge.getUdfClassName());
    } else if (genericUDF instanceof GenericUDFMacro) {
        GenericUDFMacro bridge = (GenericUDFMacro) genericUDF;
        clonedUDF = new GenericUDFMacro(bridge.getMacroName(), bridge.getBody().clone(), bridge.getColNames(), bridge.getColTypes());
    } else {
        clonedUDF = ReflectionUtils.newInstance(genericUDF.getClass(), null);
    }
    if (clonedUDF != null) {
        // The SettableUDF calls below could be replaced using this mechanism as well.
        try {
            genericUDF.copyToNewInstance(clonedUDF);
        } catch (UDFArgumentException err) {
            throw new IllegalArgumentException(err);
        }
        // The original may have settable info that needs to be added to the new copy.
        if (genericUDF instanceof SettableUDF) {
            try {
                TypeInfo typeInfo = ((SettableUDF) genericUDF).getTypeInfo();
                if (typeInfo != null) {
                    ((SettableUDF) clonedUDF).setTypeInfo(typeInfo);
                }
            } catch (UDFArgumentException err) {
                // In theory this should not happen - if the original copy of the UDF had this
                // data, we should be able to set the UDF copy with this same settableData.
                LOG.error("Unable to add settable data to UDF " + genericUDF.getClass());
                throw new IllegalArgumentException(err);
            }
        }
    }
    return clonedUDF;
}
Also used : SettableUDF(org.apache.hadoop.hive.ql.udf.SettableUDF) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)

Aggregations

SettableUDF (org.apache.hadoop.hive.ql.udf.SettableUDF)2 RexCall (org.apache.calcite.rex.RexCall)1 FunctionInfo (org.apache.hadoop.hive.ql.exec.FunctionInfo)1 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)1 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)1 StructTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo)1 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)1