Search in sources :

Example 1 with ProjectNode

use of org.teiid.query.processor.relational.ProjectNode in project teiid by teiid.

the class TestPreparedPlanCache method testGet.

@Test
public void testGet() {
    SessionAwareCache<PreparedPlan> cache = new SessionAwareCache<PreparedPlan>("preparedplan", DefaultCacheFactory.INSTANCE, SessionAwareCache.Type.PREPAREDPLAN, 0);
    helpPutPreparedPlans(cache, token, 0, 10);
    helpPutPreparedPlans(cache, token2, 0, 15);
    // read an entry for session2 (token2)
    PreparedPlan pPlan = cache.get(new CacheID(token2, pi, EXAMPLE_QUERY + 12));
    // $NON-NLS-1$
    assertNotNull("Unable to get prepared plan from cache", pPlan);
    // $NON-NLS-1$
    assertEquals("Error getting plan from cache", new RelationalPlan(new ProjectNode(12)).toString(), pPlan.getPlan().toString());
    // $NON-NLS-1$
    assertEquals("Error getting command from cache", EXAMPLE_QUERY + 12, pPlan.getCommand().toString());
    // $NON-NLS-1$
    assertNotNull("Error getting plan description from cache", pPlan.getAnalysisRecord());
    // $NON-NLS-1$
    assertEquals("Error gettting reference from cache", new Reference(1), pPlan.getReferences().get(0));
}
Also used : CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) Reference(org.teiid.query.sql.symbol.Reference) ProjectNode(org.teiid.query.processor.relational.ProjectNode) RelationalPlan(org.teiid.query.processor.relational.RelationalPlan) Test(org.junit.Test)

Example 2 with ProjectNode

use of org.teiid.query.processor.relational.ProjectNode in project teiid by teiid.

the class TestPreparedPlanCache method helpPutPreparedPlans.

// ====Help methods====//
private void helpPutPreparedPlans(SessionAwareCache<PreparedPlan> cache, DQPWorkContext session, int start, int count) {
    for (int i = 0; i < count; i++) {
        Command dummy;
        try {
            dummy = QueryParser.getQueryParser().parseCommand(EXAMPLE_QUERY + (start + i));
        } catch (QueryParserException e) {
            throw new RuntimeException(e);
        }
        CacheID id = new CacheID(session, pi, dummy.toString());
        PreparedPlan pPlan = new PreparedPlan();
        cache.put(id, Determinism.SESSION_DETERMINISTIC, pPlan, null);
        pPlan.setCommand(dummy);
        pPlan.setPlan(new RelationalPlan(new ProjectNode(i)), new CommandContext());
        AnalysisRecord analysisRecord = new AnalysisRecord(true, false);
        pPlan.setAnalysisRecord(analysisRecord);
        ArrayList<Reference> refs = new ArrayList<Reference>();
        refs.add(new Reference(1));
        pPlan.setReferences(refs);
    }
}
Also used : QueryParserException(org.teiid.api.exception.query.QueryParserException) AnalysisRecord(org.teiid.query.analysis.AnalysisRecord) CommandContext(org.teiid.query.util.CommandContext) Reference(org.teiid.query.sql.symbol.Reference) ArrayList(java.util.ArrayList) RelationalPlan(org.teiid.query.processor.relational.RelationalPlan) Command(org.teiid.query.sql.lang.Command) CacheID(org.teiid.dqp.internal.process.SessionAwareCache.CacheID) ProjectNode(org.teiid.query.processor.relational.ProjectNode)

Example 3 with ProjectNode

use of org.teiid.query.processor.relational.ProjectNode in project teiid by teiid.

the class TestSortOptimization method testProjectionRaisingWithComplexOrdering.

@Test
public void testProjectionRaisingWithComplexOrdering() {
    // $NON-NLS-1$
    String sql = "select e1 || 1, e2 / 2 from pm1.g1 as x order by e1 || 1 limit 2";
    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" }, // $NON-NLS-1$
    TestOptimizer.SHOULD_SUCCEED);
    assertTrue(plan.getRootNode() instanceof ProjectNode);
}
Also used : FakeCapabilitiesFinder(org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder) CapabilitiesFinder(org.teiid.query.optimizer.capabilities.CapabilitiesFinder) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) ProjectNode(org.teiid.query.processor.relational.ProjectNode) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) RelationalPlan(org.teiid.query.processor.relational.RelationalPlan) Test(org.junit.Test)

Example 4 with ProjectNode

use of org.teiid.query.processor.relational.ProjectNode in project teiid by teiid.

the class BatchedUpdatePlanner method optimize.

/**
 * Optimizes batched updates by batching all contiguous commands that relate to the same physical model.
 * For example, for the following batch of commands:
 * <br/>
 * <ol>
 *      <li>1.  INSERT INTO physicalModel.myPhysical ...</li>
 *      <li>2.  UPDATE physicalModel.myPhysical ... </li>
 *      <li>3.  DELETE FROM virtualmodel.myVirtual ... </li>
 *      <li>4.  UPDATE virtualmodel.myVirtual ... </li>
 *      <li>5.  UPDATE physicalModel.myOtherPhysical ...</li>
 *      <li>6.  INSERT INTO physicalModel.myOtherPhysical ... <li>
 *      <li>7.  DELETE FROM physicalModel.myOtherPhysical ...</li>
 *      <li>8.  INSERT INTO physicalModel.myPhysical ... </li>
 *      <li>9.  INSERT INTO physicalModel.myPhysical ... </li>
 *      <li>10. INSERT INTO physicalModel.myPhysical ... </li>
 *      <li>11. INSERT INTO physicalModel.myPhysical ... </li>
 *      <li>12. INSERT INTO physicalModel.myPhysical ... </li>
 * </ol>
 * <br/> this implementation will batch as follows: (1,2), (5, 6, 7), (8 thru 12).
 * The remaining commands/plans will be executed individually.
 * @see org.teiid.query.optimizer.CommandPlanner#optimize(Command, org.teiid.core.id.IDGenerator, org.teiid.query.metadata.QueryMetadataInterface, org.teiid.query.optimizer.capabilities.CapabilitiesFinder, org.teiid.query.analysis.AnalysisRecord, CommandContext)
 * @since 4.2
 */
public ProcessorPlan optimize(Command command, IDGenerator idGenerator, QueryMetadataInterface metadata, CapabilitiesFinder capFinder, AnalysisRecord analysisRecord, CommandContext context) throws QueryPlannerException, QueryMetadataException, TeiidComponentException {
    BatchedUpdateCommand batchedUpdateCommand = (BatchedUpdateCommand) command;
    List<ProcessorPlan> childPlans = new ArrayList<ProcessorPlan>(batchedUpdateCommand.getUpdateCommands().size());
    List<Command> updateCommands = batchedUpdateCommand.getUpdateCommands();
    int numCommands = updateCommands.size();
    List<VariableContext> allContexts = batchedUpdateCommand.getVariableContexts();
    List<VariableContext> planContexts = null;
    if (allContexts != null) {
        planContexts = new ArrayList<VariableContext>(allContexts.size());
    }
    for (int commandIndex = 0; commandIndex < numCommands; commandIndex++) {
        // Potentially the first command of a batch
        Command updateCommand = updateCommands.get(commandIndex);
        boolean commandWasBatched = false;
        // If this command can be placed in a batch
        if (isEligibleForBatching(updateCommand, metadata)) {
            // Get the model ID. Subsequent and contiguous commands that update a group in this model are candidates for this batch
            Object batchModelID = metadata.getModelID(getUpdatedGroup(updateCommand).getMetadataID());
            String modelName = metadata.getFullName(batchModelID);
            SourceCapabilities caps = capFinder.findCapabilities(modelName);
            // Only attempt batching if the source supports batching
            if (caps.supportsCapability(Capability.BATCHED_UPDATES)) {
                // Start a new batch
                List<Command> batch = new ArrayList<Command>();
                List<VariableContext> contexts = new ArrayList<VariableContext>();
                List<Boolean> shouldEvaluate = new ArrayList<Boolean>();
                // This is the first command in a potential batch, so add it to the batch
                batch.add(updateCommand);
                if (allContexts != null) {
                    contexts.add(allContexts.get(commandIndex));
                    shouldEvaluate.add(Boolean.TRUE);
                } else {
                    shouldEvaluate.add(EvaluatableVisitor.needsProcessingEvaluation(updateCommand));
                }
                // immediately and contiguously after this one
                batchLoop: for (int batchIndex = commandIndex + 1; batchIndex < numCommands; batchIndex++) {
                    Command batchingCandidate = updateCommands.get(batchIndex);
                    // If this command updates the same model, and is eligible for batching, add it to the batch
                    if (canBeAddedToBatch(batchingCandidate, batchModelID, metadata, capFinder)) {
                        batch.add(batchingCandidate);
                        if (allContexts != null) {
                            contexts.add(allContexts.get(batchIndex));
                            shouldEvaluate.add(Boolean.TRUE);
                        } else {
                            shouldEvaluate.add(EvaluatableVisitor.needsProcessingEvaluation(batchingCandidate));
                        }
                    } else {
                        // Otherwise, stop batching at this point. The next command may well be the start of a new batch
                        break batchLoop;
                    }
                }
                // If two or more contiguous commands made on the same model were found, then batch them
                if (batch.size() > 1) {
                    ProjectNode projectNode = new ProjectNode(idGenerator.nextInt());
                    // Create a BatchedUpdateNode that creates a batched request for the connector
                    BatchedUpdateNode batchNode = new BatchedUpdateNode(idGenerator.nextInt(), batch, contexts, shouldEvaluate, modelName);
                    List symbols = batchedUpdateCommand.getProjectedSymbols();
                    projectNode.setSelectSymbols(symbols);
                    projectNode.setElements(symbols);
                    batchNode.setElements(symbols);
                    projectNode.addChild(batchNode);
                    // Add a new RelationalPlan that represents the plan for this batch.
                    childPlans.add(new RelationalPlan(projectNode));
                    if (planContexts != null) {
                        planContexts.add(new VariableContext());
                    }
                    // Skip those commands that were added to this batch
                    commandIndex += batch.size() - 1;
                    commandWasBatched = true;
                }
            }
        }
        if (!commandWasBatched) {
            // If the command wasn't batched, just add the plan for this command to the list of plans
            Command cmd = batchedUpdateCommand.getUpdateCommands().get(commandIndex);
            ProcessorPlan plan = cmd.getProcessorPlan();
            if (plan == null) {
                plan = QueryOptimizer.optimizePlan(cmd, metadata, idGenerator, capFinder, analysisRecord, context);
            }
            childPlans.add(plan);
            if (allContexts != null) {
                planContexts.add(allContexts.get(commandIndex));
            }
        }
    }
    return new BatchedUpdatePlan(childPlans, batchedUpdateCommand.getUpdateCommands().size(), planContexts, batchedUpdateCommand.isSingleResult());
}
Also used : ArrayList(java.util.ArrayList) VariableContext(org.teiid.query.sql.util.VariableContext) RelationalPlan(org.teiid.query.processor.relational.RelationalPlan) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) Command(org.teiid.query.sql.lang.Command) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) BatchedUpdateNode(org.teiid.query.processor.relational.BatchedUpdateNode) ProjectNode(org.teiid.query.processor.relational.ProjectNode) ArrayList(java.util.ArrayList) List(java.util.List) SourceCapabilities(org.teiid.query.optimizer.capabilities.SourceCapabilities) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) BatchedUpdatePlan(org.teiid.query.processor.BatchedUpdatePlan)

Example 5 with ProjectNode

use of org.teiid.query.processor.relational.ProjectNode in project teiid by teiid.

the class TestSortOptimization method testProjectionRaisingWithAccess1.

@Test
public void testProjectionRaisingWithAccess1() throws Exception {
    // Create query
    // $NON-NLS-1$
    String sql = "select e1, 1 as z from pm1.g1 as x group by e1 order by e1";
    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" }, // $NON-NLS-1$
    ComparisonMode.EXACT_COMMAND_STRING);
    assertTrue(plan.getRootNode() instanceof ProjectNode);
}
Also used : BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) ProjectNode(org.teiid.query.processor.relational.ProjectNode) RelationalPlan(org.teiid.query.processor.relational.RelationalPlan) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Aggregations

ProjectNode (org.teiid.query.processor.relational.ProjectNode)7 RelationalPlan (org.teiid.query.processor.relational.RelationalPlan)7 Test (org.junit.Test)5 DefaultCapabilitiesFinder (org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)4 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)3 ArrayList (java.util.ArrayList)2 CacheID (org.teiid.dqp.internal.process.SessionAwareCache.CacheID)2 CapabilitiesFinder (org.teiid.query.optimizer.capabilities.CapabilitiesFinder)2 FakeCapabilitiesFinder (org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder)2 Command (org.teiid.query.sql.lang.Command)2 Reference (org.teiid.query.sql.symbol.Reference)2 List (java.util.List)1 QueryParserException (org.teiid.api.exception.query.QueryParserException)1 AnalysisRecord (org.teiid.query.analysis.AnalysisRecord)1 SourceCapabilities (org.teiid.query.optimizer.capabilities.SourceCapabilities)1 BatchedUpdatePlan (org.teiid.query.processor.BatchedUpdatePlan)1 HardcodedDataManager (org.teiid.query.processor.HardcodedDataManager)1 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)1 BatchedUpdateNode (org.teiid.query.processor.relational.BatchedUpdateNode)1 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)1