Search in sources :

Example 6 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class CompiledSelect method applyProjectionOnCollection.

private SelectResults applyProjectionOnCollection(SelectResults resultSet, ExecutionContext context, boolean ignoreOrderBy) throws TypeMismatchException, AmbiguousNameException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    List iterators = context.getCurrentIterators();
    if (projAttrs == null && (this.orderByAttrs == null || ignoreOrderBy)) {
        // as it is.
        if (iterators.size() > 1) {
            StructType type = createStructTypeForNullProjection(iterators, context);
            ((SelectResults) resultSet).setElementType(type);
        }
        return resultSet;
    } else {
        int numElementsAdded = 0;
        SelectResults pResultSet = prepareEmptyResultSet(context, ignoreOrderBy);
        boolean isStructType = resultSet.getCollectionType().getElementType() != null && resultSet.getCollectionType().getElementType().isStructType();
        if (isStructType) {
            Iterator resultsIter = resultSet.iterator();
            // Apply limit if there is no order by.
            Integer limitValue = evaluateLimitValue(context, this.limit);
            while (((this.orderByAttrs != null && !ignoreOrderBy) || limitValue < 0 || (numElementsAdded < limitValue)) && resultsIter.hasNext()) {
                // Check if query execution on this thread is canceled.
                QueryMonitor.isQueryExecutionCanceled();
                Object[] values = ((Struct) resultsIter.next()).getFieldValues();
                for (int i = 0; i < values.length; i++) {
                    ((RuntimeIterator) iterators.get(i)).setCurrent(values[i]);
                }
                int occurence = applyProjectionAndAddToResultSet(context, pResultSet, ignoreOrderBy);
                if (occurence == 1 || (occurence > 1 && !this.distinct)) {
                    // (Unique i.e first time occurence) or subsequent occurence
                    // for non distinct query
                    ++numElementsAdded;
                }
            }
        // return pResultSet;
        } else if (iterators.size() == 1) {
            RuntimeIterator rIter = (RuntimeIterator) iterators.get(0);
            Iterator resultsIter = ((SelectResults) resultSet).iterator();
            // Apply limit if there is no order by.
            Integer limitValue = evaluateLimitValue(context, this.limit);
            while (((this.orderByAttrs != null && !ignoreOrderBy) || limitValue < 0 || (numElementsAdded < limitValue)) && resultsIter.hasNext()) {
                rIter.setCurrent(resultsIter.next());
                int occurence = applyProjectionAndAddToResultSet(context, pResultSet, ignoreOrderBy);
                if (occurence == 1 || (occurence > 1 && !this.distinct)) {
                    // (Unique i.e first time occurence) or subsequent occurence
                    // for non distinct query
                    ++numElementsAdded;
                }
            }
        } else {
            throw new RuntimeException(LocalizedStrings.CompiledSelect_RESULT_SET_DOES_NOT_MATCH_WITH_ITERATOR_DEFINITIONS_IN_FROM_CLAUSE.toLocalizedString());
        }
        return pResultSet;
    }
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) StructType(org.apache.geode.cache.query.types.StructType) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) Struct(org.apache.geode.cache.query.Struct)

Example 7 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class CompiledSelect method getEmptyResultSet.

/*
   * Gets the appropriate empty results set when outside of actual query evalutaion.
   *
   * @param parameters the parameters that will be passed into the query when evaluated
   * 
   * @param cache the cache the query will be executed in the context of
   * 
   * @return the empty result set of the appropriate type
   */
public SelectResults getEmptyResultSet(Object[] parameters, InternalCache cache, Query query) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    ExecutionContext context = new QueryExecutionContext(parameters, cache, query);
    computeDependencies(context);
    context.newScope((Integer) context.cacheGet(scopeID));
    context.pushExecCache((Integer) context.cacheGet(scopeID));
    SelectResults results = null;
    try {
        Iterator iter = iterators.iterator();
        while (iter.hasNext()) {
            CompiledIteratorDef iterDef = (CompiledIteratorDef) iter.next();
            RuntimeIterator rIter = iterDef.getRuntimeIterator(context);
            context.bindIterator(rIter);
        }
        results = prepareEmptyResultSet(context, false);
    } finally {
        context.popScope();
        context.popExecCache();
    }
    return results;
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Iterator(java.util.Iterator)

Example 8 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class CompiledGroupBySelect method applyAggregateAndGroupBy.

public SelectResults applyAggregateAndGroupBy(SelectResults baseResults, ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    ObjectType elementType = baseResults.getCollectionType().getElementType();
    boolean isStruct = elementType != null && elementType.isStructType();
    boolean isBucketNodes = context.getBucketList() != null;
    boolean createOrderedResultSet = isBucketNodes && this.orderByAttrs != null;
    boolean[] objectChangedMarker = new boolean[] { false };
    int limitValue = evaluateLimitValue(context, limit);
    SelectResults newResults = createResultSet(context, elementType, isStruct, createOrderedResultSet);
    Aggregator[] aggregators = new Aggregator[this.aggregateFunctions.length];
    refreshAggregators(aggregators, context);
    if (this.orderByAttrs != null) {
        applyGroupBy(baseResults, context, isStruct, newResults, aggregators, !createOrderedResultSet, objectChangedMarker, limitValue);
    } else {
        Iterator iter = baseResults.iterator();
        Object current = null;
        boolean unterminated = iter.hasNext();
        while (iter.hasNext()) {
            current = iter.next();
            accumulate(isStruct, aggregators, current, objectChangedMarker);
        }
        if (unterminated) {
            this.terminateAndAddToResults(isStruct, newResults, aggregators, current, context, !createOrderedResultSet, limitValue);
        }
    }
    return newResults;
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) Iterator(java.util.Iterator) Aggregator(org.apache.geode.cache.query.Aggregator)

Example 9 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class CompiledGroupBySelect method createResultSet.

private SelectResults createResultSet(ExecutionContext context, ObjectType elementType, boolean isStruct, boolean createOrderedResults) {
    elementType = createNewElementType(elementType, isStruct);
    SelectResults newResults;
    // boolean isBucketNodes = context.getBucketList() != null;
    boolean isPrQueryNode = context.getIsPRQueryNode();
    // If it is bucket nodes query, we need to return ordered data
    if (isStruct) {
        if (createOrderedResults) {
            newResults = new SortedResultsBag((StructTypeImpl) elementType, true);
        } else {
            if (this.originalOrderByClause != null) {
                Comparator comparator = new OrderByComparator(this.originalOrderByClause, elementType, context);
                newResults = new SortedStructBag(comparator, (StructType) elementType, !this.originalOrderByClause.get(0).getCriterion());
            } else {
                newResults = QueryUtils.createStructCollection(this.isDistinct, (StructType) elementType, context);
            }
        }
    } else {
        if (createOrderedResults) {
            newResults = new SortedResultsBag(elementType, true);
        } else {
            if (this.originalOrderByClause != null) {
                Comparator comparator = new OrderByComparator(this.originalOrderByClause, elementType, context);
                newResults = new SortedResultsBag(comparator, elementType, !this.originalOrderByClause.get(0).getCriterion());
            } else {
                newResults = QueryUtils.createResultCollection(this.isDistinct, elementType, context);
            }
        }
    }
    return newResults;
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) StructType(org.apache.geode.cache.query.types.StructType) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) Comparator(java.util.Comparator)

Example 10 with SelectResults

use of org.apache.geode.cache.query.SelectResults in project geode by apache.

the class CompiledLike method filterEvaluate.

@Override
public SelectResults filterEvaluate(ExecutionContext context, SelectResults intermediateResults) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    RuntimeIterator grpItr = (RuntimeIterator) QueryUtils.getCurrentScopeUltimateRuntimeIteratorsIfAny(this, context).iterator().next();
    OrganizedOperands newOperands = organizeOperands(context, true, new RuntimeIterator[] { grpItr });
    assert newOperands.iterateOperand == null;
    SelectResults result = intermediateResults;
    result = (newOperands.filterOperand).filterEvaluate(context, intermediateResults);
    return result;
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults)

Aggregations

SelectResults (org.apache.geode.cache.query.SelectResults)577 Test (org.junit.Test)423 Query (org.apache.geode.cache.query.Query)360 Region (org.apache.geode.cache.Region)336 QueryService (org.apache.geode.cache.query.QueryService)331 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)256 Portfolio (org.apache.geode.cache.query.data.Portfolio)249 Index (org.apache.geode.cache.query.Index)133 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)112 Host (org.apache.geode.test.dunit.Host)107 VM (org.apache.geode.test.dunit.VM)107 CacheException (org.apache.geode.cache.CacheException)105 Iterator (java.util.Iterator)104 AttributesFactory (org.apache.geode.cache.AttributesFactory)101 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)92 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)89 Cache (org.apache.geode.cache.Cache)84 Struct (org.apache.geode.cache.query.Struct)80 LocalRegion (org.apache.geode.internal.cache.LocalRegion)67 ObjectType (org.apache.geode.cache.query.types.ObjectType)66