@Test public void testStripAliases1() throws Exception { String sql = "select intkey as a, stringkey as b from BQT1.SmallA ORDER BY a, b"; // $NON-NLS-1$ String expected = "SELECT BQT1.SmallA.IntKey, BQT1.SmallA.StringKey FROM BQT1.SmallA ORDER BY BQT1.SmallA.IntKey, BQT1.SmallA.StringKey"; //$NON-NLS-1$ Command command = helpTest(sql, expected, false, true, RealMetadataFactory.exampleBQTCached()); LanguageBridgeFactory lbf = new LanguageBridgeFactory(RealMetadataFactory.exampleBQTCached()); org.teiid.language.Command c = lbf.translate(command); assertEquals( "SELECT SmallA.IntKey, SmallA.StringKey FROM SmallA ORDER BY SmallA.IntKey, SmallA.StringKey", c.toString()); }
@Test public void testInlineViewOrderBy() throws Exception { String sql = "select intnum from (select intnum from bqt1.smallb) b order by b.intnum"; //$NON-NLS-1$ String expected = "SELECT v_0.c_0 FROM (SELECT g_0.IntNum AS c_0 FROM BQT1.SmallB AS g_0) AS v_0 ORDER BY c_0"; //$NON-NLS-1$ Command command = helpTest(sql, expected, true, false, RealMetadataFactory.exampleBQTCached()); LanguageBridgeFactory lbf = new LanguageBridgeFactory(RealMetadataFactory.exampleBQTCached()); org.teiid.language.Command c = lbf.translate(command); assertEquals( "SELECT v_0.c_0 FROM (SELECT g_0.IntNum AS c_0 FROM SmallB AS g_0) AS v_0 ORDER BY v_0.c_0", c.toString()); }
@Test public void testStripAliases() throws Exception { String sql = "select intkey as a, stringkey as b from BQT1.SmallA ORDER BY a, b"; // $NON-NLS-1$ String expected = "SELECT g_0.IntKey, g_0.StringKey FROM BQT1.SmallA AS g_0 ORDER BY g_0.IntKey, g_0.StringKey"; //$NON-NLS-1$ helpTest(sql, expected, true, true, RealMetadataFactory.exampleBQTCached()); }
@Test public void testUnrelatedOrderBy2() throws Exception { String sql = "SELECT b.IntKey FROM (select intkey, stringkey from BQT1.SmallA) a, (select intkey, stringkey from BQT1.SmallA) b ORDER BY a.StringKey || b.intKey"; //$NON-NLS-1$ String expected = "SELECT v_1.c_0 FROM (SELECT g_0.IntKey AS c_0, g_0.StringKey AS c_1 FROM BQT1.SmallA AS g_0) AS v_0, (SELECT g_1.IntKey AS c_0, g_1.StringKey AS c_1 FROM BQT1.SmallA AS g_1) AS v_1 ORDER BY (v_0.c_1 || v_1.c_0)"; //$NON-NLS-1$ helpTest(sql, expected, true, false, RealMetadataFactory.exampleBQTCached()); }
@Test public void testUnrelatedOrderBy() throws Exception { String sql = "SELECT b.IntKey FROM BQT1.SmallA a, BQT1.SmallA b ORDER BY a.StringKey"; //$NON-NLS-1$ String expected = "SELECT g_1.IntKey AS c_0 FROM BQT1.SmallA AS g_0, BQT1.SmallA AS g_1 ORDER BY g_0.StringKey"; //$NON-NLS-1$ helpTest(sql, expected, true, false, RealMetadataFactory.exampleBQTCached()); }
@Test public void testUnionAliasing() throws Exception { String sql = "SELECT IntKey FROM BQT1.SmallA UNION ALL SELECT IntNum FROM BQT1.SmallA"; //$NON-NLS-1$ String expected = "SELECT BQT1.SmallA.IntKey AS c_0 FROM BQT1.SmallA UNION ALL SELECT BQT1.SmallA.IntNum AS c_0 FROM BQT1.SmallA"; //$NON-NLS-1$ helpTest(sql, expected, false, false, RealMetadataFactory.exampleBQTCached()); }
@Test public void testCorrelatedRefernce1() throws Exception { String sql = "select intnum, stringnum from bqt1.smallb where intnum in (select stringnum || b.intnum from (select intnum from bqt1.smalla) b) "; //$NON-NLS-1$ String expected = "SELECT g_0.IntNum, g_0.StringNum FROM BQT1.SmallB AS g_0 WHERE convert(g_0.IntNum, string) IN (SELECT concat(g_0.StringNum, convert(v_0.c_0, string)) FROM (SELECT g_1.IntNum AS c_0 FROM BQT1.SmallA AS g_1) AS v_0)"; //$NON-NLS-1$ helpTest(sql, expected, true, false, RealMetadataFactory.exampleBQTCached()); }
@Test public void testInlineViewWithOnClause() throws Exception { String sql = "select abcd.efg from (select intkey as efg from bqt1.smalla) abcd inner join (select intnum from bqt1.smallb) b on (b.intnum = abcd.efg)"; //$NON-NLS-1$ String expected = "SELECT v_0.c_0 FROM (SELECT g_0.IntKey AS c_0 FROM BQT1.SmallA AS g_0) AS v_0 INNER JOIN (SELECT g_1.IntNum AS c_0 FROM BQT1.SmallB AS g_1) AS v_1 ON v_1.c_0 = v_0.c_0"; //$NON-NLS-1$ helpTest(sql, expected, true, false, RealMetadataFactory.exampleBQTCached()); }
@Test public void testNestedInlineViewOrderBy() throws Exception { String sql = "select x from (select intnum x from (select intnum from bqt1.smallb) b order by x) y order by x"; //$NON-NLS-1$ String expected = "SELECT v_1.c_0 FROM (SELECT v_0.c_0 FROM (SELECT g_0.IntNum AS c_0 FROM BQT1.SmallB AS g_0) AS v_0) AS v_1 ORDER BY c_0"; //$NON-NLS-1$ helpTest(sql, expected, true, false, RealMetadataFactory.exampleBQTCached()); }
@Test public void testInlineViewWithSubQuery() throws Exception { String sql = "select intnum from (select intnum from bqt1.smallb where intnum in (select intnum a from bqt1.smalla)) b"; //$NON-NLS-1$ String expected = "SELECT v_0.c_0 FROM (SELECT g_0.IntNum AS c_0 FROM BQT1.SmallB AS g_0 WHERE g_0.IntNum IN (SELECT g_1.IntNum FROM BQT1.SmallA AS g_1)) AS v_0"; //$NON-NLS-1$ helpTest(sql, expected, true, false, RealMetadataFactory.exampleBQTCached()); }
@Test(expected = TeiidRuntimeException.class) public void testKeepAliases1() throws Exception { String sql = "select g_1.intkey as a, g_1.stringkey as b from BQT1.SmallA g_1, BQT1.SmallB ORDER BY a, b"; //$NON-NLS-1$ String expected = "SELECT g.IntKey AS c_0, g.StringKey AS c_1 FROM BQT1.SmallA AS g ORDER BY c_0, c_1"; //$NON-NLS-1$ AliasGenerator av = new AliasGenerator(true, false); Map<String, String> aliasMap = new HashMap<String, String>(); aliasMap.put("g_1", "g_1"); av.setAliasMapping(aliasMap); helpTest(sql, expected, RealMetadataFactory.exampleBQTCached(), av); }
@Test public void testGroupAliasNotSupported() throws Exception { String sql = "select b.intkey from bqt1.smalla b"; // $NON-NLS-1$ String expected = "SELECT BQT1.SmallA.IntKey FROM BQT1.SmallA"; // $NON-NLS-1$ helpTest(sql, expected, false, false, RealMetadataFactory.exampleBQTCached()); }
public TranslationUtility getBQTTranslationUtility() { return new TranslationUtility(RealMetadataFactory.exampleBQTCached()); }
@SuppressWarnings("nls") public class TestConnectorWorkItem { private static final QueryMetadataInterface EXAMPLE_BQT = RealMetadataFactory.exampleBQTCached(); private static Command helpGetCommand(String sql, QueryMetadataInterface metadata) throws Exception { Command command = QueryParser.getQueryParser().parseCommand(sql); QueryResolver.resolveCommand(command, metadata); return command; } static AtomicRequestMessage createNewAtomicRequestMessage(int requestid, int nodeid) throws Exception { RequestMessage rm = new RequestMessage(); DQPWorkContext workContext = RealMetadataFactory.buildWorkContext(EXAMPLE_BQT, RealMetadataFactory.exampleBQTVDB()); workContext.getSession().setSessionId(String.valueOf(1)); workContext.getSession().setUserName("foo"); // $NON-NLS-1$ AtomicRequestMessage request = new AtomicRequestMessage(rm, workContext, nodeid); request.setCommand( helpGetCommand("SELECT BQT1.SmallA.INTKEY FROM BQT1.SmallA", EXAMPLE_BQT)); // $NON-NLS-1$ request.setRequestID(new RequestID(requestid)); request.setConnectorName("testing"); // $NON-NLS-1$ request.setFetchSize(5); request.setCommandContext(new CommandContext()); return request; } @Test public void testProcedureBatching() throws Exception { ProcedureExecution exec = new FakeProcedureExecution(2, 1); // this has two result set columns and 1 out parameter int total_columns = 3; StoredProcedure command = (StoredProcedure) helpGetCommand("{call pm2.spTest8(?)}", EXAMPLE_BQT); // $NON-NLS-1$ command.getInputParameters().get(0).setExpression(new Constant(1)); Call proc = new LanguageBridgeFactory(EXAMPLE_BQT).translate(command); ProcedureBatchHandler pbh = new ProcedureBatchHandler(proc, exec); assertEquals(total_columns, pbh.padRow(Arrays.asList(null, null)).size()); List params = pbh.getParameterRow(); assertEquals(total_columns, params.size()); // check the parameter value assertEquals(Integer.valueOf(0), params.get(2)); try { pbh.padRow(Arrays.asList(1)); fail("Expected exception from resultset mismatch"); // $NON-NLS-1$ } catch (TranslatorException err) { assertEquals( "TEIID30479 Could not process stored procedure results for EXEC spTest8(1). Expected 2 result set columns, but was 1. Please update your models to allow for stored procedure results batching.", err.getMessage()); // $NON-NLS-1$ } } @Test public void testUpdateExecution() throws Throwable { AtomicResultsMessage results = helpExecuteUpdate(false, true); assertEquals(Integer.valueOf(1), results.getResults()[0].get(0)); } private AtomicResultsMessage helpExecuteUpdate(boolean batch, boolean single) throws Exception, Throwable { Command command = helpGetCommand( "update bqt1.smalla set stringkey = 1 where stringkey = 2", EXAMPLE_BQT); // $NON-NLS-1$ if (batch) { command = new BatchedUpdateCommand(Arrays.asList(command, command)); } AtomicRequestMessage arm = createNewAtomicRequestMessage(1, 1); arm.setCommand(command); ConnectorManager connectorManager = TestConnectorManager.getConnectorManager(); ((FakeConnector) connectorManager.getExecutionFactory()).setReturnSingleUpdate(single); ConnectorWorkItem synchConnectorWorkItem = new ConnectorWorkItem(arm, connectorManager); synchConnectorWorkItem.execute(); return synchConnectorWorkItem.more(); } @Test public void testBatchUpdateExecution() throws Throwable { AtomicResultsMessage results = helpExecuteUpdate(true, false); assertEquals(2, results.getResults().length); assertEquals(Integer.valueOf(1), results.getResults()[0].get(0)); assertEquals(1, results.getResults()[1].get(0)); } @Test public void testBatchUpdateExecutionSingleResult() throws Throwable { AtomicResultsMessage results = helpExecuteUpdate(true, true); assertEquals(2, results.getResults().length); assertEquals(Integer.valueOf(1), results.getResults()[0].get(0)); assertEquals(1, results.getResults()[1].get(0)); } @Test public void testExecutionWarning() throws Throwable { AtomicResultsMessage results = helpExecuteUpdate(false, false); assertEquals(1, results.getWarnings().size()); } @Test public void testSourceNotRequired() throws Exception { Command command = helpGetCommand( "update bqt1.smalla set stringkey = 1 where stringkey = 2", EXAMPLE_BQT); // $NON-NLS-1$ AtomicRequestMessage arm = createNewAtomicRequestMessage(1, 1); arm.setCommand(command); ConnectorManager cm = TestConnectorManager.getConnectorManager(); cm.getExecutionFactory().setSourceRequired(false); ConnectorWork synchConnectorWorkItem = cm.registerRequest(arm); synchConnectorWorkItem.execute(); synchConnectorWorkItem.close(); FakeConnector fc = (FakeConnector) cm.getExecutionFactory(); assertEquals(1, fc.getConnectionCount()); assertEquals(1, fc.getCloseCount()); } @Test public void testConvertIn() throws Exception { Command command = helpGetCommand( "select intkey from bqt1.smalla where stringkey in ('1', '2')", EXAMPLE_BQT); //$NON-NLS-1$ AtomicRequestMessage arm = createNewAtomicRequestMessage(1, 1); arm.setCommand(command); ConnectorManager cm = TestConnectorManager.getConnectorManager(); cm.getExecutionFactory().setSourceRequired(false); ConnectorWork synchConnectorWorkItem = cm.registerRequest(arm); synchConnectorWorkItem.execute(); synchConnectorWorkItem.close(); FakeConnector fc = (FakeConnector) cm.getExecutionFactory(); assertEquals( "SELECT SmallA.IntKey FROM SmallA WHERE SmallA.StringKey = '2' OR SmallA.StringKey = '1'", fc.getCommands().get(0).toString()); assertEquals(1, fc.getConnectionCount()); assertEquals(1, fc.getCloseCount()); } @Ignore @Test public void testIsImmutablePropertySucceeds() throws Exception { /* * Setup: * 1. requestMsg.isTransactional() must be TRUE * 2. manager.isXa() must be FALSE () * 3. command must NOT be a SELECT * 4. Then, set isImmutable to TRUE, we should SUCCEED */ ConnectorManager cm = TestConnectorManager.getConnectorManager(); ((FakeConnector) cm.getExecutionFactory()).setImmutable(true); // command must not be a SELECT Command command = helpGetCommand( "update bqt1.smalla set stringkey = 1 where stringkey = 2", EXAMPLE_BQT); // $NON-NLS-1$ AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1); requestMsg.setCommand(command); // To make the AtomicRequestMessage transactional, construct your own requestMsg.setTransactionContext( new TransactionContext() { @Override public Xid getXid() { return Mockito.mock(Xid.class); } }); new ConnectorWorkItem(requestMsg, cm); } @Ignore @Test(expected = TranslatorException.class) public void testIsImmutablePropertyFails() throws Exception { /* * Setup: * 1. requestMsg.isTransactional() must be TRUE * 2. manager.isXa() must be FALSE () * 3. command must NOT be a SELECT * 4. Then, set isImmutable to FALSE, and we should FAIL */ ConnectorManager cm = TestConnectorManager.getConnectorManager(); ((FakeConnector) cm.getExecutionFactory()).setImmutable(false); // command must not be a SELECT Command command = helpGetCommand( "update bqt1.smalla set stringkey = 1 where stringkey = 2", EXAMPLE_BQT); // $NON-NLS-1$ AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1); requestMsg.setCommand(command); // To make the AtomicRequestMessage transactional, construct your own requestMsg.setTransactionContext( new TransactionContext() { @Override public Xid getXid() { return Mockito.mock(Xid.class); } }); new ConnectorWorkItem(requestMsg, cm); } @Test public void testTypeConversion() throws Exception { BufferManager bm = BufferManagerFactory.getStandaloneBufferManager(); String str = "hello world"; Object source = new StreamSource(new StringReader(str)); XMLType xml = (XMLType) ConnectorWorkItem.convertToRuntimeType( bm, source, DataTypeManager.DefaultDataClasses.XML, null); assertEquals(str, xml.getString()); source = new StAXSource(XMLType.getXmlInputFactory().createXMLEventReader(new StringReader("<a/>"))); xml = (XMLType) ConnectorWorkItem.convertToRuntimeType( bm, source, DataTypeManager.DefaultDataClasses.XML, null); XMLInputFactory in = XMLType.getXmlInputFactory(); XMLStreamReader reader = in.createXMLStreamReader(new StringReader(xml.getString())); assertEquals(XMLEvent.START_DOCUMENT, reader.getEventType()); assertEquals(XMLEvent.START_ELEMENT, reader.next()); assertEquals("a", reader.getLocalName()); assertEquals(XMLEvent.END_ELEMENT, reader.next()); byte[] bytes = str.getBytes(Streamable.ENCODING); source = new InputStreamFactory.BlobInputStreamFactory(BlobType.createBlob(bytes)); BlobType blob = (BlobType) ConnectorWorkItem.convertToRuntimeType( bm, source, DataTypeManager.DefaultDataClasses.BLOB, null); assertArrayEquals(bytes, ObjectConverterUtil.convertToByteArray(blob.getBinaryStream())); } @Test public void testLobs() 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 { return new ResultSetExecution() { private boolean returned; @Override public void execute() throws TranslatorException {} @Override public void close() {} @Override public void cancel() throws TranslatorException {} @Override public List<?> next() throws TranslatorException, DataNotAvailableException { if (returned) { return null; } returned = true; return Arrays.asList(AutoGenDataService.CLOB_VAL); } }; } }; ConnectorManager cm = new ConnectorManager("FakeConnector", "FakeConnector") { // $NON-NLS-1$ //$NON-NLS-2$ public ExecutionFactory getExecutionFactory() { return ef; } public Object getConnectionFactory() { return null; } }; cm.start(); ef.setCopyLobs(true); AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1); requestMsg.setCommand( helpGetCommand("SELECT CLOB_COLUMN FROM LOB_TESTING_ONE", EXAMPLE_BQT)); // $NON-NLS-1$ requestMsg.setBufferManager(bm); ConnectorWorkItem cwi = new ConnectorWorkItem(requestMsg, cm); cwi.execute(); AtomicResultsMessage message = cwi.more(); List[] resutls = message.getResults(); List<?> tuple = resutls[0]; ClobType clob = (ClobType) tuple.get(0); assertEquals(StorageMode.MEMORY, InputStreamFactory.getStorageMode(clob)); assertTrue(message.supportsImplicitClose()); ef.setCopyLobs(false); cwi = new ConnectorWorkItem(requestMsg, cm); cwi.execute(); message = cwi.more(); resutls = message.getResults(); tuple = resutls[0]; clob = (ClobType) tuple.get(0); assertEquals(StorageMode.OTHER, InputStreamFactory.getStorageMode(clob)); assertFalse(message.supportsImplicitClose()); } @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 ConnectorManager("FakeConnector", "FakeConnector") { // $NON-NLS-1$ //$NON-NLS-2$ public ExecutionFactory getExecutionFactory() { return ef; } public Object getConnectionFactory() { return null; } }; cm.start(); ef.setCopyLobs(true); AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1); requestMsg.setCommand( helpGetCommand("SELECT intkey FROM bqt1.smalla", EXAMPLE_BQT)); // $NON-NLS-1$ 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 } } @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 ConnectorManager("FakeConnector", "FakeConnector") { // $NON-NLS-1$ //$NON-NLS-2$ public ExecutionFactory getExecutionFactory() { return ef; } public Object getConnectionFactory() { return null; } }; cm.start(); AtomicRequestMessage requestMsg = createNewAtomicRequestMessage(1, 1); requestMsg.setCommand( helpGetCommand("SELECT intkey FROM bqt1.smalla", EXAMPLE_BQT)); // $NON-NLS-1$ 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()); } @Test public void testSourcHints() throws Exception { Command command = helpGetCommand( "update bqt1.smalla set stringkey = 1 where stringkey = 2", EXAMPLE_BQT); // $NON-NLS-1$ command.setSourceHint(new SourceHint()); AtomicRequestMessage arm = createNewAtomicRequestMessage(1, 1); arm.setCommand(command); ConnectorManager cm = TestConnectorManager.getConnectorManager(); cm.registerRequest(arm); } }