Search in sources :

Example 1 with LogicalBinaryExpression

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

the class ExpressionUtils method binaryExpression.

public static Expression binaryExpression(LogicalBinaryExpression.Type type, Collection<Expression> expressions) {
    requireNonNull(type, "type is null");
    requireNonNull(expressions, "expressions is null");
    Preconditions.checkArgument(!expressions.isEmpty(), "expressions is empty");
    // Build balanced tree for efficient recursive processing that
    // preserves the evaluation order of the input expressions.
    //
    // The tree is built bottom up by combining pairs of elements into
    // binary AND expressions.
    //
    // Example:
    //
    // Initial state:
    //  a b c d e
    //
    // First iteration:
    //
    //  /\    /\   e
    // a  b  c  d
    //
    // Second iteration:
    //
    //    / \    e
    //  /\   /\
    // a  b c  d
    //
    //
    // Last iteration:
    //
    //      / \
    //    / \  e
    //  /\   /\
    // a  b c  d
    Queue<Expression> queue = new ArrayDeque<>(expressions);
    while (queue.size() > 1) {
        Queue<Expression> buffer = new ArrayDeque<>();
        // combine pairs of elements
        while (queue.size() >= 2) {
            buffer.add(new LogicalBinaryExpression(type, queue.remove(), queue.remove()));
        }
        // if there's and odd number of elements, just append the last one
        if (!queue.isEmpty()) {
            buffer.add(queue.remove());
        }
        // continue processing the pairs that were just built
        queue = buffer;
    }
    return queue.remove();
}
Also used : LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) ArrayDeque(java.util.ArrayDeque)

Example 2 with LogicalBinaryExpression

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

the class AstBuilder method visitLogicalBinary.

@Override
public Node visitLogicalBinary(SqlBaseParser.LogicalBinaryContext context) {
    LogicalBinaryExpression.Operator operator = getLogicalBinaryOperator(context.operator);
    boolean warningForMixedAndOr = false;
    Expression left = (Expression) visit(context.left);
    Expression right = (Expression) visit(context.right);
    if (operator.equals(LogicalBinaryExpression.Operator.OR) && (mixedAndOrOperatorParenthesisCheck(right, context.right, LogicalBinaryExpression.Operator.AND) || mixedAndOrOperatorParenthesisCheck(left, context.left, LogicalBinaryExpression.Operator.AND))) {
        warningForMixedAndOr = true;
    }
    if (operator.equals(LogicalBinaryExpression.Operator.AND) && (mixedAndOrOperatorParenthesisCheck(right, context.right, LogicalBinaryExpression.Operator.OR) || mixedAndOrOperatorParenthesisCheck(left, context.left, LogicalBinaryExpression.Operator.OR))) {
        warningForMixedAndOr = true;
    }
    if (warningForMixedAndOr) {
        warningConsumer.accept(new ParsingWarning("The Query contains OR and AND operator without proper parenthesis. " + "Make sure the operators are guarded by parenthesis in order " + "to fetch logically correct results", context.getStart().getLine(), context.getStart().getCharPositionInLine()));
    }
    return new LogicalBinaryExpression(getLocation(context.operator), operator, left, right);
}
Also used : LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) 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) BindExpression(com.facebook.presto.sql.tree.BindExpression) 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) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression)

Example 3 with LogicalBinaryExpression

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

the class TestSqlParser method testShowStatsForQuery.

@Test
public void testShowStatsForQuery() {
    final String[] tableNames = { "t", "s.t", "c.s.t" };
    for (String fullName : tableNames) {
        QualifiedName qualifiedName = makeQualifiedName(fullName);
        assertStatement(format("SHOW STATS FOR (SELECT * FROM %s)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.empty()));
        assertStatement(format("SHOW STATS FOR (SELECT * FROM %s WHERE field > 0)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.of(new ComparisonExpression(GREATER_THAN, new Identifier("field"), new LongLiteral("0")))));
        assertStatement(format("SHOW STATS FOR (SELECT * FROM %s WHERE field > 0 or field < 0)", qualifiedName), createShowStats(qualifiedName, ImmutableList.of(new AllColumns()), Optional.of(new LogicalBinaryExpression(LogicalBinaryExpression.Operator.OR, new ComparisonExpression(GREATER_THAN, new Identifier("field"), new LongLiteral("0")), new ComparisonExpression(LESS_THAN, new Identifier("field"), new LongLiteral("0"))))));
    }
}
Also used : LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) QuantifiedComparisonExpression(com.facebook.presto.sql.tree.QuantifiedComparisonExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Identifier(com.facebook.presto.sql.tree.Identifier) QueryUtil.quotedIdentifier(com.facebook.presto.sql.QueryUtil.quotedIdentifier) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) AllColumns(com.facebook.presto.sql.tree.AllColumns) Test(org.testng.annotations.Test)

Example 4 with LogicalBinaryExpression

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

the class ExpressionUtils method binaryExpression.

public static Expression binaryExpression(LogicalBinaryExpression.Operator operator, Collection<Expression> expressions) {
    requireNonNull(operator, "operator is null");
    requireNonNull(expressions, "expressions is null");
    if (expressions.isEmpty()) {
        switch(operator) {
            case AND:
                return TRUE_LITERAL;
            case OR:
                return FALSE_LITERAL;
            default:
                throw new IllegalArgumentException("Unsupported LogicalBinaryExpression operator");
        }
    }
    // Build balanced tree for efficient recursive processing that
    // preserves the evaluation order of the input expressions.
    // 
    // The tree is built bottom up by combining pairs of elements into
    // binary AND expressions.
    // 
    // Example:
    // 
    // Initial state:
    // a b c d e
    // 
    // First iteration:
    // 
    // /\    /\   e
    // a  b  c  d
    // 
    // Second iteration:
    // 
    // / \    e
    // /\   /\
    // a  b c  d
    // 
    // 
    // Last iteration:
    // 
    // / \
    // / \  e
    // /\   /\
    // a  b c  d
    Queue<Expression> queue = new ArrayDeque<>(expressions);
    while (queue.size() > 1) {
        Queue<Expression> buffer = new ArrayDeque<>();
        // combine pairs of elements
        while (queue.size() >= 2) {
            buffer.add(new LogicalBinaryExpression(operator, queue.remove(), queue.remove()));
        }
        // if there's and odd number of elements, just append the last one
        if (!queue.isEmpty()) {
            buffer.add(queue.remove());
        }
        // continue processing the pairs that were just built
        queue = buffer;
    }
    return queue.remove();
}
Also used : LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) NotExpression(com.facebook.presto.sql.tree.NotExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) LambdaExpression(com.facebook.presto.sql.tree.LambdaExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) ArrayDeque(java.util.ArrayDeque)

Example 5 with LogicalBinaryExpression

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

the class TreePrinter method print.

public void print(Node root) {
    AstVisitor<Void, Integer> printer = new DefaultTraversalVisitor<Void, Integer>() {

        @Override
        protected Void visitNode(Node node, Integer indentLevel) {
            throw new UnsupportedOperationException("not yet implemented: " + node);
        }

        @Override
        protected Void visitQuery(Query node, Integer indentLevel) {
            print(indentLevel, "Query ");
            indentLevel++;
            print(indentLevel, "QueryBody");
            process(node.getQueryBody(), indentLevel);
            if (node.getOrderBy().isPresent()) {
                print(indentLevel, "OrderBy");
                process(node.getOrderBy().get(), indentLevel + 1);
            }
            if (node.getLimit().isPresent()) {
                print(indentLevel, "Limit: " + node.getLimit().get());
            }
            return null;
        }

        @Override
        protected Void visitQuerySpecification(QuerySpecification node, Integer indentLevel) {
            print(indentLevel, "QuerySpecification ");
            indentLevel++;
            process(node.getSelect(), indentLevel);
            if (node.getFrom().isPresent()) {
                print(indentLevel, "From");
                process(node.getFrom().get(), indentLevel + 1);
            }
            if (node.getWhere().isPresent()) {
                print(indentLevel, "Where");
                process(node.getWhere().get(), indentLevel + 1);
            }
            if (node.getGroupBy().isPresent()) {
                String distinct = "";
                if (node.getGroupBy().get().isDistinct()) {
                    distinct = "[DISTINCT]";
                }
                print(indentLevel, "GroupBy" + distinct);
                for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) {
                    print(indentLevel, "SimpleGroupBy");
                    if (groupingElement instanceof SimpleGroupBy) {
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, indentLevel + 1);
                        }
                    } else if (groupingElement instanceof GroupingSets) {
                        print(indentLevel + 1, "GroupingSets");
                        for (List<Expression> set : ((GroupingSets) groupingElement).getSets()) {
                            print(indentLevel + 2, "GroupingSet[");
                            for (Expression expression : set) {
                                process(expression, indentLevel + 3);
                            }
                            print(indentLevel + 2, "]");
                        }
                    } else if (groupingElement instanceof Cube) {
                        print(indentLevel + 1, "Cube");
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, indentLevel + 1);
                        }
                    } else if (groupingElement instanceof Rollup) {
                        print(indentLevel + 1, "Rollup");
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, indentLevel + 1);
                        }
                    }
                }
            }
            if (node.getHaving().isPresent()) {
                print(indentLevel, "Having");
                process(node.getHaving().get(), indentLevel + 1);
            }
            if (node.getOrderBy().isPresent()) {
                print(indentLevel, "OrderBy");
                process(node.getOrderBy().get(), indentLevel + 1);
            }
            if (node.getLimit().isPresent()) {
                print(indentLevel, "Limit: " + node.getLimit().get());
            }
            return null;
        }

        protected Void visitOrderBy(OrderBy node, Integer indentLevel) {
            for (SortItem sortItem : node.getSortItems()) {
                process(sortItem, indentLevel);
            }
            return null;
        }

        @Override
        protected Void visitSelect(Select node, Integer indentLevel) {
            String distinct = "";
            if (node.isDistinct()) {
                distinct = "[DISTINCT]";
            }
            print(indentLevel, "Select" + distinct);
            // visit children
            super.visitSelect(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitAllColumns(AllColumns node, Integer indent) {
            if (node.getPrefix().isPresent()) {
                print(indent, node.getPrefix() + ".*");
            } else {
                print(indent, "*");
            }
            return null;
        }

        @Override
        protected Void visitSingleColumn(SingleColumn node, Integer indent) {
            if (node.getAlias().isPresent()) {
                print(indent, "Alias: " + node.getAlias().get());
            }
            // visit children
            super.visitSingleColumn(node, indent + 1);
            return null;
        }

        @Override
        protected Void visitComparisonExpression(ComparisonExpression node, Integer indentLevel) {
            print(indentLevel, node.getOperator().toString());
            super.visitComparisonExpression(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitArithmeticBinary(ArithmeticBinaryExpression node, Integer indentLevel) {
            print(indentLevel, node.getOperator().toString());
            super.visitArithmeticBinary(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitLogicalBinaryExpression(LogicalBinaryExpression node, Integer indentLevel) {
            print(indentLevel, node.getOperator().toString());
            super.visitLogicalBinaryExpression(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitStringLiteral(StringLiteral node, Integer indentLevel) {
            print(indentLevel, "String[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitBinaryLiteral(BinaryLiteral node, Integer indentLevel) {
            print(indentLevel, "Binary[" + node.toHexString() + "]");
            return null;
        }

        @Override
        protected Void visitBooleanLiteral(BooleanLiteral node, Integer indentLevel) {
            print(indentLevel, "Boolean[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitLongLiteral(LongLiteral node, Integer indentLevel) {
            print(indentLevel, "Long[" + node.getValue() + "]");
            return null;
        }

        @Override
        protected Void visitLikePredicate(LikePredicate node, Integer indentLevel) {
            print(indentLevel, "LIKE");
            super.visitLikePredicate(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitIdentifier(Identifier node, Integer indentLevel) {
            QualifiedName resolved = resolvedNameReferences.get(node);
            String resolvedName = "";
            if (resolved != null) {
                resolvedName = "=>" + resolved.toString();
            }
            print(indentLevel, "Identifier[" + node.getValue() + resolvedName + "]");
            return null;
        }

        @Override
        protected Void visitDereferenceExpression(DereferenceExpression node, Integer indentLevel) {
            QualifiedName resolved = resolvedNameReferences.get(node);
            String resolvedName = "";
            if (resolved != null) {
                resolvedName = "=>" + resolved.toString();
            }
            print(indentLevel, "DereferenceExpression[" + node + resolvedName + "]");
            return null;
        }

        @Override
        protected Void visitFunctionCall(FunctionCall node, Integer indentLevel) {
            String name = Joiner.on('.').join(node.getName().getParts());
            print(indentLevel, "FunctionCall[" + name + "]");
            super.visitFunctionCall(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitTable(Table node, Integer indentLevel) {
            String name = Joiner.on('.').join(node.getName().getParts());
            print(indentLevel, "Table[" + name + "]");
            return null;
        }

        @Override
        protected Void visitValues(Values node, Integer indentLevel) {
            print(indentLevel, "Values");
            super.visitValues(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitRow(Row node, Integer indentLevel) {
            print(indentLevel, "Row");
            super.visitRow(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitAliasedRelation(AliasedRelation node, Integer indentLevel) {
            print(indentLevel, "Alias[" + node.getAlias() + "]");
            super.visitAliasedRelation(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitSampledRelation(SampledRelation node, Integer indentLevel) {
            print(indentLevel, "TABLESAMPLE[" + node.getType() + " (" + node.getSamplePercentage() + ")]");
            super.visitSampledRelation(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitTableSubquery(TableSubquery node, Integer indentLevel) {
            print(indentLevel, "SubQuery");
            super.visitTableSubquery(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitInPredicate(InPredicate node, Integer indentLevel) {
            print(indentLevel, "IN");
            super.visitInPredicate(node, indentLevel + 1);
            return null;
        }

        @Override
        protected Void visitSubqueryExpression(SubqueryExpression node, Integer indentLevel) {
            print(indentLevel, "SubQuery");
            super.visitSubqueryExpression(node, indentLevel + 1);
            return null;
        }
    };
    printer.process(root, 0);
}
Also used : ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) SimpleGroupBy(com.facebook.presto.sql.tree.SimpleGroupBy) Query(com.facebook.presto.sql.tree.Query) Rollup(com.facebook.presto.sql.tree.Rollup) BooleanLiteral(com.facebook.presto.sql.tree.BooleanLiteral) Node(com.facebook.presto.sql.tree.Node) Values(com.facebook.presto.sql.tree.Values) AllColumns(com.facebook.presto.sql.tree.AllColumns) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) QuerySpecification(com.facebook.presto.sql.tree.QuerySpecification) SortItem(com.facebook.presto.sql.tree.SortItem) Identifier(com.facebook.presto.sql.tree.Identifier) List(java.util.List) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) SampledRelation(com.facebook.presto.sql.tree.SampledRelation) GroupingSets(com.facebook.presto.sql.tree.GroupingSets) OrderBy(com.facebook.presto.sql.tree.OrderBy) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) Table(com.facebook.presto.sql.tree.Table) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) QualifiedName(com.facebook.presto.sql.tree.QualifiedName) DefaultTraversalVisitor(com.facebook.presto.sql.tree.DefaultTraversalVisitor) SingleColumn(com.facebook.presto.sql.tree.SingleColumn) LikePredicate(com.facebook.presto.sql.tree.LikePredicate) TableSubquery(com.facebook.presto.sql.tree.TableSubquery) InPredicate(com.facebook.presto.sql.tree.InPredicate) GroupingElement(com.facebook.presto.sql.tree.GroupingElement) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) BinaryLiteral(com.facebook.presto.sql.tree.BinaryLiteral) StringLiteral(com.facebook.presto.sql.tree.StringLiteral) SubqueryExpression(com.facebook.presto.sql.tree.SubqueryExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) LogicalBinaryExpression(com.facebook.presto.sql.tree.LogicalBinaryExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) Cube(com.facebook.presto.sql.tree.Cube) Select(com.facebook.presto.sql.tree.Select) Row(com.facebook.presto.sql.tree.Row) AliasedRelation(com.facebook.presto.sql.tree.AliasedRelation)

Aggregations

LogicalBinaryExpression (com.facebook.presto.sql.tree.LogicalBinaryExpression)7 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)6 Expression (com.facebook.presto.sql.tree.Expression)5 NotExpression (com.facebook.presto.sql.tree.NotExpression)4 ArithmeticBinaryExpression (com.facebook.presto.sql.tree.ArithmeticBinaryExpression)3 DereferenceExpression (com.facebook.presto.sql.tree.DereferenceExpression)3 LambdaExpression (com.facebook.presto.sql.tree.LambdaExpression)3 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)3 AllColumns (com.facebook.presto.sql.tree.AllColumns)2 Identifier (com.facebook.presto.sql.tree.Identifier)2 QualifiedName (com.facebook.presto.sql.tree.QualifiedName)2 QuantifiedComparisonExpression (com.facebook.presto.sql.tree.QuantifiedComparisonExpression)2 SubqueryExpression (com.facebook.presto.sql.tree.SubqueryExpression)2 ArrayDeque (java.util.ArrayDeque)2 NullableValue (com.facebook.presto.common.predicate.NullableValue)1 TupleDomain (com.facebook.presto.common.predicate.TupleDomain)1 MaterializedViewStatus (com.facebook.presto.spi.MaterializedViewStatus)1 SchemaTableName (com.facebook.presto.spi.SchemaTableName)1 QueryUtil.quotedIdentifier (com.facebook.presto.sql.QueryUtil.quotedIdentifier)1 LiteralEncoder (com.facebook.presto.sql.planner.LiteralEncoder)1