Search in sources :

Example 31 with CommandContext

use of org.teiid.query.util.CommandContext in project teiid by teiid.

the class RelationalNode method getBuffer.

/**
 * return the final tuple buffer or null if not available
 * @return
 * @throws TeiidProcessingException
 * @throws TeiidComponentException
 * @throws BlockedException
 */
public final TupleBuffer getBuffer(int maxRows) throws BlockedException, TeiidComponentException, TeiidProcessingException {
    CommandContext context = this.getContext();
    if (context != null && context.isCancelled()) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30160, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30160, getContext().getRequestId()));
    }
    boolean recordStats = context != null && context.getCollectNodeStatistics() && !isLastBatch();
    try {
        // start timer for this batch
        if (recordStats) {
            this.getProcessingState().nodeStatistics.startBatchTimer();
        }
        TupleBuffer buffer = getBufferDirect(maxRows);
        terminateBatches();
        if (recordStats) {
            // stop timer for this batch (normal)
            this.getProcessingState().nodeStatistics.stopBatchTimer();
            this.getProcessingState().nodeStatistics.collectCumulativeNodeStats(buffer.getRowCount(), RelationalNodeStatistics.BATCHCOMPLETE_STOP);
            this.getProcessingState().nodeStatistics.collectNodeStats(this.getChildren());
            if (LogManager.isMessageToBeRecorded(org.teiid.logging.LogConstants.CTX_DQP, MessageLevel.TRACE) && !buffer.isForwardOnly()) {
                for (long i = 1; i <= buffer.getRowCount(); i += buffer.getBatchSize()) {
                    TupleBatch tb = buffer.getBatch(i);
                    recordBatch(tb);
                }
            }
            recordStats = false;
        }
        return buffer;
    } catch (BlockedException e) {
        if (recordStats) {
            // stop timer for this batch (BlockedException)
            this.getProcessingState().nodeStatistics.stopBatchTimer();
            this.getProcessingState().nodeStatistics.collectCumulativeNodeStats(null, RelationalNodeStatistics.BLOCKEDEXCEPTION_STOP);
            recordStats = false;
        }
        throw e;
    } finally {
        if (recordStats) {
            this.getProcessingState().nodeStatistics.stopBatchTimer();
        }
    }
}
Also used : CommandContext(org.teiid.query.util.CommandContext) TupleBuffer(org.teiid.common.buffer.TupleBuffer) BlockedException(org.teiid.common.buffer.BlockedException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 32 with CommandContext

use of org.teiid.query.util.CommandContext in project teiid by teiid.

the class RelationalNode method nextBatch.

/**
 * Wrapper for nextBatchDirect that does performance timing - callers
 * should always call this rather than nextBatchDirect().
 * @return
 * @throws BlockedException
 * @throws TeiidComponentException
 * @since 4.2
 */
public final TupleBatch nextBatch() throws BlockedException, TeiidComponentException, TeiidProcessingException {
    CommandContext context = this.getContext();
    if (context != null && context.isCancelled()) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30160, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30160, getContext().getRequestId()));
    }
    boolean recordStats = context != null && context.getCollectNodeStatistics();
    try {
        while (true) {
            // start timer for this batch
            if (recordStats) {
                this.getProcessingState().nodeStatistics.startBatchTimer();
            }
            TupleBatch batch = nextBatchDirect();
            if (recordStats) {
                // stop timer for this batch (normal)
                this.getProcessingState().nodeStatistics.stopBatchTimer();
                this.getProcessingState().nodeStatistics.collectCumulativeNodeStats((long) batch.getRowCount(), RelationalNodeStatistics.BATCHCOMPLETE_STOP);
                if (batch.getTerminationFlag()) {
                    this.getProcessingState().nodeStatistics.collectNodeStats(this.getChildren());
                // this.nodeStatistics.dumpProperties(this.getClassName());
                }
                this.recordBatch(batch);
                recordStats = false;
            }
            // this processing style however against the spirit of batch processing (but was already utilized by Sort and Grouping nodes)
            if (batch.getRowCount() != 0 || batch.getTerminationFlag()) {
                if (batch.getTerminationFlag()) {
                    close();
                }
                return batch;
            }
        }
    } catch (BlockedException e) {
        if (recordStats) {
            // stop timer for this batch (BlockedException)
            this.getProcessingState().nodeStatistics.stopBatchTimer();
            this.getProcessingState().nodeStatistics.collectCumulativeNodeStats(null, RelationalNodeStatistics.BLOCKEDEXCEPTION_STOP);
            recordStats = false;
        }
        throw e;
    } finally {
        if (recordStats) {
            this.getProcessingState().nodeStatistics.stopBatchTimer();
        }
    }
}
Also used : CommandContext(org.teiid.query.util.CommandContext) BlockedException(org.teiid.common.buffer.BlockedException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 33 with CommandContext

use of org.teiid.query.util.CommandContext in project teiid by teiid.

the class ForEachRowPlan method nextBatch.

@Override
public TupleBatch nextBatch() throws BlockedException, TeiidComponentException, TeiidProcessingException {
    if (planContext != null) {
        this.getContext().getTransactionServer().resume(planContext);
    }
    try {
        while (true) {
            if (currentTuple == null) {
                if (nextTuple != null) {
                    currentTuple = nextTuple;
                } else if (!nextNull) {
                    currentTuple = tupleSource.nextTuple();
                }
                if (currentTuple == null) {
                    if (this.planContext != null) {
                        TransactionService ts = this.getContext().getTransactionServer();
                        ts.commit(this.planContext);
                        this.planContext = null;
                    }
                    TupleBatch result = new TupleBatch(1, new List[] { Arrays.asList((int) Math.min(Integer.MAX_VALUE, updateCount)) });
                    result.setTerminationFlag(true);
                    return result;
                }
            }
            if (first) {
                TransactionContext tc = this.getContext().getTransactionContext();
                if (this.planContext == null && tc != null && tc.getTransactionType() == Scope.NONE) {
                    Boolean txnRequired = rowProcedure.requiresTransaction(false);
                    boolean start = false;
                    if (txnRequired == null) {
                        nextTuple = tupleSource.nextTuple();
                        if (nextTuple != null) {
                            start = true;
                        } else {
                            nextNull = true;
                        }
                    } else if (Boolean.TRUE.equals(txnRequired)) {
                        start = true;
                    }
                    if (start) {
                        this.getContext().getTransactionServer().begin(tc);
                        this.planContext = tc;
                    }
                }
                first = false;
            }
            if (this.rowProcessor == null) {
                rowProcedure.reset();
                CommandContext context = getContext().clone();
                this.rowProcessor = new QueryProcessor(rowProcedure, context, this.bufferMgr, this.dataMgr);
                Evaluator eval = new Evaluator(lookupMap, dataMgr, context);
                for (Map.Entry<ElementSymbol, Expression> entry : this.params.entrySet()) {
                    Integer index = this.lookupMap.get(entry.getValue());
                    if (index != null) {
                        rowProcedure.getCurrentVariableContext().setValue(entry.getKey(), this.currentTuple.get(index));
                    } else {
                        rowProcedure.getCurrentVariableContext().setValue(entry.getKey(), eval.evaluate(entry.getValue(), this.currentTuple));
                    }
                }
            }
            // save the variable context to get the key information
            VariableContext vc = rowProcedure.getCurrentVariableContext();
            // just getting the next batch is enough
            this.rowProcessor.nextBatch();
            if (insert) {
                assignGeneratedKey(vc);
            }
            this.rowProcessor.closeProcessing();
            this.rowProcessor = null;
            this.currentTuple = null;
            this.updateCount++;
        }
    } finally {
        if (planContext != null) {
            this.getContext().getTransactionServer().suspend(planContext);
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TransactionService(org.teiid.dqp.service.TransactionService) CommandContext(org.teiid.query.util.CommandContext) Evaluator(org.teiid.query.eval.Evaluator) VariableContext(org.teiid.query.sql.util.VariableContext) QueryProcessor(org.teiid.query.processor.QueryProcessor) Expression(org.teiid.query.sql.symbol.Expression) TransactionContext(org.teiid.dqp.service.TransactionContext) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 34 with CommandContext

use of org.teiid.query.util.CommandContext in project teiid by teiid.

the class ProcedurePlan method reset.

public void reset() {
    super.reset();
    if (evaluator != null) {
        evaluator.reset();
    }
    evaluatedParams = false;
    if (parentContext != null) {
        super.getContext().setVariableContext(parentContext);
    }
    createVariableContext();
    CommandContext cc = super.getContext();
    if (cc != null) {
        // create fresh local state
        setContext(cc.clone());
    }
    done = false;
    currentState = null;
    finalTupleSource = null;
    beginBatch = 1;
    batchRows = null;
    lastBatch = false;
    // reset program stack
    programs.clear();
    // $NON-NLS-1$
    LogManager.logTrace(org.teiid.logging.LogConstants.CTX_DQP, "ProcedurePlan reset");
}
Also used : CommandContext(org.teiid.query.util.CommandContext)

Example 35 with CommandContext

use of org.teiid.query.util.CommandContext in project teiid by teiid.

the class BatchedUpdatePlan method openPlan.

private void openPlan() throws TeiidComponentException, TeiidProcessingException {
    // reset prior to updating the context
    updatePlans[planIndex].reset();
    if (this.contexts != null && !this.contexts.isEmpty()) {
        CommandContext context = updatePlans[planIndex].getContext();
        VariableContext vc = context.getVariableContext();
        // this is just a safe guard against the plan not correctly resetting the context
        while (vc.getParentContext() != null) {
            vc = vc.getParentContext();
        }
        vc.clear();
        VariableContext currentValues = this.contexts.get(planIndex);
        vc.putAll(currentValues);
    }
    TransactionContext tc = this.getContext().getTransactionContext();
    if (startTxn[planIndex] && tc != null && tc.getTransactionType() == Scope.NONE) {
        this.getContext().getTransactionServer().begin(tc);
        this.planContexts[planIndex] = tc;
    }
    updatePlans[planIndex].open();
    planOpened[planIndex] = true;
}
Also used : CommandContext(org.teiid.query.util.CommandContext) TransactionContext(org.teiid.dqp.service.TransactionContext) VariableContext(org.teiid.query.sql.util.VariableContext)

Aggregations

CommandContext (org.teiid.query.util.CommandContext)257 Test (org.junit.Test)179 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)104 DefaultCapabilitiesFinder (org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)95 List (java.util.List)90 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)64 ArrayList (java.util.ArrayList)44 Command (org.teiid.query.sql.lang.Command)38 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)37 FakeCapabilitiesFinder (org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder)33 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)26 Options (org.teiid.query.util.Options)20 BufferManager (org.teiid.common.buffer.BufferManager)19 HardcodedDataManager (org.teiid.query.processor.HardcodedDataManager)19 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)18 TeiidProcessingException (org.teiid.core.TeiidProcessingException)14 BufferManagerImpl (org.teiid.common.buffer.impl.BufferManagerImpl)13 BlockedException (org.teiid.common.buffer.BlockedException)11 TeiidComponentException (org.teiid.core.TeiidComponentException)11 Table (org.teiid.metadata.Table)11