Search in sources :

Example 46 with DefaultQuery

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());
}
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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 47 with DefaultQuery

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());
}
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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 48 with DefaultQuery

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());
}
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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 49 with DefaultQuery

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());
}
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 50 with DefaultQuery

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

Aggregations

DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)66 Test (org.junit.Test)49 QueryService (org.apache.geode.cache.query.QueryService)40 SelectResults (org.apache.geode.cache.query.SelectResults)38 Query (org.apache.geode.cache.query.Query)37 Region (org.apache.geode.cache.Region)33 CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)32 Struct (org.apache.geode.cache.query.Struct)27 Iterator (java.util.Iterator)26 HashSet (java.util.HashSet)23 ObjectType (org.apache.geode.cache.query.types.ObjectType)23 StructType (org.apache.geode.cache.query.types.StructType)22 Portfolio (org.apache.geode.cache.query.data.Portfolio)18 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)13 Set (java.util.Set)11 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)11 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)11 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)10 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)10 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)9