Search in sources :

Example 71 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class CountStarJUnitTest method testCountStarQueriesOnReplicatedRegion.

/**
   * Test on Replicated Region data
   */
@Test
public void testCountStarQueriesOnReplicatedRegion() throws Exception {
    Cache cache = CacheUtils.getCache();
    createReplicatedRegion();
    assertNotNull(cache.getRegion(regionName));
    assertEquals(numElem, cache.getRegion(regionName).size());
    QueryService queryService = cache.getQueryService();
    Query query1 = null;
    Query query2 = null;
    for (String queryStr : countStarQueries.keySet()) {
        query1 = queryService.newQuery(queryStr);
        query2 = queryService.newQuery(queryStr.replace("COUNT(*)", "*"));
        SelectResults result1 = (SelectResults) query1.execute();
        SelectResults result2 = (SelectResults) query2.execute();
        assertEquals(queryStr, 1, result1.size());
        assertTrue(result1.asList().get(0) instanceof Integer);
        int count = ((Integer) result1.asList().get(0)).intValue();
        // Also verify with size of result2 to count
        assertEquals("COUNT(*) query result is wrong for query: " + queryStr, result2.size(), count);
        assertEquals("Query: " + queryStr, countStarQueries.get(queryStr).intValue(), count);
    }
    // Destroy current Region for other tests
    cache.getRegion(regionName).destroyRegion();
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 72 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class CustomerOptimizationsJUnitTest method testProjectionEvaluationOnORJunction_NOT_IMPLEMENTED.

// ideally rojection should have been evaluated while collecting index results
@Ignore
@Test
public void testProjectionEvaluationOnORJunction_NOT_IMPLEMENTED() throws Exception {
    QueryService qs = CacheUtils.getQueryService();
    Region rgn = CacheUtils.getRegion("/pos");
    for (int i = 100; i < 10000; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.setCreateTime(10l);
        rgn.put("" + i, pf);
    }
    String[] queries = new String[] { "select  distinct p.status  from /pos p  where  p.createTime IN SET( 10l ) OR  p.status IN SET( 'active') OR p.ID >  0" };
    SelectResults[][] sr = new SelectResults[queries.length][2];
    for (int i = 0; i < queries.length; ++i) {
        Query q = qs.newQuery(queries[i]);
        sr[i][0] = (SelectResults) q.execute();
    }
    final List indexUsed = new ArrayList();
    qs.createIndex("PortFolioID", IndexType.FUNCTIONAL, "ID", "/pos");
    qs.createIndex("CreateTime", IndexType.FUNCTIONAL, "createTime", "/pos");
    qs.createIndex("Status", IndexType.FUNCTIONAL, "status", "/pos");
    qs.createIndex("Type", IndexType.FUNCTIONAL, "\"type\"", "/pos");
    final boolean[] expectedIndexUsed = new boolean[] { true };
    final boolean[] actualIndexUsed = new boolean[] { false };
    final boolean[] expectedProjectionCallabck = new boolean[] { false };
    final boolean[] actualProjectionCallback = new boolean[] { false };
    final boolean[] expectedUnionCallback = { false };
    final boolean[] actualUnionCallback = new boolean[queries.length];
    final boolean[] expectedIntersectionCallback = { false };
    final boolean[] actualIntersectionCallback = new boolean[queries.length];
    ObjectType[] expectedTypes = new ObjectType[] { new ObjectTypeImpl(String.class) };
    QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private int i = 0;

        public void invokedQueryUtilsUnion(SelectResults r1, SelectResults r2) {
            actualUnionCallback[i] = true;
        }

        public void invokedQueryUtilsIntersection(SelectResults r1, SelectResults r2) {
            actualIntersectionCallback[i] = true;
        }

        public void beforeIndexLookup(Index index, int oper, Object key) {
            actualIndexUsed[i] = true;
            indexUsed.add(index);
        }

        public void beforeApplyingProjectionOnFilterEvaluatedResults(Object preProjectionApplied) {
            actualProjectionCallback[i] = true;
        }

        public void afterQueryEvaluation(Object result) {
            ++i;
        }
    });
    for (int i = 0; i < queries.length; ++i) {
        Query q = qs.newQuery(queries[i]);
        sr[i][1] = (SelectResults) q.execute();
        assertEquals(expectedUnionCallback[i], actualUnionCallback[i]);
        assertEquals(expectedTypes[i], sr[i][1].getCollectionType().getElementType());
        assertEquals(expectedIndexUsed[i], actualIndexUsed[i]);
        assertEquals(expectedIntersectionCallback[i], actualIntersectionCallback[i]);
        assertEquals(expectedProjectionCallabck[i], actualProjectionCallback[i]);
    }
    assertEquals(indexUsed.size(), 3);
    assertEquals(((Index) indexUsed.iterator().next()).getName(), "CreateTime");
    CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Also used : Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) ArrayList(java.util.ArrayList) ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) Index(org.apache.geode.cache.query.Index) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Region(org.apache.geode.cache.Region) ArrayList(java.util.ArrayList) List(java.util.List) Ignore(org.junit.Ignore) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 73 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class CountStarJUnitTest method testCountStarOnCollection.

@Test
public void testCountStarOnCollection() throws Exception {
    String collection = "$1";
    HashMap<String, Integer> countStarQueriesWithParms = new HashMap<String, Integer>();
    countStarQueriesWithParms.put("select COUNT(*) from " + collection, 100);
    countStarQueriesWithParms.put("select COUNT(*) from " + collection + " where ID > 0", 100);
    countStarQueriesWithParms.put("select COUNT(*) from " + collection + " where ID < 0", 0);
    countStarQueriesWithParms.put("select COUNT(*) from " + collection + " where ID > 0 LIMIT 50", 50);
    countStarQueriesWithParms.put("select COUNT(*) from " + collection + " where ID > 0 AND status='active'", 50);
    countStarQueriesWithParms.put("select COUNT(*) from " + collection + " where ID > 0 OR status='active'", 100);
    countStarQueriesWithParms.put("select COUNT(*) from " + collection + " where ID > 0 AND status LIKE 'act%'", 50);
    countStarQueriesWithParms.put("select COUNT(*) from " + collection + " where ID > 0 OR status LIKE 'ina%'", 100);
    countStarQueriesWithParms.put("select COUNT(*) from " + collection + " where ID IN SET(1, 2, 3, 4, 5)", 5);
    countStarQueriesWithParms.put("select COUNT(*) from " + collection + " where NOT (ID > 5)", 5);
    Cache cache = CacheUtils.getCache();
    QueryService queryService = cache.getQueryService();
    // Run queries
    Query query1 = null;
    Query query2 = null;
    // Create a collection
    ArrayList<Portfolio> portfolios = new ArrayList<Portfolio>(100);
    for (int i = 1; i <= 100; i++) {
        portfolios.add(new Portfolio(i, i));
    }
    // Without Indexes
    for (String queryStr : countStarQueriesWithParms.keySet()) {
        query1 = queryService.newQuery(queryStr);
        query2 = queryService.newQuery(queryStr.replace("COUNT(*)", "*"));
        SelectResults result1 = (SelectResults) query1.execute(new Object[] { portfolios });
        SelectResults result2 = (SelectResults) query2.execute(new Object[] { portfolios });
        assertEquals(queryStr, 1, result1.size());
        assertTrue(result1.asList().get(0) instanceof Integer);
        int count = ((Integer) result1.asList().get(0)).intValue();
        // Also verify with size of result2 to count
        assertEquals("COUNT(*) query result is wrong for query: " + queryStr, result2.size(), count);
        assertEquals("Query: " + queryStr, countStarQueriesWithParms.get(queryStr).intValue(), count);
    }
}
Also used : Query(org.apache.geode.cache.query.Query) HashMap(java.util.HashMap) Portfolio(org.apache.geode.cache.query.data.Portfolio) ArrayList(java.util.ArrayList) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 74 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class CustomerOptimizationsJUnitTest method testProjectionEvaluationDuringIndexResultsWithComplexWhereClause_UNIMPLEMENTED_1.

@Ignore
@Test
public void testProjectionEvaluationDuringIndexResultsWithComplexWhereClause_UNIMPLEMENTED_1() throws QueryException {
    QueryService qs = CacheUtils.getQueryService();
    Region rgn = CacheUtils.getRegion("/pos");
    for (int i = 100; i < 200; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.setCreateTime(10l);
        rgn.put("" + i, pf);
    }
    String[] queries = new String[] { "select  p.status as sts, p as pos from /pos p  where   (p.ID IN  SET( 0,1,2,3,4,5,101,102,103,104,105) AND p.createTime > 9l) OR (p.ID IN  SET( 20,30,110,120) AND p.createTime > 7l)" };
    SelectResults[][] sr = new SelectResults[queries.length][2];
    for (int i = 0; i < queries.length; ++i) {
        Query q = qs.newQuery(queries[i]);
        sr[i][0] = (SelectResults) q.execute();
    }
    qs.createIndex("PortFolioID", IndexType.FUNCTIONAL, "ID", "/pos");
    // qs.createIndex("CreateTime", IndexType.FUNCTIONAL,"createTime", "/pos");
    final boolean[] expectedIndexUsed = new boolean[] { true };
    final boolean[] actualIndexUsed = new boolean[] { false };
    final boolean[] expectedProjectionCallabck = new boolean[] { false };
    final boolean[] actualProjectionCallback = new boolean[] { false };
    final boolean[] expectedUnionCallback = { true };
    final boolean[] actualUnionCallback = new boolean[queries.length];
    final boolean[] expectedIntersectionCallback = { false };
    final boolean[] actualIntersectionCallback = new boolean[queries.length];
    ObjectType[] expectedTypes = new ObjectType[] { new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }) };
    QueryObserverHolder.setInstance(new QueryObserverAdapter() {

        private int i = 0;

        public void invokedQueryUtilsUnion(SelectResults r1, SelectResults r2) {
            actualUnionCallback[i] = true;
        }

        public void invokedQueryUtilsIntersection(SelectResults r1, SelectResults r2) {
            actualIntersectionCallback[i] = true;
        }

        public void beforeIndexLookup(Index index, int oper, Object key) {
            actualIndexUsed[i] = true;
        }

        public void beforeApplyingProjectionOnFilterEvaluatedResults(Object preProjectionApplied) {
            actualProjectionCallback[i] = true;
        }

        public void afterQueryEvaluation(Object result) {
            ++i;
        }
    });
    for (int i = 0; i < queries.length; ++i) {
        Query q = qs.newQuery(queries[i]);
        sr[i][1] = (SelectResults) q.execute();
        assertEquals(expectedUnionCallback[i], actualUnionCallback[i]);
        assertEquals(expectedTypes[i], sr[i][1].getCollectionType().getElementType());
        assertEquals(expectedIndexUsed[i], actualIndexUsed[i]);
        assertEquals(expectedIntersectionCallback[i], actualIntersectionCallback[i]);
        assertEquals(expectedProjectionCallabck[i], actualProjectionCallback[i]);
    }
    CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Also used : Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) Index(org.apache.geode.cache.query.Index) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) Region(org.apache.geode.cache.Region) Ignore(org.junit.Ignore) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 75 with QueryService

use of org.apache.geode.cache.query.QueryService in project geode by apache.

the class CountStarJUnitTest method testEquiJoinCountStarQueries.

@Test
public void testEquiJoinCountStarQueries() throws Exception {
    Cache cache = CacheUtils.getCache();
    createLocalRegion();
    createSecondLocalRegion();
    assertNotNull(cache.getRegion(regionName));
    assertNotNull(cache.getRegion(exampleRegionName));
    assertEquals(numElem, cache.getRegion(regionName).size());
    assertEquals(numElem, cache.getRegion(exampleRegionName).size());
    QueryService queryService = cache.getQueryService();
    // Run queries
    Query query1 = null;
    Query query2 = null;
    // Without Indexes
    for (String queryStr : countStarQueriesWithEquiJoins) {
        query1 = queryService.newQuery(queryStr);
        query2 = queryService.newQuery(queryStr.replace("COUNT(*)", "*"));
        SelectResults result1 = (SelectResults) query1.execute();
        SelectResults result2 = (SelectResults) query2.execute();
        assertEquals(queryStr, 1, result1.size());
        assertTrue(result1.asList().get(0) instanceof Integer);
        int count = ((Integer) result1.asList().get(0)).intValue();
        // Also verify with size of result2 to count
        assertEquals("COUNT(*) query result is wrong for query: " + queryStr, result2.size(), count);
    // assertIndexDetailsEquals("Query: "+ queryStr, countStarQueries.get(queryStr).intValue(),
    // count);
    }
    // CReate Index on status and ID
    queryService.createIndex("sampleIndex-1", IndexType.FUNCTIONAL, "p.ID", "/" + regionName + " p");
    queryService.createIndex("sampleIndex-2", IndexType.FUNCTIONAL, "p.status", "/" + regionName + " p");
    queryService.createIndex("sampleIndex-3", IndexType.FUNCTIONAL, "e.ID", "/" + exampleRegionName + " e");
    queryService.createIndex("sampleIndex-4", IndexType.FUNCTIONAL, "e.status", "/" + exampleRegionName + " e");
    Region region = cache.getRegion(regionName);
    Region region2 = cache.getRegion(exampleRegionName);
    // Verify Index Creation
    assertNotNull(queryService.getIndex(region, "sampleIndex-1"));
    assertNotNull(queryService.getIndex(region, "sampleIndex-2"));
    assertNotNull(queryService.getIndex(region2, "sampleIndex-3"));
    assertNotNull(queryService.getIndex(region2, "sampleIndex-4"));
    assertEquals(4, queryService.getIndexes().size());
    // With Indexes
    for (String queryStr : countStarQueriesWithEquiJoins) {
        query1 = queryService.newQuery(queryStr);
        query2 = queryService.newQuery(queryStr.replace("COUNT(*)", "*"));
        SelectResults result1 = (SelectResults) query1.execute();
        SelectResults result2 = (SelectResults) query2.execute();
        assertEquals(queryStr, 1, result1.size());
        assertTrue(result1.asList().get(0) instanceof Integer);
        int count = ((Integer) result1.asList().get(0)).intValue();
        // Also verify with size of result2 to count
        assertEquals("COUNT(*) query result is wrong for query with indexes: " + queryStr, result2.size(), count);
    // assertIndexDetailsEquals("Query: "+ queryStr, countStarQueries.get(queryStr).intValue(),
    // count);
    }
    // Destroy current Region for other tests
    region.destroyRegion();
    region2.destroyRegion();
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

QueryService (org.apache.geode.cache.query.QueryService)532 Test (org.junit.Test)383 SelectResults (org.apache.geode.cache.query.SelectResults)331 Region (org.apache.geode.cache.Region)316 Query (org.apache.geode.cache.query.Query)316 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)215 Portfolio (org.apache.geode.cache.query.data.Portfolio)199 CacheException (org.apache.geode.cache.CacheException)139 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)118 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)110 Index (org.apache.geode.cache.query.Index)109 Host (org.apache.geode.test.dunit.Host)104 VM (org.apache.geode.test.dunit.VM)104 Iterator (java.util.Iterator)102 Cache (org.apache.geode.cache.Cache)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)86 Struct (org.apache.geode.cache.query.Struct)70 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)68 IOException (java.io.IOException)67 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)65