use of org.teiid.query.processor.BatchIterator in project teiid by teiid.
the class SortNode method sortPhase.
private void sortPhase() throws BlockedException, TeiidComponentException, TeiidProcessingException {
if (this.sortUtility == null) {
TupleSource ts = null;
TupleBuffer working = null;
if (!getChildren()[0].hasBuffer()) {
ts = new BatchIterator(getChildren()[0]);
} else {
working = getChildren()[0].getBuffer(-1);
}
this.sortUtility = new SortUtility(ts, items, this.mode, getBufferManager(), getConnectionID(), getChildren()[0].getElements());
if (ts == null) {
this.sortUtility.setWorkingBuffer(working);
}
}
this.output = this.sortUtility.sort(rowLimit);
if (this.outputTs == null) {
this.outputTs = this.output.createIndexedTupleSource();
}
this.phase = OUTPUT;
}
use of org.teiid.query.processor.BatchIterator in project teiid by teiid.
the class ProcedurePlan method executePlan.
/**
* @param command
* @param rsName
* @param procAssignments
* @param mode
* @param usesLocalTemp - only matters in HOLD mode
* @throws TeiidComponentException
* @throws TeiidProcessingException
*/
public void executePlan(ProcessorPlan command, String rsName, Map<ElementSymbol, ElementSymbol> procAssignments, CreateCursorResultSetInstruction.Mode mode, boolean usesLocalTemp) throws TeiidComponentException, TeiidProcessingException {
CursorState state = (CursorState) this.cursorStates.getValue(rsName);
if (state == null || rsName == null) {
if (this.currentState != null && this.currentState.processor.getProcessorPlan() != command) {
// sanity check for non-deterministic paths
removeState(this.currentState);
this.currentState = null;
}
if (this.currentState == null) {
// this may not be the first time the plan is being run
command.reset();
CommandContext subContext = getContext().clone();
subContext.setVariableContext(this.currentVarContext);
state = new CursorState();
state.usesLocalTemp = usesLocalTemp;
state.processor = new QueryProcessor(command, subContext, this.bufferMgr, this);
state.ts = new BatchIterator(state.processor);
if (mode == Mode.HOLD && procAssignments != null && state.processor.getOutputElements().size() - procAssignments.size() > 0) {
state.resultsBuffer = bufferMgr.createTupleBuffer(state.processor.getOutputElements().subList(0, state.processor.getOutputElements().size() - procAssignments.size()), getContext().getConnectionId(), TupleSourceType.PROCESSOR);
} else if ((this.blockContext != null || this.programs.peek().isTrappingExceptions()) && (mode == Mode.HOLD || rsName != null)) {
state.resultsBuffer = bufferMgr.createTupleBuffer(state.processor.getOutputElements(), getContext().getConnectionId(), TupleSourceType.PROCESSOR);
}
this.currentState = state;
}
// force execution to the first batch to ensure that the plan is executed in the context of the procedure
this.currentState.ts.hasNext();
if (procAssignments != null) {
// proc assignments force us to scroll through the entire results and save as we go
while (this.currentState.ts.hasNext()) {
if (this.currentState.currentRow != null && this.currentState.resultsBuffer != null) {
this.currentState.resultsBuffer.addTuple(this.currentState.currentRow.subList(0, this.currentState.resultsBuffer.getSchema().size()));
this.currentState.currentRow = null;
}
this.currentState.currentRow = this.currentState.ts.nextTuple();
}
// process assignments
Assertion.assertTrue(this.currentState.currentRow != null);
for (Map.Entry<ElementSymbol, ElementSymbol> entry : procAssignments.entrySet()) {
if (entry.getValue() == null || !metadata.elementSupports(entry.getValue().getMetadataID(), SupportConstants.Element.UPDATE)) {
continue;
}
int index = this.currentState.processor.getOutputElements().indexOf(entry.getKey());
getCurrentVariableContext().setValue(entry.getValue(), DataTypeManager.transformValue(this.currentState.currentRow.get(index), entry.getValue().getType()));
}
} else if (this.currentState.resultsBuffer != null) {
// result should be saved, typically to respect txn semantics
while (this.currentState.ts.hasNext()) {
List<?> tuple = this.currentState.ts.nextTuple();
this.currentState.resultsBuffer.addTuple(tuple);
}
getCurrentVariableContext().setValue(ProcedurePlan.ROWCOUNT, 0);
} else if (mode == Mode.UPDATE) {
List<?> t = this.currentState.ts.nextTuple();
if (this.currentState.ts.hasNext()) {
// $NON-NLS-1$
throw new AssertionError("Invalid update count result - more than 1 row returned");
}
removeState(this.currentState);
this.currentState = null;
int rowCount = 0;
if (t != null) {
rowCount = (Integer) t.get(0);
}
getCurrentVariableContext().setValue(ProcedurePlan.ROWCOUNT, rowCount);
return;
}
if (rsName == null && mode == Mode.NOHOLD) {
// TODO: could set the rowcount in this case
while (this.currentState.ts.hasNext()) {
this.currentState.ts.nextTuple();
}
this.currentState = null;
getCurrentVariableContext().setValue(ProcedurePlan.ROWCOUNT, 0);
return;
}
if (this.currentState.resultsBuffer != null) {
// close the results buffer and use a buffer backed tuplesource
this.currentState.resultsBuffer.close();
this.currentState.ts = this.currentState.resultsBuffer.createIndexedTupleSource(true);
}
CursorState old = (CursorState) this.cursorStates.setValue(rsName, this.currentState);
if (old != null) {
removeState(old);
}
this.currentState = null;
}
}
use of org.teiid.query.processor.BatchIterator in project teiid by teiid.
the class SourceState method getIterator.
IndexedTupleSource getIterator() throws TeiidComponentException, TeiidProcessingException {
if (this.iterator == null) {
if (this.buffer != null) {
iterator = buffer.createIndexedTupleSource();
} else {
// return a TupleBatch tuplesource iterator
BatchIterator bi = new BatchIterator(this.source);
if (this.collector != null) {
bi.setBuffer(this.collector.getTupleBuffer(), implicitBuffer == ImplicitBuffer.ON_MARK);
if (implicitBuffer == ImplicitBuffer.NONE) {
bi.getBuffer().setForwardOnly(true);
}
this.collector = null;
} else if (implicitBuffer != ImplicitBuffer.NONE) {
bi.setBuffer(createSourceTupleBuffer(), implicitBuffer == ImplicitBuffer.ON_MARK);
}
this.iterator = bi;
}
}
return this.iterator;
}
use of org.teiid.query.processor.BatchIterator in project teiid by teiid.
the class SourceState method getTupleBuffer.
public TupleBuffer getTupleBuffer() throws TeiidComponentException, TeiidProcessingException {
if (this.buffer == null) {
if (this.iterator instanceof BatchIterator) {
// $NON-NLS-1$
throw new AssertionError("cannot buffer the source");
}
if (source.hasBuffer()) {
this.buffer = source.getBuffer(-1);
Assertion.assertTrue(this.buffer.isFinal());
return this.buffer;
}
if (collector == null) {
collector = new BatchCollector(source, source.getBufferManager(), source.getContext(), false);
}
this.buffer = collector.collectTuples();
}
return this.buffer;
}
use of org.teiid.query.processor.BatchIterator in project teiid by teiid.
the class SourceState method sort.
public void sort(SortOption sortOption) throws TeiidComponentException, TeiidProcessingException {
if (sortOption == SortOption.ALREADY_SORTED) {
return;
}
if (this.sortUtility == null) {
TupleSource ts = null;
if (source.hasBuffer()) {
this.buffer = getTupleBuffer();
} else if (this.buffer == null && this.collector != null) {
if (sortOption == SortOption.NOT_SORTED) {
// pass the buffer and the source
this.buffer = this.collector.getTupleBuffer();
ts = new BatchCollector.BatchProducerTupleSource(this.source, this.buffer.getRowCount() + 1);
} else {
// fully read
this.buffer = this.collector.collectTuples();
}
}
if (this.buffer != null) {
this.buffer.setForwardOnly(true);
} else {
ts = new BatchIterator(this.source);
}
this.sortUtility = new SortUtility(ts, expressions, Collections.nCopies(expressions.size(), OrderBy.ASC), sortOption == SortOption.SORT_DISTINCT ? Mode.DUP_REMOVE_SORT : Mode.SORT, this.source.getBufferManager(), this.source.getConnectionID(), source.getElements());
this.markExpressionsDistinct(sortOption == SortOption.SORT_DISTINCT && expressions.size() == this.getOuterVals().size());
if (this.buffer != null) {
this.sortUtility.setWorkingBuffer(this.buffer);
}
}
TupleBuffer sorted = null;
if (sortOption == SortOption.NOT_SORTED) {
if (this.buffers != null || sortUtility.isDoneReading()) {
return;
}
this.buffers = sortUtility.onePassSort(limited);
if (this.buffers.size() != 1 || !sortUtility.isDoneReading()) {
nextBuffer();
return;
}
sorted = this.buffers.get(0);
this.buffers = null;
} else {
sorted = sortUtility.sort();
}
// only remove the buffer if this is the first time through
if (this.buffer != null && this.buffer != sorted) {
this.buffer.remove();
}
this.buffer = sorted;
this.markExpressionsDistinct(sortUtility.isDistinct());
}
Aggregations