Search in sources :

Example 16 with PlanNode

use of org.teiid.query.optimizer.relational.plantree.PlanNode in project teiid by teiid.

the class TestRuleChooseDependent method helpTestChooseSiblingAndMarkDependent.

/**
 * Tests choosing from two eligible sibling access nodes, and then tests marking
 * the chosen one dependent.  This method sets up a bogus plan tree using a
 * bogus project parent node, a join node using the supplied join criteria, and
 * two access nodes using each of the groups and (optional) atomic criteria and
 * join criteria.  Then
 * this method tests that, if an access node is chosen, it is marked dependent,
 * and that the chosen one is the one which was expected to be marked dependent.
 * @param atomicRequestGroup1 GroupSymbol to select from in atomic request 1
 * @param atomicRequestCrit1 optional, may be null
 * @param atomicRequestGroup1a optional, may be null
 * @param atomicRequestCrit1a optional, may be null
 * @param atomicJoinCriteria1 optional, may be null
 * @param atomicRequestGroup2 GroupSymbol to select from in atomic request 2
 * @param atomicRequestCrit2 optional, may be null
 * @param atomicRequestGroup2a optional, may be null
 * @param atomicRequestCrit2a optional, may be null
 * @param atomicJoinCriteria2 optional, may be null
 * @param joinCriteria Collection of Criteria to add to outer join node
 * @param expectedMadeDependent one of the three outcome possibility class constants
 * @throws TeiidComponentException
 * @throws QueryMetadataException
 * @throws QueryPlannerException
 */
private void helpTestChooseSiblingAndMarkDependent(GroupSymbol atomicRequestGroup1, // optional
Criteria atomicRequestCrit1, // optional
GroupSymbol atomicRequestGroup1a, // optional
Criteria atomicRequestCrit1a, // optional
Collection atomicJoinCriteria1, GroupSymbol atomicRequestGroup2, // optional
Criteria atomicRequestCrit2, // optional
GroupSymbol atomicRequestGroup2a, // optional
Criteria atomicRequestCrit2a, // optional
Collection atomicJoinCriteria2, Collection joinCriteria, int expectedMadeDependent, Number expectedCost1, Number expectedCost2) throws QueryPlannerException, QueryMetadataException, TeiidComponentException {
    // EXAMPLE:
    // Project(groups=[])
    // Join(groups=[], props={21=joinCriteria, 23=true, 22=INNER JOIN})
    // Access(groups=[atomicRequestGroup1], props={...})
    // Source(groups=[atomicRequestGroup1])
    // Access(groups=[atomicRequestGroup2, atomicRequestGroup2a], props={...})
    // Join(groups=[atomicRequestGroup2, atomicRequestGroup2a], props={21=[atomicJoinCriteria2], 23=true, 22=INNER JOIN})
    // Select(groups=[atomicRequestGroup2], props={40=atomicRequestCrit2})
    // Source(groups=[atomicRequestGroup2])
    // Source(groups=[atomicRequestGroup2a])
    PlanNode accessNode1 = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
    accessNode1.addGroup(atomicRequestGroup1);
    if (atomicRequestGroup1a != null) {
        accessNode1.addGroup(atomicRequestGroup1a);
    }
    PlanNode accessNode2 = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
    accessNode2.addGroup(atomicRequestGroup2);
    if (atomicRequestGroup2a != null) {
        accessNode2.addGroup(atomicRequestGroup2a);
    }
    PlanNode joinNode = NodeFactory.getNewNode(NodeConstants.Types.JOIN);
    joinNode.setProperty(NodeConstants.Info.JOIN_TYPE, JoinType.JOIN_INNER);
    joinNode.setProperty(NodeConstants.Info.JOIN_CRITERIA, joinCriteria);
    joinNode.setProperty(NodeConstants.Info.JOIN_STRATEGY, JoinStrategyType.NESTED_LOOP);
    joinNode.addLastChild(accessNode1);
    joinNode.addLastChild(accessNode2);
    PlanNode bogusParentNode = NodeFactory.getNewNode(NodeConstants.Types.PROJECT);
    bogusParentNode.addLastChild(joinNode);
    // FIRST (LEFT) BRANCH OF TREE
    PlanNode sourceNode1 = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
    sourceNode1.addGroup(atomicRequestGroup1);
    if (atomicRequestCrit1 != null) {
        PlanNode selectNode1 = NodeFactory.getNewNode(NodeConstants.Types.SELECT);
        selectNode1.setProperty(NodeConstants.Info.SELECT_CRITERIA, atomicRequestCrit1);
        selectNode1.addGroup(atomicRequestGroup1);
        selectNode1.addFirstChild(sourceNode1);
        if (atomicRequestGroup1a != null) {
            PlanNode atomicJoinNode1 = NodeFactory.getNewNode(NodeConstants.Types.JOIN);
            if (atomicJoinCriteria1.isEmpty()) {
                atomicJoinNode1.setProperty(NodeConstants.Info.JOIN_TYPE, JoinType.JOIN_CROSS);
            } else {
                atomicJoinNode1.setProperty(NodeConstants.Info.JOIN_TYPE, JoinType.JOIN_INNER);
                atomicJoinNode1.setProperty(NodeConstants.Info.JOIN_CRITERIA, atomicJoinCriteria1);
            }
            atomicJoinNode1.addGroup(atomicRequestGroup1);
            atomicJoinNode1.addGroup(atomicRequestGroup1a);
            atomicJoinNode1.addLastChild(selectNode1);
            PlanNode sourceNode1a = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
            sourceNode1a.addGroup(atomicRequestGroup1a);
            if (atomicRequestCrit1a != null) {
                PlanNode selectNode1a = NodeFactory.getNewNode(NodeConstants.Types.SELECT);
                selectNode1a.setProperty(NodeConstants.Info.SELECT_CRITERIA, atomicRequestCrit1a);
                selectNode1a.addGroup(atomicRequestGroup1a);
                selectNode1a.addFirstChild(sourceNode1a);
                atomicJoinNode1.addLastChild(selectNode1a);
            } else {
                atomicJoinNode1.addLastChild(sourceNode1a);
            }
            accessNode1.addLastChild(atomicJoinNode1);
        } else {
            accessNode1.addFirstChild(selectNode1);
        }
    } else {
        accessNode1.addFirstChild(sourceNode1);
    }
    // SECOND (RIGHT) BRANCH OF TREE
    PlanNode sourceNode2 = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
    sourceNode2.addGroup(atomicRequestGroup2);
    if (atomicRequestCrit2 != null) {
        PlanNode selectNode2 = NodeFactory.getNewNode(NodeConstants.Types.SELECT);
        selectNode2.setProperty(NodeConstants.Info.SELECT_CRITERIA, atomicRequestCrit2);
        selectNode2.addGroup(atomicRequestGroup2);
        selectNode2.addFirstChild(sourceNode2);
        if (atomicRequestGroup2a != null) {
            PlanNode atomicJoinNode2 = NodeFactory.getNewNode(NodeConstants.Types.JOIN);
            if (atomicJoinCriteria2.isEmpty()) {
                atomicJoinNode2.setProperty(NodeConstants.Info.JOIN_TYPE, JoinType.JOIN_CROSS);
            } else {
                atomicJoinNode2.setProperty(NodeConstants.Info.JOIN_TYPE, JoinType.JOIN_INNER);
                atomicJoinNode2.setProperty(NodeConstants.Info.JOIN_CRITERIA, atomicJoinCriteria2);
            }
            atomicJoinNode2.addGroup(atomicRequestGroup2);
            atomicJoinNode2.addGroup(atomicRequestGroup2a);
            atomicJoinNode2.addLastChild(selectNode2);
            PlanNode sourceNode2a = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
            sourceNode2a.addGroup(atomicRequestGroup2a);
            if (atomicRequestCrit2a != null) {
                PlanNode selectNode2a = NodeFactory.getNewNode(NodeConstants.Types.SELECT);
                selectNode2a.setProperty(NodeConstants.Info.SELECT_CRITERIA, atomicRequestCrit2a);
                selectNode2a.addGroup(atomicRequestGroup2a);
                selectNode2a.addFirstChild(sourceNode2a);
                atomicJoinNode2.addLastChild(selectNode2a);
            } else {
                atomicJoinNode2.addLastChild(sourceNode2a);
            }
            accessNode2.addLastChild(atomicJoinNode2);
        } else {
            accessNode2.addFirstChild(selectNode2);
        }
    } else {
        accessNode2.addFirstChild(sourceNode2);
    }
    // Add access pattern(s)
    RulePlaceAccess.addAccessPatternsProperty(accessNode1, metadata);
    RulePlaceAccess.addAccessPatternsProperty(accessNode2, metadata);
    if (DEBUG) {
        // $NON-NLS-1$
        System.out.println("Before.");
        System.out.println(bogusParentNode);
    }
    RuleChooseDependent rule = new RuleChooseDependent();
    RuleChooseJoinStrategy.chooseJoinStrategy(joinNode, metadata);
    FakeCapabilitiesFinder capFinder = new FakeCapabilitiesFinder();
    // $NON-NLS-1$
    capFinder.addCapabilities("pm1", TestOptimizer.getTypicalCapabilities());
    // $NON-NLS-1$
    capFinder.addCapabilities("pm2", TestOptimizer.getTypicalCapabilities());
    // $NON-NLS-1$
    capFinder.addCapabilities("pm3", TestOptimizer.getTypicalCapabilities());
    // $NON-NLS-1$
    capFinder.addCapabilities("pm4", TestOptimizer.getTypicalCapabilities());
    rule.execute(bogusParentNode, metadata, capFinder, new RuleStack(), null, new CommandContext());
    if (DEBUG) {
        // $NON-NLS-1$
        System.out.println("Done.");
        System.out.println(bogusParentNode);
    }
    Object prop1 = joinNode.getProperty(NodeConstants.Info.DEPENDENT_VALUE_SOURCE);
    if (expectedMadeDependent == LEFT_SIDE) {
        // $NON-NLS-1$
        assertNotNull("Expected one side to be made dependent", prop1);
        assertEquals(accessNode1, FrameUtil.findJoinSourceNode(joinNode.getLastChild()));
    } else if (expectedMadeDependent == RIGHT_SIDE) {
        // $NON-NLS-1$
        assertNotNull("Expected one side to be made dependent", prop1);
        assertEquals(accessNode2, FrameUtil.findJoinSourceNode(joinNode.getLastChild()));
    } else if (expectedMadeDependent == NEITHER_SIDE) {
        // $NON-NLS-1$
        assertNull("Neither side should be dependent", prop1);
    } else {
        // $NON-NLS-1$
        fail("Invalid test constant " + expectedMadeDependent);
    }
    Float cost1 = (Float) accessNode1.getProperty(NodeConstants.Info.EST_CARDINALITY);
    Float cost2 = (Float) accessNode2.getProperty(NodeConstants.Info.EST_CARDINALITY);
    assertNotNull(cost2);
    assertNotNull(cost1);
    if (expectedCost1 != null) {
        assertEquals(expectedCost1.longValue(), cost1.longValue());
        assertEquals(expectedCost2.longValue(), cost2.longValue());
    }
}
Also used : FakeCapabilitiesFinder(org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) CommandContext(org.teiid.query.util.CommandContext) RuleStack(org.teiid.query.optimizer.relational.RuleStack)

Example 17 with PlanNode

use of org.teiid.query.optimizer.relational.plantree.PlanNode in project teiid by teiid.

the class TestRulePushSelectCriteria method testPushAcrossFrameWithAccessNode.

@Test
public void testPushAcrossFrameWithAccessNode() throws Exception {
    QueryMetadataInterface metadata = new TempMetadataAdapter(RealMetadataFactory.example1Cached(), new TempMetadataStore());
    // $NON-NLS-1$
    Command command = TestOptimizer.helpGetCommand("select * from (select * from pm1.g1 union select * from pm1.g2) x where e1 = 1", metadata, null);
    // $NON-NLS-1$
    Command subCommand = TestOptimizer.helpGetCommand("select * from pm1.g1 union select * from pm1.g2", metadata, null);
    RelationalPlanner p = new RelationalPlanner();
    CommandContext cc = new CommandContext();
    p.initialize(command, null, metadata, null, null, cc);
    PlanNode root = p.generatePlan(command);
    PlanNode child = p.generatePlan(subCommand);
    PlanNode sourceNode = NodeEditor.findNodePreOrder(root, NodeConstants.Types.SOURCE);
    sourceNode.addFirstChild(child);
    sourceNode.setProperty(NodeConstants.Info.SYMBOL_MAP, SymbolMap.createSymbolMap(sourceNode.getGroups().iterator().next(), (List<Expression>) child.getFirstChild().getProperty(Info.PROJECT_COLS), metadata));
    // add a dummy access node
    PlanNode accessNode = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
    accessNode.addGroups(child.getFirstChild().getGroups());
    child.getFirstChild().addAsParent(accessNode);
    new RulePushSelectCriteria().execute(root, metadata, new DefaultCapabilitiesFinder(), new RuleStack(), AnalysisRecord.createNonRecordingRecord(), cc);
    // the select node should still be above the access node
    accessNode = NodeEditor.findNodePreOrder(root, NodeConstants.Types.ACCESS);
    assertEquals(NodeConstants.Types.SELECT, accessNode.getParent().getType());
    assertNull(NodeEditor.findNodePreOrder(accessNode, NodeConstants.Types.SELECT));
}
Also used : TempMetadataAdapter(org.teiid.query.metadata.TempMetadataAdapter) RelationalPlanner(org.teiid.query.optimizer.relational.RelationalPlanner) PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode) CommandContext(org.teiid.query.util.CommandContext) Command(org.teiid.query.sql.lang.Command) List(java.util.List) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) RuleStack(org.teiid.query.optimizer.relational.RuleStack) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) TempMetadataStore(org.teiid.query.metadata.TempMetadataStore) Test(org.junit.Test)

Example 18 with PlanNode

use of org.teiid.query.optimizer.relational.plantree.PlanNode in project teiid by teiid.

the class TestNodeEditor method testFindNodePreOrder1.

public void testFindNodePreOrder1() {
    PlanNode node1 = NodeFactory.getNewNode(NodeConstants.Types.PROJECT);
    PlanNode node2 = NodeFactory.getNewNode(NodeConstants.Types.JOIN);
    PlanNode node3 = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
    PlanNode node4 = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
    node1.addLastChild(node2);
    node2.addLastChild(node3);
    node2.addLastChild(node4);
    // $NON-NLS-1$
    assertEquals("Found wrong node", node1, NodeEditor.findNodePreOrder(node1, NodeConstants.Types.PROJECT));
    // $NON-NLS-1$
    assertEquals("Found wrong node", node2, NodeEditor.findNodePreOrder(node1, NodeConstants.Types.JOIN));
    // $NON-NLS-1$
    assertEquals("Found wrong node", node3, NodeEditor.findNodePreOrder(node1, NodeConstants.Types.ACCESS));
    // $NON-NLS-1$
    assertEquals("Found wrong node", null, NodeEditor.findNodePreOrder(node1, NodeConstants.Types.GROUP));
    // $NON-NLS-1$
    assertEquals("Found wrong node", null, NodeEditor.findNodePreOrder(node1, NodeConstants.Types.ACCESS, NodeConstants.Types.JOIN));
}
Also used : PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode)

Example 19 with PlanNode

use of org.teiid.query.optimizer.relational.plantree.PlanNode in project teiid by teiid.

the class TestNodeEditor method testFindParent.

public void testFindParent() {
    PlanNode node0 = NodeFactory.getNewNode(NodeConstants.Types.SOURCE);
    PlanNode node1 = NodeFactory.getNewNode(NodeConstants.Types.PROJECT);
    PlanNode node2 = NodeFactory.getNewNode(NodeConstants.Types.JOIN);
    PlanNode node3 = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
    PlanNode node4 = NodeFactory.getNewNode(NodeConstants.Types.ACCESS);
    node0.addLastChild(node1);
    node1.addLastChild(node2);
    node2.addLastChild(node3);
    node2.addLastChild(node4);
    // $NON-NLS-1$
    assertEquals("Found wrong node", node1, NodeEditor.findParent(node4, NodeConstants.Types.PROJECT));
    // $NON-NLS-1$
    assertNull("Found wrong node", NodeEditor.findParent(node1, NodeConstants.Types.PROJECT));
    // $NON-NLS-1$
    assertNull("Found wrong node", NodeEditor.findParent(node4, NodeConstants.Types.PROJECT, NodeConstants.Types.JOIN | NodeConstants.Types.SELECT));
    // $NON-NLS-1$
    assertNull("Found wrong node", NodeEditor.findParent(node1, NodeConstants.Types.GROUP));
}
Also used : PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode)

Example 20 with PlanNode

use of org.teiid.query.optimizer.relational.plantree.PlanNode in project teiid by teiid.

the class TestCalculateCostUtil method helpGetJoinNode.

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;
}
Also used : PlanNode(org.teiid.query.optimizer.relational.plantree.PlanNode)

Aggregations

PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)204 Expression (org.teiid.query.sql.symbol.Expression)50 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)50 ArrayList (java.util.ArrayList)47 List (java.util.List)43 SymbolMap (org.teiid.query.sql.util.SymbolMap)42 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)36 Criteria (org.teiid.query.sql.lang.Criteria)35 LinkedList (java.util.LinkedList)24 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)24 Test (org.junit.Test)22 HashSet (java.util.HashSet)17 JoinType (org.teiid.query.sql.lang.JoinType)17 LinkedHashSet (java.util.LinkedHashSet)16 CompoundCriteria (org.teiid.query.sql.lang.CompoundCriteria)12 DependentSetCriteria (org.teiid.query.sql.lang.DependentSetCriteria)12 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)11 LanguageObject (org.teiid.query.sql.LanguageObject)11 OrderBy (org.teiid.query.sql.lang.OrderBy)10 IsNullCriteria (org.teiid.query.sql.lang.IsNullCriteria)9