Search in sources :

Example 11 with RowExpression

use of com.facebook.presto.sql.relational.RowExpression in project presto by prestodb.

the class JoinFilterFunctionCompiler method generateMethodsForLambdaAndTry.

private PreGeneratedExpressions generateMethodsForLambdaAndTry(ClassDefinition containerClassDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, int leftBlocksSize, RowExpression filter) {
    Set<RowExpression> lambdaAndTryExpressions = ImmutableSet.copyOf(extractLambdaAndTryExpressions(filter));
    ImmutableMap.Builder<CallExpression, MethodDefinition> tryMethodMap = ImmutableMap.builder();
    ImmutableMap.Builder<LambdaDefinitionExpression, FieldDefinition> lambdaFieldMap = ImmutableMap.builder();
    int counter = 0;
    for (RowExpression expression : lambdaAndTryExpressions) {
        if (expression instanceof CallExpression) {
            CallExpression tryExpression = (CallExpression) expression;
            verify(!Signatures.TRY.equals(tryExpression.getSignature().getName()));
            Parameter session = arg("session", ConnectorSession.class);
            Parameter leftPosition = arg("leftPosition", int.class);
            Parameter leftBlocks = arg("leftBlocks", Block[].class);
            Parameter rightPosition = arg("rightPosition", int.class);
            Parameter rightBlocks = arg("rightBlocks", Block[].class);
            BytecodeExpressionVisitor innerExpressionVisitor = new BytecodeExpressionVisitor(callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(callSiteBinder, leftPosition, leftBlocks, rightPosition, rightBlocks, leftBlocksSize), metadata.getFunctionRegistry(), new PreGeneratedExpressions(tryMethodMap.build(), lambdaFieldMap.build()));
            List<Parameter> inputParameters = ImmutableList.<Parameter>builder().add(session).add(leftPosition).add(leftBlocks).add(rightPosition).add(rightBlocks).build();
            MethodDefinition tryMethod = defineTryMethod(innerExpressionVisitor, containerClassDefinition, "try_" + counter, inputParameters, Primitives.wrap(tryExpression.getType().getJavaType()), tryExpression, callSiteBinder);
            tryMethodMap.put(tryExpression, tryMethod);
        } else if (expression instanceof LambdaDefinitionExpression) {
            LambdaDefinitionExpression lambdaExpression = (LambdaDefinitionExpression) expression;
            PreGeneratedExpressions preGeneratedExpressions = new PreGeneratedExpressions(tryMethodMap.build(), lambdaFieldMap.build());
            FieldDefinition methodHandleField = LambdaBytecodeGenerator.preGenerateLambdaExpression(lambdaExpression, "lambda_" + counter, containerClassDefinition, preGeneratedExpressions, callSiteBinder, cachedInstanceBinder, metadata.getFunctionRegistry());
            lambdaFieldMap.put(lambdaExpression, methodHandleField);
        } else {
            throw new VerifyException(format("unexpected expression: %s", expression.toString()));
        }
        counter++;
    }
    return new PreGeneratedExpressions(tryMethodMap.build(), lambdaFieldMap.build());
}
Also used : FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) RowExpression(com.facebook.presto.sql.relational.RowExpression) ImmutableMap(com.google.common.collect.ImmutableMap) VerifyException(com.google.common.base.VerifyException) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) Block(com.facebook.presto.spi.block.Block) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) CallExpression(com.facebook.presto.sql.relational.CallExpression) LambdaDefinitionExpression(com.facebook.presto.sql.relational.LambdaDefinitionExpression)

Example 12 with RowExpression

use of com.facebook.presto.sql.relational.RowExpression in project presto by prestodb.

the class CastCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
    RowExpression argument = arguments.get(0);
    Signature function = generatorContext.getRegistry().getCoercion(argument.getType(), returnType);
    return generatorContext.generateCall(function.getName(), generatorContext.getRegistry().getScalarFunctionImplementation(function), ImmutableList.of(generatorContext.generate(argument)));
}
Also used : Signature(com.facebook.presto.metadata.Signature) RowExpression(com.facebook.presto.sql.relational.RowExpression)

Example 13 with RowExpression

use of com.facebook.presto.sql.relational.RowExpression in project presto by prestodb.

the class CoalesceCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(Signature signature, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
    List<BytecodeNode> operands = new ArrayList<>();
    for (RowExpression expression : arguments) {
        operands.add(generatorContext.generate(expression));
    }
    Variable wasNull = generatorContext.wasNull();
    BytecodeNode nullValue = new BytecodeBlock().append(wasNull.set(constantTrue())).pushJavaDefault(returnType.getJavaType());
    // reverse list because current if statement builder doesn't support if/else so we need to build the if statements bottom up
    for (BytecodeNode operand : Lists.reverse(operands)) {
        IfStatement ifStatement = new IfStatement();
        ifStatement.condition().append(operand).append(wasNull);
        // if value was null, pop the null value, clear the null flag, and process the next operand
        ifStatement.ifTrue().pop(returnType.getJavaType()).append(wasNull.set(constantFalse())).append(nullValue);
        nullValue = ifStatement;
    }
    return nullValue;
}
Also used : IfStatement(com.facebook.presto.bytecode.control.IfStatement) Variable(com.facebook.presto.bytecode.Variable) ArrayList(java.util.ArrayList) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) RowExpression(com.facebook.presto.sql.relational.RowExpression) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode)

Example 14 with RowExpression

use of com.facebook.presto.sql.relational.RowExpression in project presto by prestodb.

the class ExpressionEquivalence method areExpressionsEquivalent.

public boolean areExpressionsEquivalent(Session session, Expression leftExpression, Expression rightExpression, Map<Symbol, Type> types) {
    Map<Symbol, Integer> symbolInput = new HashMap<>();
    Map<Integer, Type> inputTypes = new HashMap<>();
    int inputId = 0;
    for (Entry<Symbol, Type> entry : types.entrySet()) {
        symbolInput.put(entry.getKey(), inputId);
        inputTypes.put(inputId, entry.getValue());
        inputId++;
    }
    RowExpression leftRowExpression = toRowExpression(session, leftExpression, symbolInput, inputTypes);
    RowExpression rightRowExpression = toRowExpression(session, rightExpression, symbolInput, inputTypes);
    RowExpression canonicalizedLeft = leftRowExpression.accept(CANONICALIZATION_VISITOR, null);
    RowExpression canonicalizedRight = rightRowExpression.accept(CANONICALIZATION_VISITOR, null);
    return canonicalizedLeft.equals(canonicalizedRight);
}
Also used : Type(com.facebook.presto.spi.type.Type) HashMap(java.util.HashMap) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) Symbol(com.facebook.presto.sql.planner.Symbol) RowExpression(com.facebook.presto.sql.relational.RowExpression)

Example 15 with RowExpression

use of com.facebook.presto.sql.relational.RowExpression in project presto by prestodb.

the class ExpressionEquivalence method toRowExpression.

private RowExpression toRowExpression(Session session, Expression expression, Map<Symbol, Integer> symbolInput, Map<Integer, Type> inputTypes) {
    // replace qualified names with input references since row expressions do not support these
    Expression expressionWithInputReferences = new SymbolToInputRewriter(symbolInput).rewrite(expression);
    // determine the type of every expression
    IdentityLinkedHashMap<Expression, Type> expressionTypes = getExpressionTypesFromInput(session, metadata, sqlParser, inputTypes, expressionWithInputReferences, emptyList());
    // convert to row expression
    return translate(expressionWithInputReferences, SCALAR, expressionTypes, metadata.getFunctionRegistry(), metadata.getTypeManager(), session, false);
}
Also used : Type(com.facebook.presto.spi.type.Type) LambdaDefinitionExpression(com.facebook.presto.sql.relational.LambdaDefinitionExpression) CallExpression(com.facebook.presto.sql.relational.CallExpression) RowExpression(com.facebook.presto.sql.relational.RowExpression) VariableReferenceExpression(com.facebook.presto.sql.relational.VariableReferenceExpression) ConstantExpression(com.facebook.presto.sql.relational.ConstantExpression) InputReferenceExpression(com.facebook.presto.sql.relational.InputReferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) SymbolToInputRewriter(com.facebook.presto.sql.planner.SymbolToInputRewriter)

Aggregations

RowExpression (com.facebook.presto.sql.relational.RowExpression)29 Signature (com.facebook.presto.metadata.Signature)12 CallExpression (com.facebook.presto.sql.relational.CallExpression)12 ConstantExpression (com.facebook.presto.sql.relational.ConstantExpression)12 Test (org.testng.annotations.Test)9 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)8 Type (com.facebook.presto.spi.type.Type)8 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)7 ArrayList (java.util.ArrayList)7 Variable (com.facebook.presto.bytecode.Variable)6 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)5 Parameter (com.facebook.presto.bytecode.Parameter)5 Scope (com.facebook.presto.bytecode.Scope)5 IfStatement (com.facebook.presto.bytecode.control.IfStatement)5 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableMap (com.google.common.collect.ImmutableMap)5 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)4 FunctionRegistry (com.facebook.presto.metadata.FunctionRegistry)4 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)4 LambdaDefinitionExpression (com.facebook.presto.sql.relational.LambdaDefinitionExpression)4