use of org.teiid.query.eval.Evaluator in project teiid by teiid.
the class ProjectIntoNode method nextBatchDirect.
/**
* Get batch from child node
* Walk through each row of child batch
* Bind values to insertCommand
* Execute insertCommand
* Update insertCount
* When no more data is available, output batch with single row containing insertCount
*/
public TupleBatch nextBatchDirect() throws BlockedException, TeiidComponentException, TeiidProcessingException {
while (phase == REQUEST_CREATION) {
/* If we don't have a batch to work, get the next
*/
if (currentBatch == null) {
if (sourceDone) {
phase = RESPONSE_PROCESSING;
break;
}
// can throw BlockedException
currentBatch = getChildren()[0].nextBatch();
sourceDone = currentBatch.getTerminationFlag();
this.batchRow = currentBatch.getBeginRow();
// and for implicit temp tables we need to issue an empty insert
if (currentBatch.getRowCount() == 0 && (!currentBatch.getTerminationFlag() || mode != Mode.ITERATOR)) {
currentBatch = null;
continue;
}
if (this.constraint != null) {
// row based security check
if (eval == null) {
eval = new Evaluator(createLookupMap(this.intoElements), this.getDataManager(), getContext());
}
List<List<?>> tuples = this.currentBatch.getTuples();
for (int i = 0; i < tuples.size(); i++) {
if (!eval.evaluate(constraint, tuples.get(i))) {
throw new QueryProcessingException(QueryPlugin.Event.TEIID31130, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31130, new Insert(intoGroup, this.intoElements, convertValuesToConstants(tuples.get(i), intoElements))));
}
}
}
}
if (mode != Mode.ITERATOR) {
// delay the check in the iterator case to accumulate batches
checkExitConditions();
}
int batchSize = currentBatch.getRowCount();
int requests = 1;
switch(mode) {
case ITERATOR:
if (buffer == null) {
buffer = getBufferManager().createTupleBuffer(intoElements, getConnectionID(), TupleSourceType.PROCESSOR);
}
if (sourceDone) {
// if there is a pending request we can't process the last until it is done
checkExitConditions();
}
for (List<?> tuple : currentBatch.getTuples()) {
buffer.addTuple(tuple);
}
try {
checkExitConditions();
} catch (BlockedException e) {
// move to the next batch
this.batchRow += batchSize;
currentBatch = null;
continue;
}
if (currentBatch.getTerminationFlag() && (buffer.getRowCount() != 0 || intoGroup.isImplicitTempGroupSymbol())) {
registerIteratorRequest();
} else if (buffer.getRowCount() >= buffer.getBatchSize() * 4) {
registerIteratorRequest();
} else {
requests = 0;
}
break;
case BATCH:
// Register batched update command against source
long endRow = currentBatch.getEndRow();
List<Command> rows = new ArrayList<Command>((int) (endRow - batchRow));
for (long rowNum = batchRow; rowNum <= endRow; rowNum++) {
Insert insert = new Insert(intoGroup, intoElements, convertValuesToConstants(currentBatch.getTuple(rowNum), intoElements));
insert.setSourceHint(sourceHint);
insert.setUpsert(upsert);
rows.add(insert);
}
registerRequest(new BatchedUpdateCommand(rows));
break;
case SINGLE:
batchSize = 1;
// Register insert command against source
// Defect 16036 - submit a new INSERT command to the DataManager.
Insert insert = new Insert(intoGroup, intoElements, convertValuesToConstants(currentBatch.getTuple(batchRow), intoElements));
insert.setSourceHint(sourceHint);
insert.setUpsert(upsert);
registerRequest(insert);
}
this.batchRow += batchSize;
if (batchRow > currentBatch.getEndRow()) {
currentBatch = null;
}
this.requestsRegistered += requests;
}
checkExitConditions();
if (this.buffer != null) {
this.buffer.remove();
this.buffer = null;
}
// End this node's work
// report only a max int
int count = (int) Math.min(Integer.MAX_VALUE, insertCount);
addBatchRow(Arrays.asList(count));
terminateBatches();
return pullBatch();
}
use of org.teiid.query.eval.Evaluator 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);
}
}
}
use of org.teiid.query.eval.Evaluator in project teiid by teiid.
the class TestQueryRewriter method helpTestRewriteCriteria.
private Criteria helpTestRewriteCriteria(String original, Criteria expectedCrit, QueryMetadataInterface metadata) {
Criteria origCrit = parseCriteria(original, metadata);
Criteria actual = null;
// rewrite
try {
ArrayList<Boolean> booleanVals = new ArrayList<Boolean>(tuples.size());
for (List<?> tuple : tuples) {
booleanVals.add(new Evaluator(elements, null, null).evaluate(origCrit, tuple));
}
actual = QueryRewriter.rewriteCriteria(origCrit, null, metadata);
// $NON-NLS-1$
assertEquals("Did not rewrite correctly: ", expectedCrit, actual);
for (int i = 0; i < tuples.size(); i++) {
assertEquals(tuples.get(i).toString(), booleanVals.get(i), new Evaluator(elements, null, null).evaluate(actual, tuples.get(i)));
}
} catch (TeiidException e) {
throw new RuntimeException(e);
}
return actual;
}
use of org.teiid.query.eval.Evaluator in project teiid by teiid.
the class TestExpressionEvaluator method helpTestWithValueIterator.
private void helpTestWithValueIterator(ScalarSubquery expr, List<?> values, Object expected) throws BlockedException, TeiidComponentException, ExpressionEvaluationException {
final CollectionValueIterator valueIter = new CollectionValueIterator(values);
CommandContext cc = new CommandContext();
assertEquals(expected, new Evaluator(Collections.emptyMap(), null, cc) {
@Override
protected ValueIterator evaluateSubquery(SubqueryContainer container, List tuple) throws TeiidProcessingException, BlockedException, TeiidComponentException {
return valueIter;
}
}.evaluate(expr, null));
}
use of org.teiid.query.eval.Evaluator in project teiid by teiid.
the class TestExpressionEvaluator method helpTestCommandPayload.
public void helpTestCommandPayload(Serializable payload, String property, String expectedValue) throws Exception {
// $NON-NLS-1$
Function func = new Function("commandpayload", new Expression[] {});
Class[] parameterSignature = null;
if (property == null) {
parameterSignature = new Class[] {};
} else {
parameterSignature = new Class[] { String.class };
}
// $NON-NLS-1$
FunctionDescriptor desc = RealMetadataFactory.SFM.getSystemFunctionLibrary().findFunction("commandpayload", parameterSignature);
func.setFunctionDescriptor(desc);
FakeDataManager dataMgr = new FakeDataManager();
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
CommandContext context = new CommandContext(null, "user", payload, "vdb", 1, false);
if (property != null) {
func.setArgs(new Expression[] { new Constant(property) });
}
String actual = (String) new Evaluator(Collections.emptyMap(), dataMgr, context).evaluate(func, Collections.emptyList());
assertEquals(expectedValue, actual);
}
Aggregations