use of org.teiid.query.sql.symbol.ElementSymbol 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.sql.symbol.ElementSymbol in project teiid by teiid.
the class ForEachRowPlan method initialize.
@Override
public void initialize(CommandContext context, ProcessorDataManager dataMgr, BufferManager bufferMgr) {
setContext(context);
this.dataMgr = dataMgr;
this.bufferMgr = bufferMgr;
for (ElementSymbol elem : this.params.keySet()) {
if (elem.getGroupSymbol().getName().equalsIgnoreCase(SQLConstants.NonReserved.KEY)) {
this.insert = true;
break;
}
}
}
use of org.teiid.query.sql.symbol.ElementSymbol 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();
}
use of org.teiid.query.sql.symbol.ElementSymbol in project teiid by teiid.
the class ProcedurePlan method open.
public void open() throws TeiidProcessingException, TeiidComponentException {
if (!this.evaluatedParams) {
if (this.outParams != null) {
for (ElementSymbol param : this.outParams) {
setParameterValue(param, getCurrentVariableContext(), null);
}
}
if (this.params != null) {
for (Map.Entry<ElementSymbol, Expression> entry : this.params.entrySet()) {
ElementSymbol param = entry.getKey();
Expression expr = entry.getValue();
VariableContext context = getCurrentVariableContext();
if (context.getVariableMap().containsKey(param)) {
continue;
}
Object value = this.evaluateExpression(expr);
// check constraint
checkNotNull(param, value);
setParameterValue(param, context, value);
}
this.evaluator.close();
} else if (runInContext) {
// if there are no params, this needs to run in the current variable context
this.currentVarContext.setParentContext(parentContext);
}
this.push(originalProgram);
}
this.evaluatedParams = true;
}
use of org.teiid.query.sql.symbol.ElementSymbol in project teiid by teiid.
the class SQLParserUtil method addFBI.
KeyRecord addFBI(MetadataFactory factory, List<Expression> expressions, Table table, String name) throws MetadataException {
List<String> columnNames = new ArrayList<String>(expressions.size());
List<Boolean> nonColumnExpressions = new ArrayList<Boolean>(expressions.size());
boolean fbi = false;
for (int i = 0; i < expressions.size(); i++) {
Expression ex = expressions.get(i);
if (ex instanceof ElementSymbol) {
columnNames.add(((ElementSymbol) ex).getName());
nonColumnExpressions.add(Boolean.FALSE);
} else {
columnNames.add(ex.toString());
nonColumnExpressions.add(Boolean.TRUE);
fbi = true;
}
}
return factory.addFunctionBasedIndex(name != null ? name : (SQLConstants.NonReserved.INDEX + (fbi ? table.getFunctionBasedIndexes().size() : table.getIndexes().size())), columnNames, nonColumnExpressions, table);
}
Aggregations