Search in sources :

Example 6 with Evaluator

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();
}
Also used : ArrayList(java.util.ArrayList) Evaluator(org.teiid.query.eval.Evaluator) Insert(org.teiid.query.sql.lang.Insert) BlockedException(org.teiid.common.buffer.BlockedException) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) SourceHint(org.teiid.query.sql.lang.SourceHint) Command(org.teiid.query.sql.lang.Command) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) ArrayList(java.util.ArrayList) List(java.util.List) QueryProcessingException(org.teiid.api.exception.query.QueryProcessingException)

Example 7 with Evaluator

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);
        }
    }
}
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 8 with Evaluator

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;
}
Also used : TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ArrayList(java.util.ArrayList) Evaluator(org.teiid.query.eval.Evaluator) TeiidException(org.teiid.core.TeiidException)

Example 9 with Evaluator

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));
}
Also used : SubqueryContainer(org.teiid.query.sql.lang.SubqueryContainer) CommandContext(org.teiid.query.util.CommandContext) ArrayList(java.util.ArrayList) List(java.util.List) CollectionValueIterator(org.teiid.query.sql.lang.CollectionValueIterator) Evaluator(org.teiid.query.eval.Evaluator)

Example 10 with Evaluator

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);
}
Also used : CommandContext(org.teiid.query.util.CommandContext) FakeDataManager(org.teiid.query.processor.FakeDataManager) FunctionDescriptor(org.teiid.query.function.FunctionDescriptor) Evaluator(org.teiid.query.eval.Evaluator)

Aggregations

Evaluator (org.teiid.query.eval.Evaluator)20 ArrayList (java.util.ArrayList)12 List (java.util.List)7 CommandContext (org.teiid.query.util.CommandContext)7 TeiidComponentException (org.teiid.core.TeiidComponentException)6 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)6 HashMap (java.util.HashMap)5 Map (java.util.Map)4 Test (org.junit.Test)4 Expression (org.teiid.query.sql.symbol.Expression)4 ExpressionEvaluationException (org.teiid.api.exception.query.ExpressionEvaluationException)3 BlockedException (org.teiid.common.buffer.BlockedException)3 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)3 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)2 LanguageBridgeFactory (org.teiid.dqp.internal.datamgr.LanguageBridgeFactory)2 Call (org.teiid.language.Call)2 FunctionDescriptor (org.teiid.query.function.FunctionDescriptor)2 FakeDataManager (org.teiid.query.processor.FakeDataManager)2