use of org.apache.geode.cache.query.internal.QueryObserverAdapter in project geode by apache.
the class LimitClauseJUnitTest method testLimitNonDistinctQueryWithTwoCondTwoIndex.
@Test
public void testLimitNonDistinctQueryWithTwoCondTwoIndex() {
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);
}
MyQueryObserverAdapter observer = new MyQueryObserverAdapter();
QueryObserver old = QueryObserverHolder.setInstance(observer);
// Create Index on ID
Index index = qs.createIndex("idIndex", "ID", "/portfolios1");
assertNotNull(index);
index = qs.createIndex("statusIndex", "status", "/portfolios1");
assertNotNull(index);
String[] queryString = new String[] { "select * from /portfolios1 where status ='inactive' AND (ID > 0 AND ID < 100) limit 10" };
for (String qstr : queryString) {
query = qs.newQuery(qstr);
result = (SelectResults) query.execute();
assertEquals(10, result.size());
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.internal.QueryObserverAdapter in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryForStructBagWithProjectionAttributeWithIndex.
/**
* Tests the limit functionality for Iter evaluated query with distinct clause This tests the
* basic limit functionality for StructBag 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 StructBag behaviour
*
*/
@Test
public void testLimitDistinctIterEvaluatedQueryForStructBagWithProjectionAttributeWithIndex() {
try {
Query query;
SelectResults result;
query = qs.newQuery("SELECT DISTINCT pf.ID, pf.createTime FROM /portfolios pf WHERE pf.ID > 0 limit 5");
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(wrapper.getCollectionType().getElementType() instanceof StructType);
assertTrue(observer.limitAppliedAtIndex);
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
use of org.apache.geode.cache.query.internal.QueryObserverAdapter in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctQueryWithDuplicateValues.
@Test
public void testLimitDistinctQueryWithDuplicateValues() {
try {
Query query;
SelectResults result;
Region region = CacheUtils.createRegion("portfolios1", Portfolio.class);
for (int i = 1; i < 10; i++) {
Portfolio p = new Portfolio(i);
if (i == 2)
p = new Portfolio(1);
region.put(Integer.toString(i), p);
}
// Create Index on ID
Index index = qs.createIndex("idIndex", "ID", "/portfolios1");
assertNotNull(index);
String queryString = "select DISTINCT * from /portfolios1 where status ='inactive' AND ID > 0 limit 2";
query = qs.newQuery(queryString);
result = (SelectResults) query.execute();
assertEquals(2, result.size());
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
use of org.apache.geode.cache.query.internal.QueryObserverAdapter in project geode by apache.
the class CustomerOptimizationsJUnitTest method testProjectionEvaluationDuringIndexResults.
@Test
public void testProjectionEvaluationDuringIndexResults() throws QueryException {
QueryService qs = CacheUtils.getQueryService();
String[] queries = new String[] { "select p.status from /pos p where p.ID > 0 ", "select p.status from /pos p, p.positions pos where p.ID > 0 ", "select p.status from /pos p where p.ID > 0 and p.createTime > 0", "select p.status as sts, p as pos from /pos p where p.ID > 0 and p.createTime > 0", "select p.status as sts, p as pos from /pos p where p.ID IN SET( 0,1,2,3) and p.createTime > 0", "select p.status as sts, p as pos from /pos p where ( p.ID IN SET( 0,1,2,3) and p.createTime > 0L) OR (p.ID IN SET( 2,3) and p.createTime > 5L)" };
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");
ObjectType[] expectedTypes = new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(String.class), new ObjectTypeImpl(String.class), new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }), new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }), new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }), new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }) };
final boolean[] expectedCallback = { false, true, false, false, false, true };
final boolean[] actualCallback = new boolean[queries.length];
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private int i = 0;
public void beforeApplyingProjectionOnFilterEvaluatedResults(Object preProjectionApplied) {
actualCallback[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(expectedCallback[i], actualCallback[i]);
assertEquals(expectedTypes[i], sr[i][1].getCollectionType().getElementType());
}
CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
use of org.apache.geode.cache.query.internal.QueryObserverAdapter in project geode by apache.
the class CustomerOptimizationsJUnitTest method testNotFilterableNestedJunction.
@Test
public void testNotFilterableNestedJunction() 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') )AND p.ID > 0 AND p.createTime = 10l" };
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(), 1);
assertEquals(((Index) indexUsed.iterator().next()).getName(), "CreateTime");
CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Aggregations