Search in sources :

Example 1 with ObjectInputDecoder

use of com.facebook.presto.hive.functions.type.ObjectInputDecoder in project presto by prestodb.

the class HiveScalarFunctionInvoker method createFunctionInvoker.

public static HiveScalarFunctionInvoker createFunctionInvoker(Class<?> cls, QualifiedObjectName name, List<TypeSignature> arguments, TypeManager typeManager) {
    final List<Type> argumentTypes = arguments.stream().map(typeManager::getType).collect(Collectors.toList());
    try {
        // Step 1: Create function instance
        final GenericUDF udf = createGenericUDF(name, cls);
        // Step 2: Initialize function
        ObjectInspector[] inputInspectors = argumentTypes.stream().map(argumentType -> ObjectInspectors.create(argumentType, typeManager)).toArray(ObjectInspector[]::new);
        ObjectInspector resultInspector = udf.initialize(inputInspectors);
        // Step 3: Create invoker
        Type resultType = PrestoTypes.fromObjectInspector(resultInspector, typeManager);
        ObjectInputDecoder[] argumentDecoders = argumentTypes.stream().map(argumentsType -> createDecoder(argumentsType, typeManager)).toArray(ObjectInputDecoder[]::new);
        ObjectEncoder resultEncoder = createEncoder(resultType, resultInspector);
        Signature signature = new Signature(name, SCALAR, resultType.getTypeSignature(), arguments);
        // Step 4: Create ThreadLocal GenericUDF
        final ThreadLocal<GenericUDF> genericUDFSupplier = ThreadLocal.withInitial(() -> {
            try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(cls.getClassLoader())) {
                GenericUDF ret = createGenericUDF(name, cls);
                ret.initialize(inputInspectors);
                return ret;
            } catch (Exception e) {
                throw initializationError(e);
            }
        });
        return new HiveScalarFunctionInvoker(signature, genericUDFSupplier::get, argumentDecoders, resultEncoder);
    } catch (Exception e) {
        throw initializationError(e);
    }
}
Also used : Constructor(java.lang.reflect.Constructor) Supplier(java.util.function.Supplier) TypeSignature(com.facebook.presto.common.type.TypeSignature) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) UDF(org.apache.hadoop.hive.ql.exec.UDF) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) ObjectEncoders.createEncoder(com.facebook.presto.hive.functions.type.ObjectEncoders.createEncoder) TypeManager(com.facebook.presto.common.type.TypeManager) HiveFunctionErrorCode.initializationError(com.facebook.presto.hive.functions.HiveFunctionErrorCode.initializationError) Objects.requireNonNull(java.util.Objects.requireNonNull) QualifiedObjectName(com.facebook.presto.common.QualifiedObjectName) ThreadContextClassLoader(com.facebook.presto.spi.classloader.ThreadContextClassLoader) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) Type(com.facebook.presto.common.type.Type) GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) ObjectInputDecoders.createDecoder(com.facebook.presto.hive.functions.type.ObjectInputDecoders.createDecoder) ObjectInputDecoder(com.facebook.presto.hive.functions.type.ObjectInputDecoder) GenericUDFBridge(org.apache.hadoop.hive.ql.udf.generic.GenericUDFBridge) PrestoTypes(com.facebook.presto.hive.functions.type.PrestoTypes) Collectors(java.util.stream.Collectors) SCALAR(com.facebook.presto.spi.function.FunctionKind.SCALAR) InvocationTargetException(java.lang.reflect.InvocationTargetException) HiveFunctionErrorCode.unsupportedFunctionType(com.facebook.presto.hive.functions.HiveFunctionErrorCode.unsupportedFunctionType) List(java.util.List) ObjectEncoder(com.facebook.presto.hive.functions.type.ObjectEncoder) ObjectInspectors(com.facebook.presto.hive.functions.type.ObjectInspectors) Signature(com.facebook.presto.spi.function.Signature) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) HiveFunctionErrorCode.executionError(com.facebook.presto.hive.functions.HiveFunctionErrorCode.executionError) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) InvocationTargetException(java.lang.reflect.InvocationTargetException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) Type(com.facebook.presto.common.type.Type) HiveFunctionErrorCode.unsupportedFunctionType(com.facebook.presto.hive.functions.HiveFunctionErrorCode.unsupportedFunctionType) GenericUDF(org.apache.hadoop.hive.ql.udf.generic.GenericUDF) ObjectInputDecoder(com.facebook.presto.hive.functions.type.ObjectInputDecoder) TypeSignature(com.facebook.presto.common.type.TypeSignature) Signature(com.facebook.presto.spi.function.Signature) ObjectEncoder(com.facebook.presto.hive.functions.type.ObjectEncoder) ThreadContextClassLoader(com.facebook.presto.spi.classloader.ThreadContextClassLoader)

Aggregations

QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)1 Type (com.facebook.presto.common.type.Type)1 TypeManager (com.facebook.presto.common.type.TypeManager)1 TypeSignature (com.facebook.presto.common.type.TypeSignature)1 HiveFunctionErrorCode.executionError (com.facebook.presto.hive.functions.HiveFunctionErrorCode.executionError)1 HiveFunctionErrorCode.initializationError (com.facebook.presto.hive.functions.HiveFunctionErrorCode.initializationError)1 HiveFunctionErrorCode.unsupportedFunctionType (com.facebook.presto.hive.functions.HiveFunctionErrorCode.unsupportedFunctionType)1 ObjectEncoder (com.facebook.presto.hive.functions.type.ObjectEncoder)1 ObjectEncoders.createEncoder (com.facebook.presto.hive.functions.type.ObjectEncoders.createEncoder)1 ObjectInputDecoder (com.facebook.presto.hive.functions.type.ObjectInputDecoder)1 ObjectInputDecoders.createDecoder (com.facebook.presto.hive.functions.type.ObjectInputDecoders.createDecoder)1 ObjectInspectors (com.facebook.presto.hive.functions.type.ObjectInspectors)1 PrestoTypes (com.facebook.presto.hive.functions.type.PrestoTypes)1 ThreadContextClassLoader (com.facebook.presto.spi.classloader.ThreadContextClassLoader)1 SCALAR (com.facebook.presto.spi.function.FunctionKind.SCALAR)1 Signature (com.facebook.presto.spi.function.Signature)1 Constructor (java.lang.reflect.Constructor)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1