use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncCountDistinctStar_1.
@Override
@Test
public void testAggregateFuncCountDistinctStar_1() 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, count(distinct p.shortID) as countt 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");
Set<Object> distinctShortIDActive = new HashSet<Object>();
Set<Object> distinctShortIDInactive = new HashSet<Object>();
int inactiveCount = 0;
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.status.equals("active")) {
distinctShortIDActive.add(pf.shortID);
} else if (pf.status.equals("inactive")) {
distinctShortIDInactive.add(pf.shortID);
} else {
fail("unexpected status");
}
}
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
StructType structType = struct.getStructType();
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Integer", fieldTypes[1].getSimpleClassName());
if (struct.get("status").equals("active")) {
assertEquals(distinctShortIDActive.size(), ((Integer) struct.get("countt")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(distinctShortIDInactive.size(), ((Integer) struct.get("countt")).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("Integer", fieldTypes[1].getSimpleClassName());
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncCountDistinctStar_2.
@Override
@Test
public void testAggregateFuncCountDistinctStar_2() 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, COUNT(distinct p.shortID) as countt 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");
Set<Object> distinctShortIDActive = new HashSet<Object>();
Set<Object> distinctShortIDInactive = new HashSet<Object>();
int inactiveCount = 0;
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.status.equals("active")) {
distinctShortIDActive.add(pf.shortID);
} else if (pf.status.equals("inactive")) {
distinctShortIDInactive.add(pf.shortID);
} else {
fail("unexpected status");
}
}
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
StructType structType = struct.getStructType();
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Integer", fieldTypes[1].getSimpleClassName());
if (struct.get("status").equals("active")) {
assertEquals(distinctShortIDActive.size(), ((Integer) struct.get("countt")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(distinctShortIDInactive.size(), ((Integer) struct.get("countt")).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("Integer", fieldTypes[1].getSimpleClassName());
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncSumDistinct.
@Override
@Test
public void testAggregateFuncSumDistinct() 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, SUM (distinct p.shortID) as summ 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");
Set<Short> activeSum = new HashSet<Short>();
Set<Short> inactiveSum = new HashSet<Short>();
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.status.equals("active")) {
activeSum.add(pf.shortID);
} else if (pf.status.equals("inactive")) {
inactiveSum.add(pf.shortID);
} else {
fail("unexpected value of status");
}
}
int activeSumm = 0, inactiveSumm = 0;
for (Short val : activeSum) {
activeSumm += val.intValue();
}
for (Short val : inactiveSum) {
inactiveSumm += val.intValue();
}
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(activeSumm, ((Integer) struct.get("summ")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(inactiveSumm, ((Integer) struct.get("summ")).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());
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncMin.
@Override
@Test
public void testAggregateFuncMin() 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, Min(p.ID) as Minn 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 activeMinID = Integer.MAX_VALUE;
int inactiveMinID = Integer.MAX_VALUE;
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.status.equals("active")) {
if (pf.getID() < activeMinID) {
activeMinID = pf.getID();
}
} else if (pf.status.equals("inactive")) {
if (pf.getID() < inactiveMinID) {
inactiveMinID = 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(activeMinID, ((Integer) struct.get("Minn")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(inactiveMinID, ((Integer) struct.get("Minn")).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());
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class PdxGroupByTestImpl method testConvertibleGroupByQuery_1.
@Override
@Test
public void testConvertibleGroupByQuery_1() 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, p.ID from /portfolio p where p.ID > 0 group by status, p.ID ";
QueryService qs = CacheUtils.getQueryService();
Query query = qs.newQuery(queryStr);
CompiledSelect cs = ((DefaultQuery) query).getSelect();
assertTrue(cs.isDistinct());
assertTrue(cs.isOrderBy());
assertFalse(cs.isGroupBy());
}
Aggregations