use of org.teiid.query.unittest.RealMetadataFactory.DDLHolder in project teiid by teiid.
the class TestODataSQLBuilder method setup.
private BaseState setup(String ddl, String url, String method, ServletInputStream stream, BaseState state) throws Exception {
Client client = Mockito.mock(Client.class);
DDLHolder model = new DDLHolder("PM1", ddl);
TransformationMetadata metadata = RealMetadataFactory.fromDDL("vdb", model);
MetadataStore store = metadata.getMetadataStore();
// TranslationUtility utility = new TranslationUtility(metadata);
OData odata = OData4Impl.newInstance();
org.teiid.metadata.Schema teiidSchema = store.getSchema("PM1");
CsdlSchema schema = ODataSchemaBuilder.buildMetadata("vdb", teiidSchema);
TeiidEdmProvider edmProvider = new TeiidEdmProvider("baseuri", schema, "x");
ServiceMetadata serviceMetadata = odata.createServiceMetadata(edmProvider, Collections.<EdmxReference>emptyList());
ODataHttpHandler handler = odata.createHandler(serviceMetadata);
Hashtable<String, String> headers = new Hashtable<String, String>();
headers.put("Content-Type", "application/json");
Mockito.stub(client.getMetadataStore()).toReturn(store);
Mockito.stub(client.executeCount(Mockito.any(Query.class), Mockito.anyListOf(SQLParameter.class))).toReturn(new CountResponse() {
@Override
public int getCount() {
return 10;
}
});
HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
Mockito.stub(request.getHeaderNames()).toReturn(headers.keys());
Mockito.stub(request.getHeaders("Content-Type")).toReturn(headers.elements());
Mockito.stub(request.getMethod()).toReturn(method);
String requestURL = url;
String queryString = "";
int idx = url.indexOf("?");
if (idx != -1) {
requestURL = url.substring(0, idx);
queryString = url.substring(idx + 1);
}
Mockito.stub(request.getRequestURL()).toReturn(new StringBuffer(requestURL));
Mockito.stub(request.getQueryString()).toReturn(queryString);
Mockito.stub(request.getServletPath()).toReturn("");
Mockito.stub(request.getContextPath()).toReturn("/odata4/vdb/PM1");
Mockito.stub(request.getInputStream()).toReturn(stream);
final StringBuffer sb = new StringBuffer();
ServletOutputStream out = new ServletOutputStream() {
@Override
public void write(int b) throws IOException {
sb.append((char) b);
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setWriteListener(WriteListener writeListener) {
}
};
HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
Mockito.stub(response.getOutputStream()).toReturn(out);
try {
TeiidServiceHandler tsh = new TeiidServiceHandler("PM1");
tsh.setPrepared(false);
TeiidServiceHandler.setClient(client);
handler.register(tsh);
handler.process(request, response);
} finally {
TeiidServiceHandler.setClient(null);
}
ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class);
Mockito.verify(response).setStatus(statusCapture.capture());
state.client = client;
state.response = sb.toString();
state.status = statusCapture.getValue();
return state;
}
use of org.teiid.query.unittest.RealMetadataFactory.DDLHolder in project teiid by teiid.
the class TestSubqueryPushdown method testPreEvaluationInAggregate1.
@Test
public void testPreEvaluationInAggregate1() throws Exception {
TransformationMetadata tm = RealMetadataFactory.fromDDL("x", new DDLHolder("my", "CREATE foreign TABLE test_b (b integer, c integer)"), new DDLHolder("pg", "CREATE foreign TABLE test_a (a integer, b integer); CREATE foreign TABLE test_only_pg (a integer, b integer);"));
String sql = "SELECT SUM(x.b - (SELECT a FROM pg.test_only_pg WHERE b = 1)) FROM my.test_b x";
BasicSourceCapabilities bsc = getTypicalCapabilities();
bsc.setCapabilitySupport(Capability.QUERY_SUBQUERIES_SCALAR, true);
bsc.setCapabilitySupport(Capability.QUERY_SUBQUERIES_SCALAR_PROJECTION, true);
bsc.setCapabilitySupport(Capability.QUERY_GROUP_BY, true);
bsc.setCapabilitySupport(Capability.QUERY_AGGREGATES_SUM, true);
bsc.setFunctionSupport("-", true);
ProcessorPlan plan = // $NON-NLS-1$
TestOptimizer.helpPlan(// $NON-NLS-1$
sql, tm, null, new DefaultCapabilitiesFinder(bsc), new String[] { "SELECT SUM((g_0.b - (SELECT a FROM pg.test_only_pg WHERE b = 1 LIMIT 2))) FROM my.test_b AS g_0" }, // $NON-NLS-1$
ComparisonMode.EXACT_COMMAND_STRING);
HardcodedDataManager hdm = new HardcodedDataManager(tm);
hdm.addData("SELECT g_0.a FROM test_only_pg AS g_0 WHERE g_0.b = 1", Arrays.asList(2));
hdm.addData("SELECT SUM((g_0.b - 2)) FROM test_b AS g_0", Arrays.asList(Long.valueOf(3)));
CommandContext cc = TestProcessor.createCommandContext();
cc.setMetadata(tm);
TestProcessor.helpProcess(plan, cc, hdm, new List[] { Arrays.asList(Long.valueOf(3)) });
}
use of org.teiid.query.unittest.RealMetadataFactory.DDLHolder in project teiid by teiid.
the class TestFunctionResolving method testAmbiguousUDF.
@Test
public void testAmbiguousUDF() throws Exception {
TransformationMetadata tm = RealMetadataFactory.fromDDL("x", new DDLHolder("y", "create foreign function f () returns string"), new DDLHolder("z", "create foreign function f () returns string"));
String sql = "f()";
Function func = (Function) QueryParser.getQueryParser().parseExpression(sql);
try {
ResolverVisitor.resolveLanguageObject(func, tm);
fail();
} catch (QueryResolverException e) {
}
sql = "z.f()";
func = (Function) QueryParser.getQueryParser().parseExpression(sql);
ResolverVisitor.resolveLanguageObject(func, tm);
}
use of org.teiid.query.unittest.RealMetadataFactory.DDLHolder in project teiid by teiid.
the class TestFunctionPushdown method testSimpleFunctionPushdown2.
@Test
public void testSimpleFunctionPushdown2() throws Exception {
TransformationMetadata tm = RealMetadataFactory.fromDDL("x", new DDLHolder("y", "CREATE FOREIGN FUNCTION func(a object, b object) RETURNS string;"), new DDLHolder("z", "CREATE FOREIGN FUNCTION func1(a object, b object) RETURNS string; create foreign table g1 (e1 object)"));
BasicSourceCapabilities bsc = new BasicSourceCapabilities();
bsc.setCapabilitySupport(Capability.SELECT_WITHOUT_FROM, true);
bsc.setCapabilitySupport(Capability.CRITERIA_COMPARE_EQ, true);
final DefaultCapabilitiesFinder capFinder = new DefaultCapabilitiesFinder(bsc);
CommandContext cc = TestProcessor.createCommandContext();
cc.setQueryProcessorFactory(new QueryProcessor.ProcessorFactory() {
@Override
public PreparedPlan getPreparedPlan(String query, String recursionGroup, CommandContext commandContext, QueryMetadataInterface metadata) throws TeiidProcessingException, TeiidComponentException {
return null;
}
@Override
public CapabilitiesFinder getCapabiltiesFinder() {
return capFinder;
}
@Override
public QueryProcessor createQueryProcessor(String query, String recursionGroup, CommandContext commandContext, Object... params) throws TeiidProcessingException, TeiidComponentException {
// TODO Auto-generated method stub
return null;
}
});
cc.setMetadata(tm);
// $NON-NLS-1$
String sql = "select e1 from g1 where func(1, 1) = '2'";
ProcessorPlan plan = helpPlan(sql, tm, null, capFinder, new String[] { "SELECT z.g1.e1 FROM z.g1 WHERE func(1, 1) = '2'" }, // $NON-NLS-1$
ComparisonMode.EXACT_COMMAND_STRING);
HardcodedDataManager dataManager = new HardcodedDataManager(tm);
dataManager.addData("SELECT func(1, 1)", new List[] { Arrays.asList("hello world") });
TestProcessor.helpProcess(plan, cc, dataManager, new List[] {});
// $NON-NLS-1$
sql = "select e1 from g1 where func1(1, 1) = '2'";
plan = helpPlan(sql, tm, null, capFinder, new String[] { "SELECT z.g1.e1 FROM z.g1 WHERE func1(1, 1) = '2'" }, // $NON-NLS-1$
ComparisonMode.EXACT_COMMAND_STRING);
dataManager = new HardcodedDataManager(tm);
dataManager.addData("SELECT g1.e1 FROM g1 WHERE func1(1, 1) = '2'", new List[] { Arrays.asList("hello world") });
TestProcessor.helpProcess(plan, cc, dataManager, new List[] { Arrays.asList("hello world") });
}
use of org.teiid.query.unittest.RealMetadataFactory.DDLHolder in project teiid by teiid.
the class TestSubqueryPushdown method testPreEvaluationInAggregate.
@Test
public void testPreEvaluationInAggregate() throws Exception {
TransformationMetadata tm = RealMetadataFactory.fromDDL("x", new DDLHolder("my", "CREATE foreign TABLE test_b (b integer, c integer)"), new DDLHolder("pg", "CREATE foreign TABLE test_a (a integer, b integer); CREATE foreign TABLE test_only_pg (a integer, b integer);"));
String sql = "SELECT SUM(x.b - (SELECT a FROM pg.test_only_pg WHERE b = 1)) FROM my.test_b x INNER JOIN pg.test_a y ON x.b = y.b";
BasicSourceCapabilities bsc = getTypicalCapabilities();
bsc.setCapabilitySupport(Capability.QUERY_SUBQUERIES_SCALAR, true);
bsc.setCapabilitySupport(Capability.QUERY_SUBQUERIES_SCALAR_PROJECTION, true);
bsc.setCapabilitySupport(Capability.QUERY_GROUP_BY, true);
bsc.setCapabilitySupport(Capability.QUERY_AGGREGATES_SUM, true);
bsc.setFunctionSupport("-", true);
ProcessorPlan plan = // $NON-NLS-1$
TestOptimizer.helpPlan(// $NON-NLS-1$
sql, tm, null, new DefaultCapabilitiesFinder(bsc), new String[] { "SELECT g_0.b AS c_0, SUM((g_0.b - (SELECT a FROM pg.test_only_pg WHERE b = 1 LIMIT 2))) AS c_1 FROM my.test_b AS g_0 GROUP BY g_0.b ORDER BY c_0", "SELECT g_0.b AS c_0 FROM pg.test_a AS g_0 ORDER BY c_0" }, // $NON-NLS-1$
ComparisonMode.EXACT_COMMAND_STRING);
HardcodedDataManager hdm = new HardcodedDataManager(tm);
hdm.addData("SELECT g_0.a FROM test_only_pg AS g_0 WHERE g_0.b = 1", Arrays.asList(2));
hdm.addData("SELECT g_0.b AS c_0, SUM((g_0.b - 2)) AS c_1 FROM test_b AS g_0 GROUP BY g_0.b ORDER BY c_0", Arrays.asList(3, 1));
hdm.addData("SELECT g_0.b AS c_0 FROM test_a AS g_0 ORDER BY c_0", Arrays.asList(3));
CommandContext cc = TestProcessor.createCommandContext();
cc.setMetadata(tm);
TestProcessor.helpProcess(plan, cc, hdm, new List[] { Arrays.asList(Long.valueOf(1)) });
}
Aggregations