Search in sources :

Example 71 with MethodHandle

use of java.lang.invoke.MethodHandle in project groovy by apache.

the class IndyArrayAccess method buildSetter.

private static MethodHandle buildSetter(Class arrayClass) {
    MethodHandle set = MethodHandles.arrayElementSetter(arrayClass);
    MethodHandle fallback = MethodHandles.explicitCastArguments(set, set.type().changeParameterType(0, Object.class));
    fallback = MethodHandles.dropArguments(fallback, 3, int.class);
    MethodType reorderType = fallback.type().insertParameterTypes(0, int.class).dropParameterTypes(4, 5);
    fallback = MethodHandles.permuteArguments(fallback, reorderType, 1, 0, 3, 0);
    fallback = MethodHandles.foldArguments(fallback, normalizeIndex);
    fallback = MethodHandles.explicitCastArguments(fallback, set.type());
    MethodHandle guard = MethodHandles.dropArguments(notNegative, 0, arrayClass);
    MethodHandle handle = MethodHandles.guardWithTest(guard, set, fallback);
    return handle;
}
Also used : MethodType(java.lang.invoke.MethodType) MethodHandle(java.lang.invoke.MethodHandle)

Example 72 with MethodHandle

use of java.lang.invoke.MethodHandle in project groovy by apache.

the class IndyArrayAccess method arraySet.

public static MethodHandle arraySet(MethodType type) {
    Class key = type.parameterType(0);
    MethodHandle res = setterMap.get(key);
    if (res != null)
        return res;
    res = buildSetter(key);
    res = MethodHandles.explicitCastArguments(res, type);
    return res;
}
Also used : MethodHandle(java.lang.invoke.MethodHandle)

Example 73 with MethodHandle

use of java.lang.invoke.MethodHandle in project presto by prestodb.

the class PolymorphicScalarFunction method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    List<TypeSignature> resolvedParameterTypeSignatures = applyBoundVariables(getSignature().getArgumentTypes(), boundVariables);
    List<Type> resolvedParameterTypes = resolveTypes(resolvedParameterTypeSignatures, typeManager);
    TypeSignature resolvedReturnTypeSignature = applyBoundVariables(getSignature().getReturnType(), boundVariables);
    Type resolvedReturnType = typeManager.getType(resolvedReturnTypeSignature);
    SpecializeContext context = new SpecializeContext(boundVariables, resolvedParameterTypes, resolvedReturnType, typeManager, functionRegistry);
    Optional<Method> matchingMethod = Optional.empty();
    Optional<MethodsGroup> matchingMethodsGroup = Optional.empty();
    for (MethodsGroup candidateMethodsGroup : methodsGroups) {
        for (Method candidateMethod : candidateMethodsGroup.getMethods()) {
            if (matchesParameterAndReturnTypes(candidateMethod, resolvedParameterTypes, resolvedReturnType) && predicateIsTrue(candidateMethodsGroup, context)) {
                if (matchingMethod.isPresent()) {
                    if (onlyFirstMatchedMethodHasPredicate(matchingMethodsGroup.get(), candidateMethodsGroup)) {
                        continue;
                    }
                    throw new IllegalStateException("two matching methods (" + matchingMethod.get().getName() + " and " + candidateMethod.getName() + ") for parameter types " + resolvedParameterTypeSignatures);
                }
                matchingMethod = Optional.of(candidateMethod);
                matchingMethodsGroup = Optional.of(candidateMethodsGroup);
            }
        }
    }
    checkState(matchingMethod.isPresent(), "no matching method for parameter types %s", resolvedParameterTypes);
    List<Object> extraParameters = computeExtraParameters(matchingMethodsGroup.get(), context);
    MethodHandle matchingMethodHandle = applyExtraParameters(matchingMethod.get(), extraParameters);
    return new ScalarFunctionImplementation(nullableResult, nullableArguments, nullFlags, matchingMethodHandle, deterministic);
}
Also used : ScalarFunctionImplementation(com.facebook.presto.operator.scalar.ScalarFunctionImplementation) MethodsGroup(com.facebook.presto.metadata.SqlScalarFunctionBuilder.MethodsGroup) Method(java.lang.reflect.Method) SpecializeContext(com.facebook.presto.metadata.SqlScalarFunctionBuilder.SpecializeContext) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Type(com.facebook.presto.spi.type.Type) MethodHandle(java.lang.invoke.MethodHandle)

Example 74 with MethodHandle

use of java.lang.invoke.MethodHandle in project presto by prestodb.

the class AbstractMinMaxAggregationFunction method specialize.

@Override
public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    Type type = boundVariables.getTypeVariable("E");
    MethodHandle compareMethodHandle = functionRegistry.getScalarFunctionImplementation(internalOperator(operatorType, BOOLEAN, ImmutableList.of(type, type))).getMethodHandle();
    return generateAggregation(type, compareMethodHandle);
}
Also used : Type(com.facebook.presto.spi.type.Type) OperatorType(com.facebook.presto.spi.function.OperatorType) MethodHandle(java.lang.invoke.MethodHandle)

Example 75 with MethodHandle

use of java.lang.invoke.MethodHandle in project presto by prestodb.

the class AbstractMinMaxAggregationFunction method generateAggregation.

protected InternalAggregationFunction generateAggregation(Type type, MethodHandle compareMethodHandle) {
    DynamicClassLoader classLoader = new DynamicClassLoader(AbstractMinMaxAggregationFunction.class.getClassLoader());
    List<Type> inputTypes = ImmutableList.of(type);
    MethodHandle inputFunction;
    MethodHandle combineFunction;
    MethodHandle outputFunction;
    Class<? extends AccumulatorState> stateInterface;
    AccumulatorStateSerializer<?> stateSerializer;
    if (type.getJavaType() == long.class) {
        stateInterface = NullableLongState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = LONG_INPUT_FUNCTION;
        combineFunction = LONG_COMBINE_FUNCTION;
        outputFunction = LONG_OUTPUT_FUNCTION;
    } else if (type.getJavaType() == double.class) {
        stateInterface = NullableDoubleState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = DOUBLE_INPUT_FUNCTION;
        combineFunction = DOUBLE_COMBINE_FUNCTION;
        outputFunction = DOUBLE_OUTPUT_FUNCTION;
    } else if (type.getJavaType() == Slice.class) {
        stateInterface = SliceState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = SLICE_INPUT_FUNCTION;
        combineFunction = SLICE_COMBINE_FUNCTION;
        outputFunction = SLICE_OUTPUT_FUNCTION;
    } else if (type.getJavaType() == boolean.class) {
        stateInterface = NullableBooleanState.class;
        stateSerializer = StateCompiler.generateStateSerializer(stateInterface, classLoader);
        inputFunction = BOOLEAN_INPUT_FUNCTION;
        combineFunction = BOOLEAN_COMBINE_FUNCTION;
        outputFunction = BOOLEAN_OUTPUT_FUNCTION;
    } else {
        stateInterface = BlockState.class;
        stateSerializer = new BlockStateSerializer(type);
        inputFunction = BLOCK_INPUT_FUNCTION;
        combineFunction = BLOCK_COMBINE_FUNCTION;
        outputFunction = BLOCK_OUTPUT_FUNCTION;
    }
    inputFunction = inputFunction.bindTo(compareMethodHandle);
    combineFunction = combineFunction.bindTo(compareMethodHandle);
    outputFunction = outputFunction.bindTo(type);
    AccumulatorStateFactory<?> stateFactory = StateCompiler.generateStateFactory(stateInterface, classLoader);
    Type intermediateType = stateSerializer.getSerializedType();
    AggregationMetadata metadata = new AggregationMetadata(generateAggregationName(getSignature().getName(), type.getTypeSignature(), inputTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())), createParameterMetadata(type), inputFunction, combineFunction, outputFunction, stateInterface, stateSerializer, stateFactory, type);
    GenericAccumulatorFactoryBinder factory = AccumulatorCompiler.generateAccumulatorFactoryBinder(metadata, classLoader);
    return new InternalAggregationFunction(getSignature().getName(), inputTypes, intermediateType, type, true, factory);
}
Also used : DynamicClassLoader(com.facebook.presto.bytecode.DynamicClassLoader) BlockStateSerializer(com.facebook.presto.operator.aggregation.state.BlockStateSerializer) NullableBooleanState(com.facebook.presto.operator.aggregation.state.NullableBooleanState) Type(com.facebook.presto.spi.type.Type) OperatorType(com.facebook.presto.spi.function.OperatorType) BlockState(com.facebook.presto.operator.aggregation.state.BlockState) NullableDoubleState(com.facebook.presto.operator.aggregation.state.NullableDoubleState) MethodHandle(java.lang.invoke.MethodHandle)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)302 Test (org.junit.Test)101 MethodType (java.lang.invoke.MethodType)44 Type (com.facebook.presto.spi.type.Type)37 Method (java.lang.reflect.Method)18 OperatorType (com.facebook.presto.spi.function.OperatorType)14 MethodHandles (java.lang.invoke.MethodHandles)13 DynamicClassLoader (com.facebook.presto.bytecode.DynamicClassLoader)11 Signature (com.facebook.presto.metadata.Signature)10 CallSite (java.lang.invoke.CallSite)10 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)9 ImmutableList (com.google.common.collect.ImmutableList)9 List (java.util.List)8 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)7 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)7 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)6 Parameter (com.facebook.presto.bytecode.Parameter)6 PrestoException (com.facebook.presto.spi.PrestoException)6 ComponentInjectionException (org.neo4j.kernel.api.exceptions.ComponentInjectionException)6 ProcedureException (org.neo4j.kernel.api.exceptions.ProcedureException)6