use of org.apache.geode.cache.query.internal.CompiledSelect in project geode by apache.
the class GroupByTestImpl method testComplexValueAggregateFuncAvgDistinct.
@Test
public void testComplexValueAggregateFuncAvgDistinct() 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(distinct element(select iter.shortID from /portfolio iter where iter.ID = 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");
Set<Short> sumIDActiveSet = new HashSet<Short>(), sumIDInactiveSet = new HashSet<Short>();
for (Object o : rgn.values()) {
Portfolio pf = (Portfolio) o;
if (pf.ID > 0) {
if (pf.status.equals("active")) {
sumIDActiveSet.add(pf.shortID);
} else if (pf.status.equals("inactive")) {
sumIDInactiveSet.add(pf.shortID);
}
}
}
double sumActive = 0, sumInactive = 0;
for (Short shortt : sumIDActiveSet) {
sumActive += shortt.doubleValue();
}
for (Short shortt : sumIDInactiveSet) {
sumInactive += shortt.doubleValue();
}
Number avgActive = AbstractAggregator.downCast(sumActive / sumIDActiveSet.size());
Number avgInactive = AbstractAggregator.downCast(sumInactive / sumIDInactiveSet.size());
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.CompiledSelect in project geode by apache.
the class PRQueryProcessor method executeWithThreadPool.
private void executeWithThreadPool(Collection<Collection> resultCollector) throws QueryException, InterruptedException, ForceReattemptException {
if (Thread.interrupted())
throw new InterruptedException();
java.util.List callableTasks = buildCallableTaskList(resultCollector);
ExecutorService execService = PRQueryExecutor.getExecutorService();
boolean reattemptNeeded = false;
ForceReattemptException fre = null;
if (callableTasks != null && !callableTasks.isEmpty()) {
List futures = null;
try {
futures = execService.invokeAll(callableTasks, 300, TimeUnit.SECONDS);
} catch (RejectedExecutionException rejectedExecutionEx) {
throw rejectedExecutionEx;
}
if (futures != null) {
Iterator itr = futures.iterator();
while (itr.hasNext() && !execService.isShutdown() && !execService.isTerminated()) {
// this._prds.partitionedRegion.checkReadiness();
Future fut = (Future) itr.next();
QueryTask.BucketQueryResult bqr = null;
try {
bqr = (QueryTask.BucketQueryResult) fut.get(BUCKET_QUERY_TIMEOUT, TimeUnit.SECONDS);
// if (retry.booleanValue()) {
// reattemptNeeded = true;
// fre = (ForceReattemptException)bqr.getException();
// } else {
// handles an exception if there was one,
bqr.handleAndThrowException();
// }
if (bqr.retry) {
reattemptNeeded = true;
}
} catch (TimeoutException e) {
throw new InternalGemFireException(LocalizedStrings.PRQueryProcessor_TIMED_OUT_WHILE_EXECUTING_QUERY_TIME_EXCEEDED_0.toLocalizedString(BUCKET_QUERY_TIMEOUT), e);
} catch (ExecutionException ee) {
Throwable cause = ee.getCause();
if (cause instanceof QueryException) {
throw (QueryException) cause;
} else {
throw new InternalGemFireException(LocalizedStrings.PRQueryProcessor_GOT_UNEXPECTED_EXCEPTION_WHILE_EXECUTING_QUERY_ON_PARTITIONED_REGION_BUCKET.toLocalizedString(), cause);
}
}
}
CompiledSelect cs = this.query.getSimpleSelect();
if (cs != null && (cs.isOrderBy() || cs.isGroupBy())) {
ExecutionContext context = new QueryExecutionContext(this.parameters, pr.getCache());
int limit = this.query.getLimit(parameters);
Collection mergedResults = coalesceOrderedResults(resultCollector, context, cs, limit);
resultCollector.clear();
resultCollector.add(mergedResults);
}
}
}
if (execService == null || execService.isShutdown() || execService.isTerminated()) {
this._prds.partitionedRegion.checkReadiness();
}
if (reattemptNeeded) {
throw fre;
}
}
use of org.apache.geode.cache.query.internal.CompiledSelect in project geode by apache.
the class GroupByTestImpl method testAggregateFuncAvgDistinct.
@Test
public void testAggregateFuncAvgDistinct() 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(distinct p.shortID) 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");
Set<Short> sumIDActiveSet = new HashSet<Short>(), sumIDInactiveSet = new HashSet<Short>();
for (Object o : rgn.values()) {
Portfolio pf = (Portfolio) o;
if (pf.ID > 0) {
if (pf.status.equals("active")) {
sumIDActiveSet.add(pf.shortID);
} else if (pf.status.equals("inactive")) {
sumIDInactiveSet.add(pf.shortID);
}
}
}
double sumActive = 0, sumInactive = 0;
for (Short shortt : sumIDActiveSet) {
sumActive += shortt.doubleValue();
}
for (Short shortt : sumIDInactiveSet) {
sumInactive += shortt.doubleValue();
}
Number avgActive = AbstractAggregator.downCast(sumActive / sumIDActiveSet.size());
Number avgInactive = AbstractAggregator.downCast(sumInactive / sumIDInactiveSet.size());
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.CompiledSelect in project geode by apache.
the class GroupByTestImpl method testConvertibleGroupByQuery_1.
@Test
public void testConvertibleGroupByQuery_1() 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, 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());
}
use of org.apache.geode.cache.query.internal.CompiledSelect in project geode by apache.
the class GroupByTestImpl method testAggregateFuncCountDistinctStar_2.
@Test
public void testAggregateFuncCountDistinctStar_2() 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, 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()) {
Portfolio pf = (Portfolio) 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());
}
Aggregations