Search in sources :

Example 1 with QueryExpression

use of org.teiid.language.QueryExpression in project teiid by teiid.

the class TestConnectorWorkItem method testConversionError.

@Test
public void testConversionError() throws Exception {
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    final ExecutionFactory<Object, Object> ef = new ExecutionFactory<Object, Object>() {

        @Override
        public boolean isSourceRequired() {
            return false;
        }

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            List<String> list1 = new ArrayList<String>();
            list1.add("1");
            List<String> list2 = new ArrayList<String>();
            list2.add("a");
            final Iterator<List<String>> iter = Arrays.asList(list1, list2).iterator();
            return new ResultSetExecution() {

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (iter.hasNext()) {
                        return iter.next();
                    }
                    return null;
                }
            };
        }
    };
    ConnectorManager cm = new // $NON-NLS-1$ //$NON-NLS-2$
    ConnectorManager(// $NON-NLS-1$ //$NON-NLS-2$
    "FakeConnector", // $NON-NLS-1$ //$NON-NLS-2$
    "FakeConnector") {

        public ExecutionFactory getExecutionFactory() {
            return ef;
        }

        public Object getConnectionFactory() {
            return null;
        }
    };
    cm.start();
    ef.setCopyLobs(true);
    AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1);
    // $NON-NLS-1$
    requestMsg.setCommand(helpGetCommand("SELECT intkey FROM bqt1.smalla", EXAMPLE_BQT));
    requestMsg.setBufferManager(bm);
    ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm);
    cwi.execute();
    AtomicResultsMessage message = cwi.more();
    List[] results = message.getResults();
    assertEquals(1, results.length);
    List<?> tuple = results[0];
    assertEquals(1, tuple.get(0));
    assertEquals(-1, message.getFinalRow());
    try {
        cwi.more();
        fail();
    } catch (TranslatorException e) {
    // should throw the conversion error
    }
}
Also used : ArrayList(java.util.ArrayList) ExecutionFactory(org.teiid.translator.ExecutionFactory) BufferManager(org.teiid.common.buffer.BufferManager) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) List(java.util.List) ArrayList(java.util.ArrayList) AtomicResultsMessage(org.teiid.dqp.message.AtomicResultsMessage) TranslatorException(org.teiid.translator.TranslatorException) QueryExpression(org.teiid.language.QueryExpression) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) Test(org.junit.Test)

Example 2 with QueryExpression

use of org.teiid.language.QueryExpression in project teiid by teiid.

the class TestConnectorWorkItem method testUnmodifibleList.

@Test
public void testUnmodifibleList() throws Exception {
    BufferManager bm = BufferManagerFactory.getStandaloneBufferManager();
    final ExecutionFactory<Object, Object> ef = new ExecutionFactory<Object, Object>() {

        @Override
        public boolean isSourceRequired() {
            return false;
        }

        @Override
        public ResultSetExecution createResultSetExecution(QueryExpression command, ExecutionContext executionContext, RuntimeMetadata metadata, Object connection) throws TranslatorException {
            List<String> list1 = Collections.singletonList("1");
            final Iterator<List<String>> iter = Arrays.asList(list1).iterator();
            return new ResultSetExecution() {

                @Override
                public void execute() throws TranslatorException {
                }

                @Override
                public void close() {
                }

                @Override
                public void cancel() throws TranslatorException {
                }

                @Override
                public List<?> next() throws TranslatorException, DataNotAvailableException {
                    if (iter.hasNext()) {
                        return iter.next();
                    }
                    return null;
                }
            };
        }
    };
    ConnectorManager cm = new // $NON-NLS-1$ //$NON-NLS-2$
    ConnectorManager(// $NON-NLS-1$ //$NON-NLS-2$
    "FakeConnector", // $NON-NLS-1$ //$NON-NLS-2$
    "FakeConnector") {

        public ExecutionFactory getExecutionFactory() {
            return ef;
        }

        public Object getConnectionFactory() {
            return null;
        }
    };
    cm.start();
    AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1);
    // $NON-NLS-1$
    requestMsg.setCommand(helpGetCommand("SELECT intkey FROM bqt1.smalla", EXAMPLE_BQT));
    requestMsg.setBufferManager(bm);
    ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm);
    cwi.execute();
    AtomicResultsMessage message = cwi.more();
    List[] results = message.getResults();
    assertEquals(1, results.length);
    List<?> tuple = results[0];
    assertEquals(1, tuple.get(0));
    assertEquals(1, message.getFinalRow());
}
Also used : ExecutionFactory(org.teiid.translator.ExecutionFactory) BufferManager(org.teiid.common.buffer.BufferManager) RuntimeMetadata(org.teiid.metadata.RuntimeMetadata) ResultSetExecution(org.teiid.translator.ResultSetExecution) ExecutionContext(org.teiid.translator.ExecutionContext) List(java.util.List) ArrayList(java.util.ArrayList) AtomicResultsMessage(org.teiid.dqp.message.AtomicResultsMessage) QueryExpression(org.teiid.language.QueryExpression) AtomicRequestMessage(org.teiid.dqp.message.AtomicRequestMessage) Test(org.junit.Test)

Example 3 with QueryExpression

use of org.teiid.language.QueryExpression in project teiid by teiid.

the class TestHotrodExecution method testServer.

@Test
public void testServer() throws Exception {
    InfinispanConnection connection = getConnection("default");
    CACHE_NAME = connection.getCache().getName();
    ResultSetExecution exec = null;
    Command command = null;
    UpdateExecution update = null;
    // the below also test one-2-one relation.
    command = UTILITY.parseCommand("DELETE FROM G2");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, g3_e1, g3_e2 FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertNull(exec.next());
    command = UTILITY.parseCommand("INSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (1, 'one', 1, 'one')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (2, 'two', 2, 'two')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, g3_e1, g3_e2 FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two", new Integer(2), "two" }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (1, 'one', 1)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (2, 'one-one', 1)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (3, 'two', 2)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("INSERT INTO G4 (e1, e2, G2_e1) values (4, 'two-two', 2)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "one-one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(4), "two-two" }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("SELECT g2.e1, g4.e1, g4.e2 FROM G2 g2 JOIN G4 g4 " + "ON g2.e1 = g4.G2_e1 WHERE g2.e2 = 'two'");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(2), new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), new Integer(4), "two-two" }, exec.next().toArray());
    assertNull(exec.next());
    // updates
    command = UTILITY.parseCommand("UPDATE G2 SET e2 = 'two-m', g3_e2 = 'two-mm' WHERE e1 = 2");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, g3_e1, g3_e2 FROM G2 ORDER BY e1");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two-m", new Integer(2), "two-mm" }, exec.next().toArray());
    assertNull(exec.next());
    // complex updates
    command = UTILITY.parseCommand("UPDATE G4 SET e2 = 'two-2' WHERE e2 = 'two-two' OR e2 = 'one-one'");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two-2" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(4), "two-2" }, exec.next().toArray());
    assertNull(exec.next());
    // deletes
    command = UTILITY.parseCommand("DELETE FROM G4 where e2 = 'two-2'");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("DELETE FROM G2 where e1 = 1");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT * FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(2), "two-m", new Integer(2), "two-mm", null, null }, exec.next().toArray());
    assertNull(exec.next());
    // upsert
    command = UTILITY.parseCommand("UPSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (1, 'one', 1, 'one')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT * FROM G2 order by e1");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one", null, null }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two-m", new Integer(2), "two-mm", null, null }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("UPSERT INTO G2 (e1, e2, g3_e1, g3_e2) values (2, 'two', 2, 'two')");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT * FROM G2");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(1), "one", new Integer(1), "one", null, null }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(2), "two", new Integer(2), "two", null, null }, exec.next().toArray());
    assertNull(exec.next());
    command = UTILITY.parseCommand("UPSERT INTO G4 (e1, e2, G2_e1) values (5, 'upsert', 2)");
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2 FROM G4");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    assertArrayEquals(new Object[] { new Integer(3), "two" }, exec.next().toArray());
    assertArrayEquals(new Object[] { new Integer(5), "upsert" }, exec.next().toArray());
    assertNull(exec.next());
    Timestamp timestamp = new Timestamp(1504889513361L);
    Date date = new Date(1504889513361L);
    Time time = new Time(1504889513361L);
    String sql = "UPSERT INTO G5 (e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18) " + "values (512, 'one', convert(512.34, double), 512.25, 256, 1, 't', 1504889513361, convert(123445656.12313, bigdecimal), " + "convert(1332434343, biginteger), " + "{t '" + new SimpleDateFormat("HH:mm:ss").format(time) + "'}, " + "{ts '" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp) + "'}, " + "{d '" + new SimpleDateFormat("yyyy-MM-dd").format(date) + "'}, " + "null, null, " + "convert('clob contents', clob), xmlparse(CONTENT '<a>foo</a>'), null)";
    System.out.println(sql);
    command = UTILITY.parseCommand(sql);
    update = EF.createUpdateExecution(command, EC, METADATA, connection);
    update.execute();
    command = UTILITY.parseCommand("SELECT e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11, e12, e13, e14, e15, e16, e17, e18 FROM G5");
    exec = EF.createResultSetExecution((QueryExpression) command, EC, METADATA, connection);
    exec.execute();
    List<?> results = exec.next();
    assertEquals(new Integer(512), (Integer) results.get(0));
    assertEquals("one", (String) results.get(1));
    // assertEquals(512.34, (double)results.get(2));
    // assertEquals(512.25, (float)results.get(3));
    assertEquals(new Short("256"), results.get(4));
    assertEquals(new Byte("1"), results.get(5));
    assertEquals(new String("t"), results.get(6));
    assertEquals(new Long(1504889513361L), results.get(7));
    assertEquals(new BigDecimal("123445656.12313").toPlainString(), ((BigDecimal) results.get(8)).toPlainString());
    assertEquals(new BigInteger("1332434343").toString(), ((BigInteger) results.get(9)).toString());
    assertEquals(new Time(new SimpleDateFormat("HH:mm:ss").parse(time.toString()).getTime()), results.get(10));
    assertEquals(new Timestamp(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-09-08 11:51:53").getTime()), results.get(11));
    assertEquals(new Date(new SimpleDateFormat("yyyy-MM-dd").parse("2017-09-08").getTime()), results.get(12));
    assertEquals("clob contents", ObjectConverterUtil.convertToString(((Clob) results.get(15)).getCharacterStream()));
    assertEquals("<a>foo</a>", ObjectConverterUtil.convertToString(((SQLXML) results.get(16)).getCharacterStream()));
    assertNull(exec.next());
}
Also used : UpdateExecution(org.teiid.translator.UpdateExecution) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger) ResultSetExecution(org.teiid.translator.ResultSetExecution) SQLXML(java.sql.SQLXML) Command(org.teiid.language.Command) InfinispanConnection(org.teiid.infinispan.api.InfinispanConnection) BigInteger(java.math.BigInteger) QueryExpression(org.teiid.language.QueryExpression) Clob(java.sql.Clob) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test)

Example 4 with QueryExpression

use of org.teiid.language.QueryExpression in project teiid by teiid.

the class TestJDBCSocketPerformance method oneTimeSetup.

@BeforeClass
public static void oneTimeSetup() throws Exception {
    SocketConfiguration config = new SocketConfiguration();
    config.setSSLConfiguration(new SSLConfiguration());
    addr = new InetSocketAddress(0);
    config.setBindAddress(addr.getHostName());
    config.setPortNumber(0);
    EmbeddedConfiguration dqpConfig = new EmbeddedConfiguration();
    dqpConfig.setMaxActivePlans(2);
    server = new FakeServer(false);
    server.start(dqpConfig);
    ModelMetaData mmd = new ModelMetaData();
    mmd.setName("x");
    mmd.setModelType(Type.PHYSICAL);
    mmd.addSourceMapping("x", "hc", null);
    mmd.setSchemaSourceType("ddl");
    StringBuffer ddl = new StringBuffer("create foreign table x (col0 string");
    for (int i = 1; i < 10; i++) {
        ddl.append(",").append(" col").append(i).append(" string");
    }
    ddl.append(");");
    mmd.setSchemaText(ddl.toString());
    server.addTranslator("hc", new HardCodedExecutionFactory() {

        @Override
        protected List<? extends List<?>> getData(QueryExpression command) {
            List<List<String>> result = new ArrayList<List<String>>();
            int size = command.getProjectedQuery().getDerivedColumns().size();
            for (int i = 0; i < 64; i++) {
                List<String> row = new ArrayList<String>(size);
                for (int j = 0; j < size; j++) {
                    row.add("abcdefghi" + j);
                }
                result.add(row);
            }
            return result;
        }
    });
    server.deployVDB("x", mmd);
    jdbcTransport = new SocketListener(addr, config, server.getClientServiceRegistry(), BufferManagerFactory.getStandaloneBufferManager());
}
Also used : FakeServer(org.teiid.jdbc.FakeServer) InetSocketAddress(java.net.InetSocketAddress) SocketConfiguration(org.teiid.transport.SocketConfiguration) EmbeddedConfiguration(org.teiid.runtime.EmbeddedConfiguration) ModelMetaData(org.teiid.adminapi.impl.ModelMetaData) SSLConfiguration(org.teiid.transport.SSLConfiguration) SocketListener(org.teiid.transport.SocketListener) HardCodedExecutionFactory(org.teiid.runtime.HardCodedExecutionFactory) ArrayList(java.util.ArrayList) List(java.util.List) QueryExpression(org.teiid.language.QueryExpression) BeforeClass(org.junit.BeforeClass)

Example 5 with QueryExpression

use of org.teiid.language.QueryExpression in project teiid by teiid.

the class LoopbackExecution method execute.

@Override
public void execute() throws TranslatorException {
    // Log our command
    // $NON-NLS-1$
    LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Loopback executing command: " + command);
    if (this.config.isThrowError()) {
        // $NON-NLS-1$
        throw new TranslatorException("Failing because Error=true");
    }
    this.rowsNeeded = this.config.getRowCount();
    if (command instanceof QueryExpression) {
        QueryExpression queryCommand = (QueryExpression) command;
        if (queryCommand.getLimit() != null) {
            this.rowsNeeded = queryCommand.getLimit().getRowLimit();
        }
    }
    // Prepare for execution
    determineOutputTypes();
    generateRow();
}
Also used : TranslatorException(org.teiid.translator.TranslatorException) QueryExpression(org.teiid.language.QueryExpression)

Aggregations

QueryExpression (org.teiid.language.QueryExpression)16 ResultSetExecution (org.teiid.translator.ResultSetExecution)12 Test (org.junit.Test)11 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)11 ExecutionContext (org.teiid.translator.ExecutionContext)10 TranslatorException (org.teiid.translator.TranslatorException)9 List (java.util.List)7 ExecutionFactory (org.teiid.translator.ExecutionFactory)7 ArrayList (java.util.ArrayList)6 ModelMetaData (org.teiid.adminapi.impl.ModelMetaData)5 Command (org.teiid.language.Command)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 BeforeClass (org.junit.BeforeClass)3 BufferManager (org.teiid.common.buffer.BufferManager)3 FakeServer (org.teiid.jdbc.FakeServer)3 HardCodedExecutionFactory (org.teiid.runtime.HardCodedExecutionFactory)3 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)3 UpdateExecution (org.teiid.translator.UpdateExecution)3 BigInteger (java.math.BigInteger)2