Search in sources :

Example 61 with ObjectType

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

the class PartitionedRegionQueryEvaluator method sortIncomingData.

// TODO Asif: optimize it by creating a Sorted SelectResults Object at the time of fromData , so
// that processData already recieves ordered data.
private List sortIncomingData(List objects, List<CompiledSortCriterion> orderByAttribs) {
    ObjectType resultType = cumulativeResults.getCollectionType().getElementType();
    ExecutionContext local = new ExecutionContext(null, this.pr.cache);
    Comparator comparator = new OrderByComparator(orderByAttribs, resultType, local);
    boolean nullAtStart = !orderByAttribs.get(0).getCriterion();
    final SelectResults newResults;
    // of adding the struct objects as is, add fieldValues.
    if (resultType != null && resultType.isStructType()) {
        SortedStructBag sortedStructBag = new SortedStructBag(comparator, (StructType) resultType, nullAtStart);
        for (Object o : objects) {
            Struct s = (Struct) o;
            sortedStructBag.addFieldValues(s.getFieldValues());
        }
        newResults = sortedStructBag;
    } else {
        newResults = new SortedResultsBag(comparator, resultType, nullAtStart);
        newResults.addAll(objects);
    }
    objects = newResults.asList();
    return objects;
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType)

Example 62 with ObjectType

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

the class StructSetOrResultsSet method getOrderByComparatorAndLimitForQuery.

public Wrapper getOrderByComparatorAndLimitForQuery(String orderByQuery, int unorderedResultSize) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
    DefaultQuery q = (DefaultQuery) CacheUtils.getQueryService().newQuery(orderByQuery);
    CompiledSelect cs = q.getSimpleSelect();
    List<CompiledSortCriterion> orderByAttribs = null;
    if (cs.getType() == CompiledValue.GROUP_BY_SELECT) {
        Field originalOrderByMethod = CompiledGroupBySelect.class.getDeclaredField("originalOrderByClause");
        originalOrderByMethod.setAccessible(true);
        orderByAttribs = (List<CompiledSortCriterion>) originalOrderByMethod.get(cs);
    } else {
        orderByAttribs = cs.getOrderByAttrs();
    }
    ObjectType resultType = cs.getElementTypeForOrderByQueries();
    ExecutionContext context = new ExecutionContext(null, CacheUtils.getCache());
    final OrderByComparator obc = new OrderByComparator(orderByAttribs, resultType, context);
    Comparator baseComparator = obc;
    if (resultType.isStructType()) {
        baseComparator = new Comparator<Struct>() {

            @Override
            public int compare(Struct o1, Struct o2) {
                return obc.compare(o1.getFieldValues(), o2.getFieldValues());
            }
        };
    }
    final Comparator secondLevelComparator = baseComparator;
    final Comparator finalComparator = new Comparator() {

        @Override
        public int compare(Object o1, Object o2) {
            final boolean[] orderByColsEqual = new boolean[] { false };
            QueryObserverHolder.setInstance(new QueryObserverAdapter() {

                @Override
                public void orderByColumnsEqual() {
                    orderByColsEqual[0] = true;
                }
            });
            int result = secondLevelComparator.compare(o1, o2);
            if (result != 0 && orderByColsEqual[0]) {
                result = 0;
            }
            return result;
        }
    };
    Field hasUnmappedOrderByColsField = CompiledSelect.class.getDeclaredField("hasUnmappedOrderByCols");
    hasUnmappedOrderByColsField.setAccessible(true);
    boolean skip = ((Boolean) hasUnmappedOrderByColsField.get(cs)).booleanValue();
    ValidationLevel validationLevel = ValidationLevel.ALL;
    int limit;
    if (cs.getType() == CompiledValue.GROUP_BY_SELECT) {
        Field limitCVField = CompiledGroupBySelect.class.getDeclaredField("limit");
        limitCVField.setAccessible(true);
        CompiledValue limitCV = (CompiledValue) limitCVField.get(cs);
        Method evaluateLimitMethod = CompiledSelect.class.getDeclaredMethod("evaluateLimitValue", ExecutionContext.class, CompiledValue.class);
        evaluateLimitMethod.setAccessible(true);
        limit = ((Integer) evaluateLimitMethod.invoke(null, context, limitCV)).intValue();
    } else {
        limit = cs.getLimitValue(null);
    }
    if (limit != -1 && limit < unorderedResultSize) {
        // chances are that results will not match
        if (skip) {
            validationLevel = ValidationLevel.NONE;
        } else {
            validationLevel = ValidationLevel.ORDER_BY_ONLY;
        }
    } else {
        if (skip) {
            validationLevel = ValidationLevel.MATCH_ONLY;
        }
    }
    return new Wrapper(finalComparator, limit, validationLevel);
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) Method(java.lang.reflect.Method) Comparator(java.util.Comparator) OrderByComparator(org.apache.geode.cache.query.internal.OrderByComparator) Struct(org.apache.geode.cache.query.Struct) Field(java.lang.reflect.Field) ObjectType(org.apache.geode.cache.query.types.ObjectType) ExecutionContext(org.apache.geode.cache.query.internal.ExecutionContext) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) OrderByComparator(org.apache.geode.cache.query.internal.OrderByComparator) CompiledSelect(org.apache.geode.cache.query.internal.CompiledSelect)

Example 63 with ObjectType

use of org.apache.geode.cache.query.types.ObjectType 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 64 with ObjectType

use of org.apache.geode.cache.query.types.ObjectType 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 65 with ObjectType

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

Aggregations

ObjectType (org.apache.geode.cache.query.types.ObjectType)95 SelectResults (org.apache.geode.cache.query.SelectResults)66 QueryService (org.apache.geode.cache.query.QueryService)46 Test (org.junit.Test)46 Query (org.apache.geode.cache.query.Query)45 Iterator (java.util.Iterator)44 Region (org.apache.geode.cache.Region)40 StructType (org.apache.geode.cache.query.types.StructType)38 Portfolio (org.apache.geode.cache.query.data.Portfolio)32 Struct (org.apache.geode.cache.query.Struct)30 StructTypeImpl (org.apache.geode.cache.query.internal.types.StructTypeImpl)29 ObjectTypeImpl (org.apache.geode.cache.query.internal.types.ObjectTypeImpl)25 CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)24 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)23 ArrayList (java.util.ArrayList)21 List (java.util.List)20 Index (org.apache.geode.cache.query.Index)20 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)20 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)18 HashSet (java.util.HashSet)14