use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CustomerOptimizationsJUnitTest method testRangeQuery.
@Test
public void testRangeQuery() throws Exception {
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 distinct p.status from /pos p where p.createTime > 0 AND p.createTime <11 AND p.ID IN SET( 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();
}
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 List indexesUsed = new ArrayList();
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;
indexesUsed.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(indexesUsed.size(), 1);
assertEquals(((Index) indexesUsed.iterator().next()).getName(), "PortFolioID");
CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
use of org.apache.geode.cache.query.types.ObjectType 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);
}
use of org.apache.geode.cache.query.types.ObjectType 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);
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class PdxOrderByJUnitTest method compareResultsOfWithAndWithoutIndex.
public boolean compareResultsOfWithAndWithoutIndex(SelectResults[] r) {
boolean ok = true;
Set set1 = null;
Set set2 = null;
Iterator itert1 = null;
Iterator itert2 = null;
ObjectType type1, type2;
// outer: for (int j = 0; j < r.length; j++) {
CollectionType collType1 = r[0].getCollectionType();
CollectionType collType2 = r[1].getCollectionType();
type1 = collType1.getElementType();
type2 = collType2.getElementType();
if (r[0].size() == r[1].size()) {
System.out.println("Both SelectResults are of Same Size i.e. Size= " + r[1].size());
} else {
System.out.println("FAILED4: SelectResults size is different in both the cases. Size1=" + r[0].size() + " Size2 = " + r[1].size());
ok = false;
}
if (ok) {
set2 = (((SelectResults) r[1]).asSet());
set1 = (((SelectResults) r[0]).asSet());
boolean pass = true;
itert1 = set1.iterator();
while (itert1.hasNext()) {
Object p1 = itert1.next();
itert2 = set2.iterator();
boolean exactMatch = false;
while (itert2.hasNext()) {
Object p2 = itert2.next();
if (p1 instanceof Struct) {
Object[] values1 = ((Struct) p1).getFieldValues();
Object[] values2 = ((Struct) p2).getFieldValues();
// test.assertIndexDetailsEquals(values1.length, values2.length);
if (values1.length != values2.length) {
ok = false;
break;
}
boolean elementEqual = true;
for (int i = 0; i < values1.length; ++i) {
elementEqual = elementEqual && ((values1[i] == values2[i]) || values1[i].equals(values2[i]));
}
exactMatch = elementEqual;
} else {
exactMatch = (p2 == p1) || p2.equals(p1);
}
if (exactMatch) {
break;
}
}
if (!exactMatch) {
System.out.println("FAILED5: Atleast one element in the pair of SelectResults supposedly identical, is not equal ");
ok = false;
break;
}
}
}
return ok;
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncMax.
@Override
@Test
public void testAggregateFuncMax() throws Exception {
Region region = this.createRegion("portfolio", PortfolioPdx.class);
for (int i = 1; i < 200; ++i) {
PortfolioPdx pf = new PortfolioPdx(i);
pf.shortID = (short) ((short) i / 5);
region.put("key-" + i, pf);
}
String queryStr = "select p.status as status, Max(p.ID) as Maxx from /portfolio p where p.ID > 0 group by status ";
QueryService qs = CacheUtils.getQueryService();
Query query = qs.newQuery(queryStr);
CompiledSelect cs = ((DefaultQuery) query).getSelect();
SelectResults sr = (SelectResults) query.execute();
assertTrue(sr.getCollectionType().getElementType().isStructType());
assertEquals(2, sr.size());
Iterator iter = sr.iterator();
Region rgn = CacheUtils.getRegion("portfolio");
int activeMaxID = 0;
int inactiveMaxID = 0;
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.status.equals("active")) {
if (pf.getID() > activeMaxID) {
activeMaxID = pf.getID();
}
} else if (pf.status.equals("inactive")) {
if (pf.getID() > inactiveMaxID) {
inactiveMaxID = pf.getID();
}
} else {
fail("unexpected value of status");
}
}
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
StructType structType = struct.getStructType();
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Number", fieldTypes[1].getSimpleClassName());
if (struct.get("status").equals("active")) {
assertEquals(activeMaxID, ((Integer) struct.get("Maxx")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(inactiveMaxID, ((Integer) struct.get("Maxx")).intValue());
} else {
fail("unexpected value of status");
}
}
ObjectType elementType = sr.getCollectionType().getElementType();
assertTrue(elementType.isStructType());
StructType structType = (StructType) elementType;
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Number", fieldTypes[1].getSimpleClassName());
}
Aggregations