Search in sources :

Example 81 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class EffectivePredicateExtractor method pullExpressionThroughSymbols.

private static Expression pullExpressionThroughSymbols(Expression expression, Collection<Symbol> symbols) {
    EqualityInference equalityInference = createEqualityInference(expression);
    ImmutableList.Builder<Expression> effectiveConjuncts = ImmutableList.builder();
    for (Expression conjunct : EqualityInference.nonInferrableConjuncts(expression)) {
        if (DeterminismEvaluator.isDeterministic(conjunct)) {
            Expression rewritten = equalityInference.rewriteExpression(conjunct, in(symbols));
            if (rewritten != null) {
                effectiveConjuncts.add(rewritten);
            }
        }
    }
    effectiveConjuncts.addAll(equalityInference.generateEqualitiesPartitionedBy(in(symbols)).getScopeEqualities());
    return combineConjuncts(effectiveConjuncts.build());
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ImmutableList(com.google.common.collect.ImmutableList) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) EqualityInference.createEqualityInference(com.facebook.presto.sql.planner.EqualityInference.createEqualityInference)

Example 82 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class EffectivePredicateExtractor method visitJoin.

@Override
public Expression visitJoin(JoinNode node, Void context) {
    Expression leftPredicate = node.getLeft().accept(this, context);
    Expression rightPredicate = node.getRight().accept(this, context);
    List<Expression> joinConjuncts = new ArrayList<>();
    for (JoinNode.EquiJoinClause clause : node.getCriteria()) {
        joinConjuncts.add(new ComparisonExpression(ComparisonExpressionType.EQUAL, clause.getLeft().toSymbolReference(), clause.getRight().toSymbolReference()));
    }
    switch(node.getType()) {
        case INNER:
            return combineConjuncts(ImmutableList.<Expression>builder().add(pullExpressionThroughSymbols(leftPredicate, node.getOutputSymbols())).add(pullExpressionThroughSymbols(rightPredicate, node.getOutputSymbols())).addAll(pullExpressionsThroughSymbols(joinConjuncts, node.getOutputSymbols())).build());
        case LEFT:
            return combineConjuncts(ImmutableList.<Expression>builder().add(pullExpressionThroughSymbols(leftPredicate, node.getOutputSymbols())).addAll(pullNullableConjunctsThroughOuterJoin(extractConjuncts(rightPredicate), node.getOutputSymbols(), node.getRight().getOutputSymbols()::contains)).addAll(pullNullableConjunctsThroughOuterJoin(joinConjuncts, node.getOutputSymbols(), node.getRight().getOutputSymbols()::contains)).build());
        case RIGHT:
            return combineConjuncts(ImmutableList.<Expression>builder().add(pullExpressionThroughSymbols(rightPredicate, node.getOutputSymbols())).addAll(pullNullableConjunctsThroughOuterJoin(extractConjuncts(leftPredicate), node.getOutputSymbols(), node.getLeft().getOutputSymbols()::contains)).addAll(pullNullableConjunctsThroughOuterJoin(joinConjuncts, node.getOutputSymbols(), node.getLeft().getOutputSymbols()::contains)).build());
        case FULL:
            return combineConjuncts(ImmutableList.<Expression>builder().addAll(pullNullableConjunctsThroughOuterJoin(extractConjuncts(leftPredicate), node.getOutputSymbols(), node.getLeft().getOutputSymbols()::contains)).addAll(pullNullableConjunctsThroughOuterJoin(extractConjuncts(rightPredicate), node.getOutputSymbols(), node.getRight().getOutputSymbols()::contains)).addAll(pullNullableConjunctsThroughOuterJoin(joinConjuncts, node.getOutputSymbols(), node.getLeft().getOutputSymbols()::contains, node.getRight().getOutputSymbols()::contains)).build());
        default:
            throw new UnsupportedOperationException("Unknown join type: " + node.getType());
    }
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) JoinNode(com.facebook.presto.sql.planner.plan.JoinNode) SemiJoinNode(com.facebook.presto.sql.planner.plan.SemiJoinNode) ArrayList(java.util.ArrayList)

Example 83 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class EffectivePredicateExtractor method visitFilter.

@Override
public Expression visitFilter(FilterNode node, Void context) {
    Expression underlyingPredicate = node.getSource().accept(this, context);
    Expression predicate = node.getPredicate();
    // Remove non-deterministic conjuncts
    predicate = stripNonDeterministicConjuncts(predicate);
    return combineConjuncts(predicate, underlyingPredicate);
}
Also used : ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression)

Example 84 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class EqualityInference method rewriteExpression.

private Expression rewriteExpression(Expression expression, Predicate<Symbol> symbolScope, boolean allowFullReplacement) {
    Iterable<Expression> subExpressions = SubExpressionExtractor.extract(expression);
    if (!allowFullReplacement) {
        subExpressions = filter(subExpressions, not(equalTo(expression)));
    }
    ImmutableMap.Builder<Expression, Expression> expressionRemap = ImmutableMap.builder();
    for (Expression subExpression : subExpressions) {
        Expression canonical = getScopedCanonical(subExpression, symbolScope);
        if (canonical != null) {
            expressionRemap.put(subExpression, canonical);
        }
    }
    // Perform a naive single-pass traversal to try to rewrite non-compliant portions of the tree. Prefers to replace
    // larger subtrees over smaller subtrees
    // TODO: this rewrite can probably be made more sophisticated
    Expression rewritten = ExpressionTreeRewriter.rewriteWith(new ExpressionNodeInliner(expressionRemap.build()), expression);
    if (!symbolToExpressionPredicate(symbolScope).apply(rewritten)) {
        // If the rewritten is still not compliant with the symbol scope, just give up
        return null;
    }
    return rewritten;
}
Also used : InListExpression(com.facebook.presto.sql.tree.InListExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 85 with Expression

use of com.facebook.presto.sql.tree.Expression in project presto by prestodb.

the class ExpressionInterpreter method evaluateConstantExpression.

public static Object evaluateConstantExpression(Expression expression, Type expectedType, Metadata metadata, Session session, List<Expression> parameters) {
    ExpressionAnalyzer analyzer = createConstantAnalyzer(metadata, session, parameters);
    analyzer.analyze(expression, Scope.builder().build());
    Type actualType = analyzer.getExpressionTypes().get(expression);
    if (!metadata.getTypeManager().canCoerce(actualType, expectedType)) {
        throw new SemanticException(SemanticErrorCode.TYPE_MISMATCH, expression, String.format("Cannot cast type %s to %s", expectedType.getTypeSignature(), actualType.getTypeSignature()));
    }
    IdentityLinkedHashMap<Expression, Type> coercions = new IdentityLinkedHashMap<>();
    coercions.putAll(analyzer.getExpressionCoercions());
    coercions.put(expression, expectedType);
    return evaluateConstantExpression(expression, coercions, metadata, session, ImmutableSet.of(), parameters);
}
Also used : ComparisonExpressionType(com.facebook.presto.sql.tree.ComparisonExpressionType) RowType(com.facebook.presto.type.RowType) OperatorType(com.facebook.presto.spi.function.OperatorType) Type(com.facebook.presto.spi.type.Type) FunctionType(com.facebook.presto.type.FunctionType) VarcharType.createVarcharType(com.facebook.presto.spi.type.VarcharType.createVarcharType) ArrayType(com.facebook.presto.type.ArrayType) IdentityLinkedHashMap(com.facebook.presto.util.maps.IdentityLinkedHashMap) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SearchedCaseExpression(com.facebook.presto.sql.tree.SearchedCaseExpression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) SimpleCaseExpression(com.facebook.presto.sql.tree.SimpleCaseExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) IfExpression(com.facebook.presto.sql.tree.IfExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) InListExpression(com.facebook.presto.sql.tree.InListExpression) TryExpression(com.facebook.presto.sql.tree.TryExpression) ArithmeticUnaryExpression(com.facebook.presto.sql.tree.ArithmeticUnaryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) NullIfExpression(com.facebook.presto.sql.tree.NullIfExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LiteralInterpreter.toExpression(com.facebook.presto.sql.planner.LiteralInterpreter.toExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) ExpressionAnalyzer(com.facebook.presto.sql.analyzer.ExpressionAnalyzer) SemanticException(com.facebook.presto.sql.analyzer.SemanticException)

Aggregations

Expression (com.facebook.presto.sql.tree.Expression)137 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)74 Test (org.testng.annotations.Test)46 NotExpression (com.facebook.presto.sql.tree.NotExpression)42 InListExpression (com.facebook.presto.sql.tree.InListExpression)40 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)33 Type (com.facebook.presto.spi.type.Type)26 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)26 LiteralInterpreter.toExpression (com.facebook.presto.sql.planner.LiteralInterpreter.toExpression)25 LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)22 ImmutableList (com.google.common.collect.ImmutableList)22 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)19 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)18 Cast (com.facebook.presto.sql.tree.Cast)17 ArrayList (java.util.ArrayList)17 ExtractionResult (com.facebook.presto.sql.planner.DomainTranslator.ExtractionResult)16 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)16 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)16 SubscriptExpression (com.facebook.presto.sql.tree.SubscriptExpression)16 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)15