Search in sources :

Example 26 with SymbolReference

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

the class TransformUnCorrelatedInPredicateSubQuerySelfJoinToAggregate method getAllSymbols.

private static void getAllSymbols(Expression expression, List<SymbolReference> symbols) {
    if (expression instanceof LogicalBinaryExpression) {
        LogicalBinaryExpression logicalBinaryExpression = (LogicalBinaryExpression) expression;
        getAllSymbols(logicalBinaryExpression.getLeft(), symbols);
        getAllSymbols(logicalBinaryExpression.getRight(), symbols);
    } else if (expression instanceof ComparisonExpression) {
        ComparisonExpression comparisonExpression = (ComparisonExpression) expression;
        getAllSymbols(comparisonExpression.getLeft(), symbols);
        getAllSymbols(comparisonExpression.getRight(), symbols);
    } else if (expression instanceof SymbolReference) {
        symbols.add((SymbolReference) expression);
    }
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) SymbolReference(io.prestosql.sql.tree.SymbolReference)

Example 27 with SymbolReference

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

the class TransformUnCorrelatedInPredicateSubQuerySelfJoinToAggregate method isSelfJoin.

private static boolean isSelfJoin(ProjectNode projectNode, Expression predicate, JoinNode joinNode, Lookup lookup) {
    PlanNode left = lookup.resolve(joinNode.getLeft());
    PlanNode right = lookup.resolve(joinNode.getRight());
    // TODO FIX FOR framenwork changes
    if (joinNode.getType() == JoinNode.Type.INNER && left instanceof TableScanNode && right instanceof TableScanNode && ((TableScanNode) left).getTable().getFullyQualifiedName().equals(((TableScanNode) right).getTable().getFullyQualifiedName())) {
        if (!(predicate instanceof LogicalBinaryExpression && ((LogicalBinaryExpression) predicate).getLeft() instanceof ComparisonExpression && ((LogicalBinaryExpression) predicate).getRight() instanceof ComparisonExpression)) {
            return false;
        }
        SymbolReference projected = SymbolUtils.toSymbolReference(getOnlyElement(projectNode.getOutputSymbols()));
        ComparisonExpression leftPredicate = (ComparisonExpression) ((LogicalBinaryExpression) predicate).getLeft();
        ComparisonExpression rightPredicate = (ComparisonExpression) ((LogicalBinaryExpression) predicate).getRight();
        if (lookup.resolve(projectNode.getSource()) instanceof CTEScanNode) {
            CTEScanNode cteScanNode = (CTEScanNode) lookup.resolve(projectNode.getSource());
            ProjectNode childProjectOfCte = (ProjectNode) lookup.resolve(cteScanNode.getSource());
            RowExpression projectedExpresssion = (childProjectOfCte.getAssignments().get(getOnlyElement(projectNode.getOutputSymbols())));
            for (Symbol symbol : lookup.resolve(childProjectOfCte.getSource()).getOutputSymbols()) {
                if (symbol.getName().equals(((SymbolReference) OriginalExpressionUtils.castToExpression(projectedExpresssion)).getName())) {
                    projected = SymbolUtils.toSymbolReference(symbol);
                }
            }
        }
        if (leftPredicate.getChildren().contains(projected) && leftPredicate.getOperator() == ComparisonExpression.Operator.EQUAL && rightPredicate.getOperator() == ComparisonExpression.Operator.NOT_EQUAL) {
            return true;
        } else if (rightPredicate.getChildren().contains(projected) && rightPredicate.getOperator() == ComparisonExpression.Operator.EQUAL && leftPredicate.getOperator() == ComparisonExpression.Operator.NOT_EQUAL) {
            return true;
        }
    }
    return false;
}
Also used : LogicalBinaryExpression(io.prestosql.sql.tree.LogicalBinaryExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) PlanNode(io.prestosql.spi.plan.PlanNode) TableScanNode(io.prestosql.spi.plan.TableScanNode) SymbolReference(io.prestosql.sql.tree.SymbolReference) Symbol(io.prestosql.spi.plan.Symbol) RowExpression(io.prestosql.spi.relation.RowExpression) CTEScanNode(io.prestosql.spi.plan.CTEScanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Example 28 with SymbolReference

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

the class SetOperationNodeTranslator method appendMarkers.

private static PlanNode appendMarkers(PlanNodeIdAllocator idAllocator, PlanSymbolAllocator planSymbolAllocator, PlanNode source, int markerIndex, List<Symbol> markers, Map<Symbol, SymbolReference> projections) {
    Assignments.Builder assignments = Assignments.builder();
    // add existing intersect symbols to projection
    for (Map.Entry<Symbol, SymbolReference> entry : projections.entrySet()) {
        Symbol symbol = planSymbolAllocator.newSymbol(entry.getKey().getName(), planSymbolAllocator.getTypes().get(entry.getKey()));
        assignments.put(symbol, castToRowExpression(entry.getValue()));
    }
    // add extra marker fields to the projection
    for (int i = 0; i < markers.size(); ++i) {
        Expression expression = (i == markerIndex) ? TRUE_LITERAL : new Cast(new NullLiteral(), StandardTypes.BOOLEAN);
        assignments.put(planSymbolAllocator.newSymbol(markers.get(i).getName(), BOOLEAN), castToRowExpression(expression));
    }
    return new ProjectNode(idAllocator.getNextId(), source, assignments.build());
}
Also used : Cast(io.prestosql.sql.tree.Cast) CallExpression(io.prestosql.spi.relation.CallExpression) OriginalExpressionUtils.castToRowExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToRowExpression) ComparisonExpression(io.prestosql.sql.tree.ComparisonExpression) Expression(io.prestosql.sql.tree.Expression) Symbol(io.prestosql.spi.plan.Symbol) SymbolUtils.toSymbolReference(io.prestosql.sql.planner.SymbolUtils.toSymbolReference) SymbolReference(io.prestosql.sql.tree.SymbolReference) Assignments(io.prestosql.spi.plan.Assignments) ProjectNode(io.prestosql.spi.plan.ProjectNode) Map(java.util.Map) SetOperationNodeUtils.sourceSymbolMap(io.prestosql.sql.planner.optimizations.SetOperationNodeUtils.sourceSymbolMap) ImmutableMap(com.google.common.collect.ImmutableMap) NullLiteral(io.prestosql.sql.tree.NullLiteral)

Example 29 with SymbolReference

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

the class TablePushdown method getNewIntermediateTreeAfterInnerTableUpdate.

/**
 * @param newInnerJoinNode is the recently created join node after the outer table has been pushed into subquery
 * @param stack is the stack of nodes from the original captured JoinNode and subquery TableScanNode
 * @return the PlanNode after pulling the subquery table's Aggregation and Group By above the join
 */
private PlanNode getNewIntermediateTreeAfterInnerTableUpdate(JoinNode newInnerJoinNode, Stack<NodeWithTreeDirection> stack) {
    TableScanNode subqueryTableNode = (TableScanNode) stack.peek().getNode();
    /*
         * create assignment builder for a new intermediate ProjectNode between the newInnerJoinNode and
         * TableScanNode for subquery table
         * */
    Assignments.Builder assignmentsBuilder = Assignments.builder();
    /*
         * All symbols from TableScanNode are directly copied over
         * */
    for (Map.Entry<Symbol, ColumnHandle> tableEntry : subqueryTableNode.getAssignments().entrySet()) {
        Symbol s = tableEntry.getKey();
        assignmentsBuilder.put(s, castToRowExpression(new SymbolReference(s.getName())));
    }
    ProjectNode parentOfSubqueryTableNode = new ProjectNode(ruleContext.getIdAllocator().getNextId(), subqueryTableNode, assignmentsBuilder.build());
    List<Symbol> parentOfSubqueryTableNodeOutputSymbols = parentOfSubqueryTableNode.getOutputSymbols();
    /*
         * Recreate the inner joinNode using the new ProjectNode as one of its sources.
         * */
    PlanNodeStatsEstimate leftSourceStats = ruleContext.getStatsProvider().getStats(newInnerJoinNode.getLeft());
    PlanNodeStatsEstimate rightSourceStats = ruleContext.getStatsProvider().getStats(parentOfSubqueryTableNode);
    JoinNode newJoinNode;
    if (leftSourceStats.isOutputRowCountUnknown()) {
        /*
            * CAUTION: the stats are not available, so source reordering is not allowed. Query may fail
            * */
        newJoinNode = new JoinNode(newInnerJoinNode.getId(), newInnerJoinNode.getType(), newInnerJoinNode.getLeft(), parentOfSubqueryTableNode, newInnerJoinNode.getCriteria(), ImmutableList.<Symbol>builder().addAll(parentOfSubqueryTableNodeOutputSymbols).build(), newInnerJoinNode.getFilter(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), newInnerJoinNode.getDynamicFilters());
    } else {
        double leftSourceRowsCount = leftSourceStats.getOutputRowCount();
        double rightSourceRowsCount = rightSourceStats.getOutputRowCount();
        if (leftSourceRowsCount <= rightSourceRowsCount) {
            // We reorder the children of this new join node such that the table with more rows is on the left
            List<JoinNode.EquiJoinClause> newInnerJoinCriteria = newInnerJoinNode.getCriteria().stream().map(JoinNode.EquiJoinClause::flip).collect(toImmutableList());
            newJoinNode = new JoinNode(newInnerJoinNode.getId(), newInnerJoinNode.getType(), parentOfSubqueryTableNode, newInnerJoinNode.getLeft(), newInnerJoinCriteria, ImmutableList.<Symbol>builder().addAll(parentOfSubqueryTableNodeOutputSymbols).build(), newInnerJoinNode.getFilter(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), newInnerJoinNode.getDynamicFilters());
        } else {
            newJoinNode = new JoinNode(newInnerJoinNode.getId(), newInnerJoinNode.getType(), newInnerJoinNode.getLeft(), parentOfSubqueryTableNode, newInnerJoinNode.getCriteria(), ImmutableList.<Symbol>builder().addAll(parentOfSubqueryTableNodeOutputSymbols).build(), newInnerJoinNode.getFilter(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), newInnerJoinNode.getDynamicFilters());
        }
    }
    // Remove the TableScanNode from the stack
    stack.pop();
    AggregationNode newAggNode = (AggregationNode) stack.peek().getNode().replaceChildren(ImmutableList.of(newJoinNode));
    return stack.firstElement().getNode().replaceChildren(ImmutableList.of(newAggNode));
}
Also used : ColumnHandle(io.prestosql.spi.connector.ColumnHandle) PlanNodeStatsEstimate(io.prestosql.cost.PlanNodeStatsEstimate) Symbol(io.prestosql.spi.plan.Symbol) SymbolReference(io.prestosql.sql.tree.SymbolReference) JoinNode(io.prestosql.spi.plan.JoinNode) Assignments(io.prestosql.spi.plan.Assignments) AggregationNode(io.prestosql.spi.plan.AggregationNode) TableScanNode(io.prestosql.spi.plan.TableScanNode) ProjectNode(io.prestosql.spi.plan.ProjectNode) Map(java.util.Map) SpecialCommentFormatter.getUniqueColumnTableMap(io.prestosql.sql.util.SpecialCommentFormatter.getUniqueColumnTableMap)

Example 30 with SymbolReference

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

the class PushLimitThroughProject method apply.

@Override
public Result apply(LimitNode parent, Captures captures, Context context) {
    ProjectNode projectNode = captures.get(CHILD);
    // for a LimitNode without ties, simply reorder the nodes
    if (!parent.isWithTies()) {
        return Result.ofPlanNode(transpose(parent, projectNode));
    }
    // for a LimitNode with ties, the tiesResolvingScheme must be rewritten in terms of symbols before projection
    SymbolMapper.Builder symbolMapper = SymbolMapper.builder();
    for (Symbol symbol : parent.getTiesResolvingScheme().get().getOrderBy()) {
        Expression expression = castToExpression(projectNode.getAssignments().get(symbol));
        // if a symbol results from some computation, the translation fails
        if (!(expression instanceof SymbolReference)) {
            return Result.empty();
        }
        symbolMapper.put(symbol, SymbolUtils.from(expression));
    }
    LimitNode mappedLimitNode = symbolMapper.build().map(parent, projectNode.getSource());
    return Result.ofPlanNode(projectNode.replaceChildren(ImmutableList.of(mappedLimitNode)));
}
Also used : SymbolMapper(io.prestosql.sql.planner.optimizations.SymbolMapper) OriginalExpressionUtils.castToExpression(io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression) Expression(io.prestosql.sql.tree.Expression) LimitNode(io.prestosql.spi.plan.LimitNode) Symbol(io.prestosql.spi.plan.Symbol) SymbolReference(io.prestosql.sql.tree.SymbolReference) ProjectNode(io.prestosql.spi.plan.ProjectNode)

Aggregations

SymbolReference (io.prestosql.sql.tree.SymbolReference)57 Symbol (io.prestosql.spi.plan.Symbol)30 Expression (io.prestosql.sql.tree.Expression)29 Test (org.testng.annotations.Test)25 ComparisonExpression (io.prestosql.sql.tree.ComparisonExpression)23 Cast (io.prestosql.sql.tree.Cast)19 RowExpression (io.prestosql.spi.relation.RowExpression)17 SymbolUtils.toSymbolReference (io.prestosql.sql.planner.SymbolUtils.toSymbolReference)17 LongLiteral (io.prestosql.sql.tree.LongLiteral)14 ImmutableList (com.google.common.collect.ImmutableList)13 ImmutableMap (com.google.common.collect.ImmutableMap)12 Type (io.prestosql.spi.type.Type)12 List (java.util.List)12 Map (java.util.Map)12 ProjectNode (io.prestosql.spi.plan.ProjectNode)11 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)10 ArrayList (java.util.ArrayList)10 Assignments (io.prestosql.spi.plan.Assignments)9 CallExpression (io.prestosql.spi.relation.CallExpression)9 OriginalExpressionUtils.castToExpression (io.prestosql.sql.relational.OriginalExpressionUtils.castToExpression)9