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());
}
}
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());
}
}
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());
}
}
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());
}
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());
}
Aggregations