Search in sources :

Example 26 with TupleSource

use of org.teiid.common.buffer.TupleSource 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)

Example 27 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TestWithClauseProcessing method testWithBlockingJoin.

@Test
public void testWithBlockingJoin() throws TeiidException {
    // $NON-NLS-1$
    String sql = "with a (x, y) as /*+ no_inline */ (select e1, e2 from pm1.g1) SELECT a.x, a.y, pm1.g2.e1 from a left outer join pm1.g2 makenotdep on (rtrim(a.x) = pm1.g2.e1) order by a.y";
    HardcodedDataManager dataManager = new HardcodedDataManager() {

        @Override
        public TupleSource registerRequest(CommandContext context, Command command, String modelName, RegisterRequestParameter parameterObject) throws TeiidComponentException {
            final TupleSource ts = super.registerRequest(context, command, modelName, parameterObject);
            return new TupleSource() {

                int i = 0;

                @Override
                public List<?> nextTuple() throws TeiidComponentException, TeiidProcessingException {
                    if ((i++ % 100) < 3) {
                        throw BlockedException.INSTANCE;
                    }
                    return ts.nextTuple();
                }

                @Override
                public void closeSource() {
                    ts.closeSource();
                }
            };
        }
    };
    List<?>[] rows = new List[10];
    for (int i = 0; i < rows.length; i++) {
        rows[i] = Arrays.asList(String.valueOf(i));
    }
    dataManager.addData("SELECT g_0.e1 AS c_0 FROM pm1.g2 AS g_0 ORDER BY c_0", rows);
    rows = new List[100];
    for (int i = 0; i < rows.length; i++) {
        rows[i] = Arrays.asList(String.valueOf(i), i);
    }
    dataManager.addData("SELECT g_0.e1, g_0.e2 FROM pm1.g1 AS g_0", rows);
    dataManager.addData("WITH a (x, y) AS (SELECT 1, 2 FROM g1 AS g_0) SELECT g_0.x AS c_0 FROM a AS g_0, a AS g_1 ORDER BY c_0", new List[0]);
    ProcessorPlan plan = TestOptimizer.helpPlan(sql, RealMetadataFactory.example1Cached(), null, new DefaultCapabilitiesFinder(TestOptimizer.getTypicalCapabilities()), new String[] { "SELECT a.x, a.y FROM a", "SELECT g_0.e1 AS c_0 FROM pm1.g2 AS g_0 ORDER BY c_0" }, ComparisonMode.EXACT_COMMAND_STRING);
    // check the full pushdown command
    List<?>[] result = new List[100];
    for (int i = 0; i < result.length; i++) {
        result[i] = Arrays.asList(String.valueOf(i), i, i < 10 ? String.valueOf(i) : null);
    }
    helpProcess(plan, dataManager, result);
}
Also used : CommandContext(org.teiid.query.util.CommandContext) Command(org.teiid.query.sql.lang.Command) TupleSource(org.teiid.common.buffer.TupleSource) List(java.util.List) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Example 28 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TestQueryProcessor method helpTestProcessor.

public void helpTestProcessor(FakeProcessorPlan plan, List[] expectedResults) throws TeiidException {
    BufferManager bufferMgr = BufferManagerFactory.getStandaloneBufferManager();
    FakeDataManager dataManager = new FakeDataManager();
    // $NON-NLS-1$ //$NON-NLS-2$
    CommandContext context = new CommandContext("pid", "group", null, null, 1);
    QueryProcessor processor = new QueryProcessor(plan, context, bufferMgr, dataManager);
    BatchCollector collector = processor.createBatchCollector();
    TupleBuffer tsID = null;
    while (true) {
        try {
            tsID = collector.collectTuples();
            break;
        } catch (BlockedException e) {
        }
    }
    // Compare # of rows in actual and expected
    // $NON-NLS-1$
    assertEquals("Did not get expected # of rows", expectedResults.length, tsID.getRowCount());
    // Compare actual with expected results
    TupleSource actual = tsID.createIndexedTupleSource();
    if (expectedResults.length > 0) {
        for (int i = 0; i < expectedResults.length; i++) {
            List actRecord = actual.nextTuple();
            List expRecord = expectedResults[i];
            // $NON-NLS-1$
            assertEquals("Did not match row at row index " + i, expRecord, actRecord);
        }
    }
    tsID.remove();
}
Also used : CommandContext(org.teiid.query.util.CommandContext) TupleSource(org.teiid.common.buffer.TupleSource) TupleBuffer(org.teiid.common.buffer.TupleBuffer) ArrayList(java.util.ArrayList) List(java.util.List) BufferManager(org.teiid.common.buffer.BufferManager) BlockedException(org.teiid.common.buffer.BlockedException)

Example 29 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TestProjectIntoNode method helpTestNextBatch.

private void helpTestNextBatch(int tupleBatchSize, Mode mode) throws Exception {
    ProjectIntoNode node = new ProjectIntoNode(2);
    TupleSource tupleSource = new FakeDataTupleSource(NUM_ROWS);
    RelationalNode child = new FakeRelationalNode(1, tupleSource, tupleBatchSize);
    node.addChild(child);
    // $NON-NLS-1$
    node.setIntoGroup(new GroupSymbol("myGroup"));
    // $NON-NLS-1$
    ElementSymbol elementSymbol_1 = new ElementSymbol("myGroup.myElement1");
    // $NON-NLS-1$
    ElementSymbol elementSymbol_2 = new ElementSymbol("myGroup.myElement2");
    elementSymbol_1.setType(Integer.class);
    elementSymbol_2.setType(String.class);
    List<ElementSymbol> elements = Arrays.asList(elementSymbol_1, elementSymbol_2);
    node.setIntoElements(elements);
    child.setElements(elements);
    node.setMode(mode);
    // $NON-NLS-1$
    node.setModelName("myModel");
    CommandContext context = new CommandContext();
    BufferManager bm = BufferManagerFactory.getTestBufferManager(tupleBatchSize, tupleBatchSize);
    ProcessorDataManager dataManager = new FakePDM(tupleBatchSize);
    child.initialize(context, bm, dataManager);
    node.initialize(context, bm, dataManager);
    node.open();
    TupleBatch batch = null;
    // Do the remaining batches
    while (true) {
        try {
            batch = node.nextBatch();
            break;
        } catch (BlockedException e) {
        // Normal
        }
    }
    assertNotNull(batch);
    List[] tuples = batch.getAllTuples();
    assertEquals(1, tuples.length);
    Object[] columns = tuples[0].toArray();
    assertNotNull(columns);
    assertEquals(1, columns.length);
    // Should have inserted all rows
    assertEquals(new Integer(NUM_ROWS), columns[0]);
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) CommandContext(org.teiid.query.util.CommandContext) ProcessorDataManager(org.teiid.query.processor.ProcessorDataManager) BufferManager(org.teiid.common.buffer.BufferManager) BlockedException(org.teiid.common.buffer.BlockedException) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TupleSource(org.teiid.common.buffer.TupleSource) FakeTupleSource(org.teiid.query.processor.FakeTupleSource) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) ArrayList(java.util.ArrayList) List(java.util.List) TupleBatch(org.teiid.common.buffer.TupleBatch)

Example 30 with TupleSource

use of org.teiid.common.buffer.TupleSource in project teiid by teiid.

the class TestSortNode method testSortLimit.

@Test
public void testSortLimit() throws Exception {
    // $NON-NLS-1$
    ElementSymbol es1 = new ElementSymbol("e1");
    es1.setType(DataTypeManager.DefaultDataClasses.INTEGER);
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    // $NON-NLS-1$
    TupleBuffer tsid = bm.createTupleBuffer(Arrays.asList(es1, es1), "test", TupleSourceType.PROCESSOR);
    tsid.addTuple(Arrays.asList(4));
    tsid.addTuple(Arrays.asList(3));
    tsid.addTuple(Arrays.asList(2));
    tsid.addTuple(Arrays.asList(1));
    tsid.close();
    // $NON-NLS-1$
    SortUtility su = new SortUtility(tsid.createIndexedTupleSource(), Arrays.asList(es1), Arrays.asList(Boolean.TRUE), Mode.SORT, bm, "test", tsid.getSchema());
    su.setBatchSize(2);
    TupleBuffer out = su.sort(2);
    TupleSource ts = out.createIndexedTupleSource();
    assertEquals(Arrays.asList(1), ts.nextTuple());
    assertEquals(Arrays.asList(2), ts.nextTuple());
    assertNull(ts.nextTuple());
    // $NON-NLS-1$
    su = new SortUtility(tsid.createIndexedTupleSource(), Arrays.asList(es1), Arrays.asList(Boolean.TRUE), Mode.SORT, bm, "test", tsid.getSchema());
    su.setBatchSize(10);
    out = su.sort(2);
    ts = out.createIndexedTupleSource();
    assertEquals(Arrays.asList(1), ts.nextTuple());
    assertEquals(Arrays.asList(2), ts.nextTuple());
    assertNull(ts.nextTuple());
}
Also used : ElementSymbol(org.teiid.query.sql.symbol.ElementSymbol) TupleSource(org.teiid.common.buffer.TupleSource) TupleBuffer(org.teiid.common.buffer.TupleBuffer) BufferManager(org.teiid.common.buffer.BufferManager) Test(org.junit.Test)

Aggregations

TupleSource (org.teiid.common.buffer.TupleSource)31 CollectionTupleSource (org.teiid.query.processor.CollectionTupleSource)11 Test (org.junit.Test)10 TupleBuffer (org.teiid.common.buffer.TupleBuffer)10 CommandContext (org.teiid.query.util.CommandContext)10 List (java.util.List)9 BlockedException (org.teiid.common.buffer.BlockedException)9 TeiidProcessingException (org.teiid.core.TeiidProcessingException)9 Command (org.teiid.query.sql.lang.Command)9 ElementSymbol (org.teiid.query.sql.symbol.ElementSymbol)9 ArrayList (java.util.ArrayList)6 QueryProcessingException (org.teiid.api.exception.query.QueryProcessingException)6 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)6 RegisterRequestParameter (org.teiid.query.processor.RegisterRequestParameter)6 GroupSymbol (org.teiid.query.sql.symbol.GroupSymbol)6 BufferManager (org.teiid.common.buffer.BufferManager)5 TeiidComponentException (org.teiid.core.TeiidComponentException)5 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)4 BatchedUpdateCommand (org.teiid.query.sql.lang.BatchedUpdateCommand)4 BufferManagerImpl (org.teiid.common.buffer.impl.BufferManagerImpl)3