Search in sources :

Example 46 with ElementSymbol

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);
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TransactionService(org.teiid.dqp.service.TransactionService) CommandContext(org.teiid.query.util.CommandContext) Evaluator(org.teiid.query.eval.Evaluator) VariableContext(org.teiid.query.sql.util.VariableContext) QueryProcessor(org.teiid.query.processor.QueryProcessor) Expression(org.teiid.query.sql.symbol.Expression) TransactionContext(org.teiid.dqp.service.TransactionContext) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 47 with ElementSymbol

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;
        }
    }
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol)

Example 48 with ElementSymbol

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();
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) VariableContext(org.teiid.query.sql.util.VariableContext) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 49 with ElementSymbol

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;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Expression(org.teiid.query.sql.symbol.Expression) VariableContext(org.teiid.query.sql.util.VariableContext) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 50 with ElementSymbol

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);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) Expression(org.teiid.query.sql.symbol.Expression) ArrayList(java.util.ArrayList) SubqueryHint(org.teiid.query.sql.lang.ExistsCriteria.SubqueryHint)

Aggregations

ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)417 ArrayList (java.util.ArrayList)165 Test (org.junit.Test)157 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)148 Expression (org.teiid.query.sql.symbol.Expression)104 List (java.util.List)103 Constant (org.teiid.query.sql.symbol.Constant)94 MultipleElementSymbol (org.teiid.query.sql.symbol.MultipleElementSymbol)41 SymbolMap (org.teiid.query.sql.util.SymbolMap)40 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)36 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)29 Map (java.util.Map)28 AggregateSymbol (org.teiid.query.sql.symbol.AggregateSymbol)28 Query (org.teiid.query.sql.lang.Query)26 HashMap (java.util.HashMap)25 Select (org.teiid.query.sql.lang.Select)24 BufferManager (org.teiid.common.buffer.BufferManager)22 Criteria (org.teiid.query.sql.lang.Criteria)22 LinkedList (java.util.LinkedList)20 TupleBuffer (org.teiid.common.buffer.TupleBuffer)19