/** * Set the ProcessorPlan. * @param context */ public void setPlan(ProcessorPlan planValue, CommandContext context){ plan = planValue; this.accessInfo.populate(context, false); //TODO: expand this logic if (planValue instanceof RelationalPlan) { RelationalPlan rp = (RelationalPlan)planValue; if (rp.getRootNode() instanceof AccessNode) { this.accessInfo.setSensitiveToMetadataChanges(false); } } }
/** * Set the ProcessorPlan. * @param context */ public void setPlan(ProcessorPlan planValue, CommandContext context){ plan = planValue; this.accessInfo.populate(context, false); //TODO: expand this logic if (planValue instanceof RelationalPlan) { RelationalPlan rp = (RelationalPlan)planValue; if (rp.getRootNode() instanceof AccessNode) { this.accessInfo.setSensitiveToMetadataChanges(false); } } }
public static Set<String> getAtomicQueries(ProcessorPlan plan) { Set<Command> atomicQueries = new HashSet<Command>(); if(plan instanceof RelationalPlan) { getAtomicCommands( ((RelationalPlan)plan).getRootNode(), atomicQueries ); } Set<String> stringQueries = new HashSet<String>(); for (Command command : atomicQueries) { stringQueries.add(command.toString()); } return stringQueries; }
static void checkDependentGroups(ProcessorPlan plan, String[] groups) { if(! (plan instanceof RelationalPlan)) { return; } // Collect all the group names (uppercase) for all the dependent groups in the plan Set<String> depGroups = new HashSet<String>(); getDependentGroups(((RelationalPlan)plan).getRootNode(), depGroups, true); // Check that all the expected groups exist in depGroups Set<String> expectedGroups = new HashSet<String>(); for(int i=0; i<groups.length; i++) { expectedGroups.add(groups[i].toUpperCase()); } assertEquals("Expected groups were not made dependent", expectedGroups, depGroups); //$NON-NLS-1$ }
private void checkNotDependentGroups(ProcessorPlan plan, String[] groups) { if(! (plan instanceof RelationalPlan)) { return; } // Collect all the group names (uppercase) for all the dependent groups in the plan Set<String> notDepGroups = new HashSet<String>(); getDependentGroups(((RelationalPlan)plan).getRootNode(), notDepGroups, false); // Check that all the expected groups exist in depGroups Set<String> expectedGroups = new HashSet<String>(); for(int i=0; i<groups.length; i++) { expectedGroups.add(groups[i].toUpperCase()); } assertEquals("Expected groups were made dependent", expectedGroups, notDepGroups); //$NON-NLS-1$ }
public static void checkNodeTypes(ProcessorPlan root, int[] expectedCounts, Class<?>[] types) { if(! (root instanceof RelationalPlan)) { return; } int[] actualCounts = new int[types.length]; collectCounts(((RelationalPlan)root).getRootNode(), actualCounts, types); for(int i=0; i<expectedCounts.length; i++) { assertEquals("Did not find the correct number of nodes for type " + types[i], //$NON-NLS-1$ expectedCounts[i], actualCounts[i]); } }
private void helpAssertIsBatchedPlan(RelationalPlan plan, boolean isBatchedPlan) { RelationalNode node = plan.getRootNode(); if (node instanceof ProjectNode) { node = node.getChildren()[0]; } if (isBatchedPlan) { assertTrue("Plan should have been a batched", node instanceof BatchedUpdateNode); //$NON-NLS-1$ } else { assertTrue("Plan should not have been batched.", node instanceof AccessNode); //$NON-NLS-1$ } }
@Test public void testConformedJoin() throws Exception { String sql = "select pm1.g1.e1 from pm1.g1, pm2.g2 where g1.e1=g2.e1"; RelationalPlan plan = (RelationalPlan)helpPlan(sql, tm, new String[] {"SELECT g_0.e1 FROM pm1.g1 AS g_0, pm2.g2 AS g_1 WHERE g_0.e1 = g_1.e1"}, ComparisonMode.EXACT_COMMAND_STRING); AccessNode anode = (AccessNode) plan.getRootNode(); assertEquals("pm2", anode.getModelName()); //it should work either way sql = "select pm1.g1.e1 from pm2.g2, pm1.g1 where g1.e1=g2.e1"; plan = (RelationalPlan)helpPlan(sql, tm, new String[] {"SELECT g_1.e1 FROM pm2.g2 AS g_0, pm1.g1 AS g_1 WHERE g_1.e1 = g_0.e1"}, ComparisonMode.EXACT_COMMAND_STRING); anode = (AccessNode) plan.getRootNode(); assertEquals("pm2", anode.getModelName()); }
@Test public void testLimitWithUnknownChildCardinality() throws Exception { String query = "select e1 from pm1.g1 limit 2"; //$NON-NLS-1$ RelationalPlan plan = (RelationalPlan)TestOptimizer.helpPlan(query, RealMetadataFactory.example1Cached(), new String[] {"SELECT e1 FROM pm1.g1"}); //$NON-NLS-1$ assertEquals(new Float(2), plan.getRootNode().getEstimateNodeCardinality()); }
@Test public void testMultiReplacementWithLimit1() throws Exception { final QueryMetadataInterface metadata = RealMetadataFactory.exampleMultiBinding(); final String userSql = "SELECT a, b FROM MultiModel.Phys limit 1, 1"; //$NON-NLS-1$ final String multiModel = "MultiModel"; //$NON-NLS-1$ final int sources = 2; final List<?>[] expected = new List<?>[] { Arrays.asList("x", "z"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ }; final HardcodedDataManager dataMgr = new HardcodedDataManager(); dataMgr.addData("SELECT g_0.a AS c_0, g_0.b AS c_1 FROM MultiModel.Phys AS g_0 LIMIT 2", //$NON-NLS-1$ new List<?>[] { Arrays.asList("y", "z"), //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ Arrays.asList("x", "z")}); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ RelationalPlan plan = (RelationalPlan)helpTestMultiSourcePlan(metadata, userSql, multiModel, sources, dataMgr, expected, RealMetadataFactory.exampleMultiBindingVDB()); assertTrue(plan.getRootNode() instanceof LimitNode); }
@Test public void testSortAliasWithSameName() throws Exception { String sql = "select e1 from (select distinct pm1.g1.e1 as e1 from pm1.g1) x order by e1"; //$NON-NLS-1$ FakeCapabilitiesFinder capFinder = new FakeCapabilitiesFinder(); BasicSourceCapabilities caps = new BasicSourceCapabilities(); caps.setCapabilitySupport(Capability.QUERY_FROM_GROUP_ALIAS, true); capFinder.addCapabilities("pm1", caps); //$NON-NLS-1$ RelationalPlan plan = (RelationalPlan)TestOptimizer.helpPlan(sql, RealMetadataFactory.example1Cached(), new String[] {"SELECT g_0.e1 FROM pm1.g1 AS g_0"}, capFinder, TestOptimizer.ComparisonMode.EXACT_COMMAND_STRING); //$NON-NLS-1$ SortNode node = (SortNode)plan.getRootNode(); assertTrue("Alias was not accounted for in sort node", node.getElements().get(0).equals(node.getSortElements().get(0).getSymbol())); //$NON-NLS-1$ }
@Test public void testSortAliasWithSameNameUnion() throws Exception { String sql = "select e1 from (select distinct pm1.g1.e1 as e1 from pm1.g1) x union all select e1 from pm1.g2 order by e1"; //$NON-NLS-1$ FakeCapabilitiesFinder capFinder = new FakeCapabilitiesFinder(); BasicSourceCapabilities caps = new BasicSourceCapabilities(); caps.setCapabilitySupport(Capability.QUERY_FROM_GROUP_ALIAS, true); capFinder.addCapabilities("pm1", caps); //$NON-NLS-1$ RelationalPlan plan = (RelationalPlan)TestOptimizer.helpPlan(sql, RealMetadataFactory.example1Cached(), new String[] {"SELECT g_0.e1 FROM pm1.g1 AS g_0", "SELECT g_0.e1 FROM pm1.g2 AS g_0"}, capFinder, TestOptimizer.ComparisonMode.EXACT_COMMAND_STRING); //$NON-NLS-1$ SortNode node = (SortNode)plan.getRootNode(); assertTrue("Alias was not accounted for in sort node", node.getElements().get(0).equals(node.getSortElements().get(0).getSymbol())); //$NON-NLS-1$ }
private void helpTestQuery(float cost, String query, String[] expected) throws TeiidComponentException, TeiidProcessingException { RelationalPlan plan = (RelationalPlan)TestOptimizer.helpPlan(query, TestVirtualDepJoin.exampleVirtualDepJoin(), expected, ComparisonMode.EXACT_COMMAND_STRING); //$NON-NLS-1$ //$NON-NLS-2$ assertEquals(cost, plan.getRootNode().getEstimateNodeCardinality()); }
@Test public void testProjectionRaisingWithComplexOrdering1() { String sql = "select e1 || 1 as a, e2 / 2 from pm1.g1 as x order by a, e2 limit 2"; //$NON-NLS-1$ BasicSourceCapabilities bsc = TestOptimizer.getTypicalCapabilities(); bsc.setFunctionSupport(SourceSystemFunctions.CONCAT, true); bsc.setCapabilitySupport(Capability.ROW_LIMIT, true); CapabilitiesFinder finder = new DefaultCapabilitiesFinder(bsc); RelationalPlan plan = (RelationalPlan)helpPlan(sql, RealMetadataFactory.example1Cached(), null, finder, new String[] {"SELECT concat(g_0.e1, '1') AS c_0, g_0.e2 AS c_1 FROM pm1.g1 AS g_0 ORDER BY c_0, c_1 LIMIT 2"}, TestOptimizer.SHOULD_SUCCEED); //$NON-NLS-1$ assertTrue(plan.getRootNode() instanceof ProjectNode); HardcodedDataManager hdm = new HardcodedDataManager(); hdm.addData("SELECT concat(g_0.e1, '1') AS c_0, g_0.e2 AS c_1 FROM pm1.g1 AS g_0 ORDER BY c_0, c_1 LIMIT 2", Arrays.asList("c1", 2), Arrays.asList("d1", 3)); TestProcessor.helpProcess(plan, hdm, new List<?>[] {Arrays.asList("c1", 1), Arrays.asList("d1", 1)}); }
@Test public void testProjectionRaisingWithLimit() { // Create query String sql = "select e1, (select e1 from pm2.g1 where e2 = x.e2) from pm1.g1 as x order by e1 limit 2"; //$NON-NLS-1$ RelationalPlan plan = (RelationalPlan)helpPlan(sql, RealMetadataFactory.example1Cached(), null, new DefaultCapabilitiesFinder(), new String[] {"SELECT pm1.g1.e1, pm1.g1.e2 FROM pm1.g1"}, TestOptimizer.SHOULD_SUCCEED); //$NON-NLS-1$ assertTrue(plan.getRootNode() instanceof ProjectNode); }
@Test public void testProjectionRaisingWithLimit1() { // Create query String sql = "select (select e1 from pm2.g1 where e2 = x.e2) as z from pm1.g1 as x order by z limit 2"; //$NON-NLS-1$ RelationalPlan plan = (RelationalPlan)helpPlan(sql, RealMetadataFactory.example1Cached(), null, new DefaultCapabilitiesFinder(), new String[] {"SELECT pm1.g1.e2 FROM pm1.g1"}, TestOptimizer.SHOULD_SUCCEED); //$NON-NLS-1$ assertTrue(plan.getRootNode() instanceof LimitNode); }
@Test public void testCopyCriteriaMultiway() throws Exception { String sql = "select bqt1.smalla.intkey, bqt2.smalla.intkey from bqt1.smalla, bqt2.smalla, bqt1.smallb where bqt1.smalla.intnum = bqt2.smalla.intnum and cast(bqt1.smalla.stringkey as integer) = coalesce(bqt2.smalla.intkey, bqt1.smallb.intkey) and bqt2.smalla.intkey = 1"; //$NON-NLS-1$ ProcessorPlan plan = TestOptimizer.helpPlan(sql, RealMetadataFactory.exampleBQTCached(), new String[] {"SELECT g_0.IntKey FROM BQT1.SmallB AS g_0", "SELECT g_0.IntNum AS c_0, g_0.IntKey AS c_1 FROM BQT2.SmallA AS g_0 WHERE g_0.IntKey = 1 ORDER BY c_0", "SELECT g_0.IntNum AS c_0, g_0.StringKey AS c_1, g_0.IntKey AS c_2 FROM BQT1.SmallA AS g_0 ORDER BY c_0"}); RelationalPlan relationalPlan = (RelationalPlan)plan; JoinNode joinNode = (JoinNode) relationalPlan.getRootNode().getChildren()[0]; assertNotNull(joinNode.getJoinCriteria()); }
@Test public void testProjectionRaisingWithAccess1() throws Exception { // Create query String sql = "select e1, 1 as z from pm1.g1 as x group by e1 order by e1"; //$NON-NLS-1$ BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities(); caps.setCapabilitySupport(Capability.QUERY_GROUP_BY, true); caps.setCapabilitySupport(Capability.QUERY_SELECT_EXPRESSION, false); RelationalPlan plan = (RelationalPlan)helpPlan(sql, RealMetadataFactory.example1Cached(), null, new DefaultCapabilitiesFinder(caps), new String[] {"SELECT g_0.e1 FROM pm1.g1 AS g_0 GROUP BY g_0.e1 ORDER BY g_0.e1"}, ComparisonMode.EXACT_COMMAND_STRING); //$NON-NLS-1$ assertTrue(plan.getRootNode() instanceof ProjectNode); }
@Test public void testProjectionRaisingWithComplexOrdering() { String sql = "select e1 || 1, e2 / 2 from pm1.g1 as x order by e1 || 1 limit 2"; //$NON-NLS-1$ BasicSourceCapabilities bsc = TestOptimizer.getTypicalCapabilities(); bsc.setFunctionSupport(SourceSystemFunctions.CONCAT, true); bsc.setCapabilitySupport(Capability.ROW_LIMIT, true); CapabilitiesFinder finder = new DefaultCapabilitiesFinder(bsc); RelationalPlan plan = (RelationalPlan)helpPlan(sql, RealMetadataFactory.example1Cached(), null, finder, new String[] {"SELECT concat(g_0.e1, '1') AS c_0, g_0.e2 AS c_1 FROM pm1.g1 AS g_0 ORDER BY c_0 LIMIT 2"}, TestOptimizer.SHOULD_SUCCEED); //$NON-NLS-1$ assertTrue(plan.getRootNode() instanceof ProjectNode); }
@Test public void testOuterJoinRemoval() throws Exception { BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities(); caps.setCapabilitySupport(Capability.QUERY_FROM_JOIN_INNER, false); caps.setCapabilitySupport(Capability.QUERY_FROM_JOIN_OUTER, false); ProcessorPlan plan = TestOptimizer.helpPlan("SELECT * from pm1.g1 inner join (pm1.g2 left outer join pm1.g3 on pm1.g2.e1=pm1.g3.e1) on pm1.g1.e1=pm1.g3.e1", //$NON-NLS-1$ RealMetadataFactory.example1Cached(), new String[] { "SELECT g_0.e1 AS c_0, g_0.e2 AS c_1, g_0.e3 AS c_2, g_0.e4 AS c_3 FROM pm1.g2 AS g_0 ORDER BY c_0", "SELECT g_0.e1 AS c_0, g_0.e2 AS c_1, g_0.e3 AS c_2, g_0.e4 AS c_3 FROM pm1.g1 AS g_0 ORDER BY c_0", "SELECT g_0.e1 AS c_0, g_0.e2 AS c_1, g_0.e3 AS c_2, g_0.e4 AS c_3 FROM pm1.g3 AS g_0 ORDER BY c_0"}, new DefaultCapabilitiesFinder(caps), ComparisonMode.EXACT_COMMAND_STRING); //$NON-NLS-1$ RelationalNode node = ((RelationalPlan)plan).getRootNode().getChildren()[0]; assertTrue(node instanceof JoinNode); node = node.getChildren()[0]; assertTrue(node instanceof JoinNode); assertEquals(JoinType.JOIN_INNER, ((JoinNode)node).getJoinType()); }