Search in sources :

Example 1 with BatchedUpdatePlan

use of org.teiid.query.processor.BatchedUpdatePlan in project teiid by teiid.

the class TestBatchedUpdatePlanner method helpTestPlanner.

private void helpTestPlanner(String[] sql, boolean[] expectedBatching) throws QueryMetadataException, TeiidComponentException, TeiidProcessingException {
    BatchedUpdatePlan plan = helpPlan(sql, RealMetadataFactory.example1Cached());
    List plans = plan.getUpdatePlans();
    // $NON-NLS-1$
    assertEquals("Number of child plans did not match expected", expectedBatching.length, plans.size());
    for (int i = 0; i < expectedBatching.length; i++) {
        helpAssertIsBatchedPlan((RelationalPlan) plans.get(i), expectedBatching[i]);
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) BatchedUpdatePlan(org.teiid.query.processor.BatchedUpdatePlan)

Example 2 with BatchedUpdatePlan

use of org.teiid.query.processor.BatchedUpdatePlan in project teiid by teiid.

the class TestBatchedUpdatePlanner method helpPlanCommand.

private BatchedUpdatePlan helpPlanCommand(Command command, QueryMetadataInterface md, CapabilitiesFinder capFinder, boolean shouldSucceed) throws QueryPlannerException, QueryMetadataException, TeiidComponentException {
    // plan
    ProcessorPlan plan = null;
    AnalysisRecord analysisRecord = new AnalysisRecord(false, DEBUG);
    if (shouldSucceed) {
        try {
            // do planning
            plan = QueryOptimizer.optimizePlan(command, md, null, capFinder, analysisRecord, null);
        } finally {
            if (DEBUG) {
                System.out.println(analysisRecord.getDebugLog());
            }
        }
        return (BatchedUpdatePlan) plan;
    }
    Exception exception = null;
    try {
        // do planning
        QueryOptimizer.optimizePlan(command, md, null, capFinder, analysisRecord, null);
    } catch (QueryPlannerException e) {
        exception = e;
    } catch (TeiidComponentException e) {
        exception = e;
    } finally {
        if (DEBUG) {
            System.out.println(analysisRecord.getDebugLog());
        }
    }
    // $NON-NLS-1$
    assertNotNull("Expected exception but did not get one.", exception);
    return null;
}
Also used : AnalysisRecord(org.teiid.query.analysis.AnalysisRecord) TeiidComponentException(org.teiid.core.TeiidComponentException) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) QueryPlannerException(org.teiid.api.exception.query.QueryPlannerException) BatchedUpdatePlan(org.teiid.query.processor.BatchedUpdatePlan)

Example 3 with BatchedUpdatePlan

use of org.teiid.query.processor.BatchedUpdatePlan 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 4 with BatchedUpdatePlan

use of org.teiid.query.processor.BatchedUpdatePlan in project teiid by teiid.

the class TestBatchedUpdatePlanner method helpTestPlanner.

private void helpTestPlanner(String[] sql, boolean[] expectedBatching, CapabilitiesFinder capFinder) throws QueryMetadataException, TeiidComponentException, TeiidProcessingException {
    BatchedUpdatePlan plan = helpPlan(sql, RealMetadataFactory.example1Cached(), capFinder, true);
    List plans = plan.getUpdatePlans();
    // $NON-NLS-1$
    assertEquals("Number of child plans did not match expected", expectedBatching.length, plans.size());
    for (int i = 0; i < expectedBatching.length; i++) {
        helpAssertIsBatchedPlan((RelationalPlan) plans.get(i), expectedBatching[i]);
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) BatchedUpdatePlan(org.teiid.query.processor.BatchedUpdatePlan)

Aggregations

BatchedUpdatePlan (org.teiid.query.processor.BatchedUpdatePlan)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)2 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)1 QueryPlannerException (org.teiid.api.exception.query.QueryPlannerException)1 TeiidComponentException (org.teiid.core.TeiidComponentException)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 AnalysisRecord (org.teiid.query.analysis.AnalysisRecord)1 SourceCapabilities (org.teiid.query.optimizer.capabilities.SourceCapabilities)1 BatchedUpdateNode (org.teiid.query.processor.relational.BatchedUpdateNode)1 ProjectNode (org.teiid.query.processor.relational.ProjectNode)1 RelationalPlan (org.teiid.query.processor.relational.RelationalPlan)1 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)1 Command (org.teiid.query.sql.lang.Command)1 VariableContext (org.teiid.query.sql.util.VariableContext)1