Search in sources :

Example 1 with SubqueryExpression

use of io.prestosql.sql.tree.SubqueryExpression in project hetu-core by openlookeng.

the class SubqueryPlanner method planQuantifiedApplyNode.

private PlanBuilder planQuantifiedApplyNode(PlanBuilder inputSubPlan, QuantifiedComparisonExpression quantifiedComparison, boolean correlationAllowed) {
    PlanBuilder subPlan = inputSubPlan;
    subPlan = subPlan.appendProjections(ImmutableList.of(quantifiedComparison.getValue()), planSymbolAllocator, idAllocator);
    checkState(quantifiedComparison.getSubquery() instanceof SubqueryExpression);
    SubqueryExpression quantifiedSubquery = (SubqueryExpression) quantifiedComparison.getSubquery();
    SubqueryExpression uncoercedQuantifiedSubquery = uncoercedSubquery(quantifiedSubquery);
    PlanBuilder subqueryPlan = createPlanBuilder(uncoercedQuantifiedSubquery);
    subqueryPlan = subqueryPlan.appendProjections(ImmutableList.of(quantifiedSubquery), planSymbolAllocator, idAllocator);
    QuantifiedComparisonExpression coercedQuantifiedComparison = new QuantifiedComparisonExpression(quantifiedComparison.getOperator(), quantifiedComparison.getQuantifier(), toSymbolReference(subPlan.translate(quantifiedComparison.getValue())), toSymbolReference(subqueryPlan.translate(quantifiedSubquery)));
    Symbol coercedQuantifiedComparisonSymbol = planSymbolAllocator.newSymbol(coercedQuantifiedComparison, BOOLEAN);
    subPlan.getTranslations().put(quantifiedComparison, coercedQuantifiedComparisonSymbol);
    return appendApplyNode(subPlan, quantifiedComparison.getSubquery(), subqueryPlan.getRoot(), Assignments.of(coercedQuantifiedComparisonSymbol, castToRowExpression(coercedQuantifiedComparison)), correlationAllowed);
}
Also used : Symbol(io.prestosql.spi.plan.Symbol) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression)

Example 2 with SubqueryExpression

use of io.prestosql.sql.tree.SubqueryExpression in project hetu-core by openlookeng.

the class SubqueryPlanner method appendScalarSubqueryApplyNode.

private PlanBuilder appendScalarSubqueryApplyNode(PlanBuilder subPlan, SubqueryExpression scalarSubquery, boolean correlationAllowed) {
    if (subPlan.canTranslate(scalarSubquery)) {
        // given subquery is already appended
        return subPlan;
    }
    List<Expression> coercions = coercionsFor(scalarSubquery);
    SubqueryExpression uncoercedScalarSubquery = uncoercedSubquery(scalarSubquery);
    PlanBuilder subqueryPlan = createPlanBuilder(uncoercedScalarSubquery);
    subqueryPlan = subqueryPlan.withNewRoot(new EnforceSingleRowNode(idAllocator.getNextId(), subqueryPlan.getRoot()));
    subqueryPlan = subqueryPlan.appendProjections(coercions, planSymbolAllocator, idAllocator);
    Symbol uncoercedScalarSubquerySymbol = subqueryPlan.translate(uncoercedScalarSubquery);
    subPlan.getTranslations().put(uncoercedScalarSubquery, uncoercedScalarSubquerySymbol);
    for (Expression coercion : coercions) {
        Symbol coercionSymbol = subqueryPlan.translate(coercion);
        subPlan.getTranslations().put(coercion, coercionSymbol);
    }
    // The subquery's EnforceSingleRowNode always produces a row, so the join is effectively INNER
    return appendLateralJoin(subPlan, subqueryPlan, scalarSubquery.getQuery(), correlationAllowed, LateralJoinNode.Type.INNER, TRUE_LITERAL);
}
Also used : OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) ExpressionNodeInliner.replaceExpression(io.prestosql.sql.planner.ExpressionNodeInliner.replaceExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) NotExpression(io.prestosql.sql.tree.NotExpression) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) RowExpression(io.prestosql.spi.relation.RowExpression) Expression(io.prestosql.sql.tree.Expression) Symbol(io.prestosql.spi.plan.Symbol) EnforceSingleRowNode(io.prestosql.sql.planner.plan.EnforceSingleRowNode) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression)

Example 3 with SubqueryExpression

use of io.prestosql.sql.tree.SubqueryExpression in project hetu-core by openlookeng.

the class SubqueryPlanner method appendInPredicateApplyNode.

private PlanBuilder appendInPredicateApplyNode(PlanBuilder inputSubPlan, InPredicate inPredicate, boolean correlationAllowed, Node node) {
    PlanBuilder subPlan = inputSubPlan;
    if (subPlan.canTranslate(inPredicate)) {
        // given subquery is already appended
        return subPlan;
    }
    subPlan = handleSubqueries(subPlan, inPredicate.getValue(), node);
    subPlan = subPlan.appendProjections(ImmutableList.of(inPredicate.getValue()), planSymbolAllocator, idAllocator);
    checkState(inPredicate.getValueList() instanceof SubqueryExpression);
    SubqueryExpression valueListSubquery = (SubqueryExpression) inPredicate.getValueList();
    SubqueryExpression uncoercedValueListSubquery = uncoercedSubquery(valueListSubquery);
    PlanBuilder subqueryPlan = createPlanBuilder(uncoercedValueListSubquery);
    subqueryPlan = subqueryPlan.appendProjections(ImmutableList.of(valueListSubquery), planSymbolAllocator, idAllocator);
    SymbolReference valueList = toSymbolReference(subqueryPlan.translate(valueListSubquery));
    Symbol rewrittenValue = subPlan.translate(inPredicate.getValue());
    InPredicate inPredicateSubqueryExpression = new InPredicate(toSymbolReference(rewrittenValue), valueList);
    Symbol inPredicateSubquerySymbol = planSymbolAllocator.newSymbol(inPredicateSubqueryExpression, BOOLEAN);
    subPlan.getTranslations().put(inPredicate, inPredicateSubquerySymbol);
    return appendApplyNode(subPlan, inPredicate, subqueryPlan.getRoot(), Assignments.of(inPredicateSubquerySymbol, castToRowExpression(inPredicateSubqueryExpression)), correlationAllowed);
}
Also used : SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) SymbolReference(io.prestosql.sql.tree.SymbolReference) Symbol(io.prestosql.spi.plan.Symbol) InPredicate(io.prestosql.sql.tree.InPredicate) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression)

Example 4 with SubqueryExpression

use of io.prestosql.sql.tree.SubqueryExpression in project hetu-core by openlookeng.

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 ");
            Integer tmpIndentLevel = indentLevel;
            tmpIndentLevel++;
            print(tmpIndentLevel, "QueryBody");
            process(node.getQueryBody(), tmpIndentLevel);
            if (node.getOrderBy().isPresent()) {
                print(tmpIndentLevel, "OrderBy");
                process(node.getOrderBy().get(), tmpIndentLevel + 1);
            }
            if (node.getLimit().isPresent()) {
                print(tmpIndentLevel, "Limit: " + node.getLimit().get());
            }
            return null;
        }

        @Override
        protected Void visitQuerySpecification(QuerySpecification node, Integer indentLevel) {
            print(indentLevel, "QuerySpecification ");
            Integer tmpIndentLevel = indentLevel;
            tmpIndentLevel++;
            process(node.getSelect(), tmpIndentLevel);
            if (node.getFrom().isPresent()) {
                print(tmpIndentLevel, "From");
                process(node.getFrom().get(), tmpIndentLevel + 1);
            }
            if (node.getWhere().isPresent()) {
                print(tmpIndentLevel, "Where");
                process(node.getWhere().get(), tmpIndentLevel + 1);
            }
            if (node.getGroupBy().isPresent()) {
                String distinct = "";
                if (node.getGroupBy().get().isDistinct()) {
                    distinct = "[DISTINCT]";
                }
                print(tmpIndentLevel, "GroupBy" + distinct);
                for (GroupingElement groupingElement : node.getGroupBy().get().getGroupingElements()) {
                    print(tmpIndentLevel, "SimpleGroupBy");
                    if (groupingElement instanceof SimpleGroupBy) {
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, tmpIndentLevel + 1);
                        }
                    } else if (groupingElement instanceof GroupingSets) {
                        print(tmpIndentLevel + 1, "GroupingSets");
                        for (List<Expression> set : ((GroupingSets) groupingElement).getSets()) {
                            print(tmpIndentLevel + 2, "GroupingSet[");
                            for (Expression expression : set) {
                                process(expression, tmpIndentLevel + 3);
                            }
                            print(tmpIndentLevel + 2, "]");
                        }
                    } else if (groupingElement instanceof Cube) {
                        print(tmpIndentLevel + 1, "Cube");
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, tmpIndentLevel + 1);
                        }
                    } else if (groupingElement instanceof Rollup) {
                        print(tmpIndentLevel + 1, "Rollup");
                        for (Expression column : groupingElement.getExpressions()) {
                            process(column, tmpIndentLevel + 1);
                        }
                    }
                }
            }
            if (node.getHaving().isPresent()) {
                print(tmpIndentLevel, "Having");
                process(node.getHaving().get(), tmpIndentLevel + 1);
            }
            if (node.getOrderBy().isPresent()) {
                print(tmpIndentLevel, "OrderBy");
                process(node.getOrderBy().get(), tmpIndentLevel + 1);
            }
            if (node.getLimit().isPresent()) {
                print(tmpIndentLevel, "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(io.prestosql.sql.tree.ArithmeticBinaryExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) SimpleGroupBy(io.prestosql.sql.tree.SimpleGroupBy) Query(io.prestosql.sql.tree.Query) Rollup(io.prestosql.sql.tree.Rollup) BooleanLiteral(io.prestosql.sql.tree.BooleanLiteral) Node(io.prestosql.sql.tree.Node) Values(io.prestosql.sql.tree.Values) AllColumns(io.prestosql.sql.tree.AllColumns) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) QuerySpecification(io.prestosql.sql.tree.QuerySpecification) SortItem(io.prestosql.sql.tree.SortItem) Identifier(io.prestosql.sql.tree.Identifier) List(java.util.List) FunctionCall(io.prestosql.sql.tree.FunctionCall) SampledRelation(io.prestosql.sql.tree.SampledRelation) GroupingSets(io.prestosql.sql.tree.GroupingSets) OrderBy(io.prestosql.sql.tree.OrderBy) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) Table(io.prestosql.sql.tree.Table) LongLiteral(io.prestosql.sql.tree.LongLiteral) QualifiedName(io.prestosql.sql.tree.QualifiedName) DefaultTraversalVisitor(io.prestosql.sql.tree.DefaultTraversalVisitor) SingleColumn(io.prestosql.sql.tree.SingleColumn) LikePredicate(io.prestosql.sql.tree.LikePredicate) TableSubquery(io.prestosql.sql.tree.TableSubquery) InPredicate(io.prestosql.sql.tree.InPredicate) GroupingElement(io.prestosql.sql.tree.GroupingElement) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) BinaryLiteral(io.prestosql.sql.tree.BinaryLiteral) StringLiteral(io.prestosql.sql.tree.StringLiteral) ArithmeticBinaryExpression(io.prestosql.sql.tree.ArithmeticBinaryExpression) DereferenceExpression(io.prestosql.sql.tree.DereferenceExpression) LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) Expression(io.prestosql.sql.tree.Expression) Cube(io.prestosql.sql.tree.Cube) Select(io.prestosql.sql.tree.Select) Row(io.prestosql.sql.tree.Row) AliasedRelation(io.prestosql.sql.tree.AliasedRelation)

Example 5 with SubqueryExpression

use of io.prestosql.sql.tree.SubqueryExpression in project hetu-core by openlookeng.

the class TestSqlParser method testQuantifiedComparison.

@Test
public void testQuantifiedComparison() {
    assertExpression("col1 < ANY (SELECT col2 FROM table1)", new QuantifiedComparisonExpression(LESS_THAN, QuantifiedComparisonExpression.Quantifier.ANY, identifier("col1"), new SubqueryExpression(simpleQuery(selectList(new SingleColumn(identifier("col2"))), table(QualifiedName.of("table1"))))));
    assertExpression("col1 = ALL (VALUES ROW(1), ROW(2))", new QuantifiedComparisonExpression(ComparisonExpression.Operator.EQUAL, QuantifiedComparisonExpression.Quantifier.ALL, identifier("col1"), new SubqueryExpression(query(values(row(new LongLiteral("1")), row(new LongLiteral("2")))))));
    assertExpression("col1 >= SOME (SELECT 10)", new QuantifiedComparisonExpression(ComparisonExpression.Operator.GREATER_THAN_OR_EQUAL, QuantifiedComparisonExpression.Quantifier.SOME, identifier("col1"), new SubqueryExpression(simpleQuery(selectList(new LongLiteral("10"))))));
}
Also used : LongLiteral(io.prestosql.sql.tree.LongLiteral) QuantifiedComparisonExpression(io.prestosql.sql.tree.QuantifiedComparisonExpression) SingleColumn(io.prestosql.sql.tree.SingleColumn) SubqueryExpression(io.prestosql.sql.tree.SubqueryExpression) Test(org.testng.annotations.Test)

Aggregations

SubqueryExpression (io.prestosql.sql.tree.SubqueryExpression)5 Symbol (io.prestosql.spi.plan.Symbol)3 QuantifiedComparisonExpression (io.prestosql.sql.tree.QuantifiedComparisonExpression)3 DereferenceExpression (io.prestosql.sql.tree.DereferenceExpression)2 Expression (io.prestosql.sql.tree.Expression)2 InPredicate (io.prestosql.sql.tree.InPredicate)2 LongLiteral (io.prestosql.sql.tree.LongLiteral)2 SingleColumn (io.prestosql.sql.tree.SingleColumn)2 RowExpression (io.prestosql.spi.relation.RowExpression)1 ExpressionNodeInliner.replaceExpression (io.prestosql.sql.planner.ExpressionNodeInliner.replaceExpression)1 SymbolUtils.toSymbolReference (io.prestosql.sql.planner.SymbolUtils.toSymbolReference)1 EnforceSingleRowNode (io.prestosql.sql.planner.plan.EnforceSingleRowNode)1 OriginalExpressionUtils.castToExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression)1 OriginalExpressionUtils.castToRowExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression)1 AliasedRelation (io.prestosql.sql.tree.AliasedRelation)1 AllColumns (io.prestosql.sql.tree.AllColumns)1 ArithmeticBinaryExpression (io.prestosql.sql.tree.ArithmeticBinaryExpression)1 BinaryLiteral (io.prestosql.sql.tree.BinaryLiteral)1 BooleanLiteral (io.prestosql.sql.tree.BooleanLiteral)1 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)1