Search in sources :

Example 16 with BatchedUpdateCommand

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

the class TestInsertProcessing method helpSelectInto_Case5569Processor.

public void helpSelectInto_Case5569Processor(boolean doBatching, boolean doBulkInsert) {
    FakeCapabilitiesFinder capFinder = new FakeCapabilitiesFinder();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    caps.setCapabilitySupport(Capability.BATCHED_UPDATES, doBatching);
    caps.setCapabilitySupport(Capability.BULK_UPDATE, doBulkInsert);
    caps.setCapabilitySupport(Capability.INSERT_WITH_ITERATOR, doBulkInsert);
    caps.setCapabilitySupport(Capability.QUERY_FROM_GROUP_ALIAS, false);
    // $NON-NLS-1$
    capFinder.addCapabilities("pm1", caps);
    QueryMetadataInterface metadata = RealMetadataFactory.example1Cached();
    HardcodedDataManager dataManager = new HardcodedDataManager();
    // $NON-NLS-1$
    dataManager.addData(// $NON-NLS-1$
    "SELECT pm1.g1.e1, pm1.g1.e2, pm1.g1.e3, pm1.g1.e4 FROM pm1.g1", new List[] { // $NON-NLS-1$
    Arrays.asList(new Object[] { "1", new Integer(1), Boolean.FALSE, new Double(1) }), // $NON-NLS-1$
    Arrays.asList(new Object[] { "2", new Integer(2), Boolean.TRUE, new Double(2) }) });
    if (doBulkInsert) {
        // $NON-NLS-1$
        dataManager.addData(// $NON-NLS-1$
        "INSERT INTO pm1.g2 (e1, e2, e3, e4) VALUES (...)", new List[] { Arrays.asList(1), Arrays.asList(1) });
    } else if (doBatching) {
        // $NON-NLS-1$
        dataManager.addData(// $NON-NLS-1$
        "BatchedUpdate{I,I}", new List[] { Arrays.asList(1), Arrays.asList(1) });
    } else {
        // $NON-NLS-1$
        dataManager.addData(// $NON-NLS-1$
        "INSERT INTO pm1.g2 (e1, e2, e3, e4) VALUES ('1', 1, FALSE, 1.0)", new List[] { Arrays.asList(new Object[] { new Integer(1) }) });
        // $NON-NLS-1$
        dataManager.addData(// $NON-NLS-1$
        "INSERT INTO pm1.g2 (e1, e2, e3, e4) VALUES ('2', 2, TRUE, 2.0)", new List[] { Arrays.asList(new Object[] { new Integer(1) }) });
    }
    // $NON-NLS-1$
    String sql = "SELECT pm1.g1.e1, pm1.g1.e2, pm1.g1.e3, pm1.g1.e4 INTO pm1.g2 from pm1.g1";
    Command command = helpParse(sql);
    ProcessorPlan plan = helpGetPlan(command, metadata, capFinder);
    List<?>[] expected = new List[] { Arrays.asList(new Object[] { new Integer(2) }) };
    helpProcess(plan, dataManager, expected);
    // check the command hist to ensure it contains the expected commands
    if (!doBulkInsert && doBatching) {
        BatchedUpdateCommand bu = (BatchedUpdateCommand) dataManager.getCommandHistory().get(1);
        assertEquals(2, bu.getUpdateCommands().size());
        // $NON-NLS-1$
        assertEquals("INSERT INTO pm1.g2 (e1, e2, e3, e4) VALUES ('1', 1, FALSE, 1.0)", bu.getUpdateCommands().get(0).toString());
        // $NON-NLS-1$
        assertEquals("INSERT INTO pm1.g2 (e1, e2, e3, e4) VALUES ('2', 2, TRUE, 2.0)", bu.getUpdateCommands().get(1).toString());
    }
}
Also used : FakeCapabilitiesFinder(org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) Command(org.teiid.query.sql.lang.Command) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) List(java.util.List) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface)

Example 17 with BatchedUpdateCommand

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

the class TestInsertProcessing method helpInsertIntoWithSubquery.

public void helpInsertIntoWithSubquery(Capability cap, boolean txn) throws Exception {
    FakeCapabilitiesFinder capFinder = new FakeCapabilitiesFinder();
    BasicSourceCapabilities caps = TestOptimizer.getTypicalCapabilities();
    caps.setCapabilitySupport(cap, true);
    caps.setCapabilitySupport(Capability.QUERY_FROM_GROUP_ALIAS, false);
    // $NON-NLS-1$
    capFinder.addCapabilities("pm1", caps);
    QueryMetadataInterface metadata = RealMetadataFactory.example1Cached();
    HardcodedDataManager dataManager = new HardcodedDataManager() {

        @Override
        public TupleSource registerRequest(CommandContext context, Command command, String modelName, RegisterRequestParameter parameterObject) throws TeiidComponentException {
            if (command instanceof Insert) {
                Insert insert = (Insert) command;
                if (insert.getTupleSource() != null) {
                    commandHistory.add(insert);
                    TupleSource ts = insert.getTupleSource();
                    int count = 0;
                    try {
                        while (ts.nextTuple() != null) {
                            count++;
                        }
                        return CollectionTupleSource.createUpdateCountArrayTupleSource(count);
                    } catch (TeiidProcessingException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            return super.registerRequest(context, command, modelName, parameterObject);
        }
    };
    List[] data = new List[txn ? 2 : 50];
    for (int i = 1; i <= data.length; i++) {
        data[i - 1] = Arrays.asList(String.valueOf(i), i, (i % 2 == 0) ? Boolean.TRUE : Boolean.FALSE, Double.valueOf(i));
    }
    // $NON-NLS-1$
    dataManager.addData(// $NON-NLS-1$
    "SELECT pm1.g1.e1, pm1.g1.e2, pm1.g1.e3, pm1.g1.e4 FROM pm1.g1", data);
    if (cap != null) {
        switch(cap) {
            case BATCHED_UPDATES:
                // $NON-NLS-1$
                dataManager.addData(// $NON-NLS-1$
                "BatchedUpdate{I,I}", new List[] { Arrays.asList(1), Arrays.asList(1) });
                break;
            case INSERT_WITH_QUERYEXPRESSION:
                // $NON-NLS-1$
                dataManager.addData(// $NON-NLS-1$
                "INSERT INTO pm1.g2 (e1, e2, e3, e4) SELECT pm1.g1.e1, pm1.g1.e2, pm1.g1.e3, pm1.g1.e4 FROM pm1.g1", new List[] { Arrays.asList(new Object[] { 2 }) });
                break;
        }
    } else {
        // $NON-NLS-1$
        dataManager.addData(// $NON-NLS-1$
        "INSERT INTO pm1.g2 (e1, e2, e3, e4) VALUES ('1', 1, FALSE, 1.0)", new List[] { Arrays.asList(new Object[] { new Integer(1) }) });
        // $NON-NLS-1$
        dataManager.addData(// $NON-NLS-1$
        "INSERT INTO pm1.g2 (e1, e2, e3, e4) VALUES ('2', 2, TRUE, 2.0)", new List[] { Arrays.asList(new Object[] { new Integer(1) }) });
    }
    // $NON-NLS-1$
    String sql = "INSERT INTO pm1.g2 SELECT pm1.g1.e1, pm1.g1.e2, pm1.g1.e3, pm1.g1.e4 from pm1.g1";
    Command command = helpParse(sql);
    ProcessorPlan plan = helpGetPlan(command, metadata, capFinder);
    List<?>[] expected = new List[] { Arrays.asList(txn ? 2 : 50) };
    CommandContext cc = TestProcessor.createCommandContext();
    if (!txn) {
        TransactionContext tc = new TransactionContext();
        cc.setTransactionContext(tc);
        cc.setBufferManager(null);
        cc.setProcessorBatchSize(2);
    }
    helpProcess(plan, cc, dataManager, expected);
    // check the command hist to ensure it contains the expected commands
    if (cap == Capability.BATCHED_UPDATES) {
        BatchedUpdateCommand bu = (BatchedUpdateCommand) dataManager.getCommandHistory().get(1);
        assertEquals(2, bu.getUpdateCommands().size());
        // $NON-NLS-1$
        assertEquals("INSERT INTO pm1.g2 (e1, e2, e3, e4) VALUES ('1', 1, FALSE, 1.0)", bu.getUpdateCommands().get(0).toString());
        // $NON-NLS-1$
        assertEquals("INSERT INTO pm1.g2 (e1, e2, e3, e4) VALUES ('2', 2, TRUE, 2.0)", bu.getUpdateCommands().get(1).toString());
    } else if (cap == Capability.INSERT_WITH_ITERATOR) {
        assertEquals(txn ? 6 : 9, dataManager.getCommandHistory().size());
    }
}
Also used : FakeCapabilitiesFinder(org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) Insert(org.teiid.query.sql.lang.Insert) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) TeiidProcessingException(org.teiid.core.TeiidProcessingException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) Command(org.teiid.query.sql.lang.Command) BatchedUpdateCommand(org.teiid.query.sql.lang.BatchedUpdateCommand) TupleSource(org.teiid.common.buffer.TupleSource) TransactionContext(org.teiid.dqp.service.TransactionContext) List(java.util.List) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface)

Aggregations

BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)17 Command (org.teiid.query.sql.lang.Command)13 List (java.util.List)10 ArrayList (java.util.ArrayList)8 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)5 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)4 FakeCapabilitiesFinder (org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder)4 Insert (org.teiid.query.sql.lang.Insert)4 Test (org.junit.Test)3 BlockedException (org.teiid.common.buffer.BlockedException)2 AtomicRequestMessage (org.teiid.dqp.message.AtomicRequestMessage)2 Evaluator (org.teiid.query.eval.Evaluator)2 SourceCapabilities (org.teiid.query.optimizer.capabilities.SourceCapabilities)2 RelationalPlan (org.teiid.query.processor.relational.RelationalPlan)2 Constant (org.teiid.query.sql.symbol.Constant)2 VariableContext (org.teiid.query.sql.util.VariableContext)2 CommandContext (org.teiid.query.util.CommandContext)2 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Set (java.util.Set)1