use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class RangeIndexAPIJUnitTest method testQueryMethod_1.
@Test
public void testQueryMethod_1() throws Exception {
QueryService qs;
qs = CacheUtils.getQueryService();
AbstractIndex i1 = (AbstractIndex) qs.createIndex("idIndex", IndexType.FUNCTIONAL, "ID", "/portfolios");
AbstractIndex i2 = (AbstractIndex) qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
AbstractIndex i3 = (AbstractIndex) qs.createIndex("status.toString()", IndexType.FUNCTIONAL, "status.toString", "/portfolios");
Set results = new HashSet();
DefaultQuery q = new DefaultQuery("select * from /portfolios", CacheUtils.getCache(), false);
q.setRemoteQuery(false);
ExecutionContext context = new QueryExecutionContext(null, CacheUtils.getCache(), q);
bindIterators(context, "/portfolios");
i1.query(new Integer(1), OQLLexerTokenTypes.TOK_EQ, results, context);
assertEquals(1, results.size());
assertTrue(results.iterator().next() == region.get(new Integer(1)));
results.clear();
i1.query(new Integer(1), OQLLexerTokenTypes.TOK_GT, results, context);
assertEquals(10, results.size());
for (int i = 2; i < 12; ++i) {
assertTrue(results.contains(region.get(new Integer(i))));
}
results.clear();
i2.query(new String("active"), OQLLexerTokenTypes.TOK_NE, results, context);
assertEquals(7, results.size());
for (int i = 1; i < 12; ) {
assertTrue(results.contains(region.get(new Integer(i))));
if (i >= 9) {
++i;
} else {
i += 2;
}
}
results.clear();
i3.query(QueryService.UNDEFINED, OQLLexerTokenTypes.TOK_EQ, results, context);
assertEquals(2, results.size());
assertTrue(results.contains(region.get(new Integer(11))));
assertTrue(results.contains(region.get(new Integer(10))));
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class RangeIndexAPIJUnitTest method testQueryMethod_2.
/**
* The keysToRemove set will never contain null or UNDEFINED as those conditions never form part
* of RangeJunctionCondnEvaluator. Such null or undefined conditions are treated as separate
* filter operands. This test checks the query method of Index which takes a set of keys which
* need to be removed from the set
*/
@Test
public void testQueryMethod_2() throws Exception {
QueryService qs;
qs = CacheUtils.getQueryService();
AbstractIndex i1 = (AbstractIndex) qs.createIndex("idIndex", IndexType.FUNCTIONAL, "ID", "/portfolios");
AbstractIndex i2 = (AbstractIndex) qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
AbstractIndex i3 = (AbstractIndex) qs.createIndex("status.toString()", IndexType.FUNCTIONAL, "status.toString", "/portfolios");
Set results = new HashSet();
DefaultQuery q = new DefaultQuery("select * from /portfolios ", CacheUtils.getCache(), false);
q.setRemoteQuery(false);
ExecutionContext context = new QueryExecutionContext(null, CacheUtils.getCache(), q);
bindIterators(context, "/portfolios");
Set keysToRemove = new HashSet();
i1.query(new Integer(1), OQLLexerTokenTypes.TOK_EQ, results, context);
assertEquals(1, results.size());
assertTrue(results.iterator().next() == region.get(new Integer(1)));
results.clear();
keysToRemove.clear();
keysToRemove.add(new Integer(1));
try {
i1.query(new Integer(1), OQLLexerTokenTypes.TOK_EQ, results, keysToRemove, context);
fail("A condition having an equal will be identified at RangeJunction level itself, so this type of condition should throw error in RangeIndex where along with an equal there happens not equal conditions");
} catch (AssertionError error) {
// pass
}
keysToRemove.clear();
results.clear();
keysToRemove.add(new Integer(9));
i1.query(new Integer(1), OQLLexerTokenTypes.TOK_GT, results, keysToRemove, context);
assertEquals(9, results.size());
for (int i = 2; i < 12; ) {
if (i != 9) {
assertTrue(results.contains(region.get(new Integer(i))));
}
++i;
}
keysToRemove.clear();
results.clear();
keysToRemove.add(new Integer(1));
keysToRemove.add(new Integer(10));
i1.query(new Integer(1), OQLLexerTokenTypes.TOK_GE, results, keysToRemove, context);
assertEquals(9, results.size());
for (int i = 2; i < 12; ) {
if (i != 10) {
assertTrue(results.contains(region.get(new Integer(i))));
}
++i;
}
keysToRemove.clear();
results.clear();
keysToRemove.add(new Integer(8));
keysToRemove.add(new Integer(11));
i1.query(new Integer(11), OQLLexerTokenTypes.TOK_LT, results, keysToRemove, context);
assertEquals(10, results.size());
for (int i = 0; i < 11; ) {
if (i != 8) {
assertTrue(results.contains(region.get(new Integer(i))));
}
++i;
}
keysToRemove.clear();
results.clear();
keysToRemove.add(new Integer(8));
keysToRemove.add(new Integer(11));
i1.query(new Integer(11), OQLLexerTokenTypes.TOK_LE, results, keysToRemove, context);
assertEquals(10, results.size());
for (int i = 0; i < 11; ) {
if (i != 8) {
assertTrue(results.contains(region.get(new Integer(i))));
}
++i;
}
keysToRemove.clear();
results.clear();
keysToRemove.add(new Integer(1));
keysToRemove.add(new Integer(10));
i1.query(new Integer(1), OQLLexerTokenTypes.TOK_GT, results, keysToRemove, context);
assertEquals(9, results.size());
for (int i = 2; i < 12; ) {
if (i != 10) {
assertTrue(results.contains(region.get(new Integer(i))));
}
++i;
}
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class GroupByTestImpl method testAggregateFuncAvg.
@Test
public void testAggregateFuncAvg() throws Exception {
Region region = this.createRegion("portfolio", Portfolio.class);
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i / 5);
region.put("" + i, pf);
}
String queryStr = "select p.status as status, Avg(p.ID) as average 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");
double sumIDActive = 0, sumIDInactive = 0;
int numActive = 0, numInactive = 0;
for (Object o : rgn.values()) {
Portfolio pf = (Portfolio) o;
if (pf.ID > 0) {
if (pf.status.equals("active")) {
sumIDActive += pf.ID;
++numActive;
} else if (pf.status.equals("inactive")) {
sumIDInactive += pf.ID;
++numInactive;
}
}
}
Number avgActive = AbstractAggregator.downCast(sumIDActive / numActive);
Number avgInactive = AbstractAggregator.downCast(sumIDInactive / numInactive);
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(avgActive, struct.get("average"));
} else if (struct.get("status").equals("inactive")) {
assertEquals(avgInactive, struct.get("average"));
} 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());
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class GroupByTestImpl method testAggregateFuncNoGroupBy.
@Test
public void testAggregateFuncNoGroupBy() throws Exception {
Region region = this.createRegion("portfolio", Portfolio.class);
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i / 5);
region.put("" + i, pf);
}
String queryStr = "select sum(p.ID) as summ , Max(p.ID) as maxx, min(p.ID) as minn," + " avg(p.ID) as average from /portfolio p where p.ID > 0 ";
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(1, sr.size());
Iterator iter = sr.iterator();
Region rgn = CacheUtils.getRegion("portfolio");
int sum = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (Object o : rgn.values()) {
Portfolio pf = (Portfolio) o;
sum += pf.ID;
if (pf.ID > max) {
max = pf.ID;
}
if (pf.ID < min) {
min = pf.ID;
}
}
float avg = sum / rgn.size();
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
assertEquals(sum, ((Integer) struct.get("summ")).intValue());
assertEquals(max, ((Integer) struct.get("maxx")).intValue());
assertEquals(min, ((Integer) struct.get("minn")).intValue());
assertEquals(avg, ((Number) struct.get("average")).floatValue(), 0.0f);
}
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class GroupByTestImpl method testAggregateFuncMax.
@Test
public void testAggregateFuncMax() throws Exception {
Region region = this.createRegion("portfolio", Portfolio.class);
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i / 5);
region.put("" + 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()) {
Portfolio pf = (Portfolio) o;
if (pf.status.equals("active")) {
if (pf.ID > activeMaxID) {
activeMaxID = pf.ID;
}
} else if (pf.status.equals("inactive")) {
if (pf.ID > inactiveMaxID) {
inactiveMaxID = pf.ID;
}
} 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