Search in sources :

Example 1 with DDLHolder

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;
}
Also used : Query(org.teiid.query.sql.lang.Query) ServletOutputStream(javax.servlet.ServletOutputStream) CsdlSchema(org.apache.olingo.commons.api.edm.provider.CsdlSchema) HttpServletRequest(javax.servlet.http.HttpServletRequest) TeiidEdmProvider(org.teiid.olingo.service.TeiidEdmProvider) DDLHolder(org.teiid.query.unittest.RealMetadataFactory.DDLHolder) Client(org.teiid.odata.api.Client) WriteListener(javax.servlet.WriteListener) OData(org.apache.olingo.server.api.OData) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Hashtable(java.util.Hashtable) SQLParameter(org.teiid.odata.api.SQLParameter) CountResponse(org.teiid.odata.api.CountResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ODataHttpHandler(org.apache.olingo.server.api.ODataHttpHandler) MetadataStore(org.teiid.metadata.MetadataStore) TeiidServiceHandler(org.teiid.olingo.service.TeiidServiceHandler) ServiceMetadata(org.apache.olingo.server.api.ServiceMetadata)

Example 2 with DDLHolder

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)) });
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) HardcodedDataManager(org.teiid.query.processor.HardcodedDataManager) DDLHolder(org.teiid.query.unittest.RealMetadataFactory.DDLHolder) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Example 3 with DDLHolder

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);
}
Also used : Function(org.teiid.query.sql.symbol.Function) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) DDLHolder(org.teiid.query.unittest.RealMetadataFactory.DDLHolder) QueryResolverException(org.teiid.api.exception.query.QueryResolverException) Test(org.junit.Test)

Example 4 with DDLHolder

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") });
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) TeiidProcessingException(org.teiid.core.TeiidProcessingException) FakeCapabilitiesFinder(org.teiid.query.optimizer.capabilities.FakeCapabilitiesFinder) CapabilitiesFinder(org.teiid.query.optimizer.capabilities.CapabilitiesFinder) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) PreparedPlan(org.teiid.dqp.internal.process.PreparedPlan) DDLHolder(org.teiid.query.unittest.RealMetadataFactory.DDLHolder) TeiidComponentException(org.teiid.core.TeiidComponentException) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) Test(org.junit.Test)

Example 5 with DDLHolder

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)) });
}
Also used : TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) CommandContext(org.teiid.query.util.CommandContext) BasicSourceCapabilities(org.teiid.query.optimizer.capabilities.BasicSourceCapabilities) HardcodedDataManager(org.teiid.query.processor.HardcodedDataManager) DDLHolder(org.teiid.query.unittest.RealMetadataFactory.DDLHolder) ProcessorPlan(org.teiid.query.processor.ProcessorPlan) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Test(org.junit.Test)

Aggregations

DDLHolder (org.teiid.query.unittest.RealMetadataFactory.DDLHolder)6 Test (org.junit.Test)5 TransformationMetadata (org.teiid.query.metadata.TransformationMetadata)5 BasicSourceCapabilities (org.teiid.query.optimizer.capabilities.BasicSourceCapabilities)3 DefaultCapabilitiesFinder (org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder)3 CommandContext (org.teiid.query.util.CommandContext)3 QueryResolverException (org.teiid.api.exception.query.QueryResolverException)2 QueryMetadataInterface (org.teiid.query.metadata.QueryMetadataInterface)2 HardcodedDataManager (org.teiid.query.processor.HardcodedDataManager)2 ProcessorPlan (org.teiid.query.processor.ProcessorPlan)2 Function (org.teiid.query.sql.symbol.Function)2 Hashtable (java.util.Hashtable)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 WriteListener (javax.servlet.WriteListener)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CsdlSchema (org.apache.olingo.commons.api.edm.provider.CsdlSchema)1 OData (org.apache.olingo.server.api.OData)1 ODataHttpHandler (org.apache.olingo.server.api.ODataHttpHandler)1 ServiceMetadata (org.apache.olingo.server.api.ServiceMetadata)1