Package com.facebook.presto.metadata

Examples of com.facebook.presto.metadata.FunctionInfo


        }

        Type type = arguments.get(0).getType();
        Class<?> javaType = type.getJavaType();

        FunctionInfo hashCodeFunction = generatorContext.getRegistry().resolveOperator(OperatorType.HASH_CODE, ImmutableList.of(type));

        ImmutableListMultimap.Builder<Integer, ByteCodeNode> hashBucketsBuilder = ImmutableListMultimap.builder();
        ImmutableList.Builder<ByteCodeNode> defaultBucket = ImmutableList.builder();
        ImmutableSet.Builder<Object> constantValuesBuilder = ImmutableSet.builder();

        for (RowExpression testValue : values) {
            ByteCodeNode testByteCode = generatorContext.generate(testValue);

            if (testValue instanceof ConstantExpression && ((ConstantExpression) testValue).getValue() != null) {
                ConstantExpression constant = (ConstantExpression) testValue;
                Object object = constant.getValue();
                constantValuesBuilder.add(object);

                try {
                    int hashCode = ((Long) hashCodeFunction.getMethodHandle().invoke(object)).intValue();
                    hashBucketsBuilder.put(hashCode, testByteCode);
                }
                catch (Throwable throwable) {
                    throw new IllegalArgumentException("Error processing IN statement: error calculating hash code for " + object, throwable);
                }
            }
            else {
                defaultBucket.add(testByteCode);
            }
        }
        ImmutableListMultimap<Integer, ByteCodeNode> hashBuckets = hashBucketsBuilder.build();
        ImmutableSet<Object> constantValues = constantValuesBuilder.build();

        LabelNode end = new LabelNode("end");
        LabelNode match = new LabelNode("match");
        LabelNode noMatch = new LabelNode("noMatch");

        LabelNode defaultLabel = new LabelNode("default");

        CompilerContext context = generatorContext.getContext();

        ByteCodeNode switchBlock;
        if (constantValues.size() < 1000) {
            Block switchCaseBlocks = new Block(context);
            LookupSwitch.LookupSwitchBuilder switchBuilder = lookupSwitchBuilder();
            for (Map.Entry<Integer, Collection<ByteCodeNode>> bucket : hashBuckets.asMap().entrySet()) {
                LabelNode label = new LabelNode("inHash" + bucket.getKey());
                switchBuilder.addCase(bucket.getKey(), label);
                Collection<ByteCodeNode> testValues = bucket.getValue();

                Block caseBlock = buildInCase(generatorContext, context, type, label, match, defaultLabel, testValues, false);
                switchCaseBlocks
                        .append(caseBlock.setDescription("case " + bucket.getKey()));
            }
            switchBuilder.defaultCase(defaultLabel);

            Binding hashCodeBinding = generatorContext
                    .getCallSiteBinder()
                    .bind(hashCodeFunction.getMethodHandle());

            switchBlock = new Block(context)
                    .comment("lookupSwitch(hashCode(<stackValue>))")
                    .dup(javaType)
                    .append(invoke(generatorContext.getContext(), hashCodeBinding, hashCodeFunction.getSignature()))
                    .longToInt()
                    .append(switchBuilder.build())
                    .append(switchCaseBlocks);
        }
        else {
View Full Code Here


        }

        @Override
        public RowExpression visitCall(CallExpression call, final Void context)
        {
            FunctionInfo function;
            Signature signature = call.getSignature();

            if (signature.getName().equals(CAST)) {
                if (call.getArguments().get(0).getType().equals(UnknownType.UNKNOWN)) {
                    return constantNull(call.getType());
                }
                function = registry.getCoercion(call.getArguments().get(0).getType(), call.getType());
            }
            else {
                switch (signature.getName()) {
                    // TODO: optimize these special forms
                    case IF:
                    case NULL_IF:
                    case SWITCH:
                    case TRY_CAST:
                    case IS_NULL:
                    case "IS_DISTINCT_FROM":
                    case COALESCE:
                    case "AND":
                    case "OR":
                    case IN:
                        return call;
                    default:
                        function = registry.getExactFunction(signature);
                        if (function == null) {
                            // TODO: temporary hack to deal with magic timestamp literal functions which don't have an "exact" form and need to be "resolved"
                            function = registry.resolveFunction(QualifiedName.of(signature.getName()), signature.getArgumentTypes(), false);
                        }
                }
            }

            List<RowExpression> arguments = Lists.transform(call.getArguments(), new Function<RowExpression, RowExpression>()
            {
                @Override
                public RowExpression apply(RowExpression input)
                {
                    return input.accept(Visitor.this, context);
                }
            });

            if (Iterables.all(arguments, instanceOf(ConstantExpression.class)) && function.isDeterministic()) {
                MethodHandle method = function.getMethodHandle();

                if (method.type().parameterCount() > 0 && method.type().parameterType(0) == ConnectorSession.class) {
                    method = method.bindTo(session);
                }
View Full Code Here

                    .putVariable("wasNull");
        }

        elseBlock.gotoLabel(noMatchLabel);

        FunctionInfo operator = generatorContext.getRegistry().resolveOperator(OperatorType.EQUAL, ImmutableList.of(type, type));

        Binding equalsFunction = generatorContext
                .getCallSiteBinder()
                .bind(operator.getMethodHandle());

        ByteCodeNode elseNode = elseBlock;
        for (ByteCodeNode testNode : testValues) {
            LabelNode testLabel = new LabelNode("test");
            IfStatement.IfStatementBuilder test = ifStatementBuilder(context);

            Block condition = new Block(context)
                    .visitLabel(testLabel)
                    .dup(type.getJavaType())
                    .append(testNode);

            if (checkForNulls) {
                condition.getVariable("wasNull")
                        .putVariable(caseWasNull)
                        .append(ifWasNullPopAndGoto(context, elseLabel, void.class, type.getJavaType(), type.getJavaType()));
            }
            condition.append(invoke(generatorContext.getContext(), equalsFunction, operator.getSignature()));
            test.condition(condition);

            test.ifTrue(new Block(context).gotoLabel(matchLabel));
            test.ifFalse(elseNode);
View Full Code Here

    @Override
    public ByteCodeNode generateExpression(Signature signature, ByteCodeGeneratorContext context, Type returnType, List<RowExpression> arguments)
    {
        FunctionRegistry registry = context.getRegistry();

        FunctionInfo function = registry.getExactFunction(signature);
        if (function == null) {
            // TODO: temporary hack to deal with magic timestamp literal functions which don't have an "exact" form and need to be "resolved"
            function = registry.resolveFunction(QualifiedName.of(signature.getName()), signature.getArgumentTypes(), false);
        }
View Full Code Here

                    throw new UnsupportedOperationException("Unhandled value type: " + value.getClass().getName());
                }
                argumentValues.add(value);
                argumentTypes.add(type);
            }
            FunctionInfo function = metadata.getFunction(node.getName(), argumentTypes, false);
            // do not optimize non-deterministic functions
            if (optimize && !function.isDeterministic()) {
                return new FunctionCall(node.getName(), node.getWindow().orNull(), node.isDistinct(), toExpressions(argumentValues));
            }
            MethodHandle handle = function.getScalarFunction();
            if (handle.type().parameterCount() > 0 && handle.type().parameterType(0) == Session.class) {
                handle = handle.bindTo(session);
            }
            try {
                return handle.invokeWithArguments(argumentValues);
View Full Code Here

                String columnName = columnHandles.get(columnHandle);
                String value = null;
                if (entry.getValue() != null) {
                    ColumnMetadata columnMetadata  = metadata.getColumnMetadata(tableHandle.get(), columnHandle);
                    try {
                        FunctionInfo operator = metadata.getExactOperator(OperatorType.CAST, VarcharType.VARCHAR, ImmutableList.of(columnMetadata.getType()));
                        value = ((Slice) operator.getMethodHandle().invokeWithArguments(entry.getValue())).toStringUtf8();
                    }
                    catch (OperatorNotFoundException e) {
                        value = "<UNREPRESENTABLE VALUE>";
                    }
                    catch (Throwable throwable) {
View Full Code Here

            ImmutableList.Builder<Type> argumentTypes = ImmutableList.builder();
            for (Expression expression : node.getArguments()) {
                argumentTypes.add(process(expression, context));
            }

            FunctionInfo function = metadata.resolveFunction(node.getName(), argumentTypes.build(), context.isApproximate());
            for (int i = 0; i < node.getArguments().size(); i++) {
                Expression expression = node.getArguments().get(i);
                Type type = function.getArgumentTypes().get(i);
                coerceType(context, expression, type, String.format("Function %s argument %d", function.getSignature(), i));
            }
            resolvedFunctions.put(node, function);

            expressionTypes.put(node, function.getReturnType());

            return function.getReturnType();
        }
View Full Code Here

            ImmutableList.Builder<Type> argumentTypes = ImmutableList.builder();
            for (Expression expression : arguments) {
                argumentTypes.add(process(expression, context));
            }

            FunctionInfo operatorInfo;
            try {
                operatorInfo = metadata.resolveOperator(operatorType, argumentTypes.build());
            }
            catch (OperatorNotFoundException e) {
                throw new SemanticException(TYPE_MISMATCH, node, e.getMessage());
            }

            for (int i = 0; i < arguments.length; i++) {
                Expression expression = arguments[i];
                Type type = operatorInfo.getArgumentTypes().get(i);
                coerceType(context, expression, type, String.format("Operator %s argument %d", operatorInfo, i));
            }

            expressionTypes.put(node, operatorInfo.getReturnType());

            return operatorInfo.getReturnType();
        }
View Full Code Here

                {
                    return analysis.getType(input);
                }
            });

            FunctionInfo info = metadata.resolveFunction(windowFunction.getName(), argumentTypes, false);
            if (!info.isWindow()) {
                throw new SemanticException(MUST_BE_WINDOW_FUNCTION, node, "Not a window function: %s", windowFunction.getName());
            }
        }

        analysis.setWindowFunctions(node, windowFunctions);
View Full Code Here

        this.metadata = checkNotNull(metadata, "metadata is null");
    }

    public FunctionBinding bindFunction(QualifiedName name, ByteCodeNode getSessionByteCode, List<ByteCodeNode> arguments, List<Type> argumentTypes)
    {
        FunctionInfo function = metadata.resolveFunction(name, argumentTypes, false);
        checkArgument(function != null, "Unknown function %s%s", name, argumentTypes);

        return bindFunction(name.toString(), getSessionByteCode, arguments, function.getFunctionBinder());
    }
View Full Code Here

TOP

Related Classes of com.facebook.presto.metadata.FunctionInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.