Search in sources :

Example 51 with Signature

use of com.facebook.presto.metadata.Signature in project presto by prestodb.

the class AggregationCompiler method generateBindableAggregationFunctions.

public static List<BindableAggregationFunction> generateBindableAggregationFunctions(Class<?> aggregationDefinition) {
    AggregationFunction aggregationAnnotation = aggregationDefinition.getAnnotation(AggregationFunction.class);
    requireNonNull(aggregationAnnotation, "aggregationAnnotation is null");
    DynamicClassLoader classLoader = new DynamicClassLoader(aggregationDefinition.getClassLoader());
    ImmutableList.Builder<BindableAggregationFunction> builder = ImmutableList.builder();
    for (Class<?> stateClass : getStateClasses(aggregationDefinition)) {
        AccumulatorStateSerializer<?> stateSerializer = StateCompiler.generateStateSerializer(stateClass, classLoader);
        for (Method outputFunction : getOutputFunctions(aggregationDefinition, stateClass)) {
            for (Method inputFunction : getInputFunctions(aggregationDefinition, stateClass)) {
                List<LongVariableConstraint> longVariableConstraints = parseLongVariableConstraints(inputFunction);
                for (String name : getNames(outputFunction, aggregationAnnotation)) {
                    List<TypeSignature> inputTypes = getInputTypesSignatures(inputFunction);
                    TypeSignature outputType = TypeSignature.parseTypeSignature(outputFunction.getAnnotation(OutputFunction.class).value());
                    builder.add(new BindableAggregationFunction(new Signature(name, FunctionKind.AGGREGATE, // TODO parse constrains from annotations
                    ImmutableList.of(), longVariableConstraints, outputType, inputTypes, false), getDescription(aggregationDefinition, outputFunction), aggregationAnnotation.decomposable(), aggregationDefinition, stateClass, inputFunction, outputFunction));
                }
            }
        }
    }
    return builder.build();
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) Method(java.lang.reflect.Method) AggregationFunction(com.facebook.presto.spi.function.AggregationFunction) TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) LongVariableConstraint(com.facebook.presto.metadata.LongVariableConstraint) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)

Example 52 with Signature

use of com.facebook.presto.metadata.Signature in project presto by prestodb.

the class ArrayToArrayCast method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    checkArgument(arity == 1, "Expected arity to be 1");
    Type fromType = boundVariables.getTypeVariable("F");
    Type toType = boundVariables.getTypeVariable("T");
    Signature signature = internalOperator(CAST.name(), toType.getTypeSignature(), ImmutableList.of(fromType.getTypeSignature()));
    ScalarFunctionImplementation function = functionRegistry.getScalarFunctionImplementation(signature);
    Class<?> castOperatorClass = generateArrayCast(typeManager, signature, function);
    MethodHandle methodHandle = methodHandle(castOperatorClass, "castArray", ConnectorSession.class, Block.class);
    return new ScalarFunctionImplementation(false, ImmutableList.of(false), methodHandle, isDeterministic());
}
Also used : Type(com.facebook.presto.spi.type.Type) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) MethodHandle(java.lang.invoke.MethodHandle)

Example 53 with Signature

use of com.facebook.presto.metadata.Signature in project presto by prestodb.

the class TryCastFunction method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    Type fromType = boundVariables.getTypeVariable("F");
    Type toType = boundVariables.getTypeVariable("T");
    Class<?> returnType = Primitives.wrap(toType.getJavaType());
    List<Boolean> nullableArguments;
    MethodHandle tryCastHandle;
    if (fromType.equals(UNKNOWN)) {
        nullableArguments = ImmutableList.of(true);
        tryCastHandle = dropArguments(constant(returnType, null), 0, Void.class);
    } else {
        // the resulting method needs to return a boxed type
        Signature signature = functionRegistry.getCoercion(fromType, toType);
        ScalarFunctionImplementation implementation = functionRegistry.getScalarFunctionImplementation(signature);
        nullableArguments = implementation.getNullableArguments();
        MethodHandle coercion = implementation.getMethodHandle();
        coercion = coercion.asType(methodType(returnType, coercion.type()));
        MethodHandle exceptionHandler = dropArguments(constant(returnType, null), 0, RuntimeException.class);
        tryCastHandle = catchException(coercion, RuntimeException.class, exceptionHandler);
    }
    return new ScalarFunctionImplementation(true, nullableArguments, tryCastHandle, isDeterministic());
}
Also used : MethodType.methodType(java.lang.invoke.MethodType.methodType) Type(com.facebook.presto.spi.type.Type) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) MethodHandle(java.lang.invoke.MethodHandle)

Example 54 with Signature

use of com.facebook.presto.metadata.Signature in project presto by prestodb.

the class ScalarFromAnnotationsParser method parseParametricScalar.

private static SqlScalarFunction parseParametricScalar(ScalarHeaderAndMethods scalar, Map<Set<TypeParameter>, Constructor<?>> constructors) {
    ImmutableMap.Builder<Signature, ScalarImplementation> exactImplementations = ImmutableMap.builder();
    ImmutableList.Builder<ScalarImplementation> specializedImplementations = ImmutableList.builder();
    ImmutableList.Builder<ScalarImplementation> genericImplementations = ImmutableList.builder();
    Optional<Signature> signature = Optional.empty();
    ScalarImplementationHeader header = scalar.getHeader();
    checkArgument(!header.getName().isEmpty());
    for (Method method : scalar.getMethods()) {
        ScalarImplementation implementation = ScalarImplementation.Parser.parseImplementation(header.getName(), method, constructors);
        if (implementation.getSignature().getTypeVariableConstraints().isEmpty() && implementation.getSignature().getArgumentTypes().stream().noneMatch(TypeSignature::isCalculated) && !implementation.getSignature().getReturnType().isCalculated()) {
            exactImplementations.put(implementation.getSignature(), implementation);
            continue;
        }
        if (implementation.hasSpecializedTypeParameters()) {
            specializedImplementations.add(implementation);
        } else {
            genericImplementations.add(implementation);
        }
        signature = signature.isPresent() ? signature : Optional.of(implementation.getSignature());
        validateSignature(signature, implementation.getSignature());
    }
    Signature scalarSignature = signature.orElseGet(() -> getOnlyElement(exactImplementations.build().keySet()));
    header.getOperatorType().ifPresent(operatorType -> validateOperator(operatorType, scalarSignature.getReturnType(), scalarSignature.getArgumentTypes()));
    ScalarImplementations implementations = new ScalarImplementations(exactImplementations.build(), specializedImplementations.build(), genericImplementations.build());
    return new ParametricScalar(scalarSignature, header.getHeader(), implementations);
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) ImmutableList(com.google.common.collect.ImmutableList) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) Method(java.lang.reflect.Method) ParametricScalar(com.facebook.presto.operator.scalar.ParametricScalar) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 55 with Signature

use of com.facebook.presto.metadata.Signature in project presto by prestodb.

the class RowToRowCast method generateRowCast.

private static Class<?> generateRowCast(Type fromType, Type toType, FunctionRegistry functionRegistry) {
    List<Type> toTypes = toType.getTypeParameters();
    List<Type> fromTypes = fromType.getTypeParameters();
    CallSiteBinder binder = new CallSiteBinder();
    // Embed the MD5 hash code of input and output types into the generated class name instead of the raw type names,
    // which could prevent the class name from hitting the length limitation and invalid characters.
    byte[] md5Suffix = Hashing.md5().hashBytes((fromType + "$" + toType).getBytes()).asBytes();
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), CompilerUtils.makeClassName(Joiner.on("$").join("RowCast", BaseEncoding.base16().encode(md5Suffix))), type(Object.class));
    Parameter session = arg("session", ConnectorSession.class);
    Parameter value = arg("value", Block.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "castRow", type(Block.class), session, value);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable wasNull = scope.declareVariable(boolean.class, "wasNull");
    Variable blockBuilder = scope.createTempVariable(BlockBuilder.class);
    body.append(wasNull.set(constantBoolean(false)));
    CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder);
    // create the interleave block builder
    body.newObject(InterleavedBlockBuilder.class).dup().append(constantType(binder, toType).invoke("getTypeParameters", List.class)).append(newInstance(BlockBuilderStatus.class)).append(constantInt(toTypes.size())).invokeConstructor(InterleavedBlockBuilder.class, List.class, BlockBuilderStatus.class, int.class).putVariable(blockBuilder);
    // loop through to append member blocks
    for (int i = 0; i < toTypes.size(); i++) {
        Signature signature = internalOperator(CAST.name(), toTypes.get(i).getTypeSignature(), ImmutableList.of(fromTypes.get(i).getTypeSignature()));
        ScalarFunctionImplementation function = functionRegistry.getScalarFunctionImplementation(signature);
        Type currentFromType = fromTypes.get(i);
        if (currentFromType.equals(UNKNOWN)) {
            body.append(blockBuilder.invoke("appendNull", BlockBuilder.class).pop());
            continue;
        }
        BytecodeExpression fromElement = constantType(binder, currentFromType).getValue(value, constantInt(i));
        BytecodeExpression toElement = invokeFunction(scope, cachedInstanceBinder, signature.getName(), function, fromElement);
        IfStatement ifElementNull = new IfStatement("if the element in the row type is null...");
        ifElementNull.condition(value.invoke("isNull", boolean.class, constantInt(i))).ifTrue(blockBuilder.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(constantType(binder, toTypes.get(i)).writeValue(blockBuilder, toElement));
        body.append(ifElementNull);
    }
    // call blockBuilder.build()
    body.append(blockBuilder.invoke("build", Block.class)).retObject();
    // create constructor
    MethodDefinition constructorDefinition = definition.declareConstructor(a(PUBLIC));
    BytecodeBlock constructorBody = constructorDefinition.getBody();
    Variable thisVariable = constructorDefinition.getThis();
    constructorBody.comment("super();").append(thisVariable).invokeConstructor(Object.class);
    cachedInstanceBinder.generateInitializations(thisVariable, constructorBody);
    constructorBody.ret();
    return defineClass(definition, Object.class, binder.getBindings(), RowToRowCast.class.getClassLoader());
}
Also used : Variable(com.facebook.presto.bytecode.Variable) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Type(com.facebook.presto.spi.type.Type) SqlTypeBytecodeExpression.constantType(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression.constantType) CachedInstanceBinder(com.facebook.presto.sql.gen.CachedInstanceBinder) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) CallSiteBinder(com.facebook.presto.sql.gen.CallSiteBinder) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) Parameter(com.facebook.presto.bytecode.Parameter) Block(com.facebook.presto.spi.block.Block) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) BlockBuilder(com.facebook.presto.spi.block.BlockBuilder) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder)

Aggregations

Signature (com.facebook.presto.metadata.Signature)123 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)96 Test (org.testng.annotations.Test)91 MapType (com.facebook.presto.type.MapType)18 ImmutableList (com.google.common.collect.ImmutableList)16 RowExpression (com.facebook.presto.sql.relational.RowExpression)12 TypeSignature (com.facebook.presto.spi.type.TypeSignature)11 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)11 Block (com.facebook.presto.spi.block.Block)10 Type (com.facebook.presto.spi.type.Type)10 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)8 Page (com.facebook.presto.spi.Page)8 DecimalType (com.facebook.presto.spi.type.DecimalType)8 CallExpression (com.facebook.presto.sql.relational.CallExpression)8 MethodHandle (java.lang.invoke.MethodHandle)8 MetadataManager (com.facebook.presto.metadata.MetadataManager)7 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)7 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)7 ConstantExpression (com.facebook.presto.sql.relational.ConstantExpression)7 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)6