Search in sources :

Example 1 with TupleBatch

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

the class TestTupleBatch method exampleBatch.

public TupleBatch exampleBatch(int rowBegin, int numRows, int numColumns) {
    List rows = new ArrayList();
    for (int i = 0; i < numRows; i++) {
        List row = new ArrayList();
        for (int j = 0; j < numColumns; j++) {
            // $NON-NLS-1$ //$NON-NLS-2$
            row.add("data-" + (rowBegin + i) + "-" + j);
        }
        rows.add(row);
    }
    return new TupleBatch(rowBegin, rows);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 2 with TupleBatch

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

the class SubqueryAwareEvaluator method evaluateProcedure.

/**
 * Implements procedure function handling.
 * TODO: cache results
 */
@Override
protected Object evaluateProcedure(Function function, List<?> tuple, Object[] values) throws TeiidComponentException, TeiidProcessingException {
    QueryProcessor qp = null;
    List<?> key = Arrays.asList(function, Arrays.asList(values));
    if (procedureState != null) {
        qp = this.procedureState.get(key);
    }
    if (qp == null) {
        String args = Collections.nCopies(values.length, '?').toString().substring(1);
        args = args.substring(0, args.length() - 1);
        String fullName = function.getFunctionDescriptor().getFullName();
        // $NON-NLS-1$
        String call = String.format("call %1$s(%2$s)", fullName, args);
        qp = this.context.getQueryProcessorFactory().createQueryProcessor(call, fullName, this.context, values);
        if (this.procedureState == null) {
            this.procedureState = new HashMap<List<?>, QueryProcessor>();
        }
        this.procedureState.put(key, qp);
    }
    // just in case validate the rows being returned
    TupleBatch tb = qp.nextBatch();
    TupleBatch next = tb;
    while (!next.getTerminationFlag()) {
        if (next.getEndRow() >= 2) {
            break;
        }
        next = qp.nextBatch();
    }
    if (next.getEndRow() >= 2) {
        throw new ExpressionEvaluationException(QueryPlugin.Event.TEIID30345, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30345, function));
    }
    Object result = null;
    if (next.getRowCount() > 0) {
        result = next.getTuples().get(0).get(0);
    }
    this.procedureState.remove(key);
    qp.closeProcessing();
    return result;
}
Also used : ExpressionEvaluationException(org.teiid.api.exception.query.ExpressionEvaluationException) List(java.util.List) ArrayList(java.util.ArrayList) QueryProcessor(org.teiid.query.processor.QueryProcessor) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 3 with TupleBatch

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

the class TextTableNode method nextBatchDirect.

@Override
protected synchronized TupleBatch nextBatchDirect() throws BlockedException, TeiidComponentException, TeiidProcessingException {
    if (reader == null) {
        initReader();
    }
    if (reader == null) {
        terminateBatches();
        return pullBatch();
    }
    if (isLastBatch()) {
        return pullBatch();
    }
    if (isBatchFull()) {
        TupleBatch result = pullBatch();
        // read ahead
        processAsynch();
        return result;
    }
    unwrapException(asynchException);
    processAsynch();
    if (this.getContext().getWorkItem() == null) {
        // this is for compatibility with engine tests that are below the level of using the work item
        synchronized (this) {
            while (running) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new TeiidRuntimeException(e);
                }
            }
        }
    }
    // $NON-NLS-1$
    throw BlockedException.block("Blocking on results from file processing.");
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 4 with TupleBatch

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

the class LimitNode method nextBatchDirect.

protected TupleBatch nextBatchDirect() throws BlockedException, TeiidComponentException, TeiidProcessingException {
    // Can throw BlockedException
    TupleBatch batch = null;
    if (limit == 0) {
        this.terminateBatches();
        return pullBatch();
    }
    // If we haven't reached the offset, then skip rows/batches
    if (offsetPhase) {
        while (rowCounter <= offset) {
            // Can throw BlockedException
            batch = getChildren()[0].nextBatch();
            rowCounter += batch.getRowCount();
            if (batch.getTerminationFlag()) {
                break;
            }
        }
        List<List<?>> tuples = null;
        if (rowCounter > offset) {
            List<List<?>> originalTuples = batch.getTuples();
            int rowsToKeep = rowCounter - offset;
            tuples = new ArrayList<List<?>>(originalTuples.subList(batch.getRowCount() - rowsToKeep, batch.getRowCount()));
        } else {
            tuples = Collections.emptyList();
        }
        TupleBatch resultBatch = new TupleBatch(1, tuples);
        resultBatch.setTerminationFlag(batch.getTerminationFlag());
        batch = resultBatch;
        offsetPhase = false;
        rowCounter = 0;
    } else {
        // Can throw BlockedException
        batch = getChildren()[0].nextBatch();
    }
    List<List<?>> tuples = null;
    if (limit < 0 || rowCounter + batch.getRowCount() <= limit) {
        // Passthrough
        tuples = batch.getTuples();
    } else {
        // Partial batch
        List<List<?>> originalTuples = batch.getTuples();
        tuples = new ArrayList<List<?>>(originalTuples.subList(0, limit - rowCounter));
    }
    TupleBatch resultBatch = new TupleBatch(rowCounter + 1, tuples);
    rowCounter += resultBatch.getRowCount();
    if (rowCounter == limit || batch.getTerminationFlag()) {
        resultBatch.setTerminationFlag(true);
    }
    return resultBatch;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 5 with TupleBatch

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

the class ProjectNode method nextBatchDirect.

public TupleBatch nextBatchDirect() throws BlockedException, TeiidComponentException, TeiidProcessingException {
    if (currentBatch == null) {
        // one batch with one row
        if (this.getChildren()[0] == null) {
            currentBatch = new TupleBatch(1, EMPTY_TUPLE);
            currentBatch.setTerminationFlag(true);
        } else {
            currentBatch = this.getChildren()[0].nextBatch();
        }
        // Check for no project needed and pass through
        if (!needsProject) {
            TupleBatch result = currentBatch;
            currentBatch = null;
            return result;
        }
    }
    while (currentRow <= currentBatch.getEndRow() && !isBatchFull()) {
        List<?> tuple = currentBatch.getTuple(currentRow);
        List<Object> projectedTuple = new ArrayList<Object>(selectSymbols.size());
        // Walk through symbols
        for (int i = 0; i < expressions.size(); i++) {
            Expression symbol = expressions.get(i);
            updateTuple(symbol, i, tuple, projectedTuple);
        }
        // Add to batch
        addBatchRow(projectedTuple);
        currentRow++;
    }
    if (currentRow > currentBatch.getEndRow()) {
        if (currentBatch.getTerminationFlag()) {
            terminateBatches();
        }
        currentBatch = null;
    }
    return pullBatch();
}
Also used : Expression(org.teiid.query.sql.symbol.Expression) ArrayList(java.util.ArrayList) LanguageObject(org.teiid.query.sql.LanguageObject) 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