Search in sources :

Example 1 with CompiledSortCriterion

use of org.apache.geode.cache.query.internal.CompiledSortCriterion 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 2 with CompiledSortCriterion

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

the class CompactRangeIndex method lockedQueryPrivate.

/** Method called while appropriate lock held on index */
private void lockedQueryPrivate(Object key, int operator, Collection results, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, Set keysToRemove, List projAttrib, SelectResults intermediateResults, boolean isIntersection) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    if (keysToRemove == null) {
        keysToRemove = new HashSet(0);
    }
    int limit = -1;
    Boolean applyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX);
    if (applyLimit != null && applyLimit) {
        limit = (Integer) context.cacheGet(CompiledValue.RESULT_LIMIT);
    }
    Boolean orderByClause = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
    boolean applyOrderBy = false;
    List orderByAttrs = null;
    if (orderByClause != null && orderByClause) {
        orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttrs.get(0);
        applyOrderBy = true;
    }
    if (isEmpty()) {
        return;
    }
    key = getPdxStringForIndexedPdxKeys(key);
    evaluate(key, operator, results, iterOps, runtimeItr, context, keysToRemove, projAttrib, intermediateResults, isIntersection, limit, applyOrderBy, orderByAttrs);
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 3 with CompiledSortCriterion

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

the class CompactRangeIndex method evaluate.

private void evaluate(Object key, int operator, Collection results, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, Set keysToRemove, List projAttrib, SelectResults intermediateResults, boolean isIntersection, int limit, boolean applyOrderBy, List orderByAttribs) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    boolean multiColOrderBy = false;
    if (keysToRemove == null) {
        keysToRemove = new HashSet(0);
    }
    key = TypeUtils.indexKeyFor(key);
    if (key == null) {
        key = IndexManager.NULL;
    }
    boolean asc = true;
    if (applyOrderBy) {
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttribs.get(0);
        asc = !csc.getCriterion();
        multiColOrderBy = orderByAttribs.size() > 1;
    }
    CloseableIterator<IndexStoreEntry> iterator = null;
    try {
        switch(operator) {
            case OQLLexerTokenTypes.TOK_EQ:
                assert keysToRemove.isEmpty();
                iterator = indexStore.get(key);
                addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                break;
            case OQLLexerTokenTypes.TOK_LT:
                {
                    if (asc) {
                        iterator = indexStore.iterator(null, true, key, false, keysToRemove);
                    } else {
                        iterator = indexStore.descendingIterator(null, true, key, false, keysToRemove);
                    }
                    addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                }
                break;
            case OQLLexerTokenTypes.TOK_LE:
                {
                    if (asc) {
                        iterator = indexStore.iterator(null, true, key, true, keysToRemove);
                    } else {
                        iterator = indexStore.descendingIterator(null, true, key, true, keysToRemove);
                    }
                    addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                }
                break;
            case OQLLexerTokenTypes.TOK_GT:
                {
                    if (asc) {
                        iterator = indexStore.iterator(key, false, keysToRemove);
                    } else {
                        iterator = indexStore.descendingIterator(key, false, keysToRemove);
                    }
                    addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                }
                break;
            case OQLLexerTokenTypes.TOK_GE:
                {
                    if (asc) {
                        iterator = indexStore.iterator(key, true, keysToRemove);
                    } else {
                        iterator = indexStore.descendingIterator(key, true, keysToRemove);
                    }
                    addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                }
                break;
            case OQLLexerTokenTypes.TOK_NE_ALT:
            case OQLLexerTokenTypes.TOK_NE:
                {
                    keysToRemove.add(key);
                    if (asc) {
                        iterator = indexStore.iterator(keysToRemove);
                    } else {
                        iterator = indexStore.descendingIterator(keysToRemove);
                    }
                    addToResultsFromEntries(key, operator, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                    // If the key is not null, then add the nulls to the results as this is a not equals query
                    if (!IndexManager.NULL.equals(key)) {
                        // we pass in the operator TOK_EQ because we want to add results where the key is equal
                        // to NULL
                        addToResultsFromEntries(IndexManager.NULL, OQLLexerTokenTypes.TOK_EQ, indexStore.get(IndexManager.NULL), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                    }
                    // equals query
                    if (!QueryService.UNDEFINED.equals(key)) {
                        // we pass in the operator TOK_EQ because we want to add results where the key is equal
                        // to UNDEFINED
                        addToResultsFromEntries(QueryService.UNDEFINED, OQLLexerTokenTypes.TOK_EQ, indexStore.get(QueryService.UNDEFINED), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
                    }
                }
                break;
            default:
                throw new AssertionError("Operator = " + operator);
        }
    // end switch
    } catch (ClassCastException ex) {
        if (operator == OQLLexerTokenTypes.TOK_EQ) {
            // result is empty set
            return;
        } else if (operator == OQLLexerTokenTypes.TOK_NE || operator == OQLLexerTokenTypes.TOK_NE_ALT) {
            // put all in result
            keysToRemove.add(key);
            try {
                if (asc) {
                    iterator = indexStore.iterator(keysToRemove);
                } else {
                    iterator = indexStore.descendingIterator(keysToRemove);
                }
                addToResultsFromEntries(key, OQLLexerTokenTypes.TOK_NE, iterator, results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit);
            } finally {
                if (iterator != null) {
                    iterator.close();
                }
            }
        } else {
            // otherwise throw exception
            throw new TypeMismatchException("", ex);
        }
    } finally {
        if (iterator != null) {
            iterator.close();
        }
    }
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) MemoryIndexStoreEntry(org.apache.geode.cache.query.internal.index.MemoryIndexStore.MemoryIndexStoreEntry) IndexStoreEntry(org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry) HashSet(java.util.HashSet)

Example 4 with CompiledSortCriterion

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

the class CompactRangeIndex method lockedQuery.

/** Method called while appropriate lock held on index */
void lockedQuery(Object lowerBoundKey, int lowerBoundOperator, Object upperBoundKey, int upperBoundOperator, Collection results, Set keysToRemove, ExecutionContext context) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    lowerBoundKey = TypeUtils.indexKeyFor(lowerBoundKey);
    upperBoundKey = TypeUtils.indexKeyFor(upperBoundKey);
    boolean lowerBoundInclusive = lowerBoundOperator == OQLLexerTokenTypes.TOK_GE;
    boolean upperBoundInclusive = upperBoundOperator == OQLLexerTokenTypes.TOK_LE;
    // LowerBound Key inclusive , Upper bound key exclusive
    int limit = -1;
    Boolean applyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX);
    if (applyLimit != null && applyLimit) {
        limit = (Integer) context.cacheGet(CompiledValue.RESULT_LIMIT);
    }
    Boolean orderByClause = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
    List orderByAttrs;
    boolean asc = true;
    boolean multiColOrderBy = false;
    if (orderByClause != null && orderByClause) {
        orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttrs.get(0);
        asc = !csc.getCriterion();
        multiColOrderBy = orderByAttrs.size() > 1;
    }
    // return if the index map is still empty at this stage
    if (isEmpty()) {
        return;
    }
    lowerBoundKey = getPdxStringForIndexedPdxKeys(lowerBoundKey);
    upperBoundKey = getPdxStringForIndexedPdxKeys(upperBoundKey);
    if (keysToRemove == null) {
        keysToRemove = new HashSet();
    }
    CloseableIterator<IndexStoreEntry> iterator = null;
    try {
        if (asc) {
            iterator = indexStore.iterator(lowerBoundKey, lowerBoundInclusive, upperBoundKey, upperBoundInclusive, keysToRemove);
        } else {
            iterator = indexStore.descendingIterator(lowerBoundKey, lowerBoundInclusive, upperBoundKey, upperBoundInclusive, keysToRemove);
        }
        addToResultsFromEntries(lowerBoundKey, upperBoundKey, lowerBoundOperator, upperBoundOperator, iterator, results, null, null, context, null, null, true, multiColOrderBy ? -1 : limit);
    } finally {
        if (iterator != null) {
            iterator.close();
        }
    }
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) MemoryIndexStoreEntry(org.apache.geode.cache.query.internal.index.MemoryIndexStore.MemoryIndexStoreEntry) IndexStoreEntry(org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry) List(java.util.List) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 5 with CompiledSortCriterion

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

the class HashIndex method evaluate.

private void evaluate(Object key, int operator, Collection results, CompiledValue iterOps, RuntimeIterator runtimeItr, ExecutionContext context, Set keysToRemove, List projAttrib, SelectResults intermediateResults, boolean isIntersection, int limit, boolean applyOrderBy, List orderByAttribs) throws TypeMismatchException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    boolean multiColOrderBy = false;
    if (keysToRemove == null) {
        keysToRemove = new HashSet(0);
    }
    key = TypeUtils.indexKeyFor(key);
    if (key == null) {
        key = IndexManager.NULL;
    }
    boolean asc = true;
    if (applyOrderBy) {
        CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttribs.get(0);
        asc = !csc.getCriterion();
        multiColOrderBy = orderByAttribs.size() > 1;
    }
    try {
        long iteratorCreationTime = GemFireCacheImpl.getInstance().cacheTimeMillis();
        switch(operator) {
            case OQLLexerTokenTypes.TOK_EQ:
                assert keysToRemove.isEmpty();
                addToResultsFromEntries(this.entriesSet.get(key), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit, keysToRemove, applyOrderBy, asc, iteratorCreationTime);
                break;
            case OQLLexerTokenTypes.TOK_NE_ALT:
            case OQLLexerTokenTypes.TOK_NE:
                {
                    keysToRemove.add(key);
                    addToResultsFromEntries(this.entriesSet.getAllNotMatching(keysToRemove), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit, keysToRemove, applyOrderBy, asc, iteratorCreationTime);
                }
                break;
            default:
                throw new AssertionError("Operator = " + operator);
        }
    // end switch
    } catch (ClassCastException ex) {
        if (operator == OQLLexerTokenTypes.TOK_EQ) {
            // set
            return;
        } else if (operator == OQLLexerTokenTypes.TOK_NE || operator == OQLLexerTokenTypes.TOK_NE_ALT) {
            // put
            keysToRemove.add(key);
            long iteratorCreationTime = GemFireCacheImpl.getInstance().cacheTimeMillis();
            addToResultsFromEntries(this.entriesSet.getAllNotMatching(keysToRemove), results, iterOps, runtimeItr, context, projAttrib, intermediateResults, isIntersection, multiColOrderBy ? -1 : limit, keysToRemove, applyOrderBy, asc, iteratorCreationTime);
        } else {
            // otherwise throw exception
            throw new TypeMismatchException("", ex);
        }
    }
}
Also used : CompiledSortCriterion(org.apache.geode.cache.query.internal.CompiledSortCriterion) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) HashSet(java.util.HashSet)

Aggregations

CompiledSortCriterion (org.apache.geode.cache.query.internal.CompiledSortCriterion)12 ArrayList (java.util.ArrayList)6 List (java.util.List)6 HashSet (java.util.HashSet)5 NavigableMap (java.util.NavigableMap)4 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)4 TypeMismatchException (org.apache.geode.cache.query.TypeMismatchException)4 Struct (org.apache.geode.cache.query.Struct)3 CompiledSelect (org.apache.geode.cache.query.internal.CompiledSelect)3 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)3 Region (org.apache.geode.cache.Region)2 Query (org.apache.geode.cache.query.Query)2 QueryService (org.apache.geode.cache.query.QueryService)2 SelectResults (org.apache.geode.cache.query.SelectResults)2 Portfolio (org.apache.geode.cache.query.data.Portfolio)2 IndexStoreEntry (org.apache.geode.cache.query.internal.index.IndexStore.IndexStoreEntry)2 MemoryIndexStoreEntry (org.apache.geode.cache.query.internal.index.MemoryIndexStore.MemoryIndexStoreEntry)2 Test (org.junit.Test)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1