Search in sources :

Example 11 with TupleBatch

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

the class ProcedurePlan method nextBatchDirect.

/**
 * @see ProcessorPlan#nextBatch()
 */
public TupleBatch nextBatchDirect() throws TeiidComponentException, TeiidProcessingException, BlockedException {
    // Already returned results?
    if (done) {
        // Already returned all results
        TupleBatch emptyTerminationBatch = new TupleBatch(beginBatch, new List[0]);
        emptyTerminationBatch.setTerminationFlag(true);
        return emptyTerminationBatch;
    }
    // First attempt to process
    if (this.finalTupleSource == null) {
        // Still need to process - this should either
        // throw a BlockedException or return a finalTupleSource
        this.finalTupleSource = processProcedure();
    }
    // Next, attempt to return batches if processing completed
    while (!isBatchFull()) {
        // May throw BlockedException and exit here
        List<?> tuple = this.finalTupleSource.nextTuple();
        if (tuple == null) {
            if (outParams != null) {
                VariableContext vc = getCurrentVariableContext();
                List<Object> paramTuple = Arrays.asList(new Object[this.getOutputElements().size()]);
                int i = this.getOutputElements().size() - this.outParams.size();
                for (ElementSymbol param : outParams) {
                    Object value = vc.getValue(param);
                    checkNotNull(param, value);
                    paramTuple.set(i++, value);
                }
                addBatchRow(paramTuple, true);
            }
            terminateBatches();
            done = true;
            break;
        }
        addBatchRow(tuple, false);
    }
    return pullBatch();
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) VariableContext(org.teiid.query.sql.util.VariableContext) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 12 with TupleBatch

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

the class BatchCollector method collectTuples.

public TupleBuffer collectTuples(boolean singleBatch) throws TeiidComponentException, TeiidProcessingException {
    TupleBatch batch = null;
    while (!done) {
        if (this.hasFinalBuffer) {
            if (this.buffer == null) {
                TupleBuffer finalBuffer = this.sourceNode.getBuffer(rowLimit);
                Assertion.isNotNull(finalBuffer);
                this.buffer = finalBuffer;
            }
            if (this.buffer.isFinal()) {
                this.buffer.setForwardOnly(forwardOnly);
                done = true;
                break;
            }
        }
        batch = sourceNode.nextBatch();
        if (rowLimit > 0 && rowLimit <= batch.getEndRow()) {
            if (!done) {
                this.sourceNode.close();
            }
            List<?> lastTuple = null;
            if (saveLastRow) {
                if (batch.getTerminationFlag()) {
                    lastTuple = batch.getTuples().get(batch.getTuples().size() - 1);
                } else if (rowLimit < batch.getBeginRow()) {
                    // skip until end
                    continue;
                }
            }
            boolean modified = false;
            if (rowLimit < batch.getEndRow()) {
                // we know row limit must be smaller than max int, so an int cast is safe here
                int firstRow = (int) Math.min(rowLimit + 1, batch.getBeginRow());
                List<List<?>> tuples = batch.getTuples().subList(0, rowLimit - firstRow + 1);
                batch = new TupleBatch(firstRow, tuples);
                modified = true;
            }
            if (lastTuple != null) {
                if (!modified) {
                    batch = new TupleBatch(batch.getBeginRow(), batch.getTuples());
                }
                batch.getTuples().add(lastTuple);
            }
            batch.setTerminationFlag(true);
        }
        flushBatch(batch);
        // Check for termination condition
        if (batch.getTerminationFlag()) {
            done = true;
            if (!this.sourceNode.hasBuffer()) {
                buffer.close();
            }
            break;
        }
        if (singleBatch) {
            return null;
        }
    }
    return buffer;
}
Also used : TupleBuffer(org.teiid.common.buffer.TupleBuffer) List(java.util.List) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 13 with TupleBatch

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

the class BatchIterator method readAhead.

public void readAhead(long limit) throws TeiidComponentException, TeiidProcessingException {
    if (buffer == null || done) {
        return;
    }
    if (this.buffer.getManagedRowCount() >= limit) {
        return;
    }
    if (this.batch != null && this.buffer.getRowCount() < this.batch.getEndRow() && !this.buffer.isForwardOnly()) {
        // haven't saved already
        this.buffer.addTupleBatch(this.batch, true);
    }
    TupleBatch tb = source.nextBatch();
    done = tb.getTerminationFlag();
    this.buffer.addTupleBatch(tb, true);
    if (done) {
        this.buffer.close();
    }
}
Also used : TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 14 with TupleBatch

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

the class DdlPlan method nextBatch.

@Override
public TupleBatch nextBatch() throws BlockedException, TeiidComponentException, TeiidProcessingException {
    TupleBatch tupleBatch = new TupleBatch(1, new List[] { Arrays.asList(0) });
    tupleBatch.setTerminationFlag(true);
    return tupleBatch;
}
Also used : TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 15 with TupleBatch

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

the class QueryProcessor method nextBatchDirect.

private TupleBatch nextBatchDirect() throws BlockedException, TeiidProcessingException, TeiidComponentException {
    boolean done = false;
    TupleBatch result = null;
    try {
        init();
        long currentTime = System.currentTimeMillis();
        Assertion.assertTrue(!processorClosed);
        while (currentTime < context.getTimeSliceEnd() || context.isNonBlocking()) {
            if (requestCanceled) {
                throw new TeiidProcessingException(QueryPlugin.Event.TEIID30160, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30160, this.context.getRequestId()));
            }
            if (currentTime > context.getTimeoutEnd()) {
                throw new TeiidProcessingException(QueryPlugin.Event.TEIID30161, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30161));
            }
            result = processPlan.nextBatch();
            if (continuous) {
                result.setRowOffset(rowOffset);
                if (result.getTerminationFlag()) {
                    result.setTermination(TupleBatch.ITERATION_TERMINATED);
                    List<Object> terminationTuple = Arrays.asList(new Object[this.getOutputElements().size()]);
                    result.getTuples().add(terminationTuple);
                    this.context.getTupleSourceCache().close();
                    this.processPlan.close();
                    this.processPlan.reset();
                    this.context.incrementReuseCount();
                    this.open = false;
                }
                rowOffset = result.getEndRow() + 1;
            }
            if (result.getTermination() != TupleBatch.NOT_TERMINATED) {
                if (result.getTerminationFlag()) {
                    done = true;
                }
                break;
            }
            if (result.getRowCount() > 0) {
                break;
            }
        }
    } catch (BlockedException e) {
        throw e;
    } catch (TeiidException e) {
        closeProcessing();
        if (e instanceof TeiidProcessingException) {
            throw (TeiidProcessingException) e;
        }
        if (e instanceof TeiidComponentException) {
            throw (TeiidComponentException) e;
        }
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30162, e);
    }
    if (done) {
        closeProcessing();
    }
    if (result == null) {
        RequestWorkItem workItem = this.getContext().getWorkItem();
        if (workItem != null) {
            // if we have a workitem (non-test scenario) then before
            // throwing exprired time slice we need to indicate there's more work
            workItem.moreWork();
        }
        throw EXPIRED_TIME_SLICE;
    }
    return result;
}
Also used : RequestWorkItem(org.teiid.dqp.internal.process.RequestWorkItem) TeiidComponentException(org.teiid.core.TeiidComponentException) BlockedException(org.teiid.common.buffer.BlockedException) TupleBatch(org.teiid.common.buffer.TupleBatch) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TeiidException(org.teiid.core.TeiidException)

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