Search in sources :

Example 16 with Command

use of org.teiid.query.sql.lang.Command 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 17 with Command

use of org.teiid.query.sql.lang.Command in project teiid by teiid.

the class QueryParser method parseProcedure.

public Command parseProcedure(String sql, boolean update) throws QueryParserException {
    try {
        if (update) {
            return getSqlParser(sql).forEachRowTriggerAction(new ParseInfo());
        }
        Command result = getSqlParser(sql).procedureBodyCommand(new ParseInfo());
        result.setCacheHint(SQLParserUtil.getQueryCacheOption(sql));
        return result;
    } catch (ParseException pe) {
        throw convertParserException(pe);
    } finally {
        tm.reinit();
    }
}
Also used : Command(org.teiid.query.sql.lang.Command)

Example 18 with Command

use of org.teiid.query.sql.lang.Command in project teiid by teiid.

the class TestUpdateValidator method createView.

public static Command createView(String sql, TransformationMetadata md, String vGroup) throws QueryParserException, QueryResolverException, TeiidComponentException {
    QueryNode vm1g1n1 = new QueryNode(sql);
    Table vm1g1 = RealMetadataFactory.createUpdatableVirtualGroup(vGroup, md.getMetadataStore().getSchema("VM1"), vm1g1n1);
    Command command = QueryParser.getQueryParser().parseCommand(sql);
    QueryResolver.resolveCommand(command, md);
    List<Expression> symbols = command.getProjectedSymbols();
    String[] names = new String[symbols.size()];
    String[] types = new String[symbols.size()];
    int i = 0;
    for (Expression singleElementSymbol : symbols) {
        names[i] = Symbol.getShortName(singleElementSymbol);
        types[i++] = DataTypeManager.getDataTypeName(singleElementSymbol.getType());
    }
    RealMetadataFactory.createElements(vm1g1, names, types);
    return command;
}
Also used : Table(org.teiid.metadata.Table) Command(org.teiid.query.sql.lang.Command) Expression(org.teiid.query.sql.symbol.Expression) QueryNode(org.teiid.query.mapping.relational.QueryNode)

Example 19 with Command

use of org.teiid.query.sql.lang.Command in project teiid by teiid.

the class TestUpdateValidator method helpTest.

private UpdateValidator helpTest(String sql, TransformationMetadata md, boolean failInsert, boolean failUpdate, boolean failDelete) {
    try {
        String vGroup = "gx";
        Command command = createView(sql, md, vGroup);
        UpdateValidator uv = new UpdateValidator(md, UpdateType.INHERENT, UpdateType.INHERENT, UpdateType.INHERENT);
        GroupSymbol gs = new GroupSymbol(vGroup);
        ResolverUtil.resolveGroup(gs, md);
        uv.validate(command, ResolverUtil.resolveElementsInGroup(gs, md));
        UpdateInfo info = uv.getUpdateInfo();
        assertEquals(uv.getReport().getFailureMessage(), failInsert, info.getInsertValidationError() != null);
        assertEquals(uv.getReport().getFailureMessage(), failUpdate, info.getUpdateValidationError() != null);
        assertEquals(uv.getReport().getFailureMessage(), failDelete, info.getDeleteValidationError() != null);
        return uv;
    } catch (TeiidException e) {
        throw new RuntimeException(e);
    }
}
Also used : Command(org.teiid.query.sql.lang.Command) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) UpdateInfo(org.teiid.query.validator.UpdateValidator.UpdateInfo) TeiidException(org.teiid.core.TeiidException)

Example 20 with Command

use of org.teiid.query.sql.lang.Command in project teiid by teiid.

the class TestValidator method helpResolve.

static Command helpResolve(String sql, GroupSymbol container, int type, QueryMetadataInterface metadata) throws QueryParserException, QueryResolverException, TeiidComponentException {
    Command command = QueryParser.getQueryParser().parseCommand(sql);
    QueryResolver.resolveCommand(command, container, type, metadata, false);
    return command;
}
Also used : Command(org.teiid.query.sql.lang.Command)

Aggregations

Command (org.teiid.query.sql.lang.Command)232 Test (org.junit.Test)142 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)90 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)73 FakeCapabilitiesFinder (org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder)66 List (java.util.List)64 CommandContext (org.teiid.query.util.CommandContext)38 ArrayList (java.util.ArrayList)37 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)36 BigInteger (java.math.BigInteger)29 DefaultCapabilitiesFinder (org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)25 AnalysisRecord (org.teiid.query.analysis.AnalysisRecord)21 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)20 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)19 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)16 LinkedList (java.util.LinkedList)10 QueryParser (org.teiid.query.parser.QueryParser)10 RelationalPlan (org.teiid.query.processor.relational.RelationalPlan)10 QueryCommand (org.teiid.query.sql.lang.QueryCommand)10 StoredProcedure (org.teiid.query.sql.lang.StoredProcedure)10