Search in sources :

Example 11 with CompiledValue

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBag.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for ResultBag wrapped by a SelectResults if the iteration included
   * duplicate elements. If the distinct clause is present then duplicate elements even if
   * satisfying the where clause should not be considered as part as distinct will eliminate them.
   * This test validates the above behaviour if projection sttribute is present and the projection
   * attribute may be duplicate
   * 
   * Tests ResultBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForResultBag() {
    try {
        Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
        // Add 5 pairs of same Object starting from 11 to 20
        for (int i = 11; i < 21; ) {
            region1.put(Integer.toString(i), new Portfolio(i));
            region1.put(Integer.toString(i + 1), new Portfolio(i));
            i += 2;
        }
        Query query;
        SelectResults result;
        final int[] num = new int[1];
        num[0] = 0;
        final int[] numRepeat = new int[1];
        numRepeat[0] = 0;
        final Set data = new HashSet();
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterIterationEvaluation(Object result) {
                num[0] += 1;
            }

            public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
                if (data.contains(currObject)) {
                    numRepeat[0] += 1;
                } else {
                    data.add(currObject);
                }
            }
        });
        String queryString = "SELECT DISTINCT pf.ID FROM /portfolios1  pf WHERE pf.ID > 10 limit 5";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertEquals((5 + numRepeat[0]), num[0]);
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, wrapper.asSet().size());
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 12 with CompiledValue

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForStructBag.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for StructBag wrapped by a SelectResults if the iteration included
   * duplicate elements. If the distinct clause is present then duplicate elements even if
   * satisfying the where clause should not be considered as part of the resultset as distinct will
   * eliminate them
   * 
   * Tests StructBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForStructBag() {
    try {
        Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
        // Add 5 pairs of same Object starting from 11 to 20
        for (int i = 11; i < 21; ) {
            region1.put(Integer.toString(i), new Portfolio(i));
            region1.put(Integer.toString(i + 1), new Portfolio(i));
            i += 2;
        }
        Query query;
        SelectResults result;
        final int[] num = new int[1];
        num[0] = 0;
        final int[] numRepeat = new int[1];
        numRepeat[0] = 0;
        final Set data = new HashSet();
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterIterationEvaluation(Object result) {
                num[0] += 1;
            }

            public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
                if (data.contains(currObject)) {
                    numRepeat[0] += 1;
                } else if (currObject instanceof Portfolio) {
                    data.add(currObject);
                }
            }
        });
        String queryString = "SELECT DISTINCT * FROM /portfolios1  pf, pf.collectionHolderMap.keySet  WHERE pf.ID > 10 limit 20";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertEquals((20 + 4 * numRepeat[0]), num[0]);
        assertTrue(result instanceof SelectResults);
        assertEquals(20, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(20, wrapper.asSet().size());
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 13 with CompiledValue

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForStructBag.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for StructBag wrapped by a SelectResults if the iteration included
   * duplicate elements. If the distinct clause is present then duplicate elements even if
   * satisfying the where clause should not be considered as part as distinct will eliminate them.
   * This test validates the above behaviour if projection attribute is present and the projection
   * attribute may be duplicate
   * 
   * Tests StructBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationWithProjectionAttributeForStructBag() {
    try {
        Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
        // Add 5 pairs of same Object starting from 11 to 20
        for (int i = 11; i < 21; ) {
            region1.put(Integer.toString(i), new Portfolio(i));
            region1.put(Integer.toString(i + 1), new Portfolio(i));
            i += 2;
        }
        Query query;
        SelectResults result;
        final int[] num = new int[1];
        num[0] = 0;
        final int[] numRepeat = new int[1];
        numRepeat[0] = 0;
        final Set data = new HashSet();
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterIterationEvaluation(Object result) {
                num[0] += 1;
            }

            public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
                if (data.contains(currObject)) {
                    numRepeat[0] += 1;
                } else {
                    data.add(currObject);
                }
            }
        });
        String queryString = "SELECT DISTINCT pf.ID , pf.createTime FROM /portfolios1  pf WHERE pf.ID > 10 limit 5";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertEquals((5 + numRepeat[0]), num[0]);
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, wrapper.asSet().size());
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 14 with CompiledValue

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

the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForResultBag.

/**
   * Tests the limit functionality for Iter evaluated query with distinct clause This tests the
   * basic limit functionality for ResultBag wrapped by a SelectResults if the iteration included
   * duplicate elements. If the distinct clause is present then duplicate elements even if
   * satisfying the where clause should not be considered as part of the resultset as distinct will
   * eliminate them
   * 
   * Tests ResultBag behaviour
   */
@Test
public void testLimitDistinctIterEvaluatedQueryWithDuplicatesInIterationForResultBag() {
    try {
        Region region1 = CacheUtils.createRegion("portfolios1", Portfolio.class);
        // Add 5 pairs of same Object starting from 11 to 20
        for (int i = 11; i < 21; ) {
            region1.put(Integer.toString(i), new Portfolio(i));
            region1.put(Integer.toString(i + 1), new Portfolio(i));
            i += 2;
        }
        Query query;
        SelectResults result;
        final int[] num = new int[1];
        final int[] numRepeat = new int[1];
        numRepeat[0] = 0;
        final Set data = new HashSet();
        num[0] = 0;
        // In the worst possible case all the unique values come in
        // consecutive order & hence only 5 iterations will yield the
        // result
        QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {

            public void afterIterationEvaluation(Object result) {
                num[0] += 1;
            }

            public void beforeIterationEvaluation(CompiledValue ritr, Object currObject) {
                if (data.contains(currObject)) {
                    numRepeat[0] += 1;
                } else {
                    data.add(currObject);
                }
            }
        });
        String queryString = "SELECT DISTINCT * FROM /portfolios1  pf WHERE pf.ID > 10 limit 5";
        query = qs.newQuery(queryString);
        result = (SelectResults) query.execute();
        assertEquals((5 + numRepeat[0]), num[0]);
        assertTrue(result instanceof SelectResults);
        assertEquals(5, result.size());
        SelectResults wrapper = (SelectResults) result;
        assertEquals(5, wrapper.asSet().size());
    } catch (Exception e) {
        CacheUtils.getLogger().error(e);
        fail(e.toString());
    } finally {
        QueryObserverHolder.setInstance(new QueryObserverAdapter());
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) QueryObserver(org.apache.geode.cache.query.internal.QueryObserver) SelectResults(org.apache.geode.cache.query.SelectResults) QueryObserverAdapter(org.apache.geode.cache.query.internal.QueryObserverAdapter) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) HashSet(java.util.HashSet) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 15 with CompiledValue

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

Aggregations

CompiledValue (org.apache.geode.cache.query.internal.CompiledValue)21 HashSet (java.util.HashSet)7 List (java.util.List)6 Set (java.util.Set)6 Region (org.apache.geode.cache.Region)5 SelectResults (org.apache.geode.cache.query.SelectResults)5 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)5 RuntimeIterator (org.apache.geode.cache.query.internal.RuntimeIterator)5 LocalRegion (org.apache.geode.internal.cache.LocalRegion)5 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 IndexInvalidException (org.apache.geode.cache.query.IndexInvalidException)4 Query (org.apache.geode.cache.query.Query)4 Portfolio (org.apache.geode.cache.query.data.Portfolio)4 CompiledIteratorDef (org.apache.geode.cache.query.internal.CompiledIteratorDef)4 CompiledPath (org.apache.geode.cache.query.internal.CompiledPath)4 QueryObserver (org.apache.geode.cache.query.internal.QueryObserver)4 AmbiguousNameException (org.apache.geode.cache.query.AmbiguousNameException)3 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)3