Search in sources :

Example 76 with SelectResults

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForStructBagWithIndex.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for StructBag wrapped by a SelectResults if the iteration included
   * duplicate elements. If the distinct clause is present then duplicate elements even if
   * satisfying the where clause should not be considered as part of the resultset as distinct will
   * eliminate them
   * 
   * Tests StructBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForStructBagWithIndex() {
    try {
        Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
        // Add 5 pairs of same Object starting from 11 to 20
        for (int i = 11; i < 21; ) {
            region1.put(Integer.toString(i), new Portfolio(i));
            region1.put(Integer.toString(i + 1), new Portfolio(i));
            i += 2;
        }
        Query query;
        SelectResults result;
        MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
        QueryObserver old = QueryObserverHolder.setInstance(observer);
        Index index = qs.createIndex("idIndex", "pf.ID", "/portfolios1  pf, pf.collectionHolderMap.keySet");
        assertNotNull(index);
        String queryString = "SELECT DISTINCT * FROM /portfolios1  pf, pf.collectionHolderMap.keySet  WHERE pf.ID > 10 limit 20";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertTrue(result instanceof SelectResults);
        assertEquals(20, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(20, wrapper.asSet().size());
        // currently this is false because we disabled limit application at the range index level
        assertFalse(observer.limitAppliedAtIndex);
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Portfolio(org.apache.geode.cache.query.data.Portfolio) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 77 with SelectResults

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

the class LimitClauseJUnitTest method testLimitDistinctQueryWithTwoCondWithTwoIndex.

@Test
public void testLimitDistinctQueryWithTwoCondWithTwoIndex() {
    try {
        Query query;
        SelectResults result;
        Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
        for (int i = 1; i < 100; i++) {
            Portfolio p = new Portfolio(i);
            if (i < 50)
                p.status = "active";
            region.put(Integer.toString(i), p);
        }
        // Create Index on ID
        Index index = qs.createIndex("idIndex", "ID", "/portfolios1");
        assertNotNull(index);
        index = qs.createIndex("statusIndex", "status", "/portfolios1");
        assertNotNull(index);
        MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
        QueryObserver old = QueryObserverHolder.setInstance(observer);
        String queryString = "select DISTINCT * from /portfolios1 where status ='inactive' AND (ID > 0 AND ID < 100) limit 10";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertTrue(result instanceof SelectResults);
        assertEquals(10, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(10, wrapper.asSet().size());
        assertFalse(observer.limitAppliedAtIndex && observer.indexName.equals("idIndex"));
        assertTrue(observer.limitAppliedAtIndex && observer.indexName.equals("statusIndex"));
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Portfolio(org.apache.geode.cache.query.data.Portfolio) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 78 with SelectResults

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryForResultBag.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for ResultBag wrapped by a SelectResults
   * 
   * Tests ResultBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryForResultBag() {
    try {
        Query query;
        SelectResults result;
        String queryString = "SELECT DISTINCT * FROM /portfolios pf WHERE pf.ID > 0 limit 5";
        query = qs.newQuery(queryString);
        final int[] num = new int[1];
        num[0] = 0;
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterIterationEvaluation(Object result) {
                num[0] += 1;
            }
        });
        result = (SelectResults) query.execute();
        assertEquals(5, num[0]);
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, wrapper.asSet().size());
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 79 with SelectResults

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

the class LimitClauseJUnitTest method testLimitOnEqualsCompactRangeIndexedFieldWithAndClauseNonIndexedField.

@Test
public void testLimitOnEqualsCompactRangeIndexedFieldWithAndClauseNonIndexedField() throws Exception {
    Query query;
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 0; i <= 15; i++) {
        Portfolio p = new Portfolio(10);
        p.shortID = 1;
        p.positions.clear();
        p.positions.put("IBM", new Position("IBM", i));
        region.put("KEY" + i, p);
    }
    for (int i = 16; i < 21; i++) {
        Portfolio p = new Portfolio(10);
        p.shortID = 2;
        p.positions.clear();
        p.positions.put("VMW", new Position("VMW", i));
        region.put("KEY" + i, p);
    }
    MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
    QueryObserver old = QueryObserverHolder.setInstance(observer);
    // Create Index on ID
    Index idIndex = qs.createIndex("idIndex", "P.ID", "/portfolios1 P");
    String queryString = "SELECT * FROM /portfolios1 P WHERE P.ID = 10 AND P.shortID = 2 LIMIT 5";
    query = qs.newQuery(queryString);
    assertNotNull(idIndex);
    SelectResults resultsWithIndex = (SelectResults) query.execute();
    assertTrue(observer.limitAppliedAtIndex);
    assertEquals(5, resultsWithIndex.size());
}
Also used : QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) Query(org.apache.geode.cache.query.Query) Position(org.apache.geode.cache.query.data.Position) Portfolio(org.apache.geode.cache.query.data.Portfolio) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Index(org.apache.geode.cache.query.Index) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 80 with SelectResults

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

the class LimitClauseJUnitTest method testLikeWithLimitWithParameter.

@Test
public void testLikeWithLimitWithParameter() throws Exception {
    String queryString = "SELECT DISTINCT entry FROM $1 entry WHERE entry.key like $2 ORDER BY entry.key LIMIT $3 ";
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 0; i < 100; i++) {
        region.put("p" + i, new Portfolio(i));
    }
    Object[] params = new Object[3];
    params[0] = region.entrySet();
    params[1] = "p%";
    params[2] = 5;
    SelectResults results = (SelectResults) qs.newQuery(queryString).execute(params);
    assertEquals(5, results.size());
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Portfolio(org.apache.geode.cache.query.data.Portfolio) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

SelectResults (org.apache.geode.cache.query.SelectResults)577 Test (org.junit.Test)423 Query (org.apache.geode.cache.query.Query)360 Region (org.apache.geode.cache.Region)336 QueryService (org.apache.geode.cache.query.QueryService)331 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)256 Portfolio (org.apache.geode.cache.query.data.Portfolio)249 Index (org.apache.geode.cache.query.Index)133 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)112 Host (org.apache.geode.test.dunit.Host)107 VM (org.apache.geode.test.dunit.VM)107 CacheException (org.apache.geode.cache.CacheException)105 Iterator (java.util.Iterator)104 AttributesFactory (org.apache.geode.cache.AttributesFactory)101 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)89 Cache (org.apache.geode.cache.Cache)84 Struct (org.apache.geode.cache.query.Struct)80 LocalRegion (org.apache.geode.internal.cache.LocalRegion)67 ObjectType (org.apache.geode.cache.query.types.ObjectType)66