Search in sources :

Example 11 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class DropFunctionTask method execute.

@Override
public ListenableFuture<?> execute(DropFunction statement, TransactionManager transactionManager, Metadata metadata, AccessControl accessControl, Session session, List<Expression> parameters, WarningCollector warningCollector) {
    Analyzer analyzer = new Analyzer(session, metadata, sqlParser, accessControl, Optional.empty(), parameters, warningCollector);
    analyzer.analyze(statement);
    Optional<List<TypeSignature>> parameterTypes = statement.getParameterTypes().map(types -> types.stream().map(TypeSignature::parseTypeSignature).collect(toImmutableList()));
    if (statement.isTemporary()) {
        removeSessionFunction(session, new SqlFunctionId(QualifiedObjectName.valueOf(SESSION_NAMESPACE, statement.getFunctionName().getSuffix()), parameterTypes.orElse(emptyList())), statement.isExists());
    } else {
        metadata.getFunctionAndTypeManager().dropFunction(qualifyObjectName(statement.getFunctionName()), parameterTypes, statement.isExists());
    }
    return immediateFuture(null);
}
Also used : TypeSignature(com.facebook.presto.common.type.TypeSignature) SqlFunctionId(com.facebook.presto.spi.function.SqlFunctionId) Collections.emptyList(java.util.Collections.emptyList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) Analyzer(com.facebook.presto.sql.analyzer.Analyzer)

Example 12 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class RowParametricType method createType.

@Override
public Type createType(List<TypeParameter> parameters) {
    checkArgument(!parameters.isEmpty(), "Row type must have at least one parameter");
    checkArgument(parameters.stream().allMatch(parameter -> parameter.getKind() == ParameterKind.NAMED_TYPE), "Expected only named types as a parameters, got %s", parameters);
    List<TypeSignatureParameter> typeSignatureParameters = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> TypeSignatureParameter.of(new NamedTypeSignature(parameter.getName(), parameter.getType().getTypeSignature()))).collect(toList());
    List<RowType.Field> fields = parameters.stream().map(TypeParameter::getNamedType).map(parameter -> new RowType.Field(parameter.getName().map(RowFieldName::getName), parameter.getType())).collect(toList());
    return RowType.createWithTypeSignature(new TypeSignature(StandardTypes.ROW, typeSignatureParameters), fields);
}
Also used : StandardTypes(com.facebook.presto.common.type.StandardTypes) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) TypeSignature(com.facebook.presto.common.type.TypeSignature) ParametricType(com.facebook.presto.common.type.ParametricType) List(java.util.List) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Collectors.toList(java.util.stream.Collectors.toList) ParameterKind(com.facebook.presto.common.type.ParameterKind) RowFieldName(com.facebook.presto.common.type.RowFieldName) TypeParameter(com.facebook.presto.common.type.TypeParameter) Type(com.facebook.presto.common.type.Type) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) RowType(com.facebook.presto.common.type.RowType) TypeParameter(com.facebook.presto.common.type.TypeParameter) TypeSignature(com.facebook.presto.common.type.TypeSignature) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature)

Example 13 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class ScalarMethodHandles method generateUnbound.

/**
 * Generates an unbound MethodHandle by {@code signature}.
 * <p>
 * For example, if {@code signature} describes a method like
 * <pre>
 * {@code
 * IntegerType func(
 *              IntegerType i,
 *              VarcharType j)
 * }
 * </pre>,
 * a method is generated as
 * <pre>
 * {@code
 * static Long callEvaluate(
 *              ScalarFunctionInvoker invoker,
 *              Long input_0,
 *              Slice input_1) {
 * return (Long) Invoker.evaluate(input_0, input_1);
 * }
 * }
 * </pre>,
 * and its MethodHandle returns.
 */
public static MethodHandle generateUnbound(Signature signature, TypeManager typeManager) {
    Class<?> returnType = Primitives.wrap(typeManager.getType(signature.getReturnType()).getJavaType());
    List<TypeSignature> argumentTypes = signature.getArgumentTypes();
    List<Class<?>> argumentJavaTypes = argumentTypes.stream().map(t -> typeManager.getType(t).getJavaType()).map(Primitives::wrap).collect(toImmutableList());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName(CLASS_NAME), ParameterizedType.type(Object.class));
    // Step 1: Declare default constructor
    definition.declareDefaultConstructor(a(PRIVATE));
    // Step 2: Declare method
    Parameter[] declareParameters = new Parameter[argumentTypes.size() + 1];
    declareParameters[0] = arg("invoker", ScalarFunctionInvoker.class);
    for (int i = 0; i < argumentTypes.size(); i++) {
        declareParameters[i + 1] = arg("input_" + i, argumentJavaTypes.get(i));
    }
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), METHOD_NAME, ParameterizedType.type(returnType), declareParameters);
    // Step 3: Implement method body
    BytecodeExpression[] evaluateInputs = new BytecodeExpression[argumentTypes.size()];
    for (int i = 0; i < argumentTypes.size(); i++) {
        evaluateInputs[i] = declareParameters[i + 1];
    }
    method.getBody().append(declareParameters[0].invoke("evaluate", Object.class, newArray(type(Object[].class), evaluateInputs)).cast(returnType).ret());
    // Step 4: Generate class
    Class<?> generatedClass = defineClass(definition, Object.class, Collections.emptyMap(), ScalarMethodHandles.class.getClassLoader());
    // Step 5: Lookup MethodHandle
    Class<?>[] lookupClasses = new Class[argumentTypes.size() + 1];
    lookupClasses[0] = ScalarFunctionInvoker.class;
    for (int i = 0; i < argumentTypes.size(); i++) {
        lookupClasses[i + 1] = Primitives.wrap(typeManager.getType(argumentTypes.get(i)).getJavaType());
    }
    return methodHandle(generatedClass, METHOD_NAME, lookupClasses);
}
Also used : ScalarFunctionInvoker(com.facebook.presto.hive.functions.scalar.ScalarFunctionInvoker) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) TypeSignature(com.facebook.presto.common.type.TypeSignature) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) CompilerUtils.defineClass(com.facebook.presto.hive.functions.gen.CompilerUtils.defineClass) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression)

Example 14 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature 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)

Example 15 with TypeSignature

use of com.facebook.presto.common.type.TypeSignature in project presto by prestodb.

the class PrestoTypes method fromMap.

private static Type fromMap(MapObjectInspector inspector, TypeManager typeManager) {
    Type keyType = fromObjectInspector(inspector.getMapKeyObjectInspector(), typeManager);
    Type valueType = fromObjectInspector(inspector.getMapValueObjectInspector(), typeManager);
    return typeManager.getType(new TypeSignature(StandardTypes.MAP, TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
}
Also used : DecimalType(com.facebook.presto.common.type.DecimalType) BooleanType(com.facebook.presto.common.type.BooleanType) HiveFunctionErrorCode.unsupportedType(com.facebook.presto.hive.functions.HiveFunctionErrorCode.unsupportedType) IntegerType(com.facebook.presto.common.type.IntegerType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) SmallintType(com.facebook.presto.common.type.SmallintType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) DateType(com.facebook.presto.common.type.DateType) DoubleType(com.facebook.presto.common.type.DoubleType) RowType(com.facebook.presto.common.type.RowType) TimestampType(com.facebook.presto.common.type.TimestampType) TypeSignature(com.facebook.presto.common.type.TypeSignature)

Aggregations

TypeSignature (com.facebook.presto.common.type.TypeSignature)60 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)25 Signature (com.facebook.presto.spi.function.Signature)20 NamedTypeSignature (com.facebook.presto.common.type.NamedTypeSignature)19 ImmutableList (com.google.common.collect.ImmutableList)19 List (java.util.List)17 PrestoException (com.facebook.presto.spi.PrestoException)15 Type (com.facebook.presto.common.type.Type)14 Test (org.testng.annotations.Test)14 TypeSignatureParameter (com.facebook.presto.common.type.TypeSignatureParameter)13 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)13 Objects.requireNonNull (java.util.Objects.requireNonNull)12 RowFieldName (com.facebook.presto.common.type.RowFieldName)10 StandardTypes (com.facebook.presto.common.type.StandardTypes)9 Optional (java.util.Optional)9 QualifiedObjectName (com.facebook.presto.common.QualifiedObjectName)8 DecimalType (com.facebook.presto.common.type.DecimalType)8 ImmutableSet (com.google.common.collect.ImmutableSet)8 RowType (com.facebook.presto.common.type.RowType)7 ArrayType (com.facebook.presto.common.type.ArrayType)6