Search in sources :

Example 6 with TupleBatch

use of org.teiid.common.buffer.TupleBatch 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 7 with TupleBatch

use of org.teiid.common.buffer.TupleBatch 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 8 with TupleBatch

use of org.teiid.common.buffer.TupleBatch in project teiid by teiid.

the class RelationalNode method pullBatch.

protected TupleBatch pullBatch() {
    TupleBatch batch = null;
    if (this.getProcessingState().batchRows != null) {
        batch = new TupleBatch(this.getProcessingState().beginBatch, this.getProcessingState().batchRows);
        getProcessingState().beginBatch += this.getProcessingState().batchRows.size();
    } else {
        batch = new TupleBatch(this.getProcessingState().beginBatch, Collections.EMPTY_LIST);
    }
    batch.setTerminationFlag(this.getProcessingState().lastBatch);
    // Reset batch state
    this.getProcessingState().batchRows = null;
    this.getProcessingState().lastBatch = false;
    // Return batch
    return batch;
}
Also used : TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 9 with TupleBatch

use of org.teiid.common.buffer.TupleBatch 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 10 with TupleBatch

use of org.teiid.common.buffer.TupleBatch in project teiid by teiid.

the class ProcedurePlan method pullBatch.

protected TupleBatch pullBatch() {
    TupleBatch batch = null;
    if (this.batchRows != null) {
        batch = new TupleBatch(this.beginBatch, this.batchRows);
        beginBatch += this.batchRows.size();
    } else {
        batch = new TupleBatch(this.beginBatch, Collections.EMPTY_LIST);
    }
    batch.setTerminationFlag(this.lastBatch);
    // Reset batch state
    this.batchRows = null;
    this.lastBatch = false;
    // Return batch
    return batch;
}
Also used : TupleBatch(org.teiid.common.buffer.TupleBatch)

Aggregations

TupleBatch (org.teiid.common.buffer.TupleBatch)61 Test (org.junit.Test)26 List (java.util.List)23 ArrayList (java.util.ArrayList)22 BlockedException (org.teiid.common.buffer.BlockedException)17 CommandContext (org.teiid.query.util.CommandContext)10 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)9 TeiidProcessingException (org.teiid.core.TeiidProcessingException)8 TeiidComponentException (org.teiid.core.TeiidComponentException)6 BufferManager (org.teiid.common.buffer.BufferManager)4 TupleBuffer (org.teiid.common.buffer.TupleBuffer)4 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)3 TransactionContext (org.teiid.dqp.service.TransactionContext)3 TransactionService (org.teiid.dqp.service.TransactionService)3 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)2 BufferManagerImpl (org.teiid.common.buffer.impl.BufferManagerImpl)2 TeiidException (org.teiid.core.TeiidException)2 QueryProcessor (org.teiid.query.processor.QueryProcessor)2 FakeRelationalNode (org.teiid.query.processor.relational.FakeRelationalNode)2 CacheHint (org.teiid.query.sql.lang.CacheHint)2