Search in sources :

Example 61 with SelectResults

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

the class LimitClauseJUnitTest method testLimitOnEqualsCompactRangeIndexedFieldWithAndClauseCompactRangeIndexedField.

// This is one where the old code could apply limit but we do not. Should investigate... index
// being used is on ShortId
@Test
public void testLimitOnEqualsCompactRangeIndexedFieldWithAndClauseCompactRangeIndexedField() 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 < 20; 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);
    }
    for (int i = 20; i < 24; i++) {
        Portfolio p = new Portfolio(11);
        p.shortID = 2;
        p.positions.clear();
        p.positions.put("VMW", new Position("VMW", i));
        region.put("KEY" + i, p);
    }
    for (int i = 24; i < 28; i++) {
        Portfolio p = new Portfolio(10);
        p.shortID = (short) (i % 3);
        p.positions.clear();
        p.positions.put("VMW", new Position("VMW", i));
        region.put("KEY" + i, p);
    }
    for (int i = 100; i < 200; i++) {
        Portfolio p = new Portfolio(10);
        p.shortID = 0;
        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");
    Index shortIdIndex = qs.createIndex("shortIdIndex", "P.shortID", "/portfolios1 P");
    String queryString = "<trace>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();
    // assertFalse(observer.limitAppliedAtIndex);
    assertEquals(5, resultsWithIndex.size());
}
Also used : 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 62 with SelectResults

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

the class LimitClauseJUnitTest method testDistinctLimitWithParameter.

@Test
public void testDistinctLimitWithParameter() throws Exception {
    String queryString = "SELECT DISTINCT entry FROM $1 entry LIMIT $2 ";
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 0; i < 100; i++) {
        region.put(i, new Portfolio(i));
    }
    Object[] params = new Object[2];
    params[0] = region;
    params[1] = 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)

Example 63 with SelectResults

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

the class LimitClauseJUnitTest method testNotLimitAtIndexLevelForMultiSingleIndexOrClauseUsage.

@Test
public void testNotLimitAtIndexLevelForMultiSingleIndexOrClauseUsage() throws Exception {
    // try {
    Query query;
    SelectResults result;
    Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
    for (int i = 15; i > 0; i--) {
        Portfolio p = new Portfolio(i);
        // CacheUtils.log(p);
        p.positions.clear();
        p.positions.put("IBM", new Position("IBM", i));
        region.put("KEY" + i, p);
    }
    String queryString = "<trace>SELECT * FROM /portfolios1 P, P.positions.values POS WHERE P.ID < 4 OR P.ID > 11 AND P.ID != 13 LIMIT 5";
    query = qs.newQuery(queryString);
    SelectResults resultsNoIndex = (SelectResults) query.execute();
    // Create Index on ID and secId
    Index secIndex = qs.createIndex("secIdIndex", "pos.secId", "/portfolios1 p, p.positions.values pos");
    Index idIndex = qs.createIndex("idIndex", IndexType.FUNCTIONAL, "P.ID", "/portfolios1 P, P.positions.values pos");
    // assertNotNull(secIndex);
    assertNotNull(idIndex);
    SelectResults resultsWithIndex = (SelectResults) query.execute();
    assertEquals(resultsNoIndex.size(), resultsWithIndex.size());
}
Also used : 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 64 with SelectResults

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

the class LimitClauseJUnitTest method helpTestIndexForQuery.

/*
   * helper method to test against a compact range index
   * 
   * @param query
   * 
   * @throws Exception
   */
private SelectResults helpTestIndexForQuery(String query, String indexedExpression, String regionPath, Object[] params) throws Exception {
    QueryService qs = CacheUtils.getQueryService();
    MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
    QueryObserverHolder.setInstance(observer);
    SelectResults nonIndexedResults = (SelectResults) qs.newQuery(query).execute(params);
    assertFalse(observer.indexUsed);
    qs.createIndex("newIndex", indexedExpression, regionPath);
    SelectResults indexedResults = (SelectResults) qs.newQuery(query).execute(params);
    assertEquals(nonIndexedResults.size(), indexedResults.size());
    assertTrue(observer.indexUsed);
    return indexedResults;
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService)

Example 65 with SelectResults

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryForResultBagWithProjectionAttributeWithIndex.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for ResultBag wrapped by a SelectResults . This test contains
   * projection attributes. Since the attribute is unique every time, the limit will be satisfied
   * with first 5 iterations
   * 
   * Tests ResultBag behaviour
   * 
   */
@Test
public void testLimitDistinctIterEvaluatedQueryForResultBagWithProjectionAttributeWithIndex() {
    try {
        Query query;
        SelectResults result;
        String queryString = "SELECT DISTINCT pf.ID FROM /portfolios pf WHERE pf.ID > 0 limit 5";
        query = qs.newQuery(queryString);
        MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
        QueryObserver old = QueryObserverHolder.setInstance(observer);
        Index index = qs.createIndex("idIndex", "pf.ID", "/portfolios pf");
        assertNotNull(index);
        result = (SelectResults) query.execute();
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, wrapper.asSet().size());
        assertTrue(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) Index(org.apache.geode.cache.query.Index) 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