Package org.teiid.query.optimizer.relational.plantree

Examples of org.teiid.query.optimizer.relational.plantree.PlanNode


                            CommandContext context) throws QueryPlannerException,
                                                   QueryMetadataException,
                                                   TeiidComponentException {

        for (PlanNode groupNode : NodeEditor.findAllNodes(plan, NodeConstants.Types.GROUP, NodeConstants.Types.ACCESS)) {
            PlanNode child = groupNode.getFirstChild();

          List<SingleElementSymbol> groupingExpressions = (List<SingleElementSymbol>)groupNode.getProperty(NodeConstants.Info.GROUP_COLS);
           
            if (child.getType() == NodeConstants.Types.SOURCE) {
                PlanNode setOp = child.getFirstChild();
               
                try {
          pushGroupNodeOverUnion(plan, metadata, capFinder, groupNode, child, groupingExpressions, setOp, context);
        } catch (QueryResolverException e) {
          throw new TeiidComponentException(e);
View Full Code Here


    GroupSymbol group = sourceNode.getGroups().iterator().next().clone();

    boolean first = true;
    List<SingleElementSymbol> symbols = null;
    for (PlanNode planNode : unionChildren) {
      PlanNode groupClone = NodeFactory.getNewNode(NodeConstants.Types.GROUP);
      groupClone.setProperty(Info.GROUP_COLS, LanguageObject.Util.deepClone(groupingExpressions, SingleElementSymbol.class));
      groupClone.addGroups(groupNode.getGroups());
     
      PlanNode view = RuleDecomposeJoin.createSource(group, planNode, parentMap);
     
      view.addAsParent(groupClone);
     
      PlanNode projectPlanNode = NodeFactory.getNewNode(NodeConstants.Types.PROJECT);
     
      Select allSymbols = new Select(groupingExpressions);
      allSymbols.addSymbols(aggregates);
      if (first) {
        first = false;
        QueryRewriter.makeSelectUnique(allSymbols, false);
        symbols = allSymbols.getSymbols();
      }
      projectPlanNode.setProperty(NodeConstants.Info.PROJECT_COLS, allSymbols.getSymbols());
        projectPlanNode.addGroups(view.getGroups());
       
        groupClone.addAsParent(projectPlanNode);
       
        if (planNode.getType() == NodeConstants.Types.ACCESS) {
          //TODO: temporarily remove the access node so that the inline view could be removed if possible
View Full Code Here

      }
      setOp.setProperty(NodeConstants.Info.USE_ALL, Boolean.TRUE);
    }
   
    for (PlanNode planNode : setOp.getChildren()) {
      PlanNode child = findUnionChildren(unionChildren, carinalityDependent, planNode);
      if (child != null) {
        unionChildren.add(child);
      }
    }
   
View Full Code Here

   
  public void addView(PlanNode root, PlanNode unionSource, boolean pushdown, List<SingleElementSymbol> groupingExpressions,
      Set<AggregateSymbol> aggregates, List<ElementSymbol> virtualElements,
      QueryMetadataInterface metadata, CapabilitiesFinder capFinder)
      throws TeiidComponentException, QueryPlannerException, QueryResolverException {
    PlanNode originalNode = unionSource;
    //branches other than the first need to have their projected column names updated
    PlanNode sortNode = NodeEditor.findNodePreOrder(unionSource, NodeConstants.Types.SORT, NodeConstants.Types.SOURCE);
    List<SingleElementSymbol> sortOrder = null;
    OrderBy orderBy = null;
    if (sortNode != null) {
      orderBy = (OrderBy)sortNode.getProperty(Info.SORT_ORDER);
      sortOrder = orderBy.getSortKeys();
    }
    List<SingleElementSymbol> projectCols = FrameUtil.findTopCols(unionSource);
    for (int i = 0; i < virtualElements.size(); i++) {
      ElementSymbol virtualElem = virtualElements.get(i);
      SingleElementSymbol projectedSymbol = projectCols.get(i);
      if (!projectedSymbol.getShortCanonicalName().equals(virtualElem.getShortCanonicalName())) {
        if (sortOrder != null) {
          int sortIndex = sortOrder.indexOf(projectedSymbol);
          if (sortIndex > -1) {
            updateSymbolName(sortOrder, sortIndex, virtualElem, sortOrder.get(sortIndex));
            orderBy.getOrderByItems().get(sortIndex).setSymbol(sortOrder.get(sortIndex));
          }
        }
        updateSymbolName(projectCols, i, virtualElem, projectedSymbol);
      }
    }
    GroupSymbol group = new GroupSymbol("X"); //$NON-NLS-1$
       
    PlanNode intermediateView = createView(group, virtualElements, unionSource, metadata);
      SymbolMap symbolMap = (SymbolMap)intermediateView.getProperty(Info.SYMBOL_MAP);
      unionSource = intermediateView;
     
        Set<SingleElementSymbol> newGroupingExpressions = Collections.emptySet();
        if (groupingExpressions != null) {
          newGroupingExpressions = new HashSet<SingleElementSymbol>();
          for (SingleElementSymbol singleElementSymbol : groupingExpressions) {
        newGroupingExpressions.add((SingleElementSymbol)symbolMap.getKeys().get(virtualElements.indexOf(singleElementSymbol)).clone());
      }
        }

        List<SingleElementSymbol> projectedViewSymbols = Util.deepClone(symbolMap.getKeys(), SingleElementSymbol.class);

        PlanNode parent = NodeEditor.findParent(unionSource, NodeConstants.Types.SOURCE);
        SymbolMap parentMap = (SymbolMap) parent.getProperty(NodeConstants.Info.SYMBOL_MAP);
        SymbolMap viewMapping = SymbolMap.createSymbolMap(parentMap.getKeys(), projectedViewSymbols);
        for (AggregateSymbol agg : aggregates) {
          agg = (AggregateSymbol)agg.clone();
          ExpressionMappingVisitor.mapExpressions(agg, viewMapping.asMap());
          if (pushdown) {
            projectedViewSymbols.add(agg);
          } else {
            if (agg.getAggregateFunction() == Type.COUNT) {
              if (agg.getExpression() == null) {
              projectedViewSymbols.add(new ExpressionSymbol("stagedAgg", new Constant(1))); //$NON-NLS-1$
              } else {
                SearchedCaseExpression count = new SearchedCaseExpression(Arrays.asList(new IsNullCriteria(agg.getExpression())), Arrays.asList(new Constant(Integer.valueOf(0))));
                count.setElseExpression(new Constant(Integer.valueOf(1)));
                count.setType(DataTypeManager.DefaultDataClasses.INTEGER);
              projectedViewSymbols.add(new ExpressionSymbol("stagedAgg", count)); //$NON-NLS-1$
              }
            } else { //min, max, sum
              Expression ex = agg.getExpression();
              ex = ResolverUtil.convertExpression(ex, DataTypeManager.getDataTypeName(agg.getType()), metadata);
              projectedViewSymbols.add(new ExpressionSymbol("stagedAgg", ex)); //$NON-NLS-1$
            }
          }
    }

        if (pushdown) {
          unionSource = addGroupBy(unionSource, newGroupingExpressions, new LinkedList<AggregateSymbol>());
        }
       
        PlanNode projectPlanNode = NodeFactory.getNewNode(NodeConstants.Types.PROJECT);
        unionSource.addAsParent(projectPlanNode);
        unionSource = projectPlanNode;

        //create proper names for the aggregate symbols
        Select select = new Select(projectedViewSymbols);
        QueryRewriter.makeSelectUnique(select, false);
        projectedViewSymbols = select.getProjectedSymbols();
        projectPlanNode.setProperty(NodeConstants.Info.PROJECT_COLS, projectedViewSymbols);
        projectPlanNode.addGroup(group);
        if (pushdown) {
          while (RuleRaiseAccess.raiseAccessNode(root, originalNode, metadata, capFinder, true, null) != null) {
            //continue to raise
          }
        }
View Full Code Here

          }
        }
    }
 
  static PlanNode createView(GroupSymbol group, List<? extends SingleElementSymbol> virtualElements, PlanNode child, QueryMetadataInterface metadata) throws TeiidComponentException {
    PlanNode intermediateView = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
      SymbolMap symbolMap = createSymbolMap(group, virtualElements, child, metadata);
      intermediateView.setProperty(NodeConstants.Info.SYMBOL_MAP, symbolMap);
      child.addAsParent(intermediateView);
      intermediateView.addGroup(group);
      return intermediateView;
  }
View Full Code Here

     * @return the set of aggregate symbols found
     * @since 4.2
     */
    static LinkedHashSet<AggregateSymbol> collectAggregates(PlanNode groupNode) {
      LinkedHashSet<AggregateSymbol> aggregates = new LinkedHashSet<AggregateSymbol>();
        PlanNode currentNode = groupNode.getParent();
        while (currentNode != null) {
            if (currentNode.getType() == NodeConstants.Types.PROJECT) {
                List<SingleElementSymbol> projectedSymbols = (List<SingleElementSymbol>)currentNode.getProperty(NodeConstants.Info.PROJECT_COLS);
                for (SingleElementSymbol symbol : projectedSymbols) {
                    aggregates.addAll(AggregateSymbolCollectorVisitor.getAggregates(symbol, true));
                }
                break;
            }
            if (currentNode.getType() == NodeConstants.Types.SELECT) {
                Criteria crit = (Criteria)currentNode.getProperty(NodeConstants.Info.SELECT_CRITERIA);
                aggregates.addAll(AggregateSymbolCollectorVisitor.getAggregates(crit, true));
            }

            currentNode = currentNode.getParent();
        }
        return aggregates;
    }
View Full Code Here

            if (aggregates.isEmpty() && stagedGroupingSymbols.isEmpty()) {
                continue;
            }
            //TODO: if aggregates is empty, then could insert a dup remove node instead
           
            PlanNode stageGroup = addGroupBy(planNode, stagedGroupingSymbols, aggregates);
       
            //check for push down
            if (stageGroup.getFirstChild().getType() == NodeConstants.Types.ACCESS
                            && RuleRaiseAccess.canRaiseOverGroupBy(stageGroup, stageGroup.getFirstChild(), aggregates, metadata, capFinder, null)) {
                RuleRaiseAccess.performRaise(null, stageGroup.getFirstChild(), stageGroup);
                if (stagedGroupingSymbols.isEmpty()) {
                    RuleRaiseAccess.performRaise(null, stageGroup.getParent(), stageGroup.getParent().getParent());
                }
            }
        }
    }
View Full Code Here

    }

  private PlanNode addGroupBy(PlanNode planNode,
      Collection<SingleElementSymbol> stagedGroupingSymbols,
      Collection<AggregateSymbol> aggregates) {
    PlanNode stageGroup = NodeFactory.getNewNode(NodeConstants.Types.GROUP);
    planNode.addAsParent(stageGroup);

    if (!stagedGroupingSymbols.isEmpty()) {
        stageGroup.setProperty(NodeConstants.Info.GROUP_COLS, new ArrayList<SingleElementSymbol>(stagedGroupingSymbols));
        stageGroup.addGroups(GroupsUsedByElementsVisitor.getGroups(stagedGroupingSymbols));
    } else {
        // if the source has no rows we need to insert a select node with criteria count(*)>0
        PlanNode selectNode = NodeFactory.getNewNode(NodeConstants.Types.SELECT);
        AggregateSymbol count = new AggregateSymbol("stagedAgg", NonReserved.COUNT, false, null); //$NON-NLS-1$
        aggregates.add(count); //consider the count aggregate for the push down call below
        selectNode.setProperty(NodeConstants.Info.SELECT_CRITERIA, new CompareCriteria(count, CompareCriteria.GT,
                                                                                       new Constant(new Integer(0))));
        selectNode.setProperty(NodeConstants.Info.IS_HAVING, Boolean.TRUE);
        stageGroup.addAsParent(selectNode);
    }
    return stageGroup;
  }
View Full Code Here

        Set<AggregateSymbol> otherAggs = new HashSet<AggregateSymbol>(allAggregates);
        if (aggregates != null) {
            otherAggs.removeAll(aggregates);
        }

        PlanNode source = FrameUtil.findJoinSourceNode(current);

        for (AggregateSymbol aggregateSymbol : otherAggs) {
            for (ElementSymbol symbol : ElementCollectorVisitor.getElements(aggregateSymbol, true)) {
                if (source.getGroups().contains(symbol.getGroupSymbol())) {
                    stagedGroupingSymbols.add(symbol);
                }
            }
        }
    }
View Full Code Here

     * Ensures that we are only pushing through inner equi joins or cross joins.  Also collects the necessary staged grouping symbols
     */
    private boolean canPush(PlanNode groupNode,
                            Set<SingleElementSymbol> stagedGroupingSymbols,
                            PlanNode planNode) {
        PlanNode parentJoin = planNode.getParent();
       
        Set<GroupSymbol> groups = FrameUtil.findJoinSourceNode(planNode).getGroups();
       
        while (parentJoin != groupNode) {
            if (parentJoin.getType() != NodeConstants.Types.JOIN
                || parentJoin.hasCollectionProperty(NodeConstants.Info.NON_EQUI_JOIN_CRITERIA)
                || ((JoinType)parentJoin.getProperty(NodeConstants.Info.JOIN_TYPE)).isOuter()) {
                return false;
            }

            if (planNode == parentJoin.getFirstChild()) {
                if (parentJoin.hasCollectionProperty(NodeConstants.Info.LEFT_EXPRESSIONS) && !filterJoinColumns(stagedGroupingSymbols, groups, (List<SingleElementSymbol>)parentJoin.getProperty(NodeConstants.Info.LEFT_EXPRESSIONS))) {
                    return false;
                }
            } else {
                if (parentJoin.hasCollectionProperty(NodeConstants.Info.RIGHT_EXPRESSIONS) && !filterJoinColumns(stagedGroupingSymbols, groups, (List<SingleElementSymbol>)parentJoin.getProperty(NodeConstants.Info.RIGHT_EXPRESSIONS))) {
                    return false;
                }
            }

            planNode = parentJoin;
            parentJoin = parentJoin.getParent();
        }
        return true;
    }
View Full Code Here

TOP

Related Classes of org.teiid.query.optimizer.relational.plantree.PlanNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.