Search in sources :

Example 31 with CompiledSelect

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());
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) StructType(org.apache.geode.cache.query.types.StructType) Portfolio(org.apache.geode.cache.query.data.Portfolio) Struct(org.apache.geode.cache.query.Struct) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 32 with CompiledSelect

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;
    }
}
Also used : InternalGemFireException(org.apache.geode.InternalGemFireException) QueryExecutionContext(org.apache.geode.cache.query.internal.QueryExecutionContext) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) List(java.util.List) QueryException(org.apache.geode.cache.query.QueryException) ExecutionContext(org.apache.geode.cache.query.internal.ExecutionContext) QueryExecutionContext(org.apache.geode.cache.query.internal.QueryExecutionContext) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Future(java.util.concurrent.Future) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 33 with CompiledSelect

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());
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) StructType(org.apache.geode.cache.query.types.StructType) Portfolio(org.apache.geode.cache.query.data.Portfolio) Struct(org.apache.geode.cache.query.Struct) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 34 with CompiledSelect

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());
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Portfolio(org.apache.geode.cache.query.data.Portfolio) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Region(org.apache.geode.cache.Region) Test(org.junit.Test)

Example 35 with CompiledSelect

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());
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) StructType(org.apache.geode.cache.query.types.StructType) Portfolio(org.apache.geode.cache.query.data.Portfolio) Struct(org.apache.geode.cache.query.Struct) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)36 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)33 Region (org.apache.geode.cache.Region)28 Query (org.apache.geode.cache.query.Query)28 QueryService (org.apache.geode.cache.query.QueryService)28 SelectResults (org.apache.geode.cache.query.SelectResults)28 Test (org.junit.Test)28 Struct (org.apache.geode.cache.query.Struct)27 Iterator (java.util.Iterator)26 ObjectType (org.apache.geode.cache.query.types.ObjectType)24 StructType (org.apache.geode.cache.query.types.StructType)22 Portfolio (org.apache.geode.cache.query.data.Portfolio)15 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)13 HashSet (java.util.HashSet)12 ExecutionContext (org.apache.geode.cache.query.internal.ExecutionContext)4 CompiledSortCriterion (org.apache.geode.cache.query.internal.CompiledSortCriterion)3 QueryExecutionContext (org.apache.geode.cache.query.internal.QueryExecutionContext)3 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2