Search in sources :

Example 11 with CompiledSelect

use of org.apache.geode.cache.query.internal.CompiledSelect in project geode by apache.

the class PRQueryProcessor method executeSequentially.

private void executeSequentially(Collection<Collection> resultCollector, List buckets) throws QueryException, InterruptedException, ForceReattemptException {
    /*
     * for (Iterator itr = _bucketsToQuery.iterator(); itr.hasNext(); ) { Integer bId =
     * (Integer)itr.next(); doBucketQuery(bId, this._prds, this.query, this.parameters,
     * resultCollector); }
     */
    ExecutionContext context = new QueryExecutionContext(this.parameters, this.pr.getCache(), this.query);
    CompiledSelect cs = this.query.getSimpleSelect();
    int limit = this.query.getLimit(parameters);
    if (cs != null && cs.isOrderBy()) {
        for (Integer bucketID : this._bucketsToQuery) {
            List<Integer> singleBucket = Collections.singletonList(bucketID);
            context.setBucketList(singleBucket);
            executeQueryOnBuckets(resultCollector, context);
        }
        Collection mergedResults = coalesceOrderedResults(resultCollector, context, cs, limit);
        resultCollector.clear();
        resultCollector.add(mergedResults);
    } else {
        context.setBucketList(buckets);
        executeQueryOnBuckets(resultCollector, context);
    }
}
Also used : Integer(java.lang.Integer) ExecutionContext(org.apache.geode.cache.query.internal.ExecutionContext) QueryExecutionContext(org.apache.geode.cache.query.internal.QueryExecutionContext) QueryExecutionContext(org.apache.geode.cache.query.internal.QueryExecutionContext) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Collection(java.util.Collection)

Example 12 with CompiledSelect

use of org.apache.geode.cache.query.internal.CompiledSelect in project geode by apache.

the class OrderByTestImplementation method testOrderByWithColumnAlias_Bug52041_2.

@Test
public void testOrderByWithColumnAlias_Bug52041_2() throws Exception {
    Region region = createRegion("portfolio", Portfolio.class);
    for (int i = 0; i < 200; ++i) {
        Portfolio pf = new Portfolio(i);
        pf.shortID = (short) ((short) i / 5);
        pf.status = "active";
        region.put("" + i, pf);
    }
    String queryStr = "select distinct p.ID as _id, p.shortID as short_id  from /portfolio p where p.ID >= 0 " + "order by short_id asc, p.ID desc";
    QueryService qs = CacheUtils.getQueryService();
    Query query = qs.newQuery(queryStr);
    SelectResults<Struct> results = (SelectResults<Struct>) query.execute();
    Iterator<Struct> iter = results.asList().iterator();
    int counter = 0;
    int k = 0;
    while (iter.hasNext()) {
        k = ((counter) / 5 + 1) * 5 - 1;
        Struct str = iter.next();
        assertEquals(counter / 5, ((Short) str.get("short_id")).intValue());
        assertEquals(k - (counter) % 5, ((Integer) str.get("_id")).intValue());
        ++counter;
    }
    CompiledSelect cs = ((DefaultQuery) query).getSimpleSelect();
    List<CompiledSortCriterion> orderbyAtts = cs.getOrderByAttrs();
    assertEquals(orderbyAtts.get(0).getColumnIndex(), 1);
    assertEquals(orderbyAtts.get(1).getColumnIndex(), 0);
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Query(org.apache.geode.cache.query.Query) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Portfolio(org.apache.geode.cache.query.data.Portfolio) Struct(org.apache.geode.cache.query.Struct) SelectResults(org.apache.geode.cache.query.SelectResults) QueryService(org.apache.geode.cache.query.QueryService) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect) Region(org.apache.geode.cache.Region) Test(org.junit.Test)

Example 13 with CompiledSelect

use of org.apache.geode.cache.query.internal.CompiledSelect 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());
}
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) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) 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) Test(org.junit.Test)

Example 14 with CompiledSelect

use of org.apache.geode.cache.query.internal.CompiledSelect in project geode by apache.

the class PdxGroupByTestImpl method testSumWithMultiColumnGroupBy.

@Override
@Test
public void testSumWithMultiColumnGroupBy() 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);
    }
    Map<String, Integer> expectedData = new HashMap<String, Integer>();
    for (Object o : region.values()) {
        PortfolioPdx pf = (PortfolioPdx) o;
        String key = pf.status + "_" + pf.shortID;
        if (expectedData.containsKey(key)) {
            int sum = expectedData.get(key).intValue() + pf.getID();
            expectedData.put(key, sum);
        } else {
            expectedData.put(key, pf.getID());
        }
    }
    String queryStr = "select  p.status as status, p.shortID as shortID, sum(p.ID) as summ from /portfolio p" + " where p.ID > 0 group by status, shortID ";
    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(expectedData.size(), sr.size());
    Iterator iter = sr.iterator();
    while (iter.hasNext()) {
        Struct struct = (Struct) iter.next();
        StructType structType = struct.getStructType();
        ObjectType[] fieldTypes = structType.getFieldTypes();
        assertEquals("String", fieldTypes[0].getSimpleClassName());
        assertEquals("short", fieldTypes[1].getSimpleClassName());
        assertEquals("Number", fieldTypes[2].getSimpleClassName());
        String key = struct.get("status") + "_" + struct.get("shortID");
        int sum = ((Integer) struct.get("summ")).intValue();
        assertTrue(expectedData.containsKey(key));
        assertEquals(expectedData.get(key).intValue(), sum);
    }
    ObjectType elementType = sr.getCollectionType().getElementType();
    assertTrue(elementType.isStructType());
    StructType structType = (StructType) elementType;
    ObjectType[] fieldTypes = structType.getFieldTypes();
    assertEquals("String", fieldTypes[0].getSimpleClassName());
    assertEquals("short", fieldTypes[1].getSimpleClassName());
    assertEquals("Number", fieldTypes[2].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) HashMap(java.util.HashMap) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) 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) Test(org.junit.Test)

Example 15 with CompiledSelect

use of org.apache.geode.cache.query.internal.CompiledSelect in project geode by apache.

the class PdxGroupByTestImpl method testAggregateFuncNoGroupBy.

@Override
@Test
public void testAggregateFuncNoGroupBy() 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   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()) {
        PortfolioPdx pf = (PortfolioPdx) o;
        sum += pf.getID();
        if (pf.getID() > max) {
            max = pf.getID();
        }
        if (pf.getID() < min) {
            min = pf.getID();
        }
    }
    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);
    }
}
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) PortfolioPdx(org.apache.geode.cache.query.data.PortfolioPdx) Struct(org.apache.geode.cache.query.Struct) 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) 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