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

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


        return result;
    }
   
    private static PlanNode helpGetJoinNode(float childCost1, float childCost2, JoinType joinType){
        PlanNode joinNode = NodeFactory.getNewNode(NodeConstants.Types.JOIN);
        PlanNode child1 = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
        PlanNode child2 = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
       
        joinNode.addLastChild(child1);
        joinNode.addLastChild(child2);
       
        child1.setProperty(NodeConstants.Info.EST_CARDINALITY, new Float(childCost1));
        child2.setProperty(NodeConstants.Info.EST_CARDINALITY, new Float(childCost2));

        joinNode.setProperty(NodeConstants.Info.JOIN_TYPE, joinType);
       
        return joinNode;
    }
View Full Code Here


        return joinNode;
    }
   
    void helpTestEstimateCost(String critString, float childCost, float expectedResult, QueryMetadataInterface metadata) throws Exception {
        Criteria crit = helpGetCriteria(critString, metadata);
        PlanNode select = RelationalPlanner.createSelectNode(crit, false);
       
        float resultCost = NewCalculateCostUtil.recursiveEstimateCostOfCriteria(childCost, select, crit, metadata);
        assertEquals((int)expectedResult, (int)resultCost);
    }
View Full Code Here

        helpTestEstimateCost(critString, 200, 198, metadata);
    }
   
    @Test public void testEstimateJoinNodeCost() throws Exception {
        QueryMetadataInterface metadata = FakeMetadataFactory.example4();
        PlanNode joinNode = helpGetJoinNode(NewCalculateCostUtil.UNKNOWN_VALUE, NewCalculateCostUtil.UNKNOWN_VALUE, JoinType.JOIN_CROSS);
       
        float cost = NewCalculateCostUtil.computeCostForTree(joinNode, metadata);
        assertTrue(cost == NewCalculateCostUtil.UNKNOWN_VALUE);
    }
View Full Code Here

    }
   
    @Ignore("this logic needs to be refined to work better")
    @Test public void testEstimateJoinNodeCostOneUnknown() throws Exception {
        QueryMetadataInterface metadata = FakeMetadataFactory.example4();
        PlanNode joinNode = helpGetJoinNode(NewCalculateCostUtil.UNKNOWN_VALUE, 500, JoinType.JOIN_INNER);
        joinNode.setProperty(NodeConstants.Info.JOIN_CRITERIA, Arrays.asList(helpGetCriteria("pm1.g1.e1 = pm1.g2.e1", metadata)));
        float cost = NewCalculateCostUtil.computeCostForTree(joinNode, metadata);
        assertEquals(10000, cost, 0);
    }
View Full Code Here

     
      for (PlanNode sourceNode : NodeEditor.findAllNodes(plan, NodeConstants.Types.SOURCE, NodeConstants.Types.ACCESS)) {
        SymbolMap references = (SymbolMap)sourceNode.getProperty(NodeConstants.Info.CORRELATED_REFERENCES);
          if (references != null) {
            Set<GroupSymbol> groups = GroupsUsedByElementsVisitor.getGroups(references.getValues());
            PlanNode joinNode = NodeEditor.findParent(sourceNode, NodeConstants.Types.JOIN, NodeConstants.Types.SOURCE);
            while (joinNode != null) {
              joinNode.setProperty(NodeConstants.Info.JOIN_STRATEGY, JoinStrategyType.NESTED_TABLE);
              if (joinNode.getProperty(NodeConstants.Info.DEPENDENT_VALUE_SOURCE) != null) {
                //sanity check
                throw new AssertionError("Cannot use a depenedent join when the join involves a correlated nested table.")//$NON-NLS-1$
              }
              if (joinNode.getGroups().containsAll(groups)) {
                break;
              }
              joinNode = NodeEditor.findParent(joinNode, NodeConstants.Types.JOIN, NodeConstants.Types.SOURCE);
            }
          }
      }

        for (PlanNode joinNode : NodeEditor.findAllNodes(plan, NodeConstants.Types.JOIN, NodeConstants.Types.ACCESS)) {
            JoinStrategyType stype = (JoinStrategyType) joinNode.getProperty(NodeConstants.Info.JOIN_STRATEGY);
            if (!JoinStrategyType.MERGE.equals(stype)) {
              continue;
            }
           
            /**
             * Don't push sorts for unbalanced inner joins, we prefer to use a processing time cost based decision
             */
            boolean pushLeft = true;
            boolean pushRight = true;
            if (joinNode.getProperty(NodeConstants.Info.JOIN_TYPE) == JoinType.JOIN_INNER && context != null) {
              float leftCost = NewCalculateCostUtil.computeCostForTree(joinNode.getFirstChild(), metadata);
              float rightCost = NewCalculateCostUtil.computeCostForTree(joinNode.getLastChild(), metadata);
              if (leftCost != NewCalculateCostUtil.UNKNOWN_VALUE && rightCost != NewCalculateCostUtil.UNKNOWN_VALUE
                  && (leftCost > context.getProcessorBatchSize() || rightCost > context.getProcessorBatchSize())) {
                //we use a larger constant here to ensure that we don't unwisely prevent pushdown
                pushLeft = leftCost < context.getProcessorBatchSize() || leftCost / rightCost < 8;
                pushRight = rightCost < context.getProcessorBatchSize() || rightCost / leftCost < 8 || joinNode.getProperty(NodeConstants.Info.DEPENDENT_VALUE_SOURCE) != null;
              }
            }

            List<SingleElementSymbol> leftExpressions = (List<SingleElementSymbol>) joinNode.getProperty(NodeConstants.Info.LEFT_EXPRESSIONS);
            List<SingleElementSymbol> rightExpressions = (List<SingleElementSymbol>) joinNode.getProperty(NodeConstants.Info.RIGHT_EXPRESSIONS);

            //check index information on each side
            //TODO: don't do null order compensation - in fact we should check what the order actually is, but we don't have that metadata
            Object key = null;
            boolean right = true;
            //we check the right first, since it should be larger
            if (joinNode.getLastChild().getType() == NodeConstants.Types.ACCESS && NewCalculateCostUtil.isSingleTable(joinNode.getLastChild())) {
              key = NewCalculateCostUtil.getKeyUsed(rightExpressions, null, metadata, null);
            }
            if (key == null && joinNode.getFirstChild().getType() == NodeConstants.Types.ACCESS && NewCalculateCostUtil.isSingleTable(joinNode.getFirstChild())) {
              key = NewCalculateCostUtil.getKeyUsed(leftExpressions, null, metadata, null);
              right = false;
            }
            if (key != null) {
              //redo the join predicates based upon the key alone
              List<Object> keyCols = metadata.getElementIDsInKey(key);
              int[] reorder = new int[keyCols.size()];
              List<Integer> toCriteria = new ArrayList<Integer>(rightExpressions.size() - keyCols.size());
              List<SingleElementSymbol> keyExpressions = right?rightExpressions:leftExpressions;
            for (int j = 0; j < keyExpressions.size(); j++) {
          SingleElementSymbol ses = keyExpressions.get(j);
          if (!(ses instanceof ElementSymbol)) {
            continue;
          }
          ElementSymbol es = (ElementSymbol)ses;
          boolean found = false;
          for (int i = 0; !found && i < keyCols.size(); i++) {
            if (es.getMetadataID().equals(keyCols.get(i))) {
              reorder[i] = j;
              found = true;
            }
          }
          if (!found) {
            toCriteria.add(j);
          }
        }
            List<Criteria> joinCriteria = (List<Criteria>) joinNode.getProperty(Info.JOIN_CRITERIA);
            for (int index : toCriteria) {
          SingleElementSymbol lses = leftExpressions.get(index);
          SingleElementSymbol rses = rightExpressions.get(index);
          CompareCriteria cc = new CompareCriteria(lses, CompareCriteria.EQ, rses);
          if (joinCriteria == null || joinCriteria.isEmpty()) {
            joinCriteria = new ArrayList<Criteria>();
            joinCriteria.add(cc);
            joinNode.setProperty(Info.JOIN_TYPE, JoinType.JOIN_INNER);
          }
        }
            joinNode.setProperty(Info.JOIN_CRITERIA, joinCriteria);
            leftExpressions = RelationalNode.projectTuple(reorder, leftExpressions);
              rightExpressions = RelationalNode.projectTuple(reorder, rightExpressions);
              joinNode.setProperty(NodeConstants.Info.LEFT_EXPRESSIONS, leftExpressions);
              joinNode.setProperty(NodeConstants.Info.RIGHT_EXPRESSIONS, rightExpressions);
            }
           
      boolean pushedLeft = insertSort(joinNode.getFirstChild(), leftExpressions, joinNode, metadata, capabilitiesFinder, pushLeft)
      insertSort(joinNode.getLastChild(), rightExpressions, joinNode, metadata, capabilitiesFinder, pushRight);
         
          if (joinNode.getProperty(NodeConstants.Info.JOIN_TYPE) == JoinType.JOIN_INNER && (!pushRight || !pushedLeft)) {
            joinNode.setProperty(NodeConstants.Info.JOIN_STRATEGY, JoinStrategyType.ENHANCED_SORT);
          }
        }
       
        return plan;
    }
View Full Code Here

   
    public void testFindJoinRegions() {
       
        List regions = new ArrayList();
       
        PlanNode joinRoot = TestFrameUtil.getExamplePlan();
       
        PlanNode joinRoot1 = TestFrameUtil.getExamplePlan();
       
        PlanNode outerJoin = NodeFactory.getNewNode(NodeConstants.Types.JOIN);
       
        outerJoin.setProperty(NodeConstants.Info.JOIN_TYPE, JoinType.JOIN_LEFT_OUTER);
       
        outerJoin.addFirstChild(joinRoot);
        outerJoin.addFirstChild(joinRoot1);
       
        PlanNode source = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
       
        source.addFirstChild(outerJoin);
       
        RulePlanJoins.findJoinRegions(source, null, regions);
       
        assertEquals(3, regions.size());
       
View Full Code Here

    public void testReconstruction() {
       
        List regions = new ArrayList();
       
        PlanNode joinRoot = TestFrameUtil.getExamplePlan();
       
        PlanNode source = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
       
        source.addFirstChild(joinRoot);
       
        RulePlanJoins.findJoinRegions(source, null, regions);
       
        assertEquals(1, regions.size());
       
        JoinRegion region = (JoinRegion)regions.get(0);
       
        assertEquals(3, region.getJoinSourceNodes().size());
       
        assertEquals(joinRoot, region.getJoinRoot());
       
        region.changeJoinOrder(new Object[] {new Integer(1), new Integer(0), new Integer(2)});
       
        region.reconstructJoinRegoin();
       
        PlanNode root = region.getJoinRoot();
       
        assertEquals(NodeConstants.Types.JOIN, root.getFirstChild().getType());
       
        //the tree is now left linear so go down a couple of levels to get to the first source
        assertEquals(NodeConstants.Types.SOURCE, root.getFirstChild().getFirstChild().getFirstChild().getType());
    }
View Full Code Here

    /**
     * Simple test to ensure that the reconstruction logic doesn't fail with a single source
     */
    public void testReconstructionOf1Source() {
       
        PlanNode source = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
       
        PlanNode accessNode = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
       
        source.addFirstChild(accessNode);
       
        JoinRegion region = new JoinRegion();
       
View Full Code Here

     */
    static boolean insertSort(PlanNode childNode, List<SingleElementSymbol> expressions, PlanNode jnode, QueryMetadataInterface metadata, CapabilitiesFinder capFinder,
        boolean attemptPush) throws QueryMetadataException, TeiidComponentException {
        Set<SingleElementSymbol> orderSymbols = new LinkedHashSet<SingleElementSymbol>(expressions);

        PlanNode sourceNode = FrameUtil.findJoinSourceNode(childNode);
        PlanNode joinNode = childNode.getParent();

        Set<SingleElementSymbol> outputSymbols = new LinkedHashSet<SingleElementSymbol>((List<SingleElementSymbol>)childNode.getProperty(NodeConstants.Info.OUTPUT_COLS));
       
        int oldSize = outputSymbols.size();
       
        outputSymbols.addAll(expressions);
       
        boolean needsCorrection = outputSymbols.size() > oldSize;
               
        PlanNode sortNode = createSortNode(new ArrayList<SingleElementSymbol>(orderSymbols), outputSymbols);
       
        if (sourceNode.getType() == NodeConstants.Types.ACCESS) {
          if (NewCalculateCostUtil.usesKey(sourceNode, expressions, metadata)) {
                joinNode.setProperty(joinNode.getFirstChild() == childNode ? NodeConstants.Info.IS_LEFT_DISTINCT : NodeConstants.Info.IS_RIGHT_DISTINCT, true);
          }
          if (attemptPush && RuleRaiseAccess.canRaiseOverSort(sourceNode, metadata, capFinder, sortNode, null, false)) {
              sourceNode.getFirstChild().addAsParent(sortNode);
             
              if (needsCorrection) {
                  correctOutputElements(joinNode, outputSymbols, sortNode);
              }
              return true;
          }
        }
       
        joinNode.setProperty(joinNode.getFirstChild() == childNode ? NodeConstants.Info.SORT_LEFT : NodeConstants.Info.SORT_RIGHT, SortOption.SORT);
       
        if (needsCorrection) {
            PlanNode projectNode = NodeFactory.getNewNode(NodeConstants.Types.PROJECT);
            projectNode.setProperty(NodeConstants.Info.PROJECT_COLS, new ArrayList(outputSymbols));
            childNode.addAsParent(projectNode);
            correctOutputElements(joinNode, outputSymbols, projectNode);
        }       
        return false;
    }
View Full Code Here

        return false;
    }

    private static PlanNode createSortNode(List<SingleElementSymbol> orderSymbols,
                                           Collection outputElements) {
        PlanNode sortNode = NodeFactory.getNewNode(NodeConstants.Types.SORT);
        sortNode.setProperty(NodeConstants.Info.SORT_ORDER, new OrderBy(orderSymbols));
        sortNode.setProperty(NodeConstants.Info.OUTPUT_COLS, new ArrayList(outputElements));
        return sortNode;
    }
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.