Search in sources :

Example 46 with StructType

use of org.apache.geode.cache.query.types.StructType 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)

Example 47 with StructType

use of org.apache.geode.cache.query.types.StructType in project geode by apache.

the class IndexCreationJUnitTest method testIndexObjectTypeWithRegionConstraint.

@Test
public void testIndexObjectTypeWithRegionConstraint() throws Exception {
    QueryService qs;
    qs = CacheUtils.getQueryService();
    Index i1 = qs.createIndex("Index1", IndexType.FUNCTIONAL, "b.secId", "/portfolios pf, pf.positions.values b");
    ObjectType type = ((IndexProtocol) i1).getResultSetType();
    String[] fieldNames = { "index_iter1", "index_iter2" };
    ObjectType[] fieldTypes = { new ObjectTypeImpl(Portfolio.class), new ObjectTypeImpl(Object.class) };
    // ObjectType expectedType = new StructTypeImpl( fieldNames,fieldTypes);
    ObjectType expectedType = new StructTypeImpl(fieldNames, fieldTypes);
    if (!(type instanceof StructType && type.equals(expectedType))) {
        fail("The ObjectType obtained from index is not of the expected type. Type obtained from index=" + type);
    }
    Index i2 = qs.createIndex("Index2", IndexType.FUNCTIONAL, "pf.ID", "/portfolios.values pf");
    type = ((IndexProtocol) i2).getResultSetType();
    expectedType = new ObjectTypeImpl(Portfolio.class);
    if (!type.equals(expectedType)) {
        fail("The ObjectType obtained from index is not of the expected type. Type obtained from index=" + type);
    }
    Index i3 = qs.createIndex("Index3", IndexType.FUNCTIONAL, "pos.secId", "/portfolios['0'].positions.values pos");
    type = ((IndexProtocol) i3).getResultSetType();
    expectedType = new ObjectTypeImpl(Object.class);
    if (!type.equals(expectedType)) {
        fail("The ObjectType obtained from index is not of the expected type. Type obtained from index=" + type);
    }
    Index i4 = qs.createIndex("Index4", IndexType.PRIMARY_KEY, "ID", "/portfolios");
    type = ((IndexProtocol) i4).getResultSetType();
    expectedType = new ObjectTypeImpl(Portfolio.class);
    if (!type.equals(expectedType)) {
        fail("The ObjectType obtained from index is not of the expected type. Type obtained from index=" + type);
    }
}
Also used : IndexProtocol(org.apache.geode.cache.query.internal.index.IndexProtocol) ObjectType(org.apache.geode.cache.query.types.ObjectType) StructType(org.apache.geode.cache.query.types.StructType) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) Portfolio(org.apache.geode.cache.query.data.Portfolio) ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) RangeIndex(org.apache.geode.cache.query.internal.index.RangeIndex) CompactMapRangeIndex(org.apache.geode.cache.query.internal.index.CompactMapRangeIndex) Index(org.apache.geode.cache.query.Index) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

StructType (org.apache.geode.cache.query.types.StructType)47 ObjectType (org.apache.geode.cache.query.types.ObjectType)38 SelectResults (org.apache.geode.cache.query.SelectResults)36 Test (org.junit.Test)30 Iterator (java.util.Iterator)28 Query (org.apache.geode.cache.query.Query)28 Struct (org.apache.geode.cache.query.Struct)26 Region (org.apache.geode.cache.Region)25 QueryService (org.apache.geode.cache.query.QueryService)25 CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)22 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)22 Portfolio (org.apache.geode.cache.query.data.Portfolio)15 StructTypeImpl (org.apache.geode.cache.query.internal.types.StructTypeImpl)14 HashSet (java.util.HashSet)13 PortfolioPdx (org.apache.geode.cache.query.data.PortfolioPdx)11 List (java.util.List)8 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)7 ArrayList (java.util.ArrayList)6 ObjectTypeImpl (org.apache.geode.cache.query.internal.types.ObjectTypeImpl)4 ListIterator (java.util.ListIterator)3