Search in sources :

Example 26 with Expression

use of org.teiid.query.sql.symbol.Expression in project teiid by teiid.

the class ProjectNode method setSelectSymbols.

public void setSelectSymbols(List<? extends Expression> symbols) {
    this.selectSymbols = symbols;
    elementMap = Collections.emptyMap();
    this.projectionIndexes = new int[this.selectSymbols.size()];
    Arrays.fill(this.projectionIndexes, -1);
    this.expressions = new ArrayList<Expression>(this.selectSymbols.size());
    for (Expression ses : this.selectSymbols) {
        this.expressions.add(SymbolMap.getExpression(ses));
    }
}
Also used : Expression(org.teiid.query.sql.symbol.Expression)

Example 27 with Expression

use of org.teiid.query.sql.symbol.Expression 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)

Example 28 with Expression

use of org.teiid.query.sql.symbol.Expression in project teiid by teiid.

the class ExecDynamicSqlInstruction method createVariableValuesMap.

/**
 * @param localContext
 * @return
 */
private Map<ElementSymbol, Expression> createVariableValuesMap(VariableContext localContext) {
    Map<ElementSymbol, Object> variableMap = new HashMap<ElementSymbol, Object>();
    localContext.getFlattenedContextMap(variableMap);
    Map<ElementSymbol, Expression> nameValueMap = new HashMap<ElementSymbol, Expression>(variableMap.size());
    for (Map.Entry<ElementSymbol, Object> entry : variableMap.entrySet()) {
        if (entry.getKey() instanceof ElementSymbol) {
            if (entry.getValue() instanceof Expression) {
                nameValueMap.put(entry.getKey(), (Expression) entry.getValue());
            } else {
                nameValueMap.put(entry.getKey(), new Constant(entry.getValue(), entry.getKey().getType()));
            }
        }
    }
    return nameValueMap;
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) HashMap(java.util.HashMap) Expression(org.teiid.query.sql.symbol.Expression) Constant(org.teiid.query.sql.symbol.Constant) Map(java.util.Map) HashMap(java.util.HashMap)

Example 29 with Expression

use of org.teiid.query.sql.symbol.Expression 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 30 with Expression

use of org.teiid.query.sql.symbol.Expression 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)

Aggregations

Expression (org.teiid.query.sql.symbol.Expression)257 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)104 ArrayList (java.util.ArrayList)74 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)54 Test (org.junit.Test)53 PlanNode (org.teiid.query.optimizer.relational.plantree.PlanNode)50 List (java.util.List)49 Constant (org.teiid.query.sql.symbol.Constant)38 SymbolMap (org.teiid.query.sql.util.SymbolMap)37 HashMap (java.util.HashMap)22 LinkedList (java.util.LinkedList)22 Criteria (org.teiid.query.sql.lang.Criteria)22 Map (java.util.Map)21 ClobType (org.teiid.core.types.ClobType)16 CompareCriteria (org.teiid.query.sql.lang.CompareCriteria)16 HashSet (java.util.HashSet)13 LinkedHashSet (java.util.LinkedHashSet)13 AliasSymbol (org.teiid.query.sql.symbol.AliasSymbol)13 SearchedCaseExpression (org.teiid.query.sql.symbol.SearchedCaseExpression)13 OrderBy (org.teiid.query.sql.lang.OrderBy)12