Search in sources :

Example 1 with SelectResults

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

the class CompositeGroupJunction method evaluateOrJunction.

/** invariant: the operand is known to be evaluated by iteration */
private SelectResults evaluateOrJunction(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    SelectResults intermediateResults = null;
    Iterator itr = this.filterableCC.iterator();
    Support.Assert(this.iterOperands == null || this.iterOperands.isEmpty(), "The iter operands shoudl not have been present for OR junction");
    while (itr.hasNext()) {
        CompiledValue cc = (CompiledComparison) itr.next();
        SelectResults sr = ((Filter) cc).filterEvaluate(context, null, this.completeExpansion, null, this.indpndnts, false, true, false);
        intermediateResults = (intermediateResults == null) ? sr : QueryUtils.union(intermediateResults, sr, context);
    }
    // TODO:Asif Identify a cleaner approach
    if (this.groupJunctions != null) {
        int len = this.groupJunctions.size();
        if (len > 1) {
            // Asif : Create an array of SelectResults for each of the GroupJunction
            // For each Group Junction there will be a corresponding array of
            // RuntimeIterators which will map to the fields of the ResultSet
            // obtained from Group Junction.
            SelectResults[] grpResults = new SelectResults[1];
            // Asif : the final list will be some of all the iterators depending on
            // each of indpendent group if complete expansion is not needed
            List finalList = null;
            if (this.completeExpansion) {
                finalList = context.getCurrentIterators();
            } else {
                finalList = QueryUtils.getDependentItrChainForIndpndntItrs(this.indpndnts, context);
            }
            RuntimeIterator[][] itrsForResultFields = new RuntimeIterator[1][];
            AbstractGroupOrRangeJunction gj = null;
            Iterator junctionItr = this.groupJunctions.iterator();
            List grpItrs = null;
            RuntimeIterator tempItr = null;
            while (junctionItr.hasNext()) {
                List expansionList = new LinkedList(finalList);
                gj = (AbstractGroupOrRangeJunction) junctionItr.next();
                grpResults[0] = ((Filter) gj).filterEvaluate(context, null);
                grpItrs = context.getCurrScopeDpndntItrsBasedOnSingleIndpndntItr(gj.getIndependentIteratorForGroup()[0]);
                itrsForResultFields[0] = new RuntimeIterator[grpItrs.size()];
                Iterator grpItr = grpItrs.iterator();
                int k = 0;
                while (grpItr.hasNext()) {
                    tempItr = (RuntimeIterator) grpItr.next();
                    itrsForResultFields[0][k++] = tempItr;
                    expansionList.remove(tempItr);
                }
                SelectResults expandedResult = QueryUtils.cartesian(grpResults, itrsForResultFields, expansionList, finalList, context, null);
                intermediateResults = (intermediateResults == null) ? expandedResult : QueryUtils.union(expandedResult, intermediateResults, context);
            }
        } else {
            // Asif : If there exists only one GroupJunction in a
            // CompsoiteGroupJunction , then we can afford to expand the
            // result of GroupJunction to the level of CompositeGroupJunction
            // directly within the GroupJunction Thus create a new GroupJunction
            // with independent group of iterators same as that of
            // CompositeGroupJunction This is the only special case where in
            // GroupJunction will contain more than one independent iterators If
            // Complete expansion is true, In this case we should not pass the
            // Group of iterators belonging to the CompositeGroupJunction but stick
            // to independent iterator for the group.
            AbstractGroupOrRangeJunction newGJ = (AbstractGroupOrRangeJunction) this.groupJunctions.get(0);
            if (!this.completeExpansion) {
                // TODO:Asif: Check it out ..................
                newGJ = newGJ.recreateFromOld(this.completeExpansion, this.indpndnts, null);
            }
            SelectResults rs = newGJ.filterEvaluate(context, null);
            intermediateResults = (intermediateResults == null) ? rs : QueryUtils.union(rs, intermediateResults, context);
        }
    }
    return intermediateResults;
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList)

Example 2 with SelectResults

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

the class CompiledJunction method auxIterateEvaluate.

/** invariant: the operand is known to be evaluated by iteration */
SelectResults auxIterateEvaluate(CompiledValue operand, ExecutionContext context, SelectResults intermediateResults) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    // signifies that the junction cannot be evaluated as a filter.
    if (intermediateResults == null)
        throw new RuntimeException(LocalizedStrings.CompiledJunction_INTERMEDIATERESULTS_CAN_NOT_BE_NULL.toLocalizedString());
    if (// short circuit
    intermediateResults.isEmpty())
        return intermediateResults;
    List currentIters = context.getCurrentIterators();
    RuntimeIterator[] rIters = new RuntimeIterator[currentIters.size()];
    currentIters.toArray(rIters);
    ObjectType elementType = intermediateResults.getCollectionType().getElementType();
    SelectResults resultSet;
    if (elementType.isStructType()) {
        resultSet = QueryUtils.createStructCollection(context, (StructTypeImpl) elementType);
    } else {
        resultSet = QueryUtils.createResultCollection(context, elementType);
    }
    QueryObserver observer = QueryObserverHolder.getInstance();
    try {
        observer.startIteration(intermediateResults, operand);
        Iterator iResultsIter = intermediateResults.iterator();
        while (iResultsIter.hasNext()) {
            Object tuple = iResultsIter.next();
            if (tuple instanceof Struct) {
                Object[] values = ((Struct) tuple).getFieldValues();
                for (int i = 0; i < values.length; i++) {
                    rIters[i].setCurrent(values[i]);
                }
            } else {
                rIters[0].setCurrent(tuple);
            }
            Object result = null;
            try {
                result = operand.evaluate(context);
            } finally {
                observer.afterIterationEvaluation(result);
            }
            if (result instanceof Boolean) {
                if (((Boolean) result).booleanValue())
                    resultSet.add(tuple);
            } else if (result != null && result != QueryService.UNDEFINED)
                throw new TypeMismatchException(LocalizedStrings.CompiledJunction_ANDOR_OPERANDS_MUST_BE_OF_TYPE_BOOLEAN_NOT_TYPE_0.toLocalizedString(result.getClass().getName()));
        }
    } finally {
        observer.endIteration(resultSet);
    }
    return resultSet;
}
Also used : TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) Struct(org.apache.geode.cache.query.Struct) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with SelectResults

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

the class CompiledJunction method filterEvaluate.

@Override
public SelectResults filterEvaluate(ExecutionContext context, SelectResults intermediateResults) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    OrganizedOperands newOperands = organizeOperands(context);
    SelectResults result = intermediateResults;
    Support.Assert(newOperands.filterOperand != null);
    // evaluate directly on the operand
    result = newOperands.isSingleFilter ? (newOperands.filterOperand).filterEvaluate(context, result) : (newOperands.filterOperand).auxFilterEvaluate(context, result);
    if (!newOperands.isSingleFilter) {
        List unevaluatedOps = ((CompiledJunction) newOperands.filterOperand).unevaluatedFilterOperands;
        if (unevaluatedOps != null) {
            if (newOperands.iterateOperand == null) {
                if (unevaluatedOps.size() == 1) {
                    newOperands.iterateOperand = (CompiledValue) unevaluatedOps.get(0);
                } else {
                    int len = unevaluatedOps.size();
                    CompiledValue[] iterOps = new CompiledValue[len];
                    for (int i = 0; i < len; i++) {
                        iterOps[i] = (CompiledValue) unevaluatedOps.get(i);
                    }
                    newOperands.iterateOperand = new CompiledJunction(iterOps, getOperator());
                }
            } else {
                // You cannot have two CompiledJunctions a the same level
                if (newOperands.iterateOperand instanceof CompiledJunction) {
                    CompiledJunction temp = (CompiledJunction) newOperands.iterateOperand;
                    List prevOps = temp.getOperands();
                    unevaluatedOps.addAll(prevOps);
                } else {
                    unevaluatedOps.add(newOperands.iterateOperand);
                }
                int totalLen = unevaluatedOps.size();
                CompiledValue[] combinedOps = new CompiledValue[totalLen];
                Iterator itr = unevaluatedOps.iterator();
                int j = 0;
                while (itr.hasNext()) {
                    combinedOps[j++] = (CompiledValue) itr.next();
                }
                newOperands.iterateOperand = new CompiledJunction(combinedOps, getOperator());
            }
        }
    }
    if (newOperands.iterateOperand != null)
        // call private method here to evaluate
        result = auxIterateEvaluate(newOperands.iterateOperand, context, result);
    return result;
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with SelectResults

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

the class CompiledSelect method prepareResultType.

protected ObjectType prepareResultType(ExecutionContext context) throws TypeMismatchException, AmbiguousNameException {
    // if no projection attributes or '*'as projection attribute
    // & more than one/RunTimeIterator then create a StrcutSet.
    // If attribute is null or '*' & only one RuntimeIterator then create a
    // ResultSet.
    // If single attribute is present without alias name , then create
    // ResultSet
    // Else if more than on attribute or single attribute with alias is
    // present then return a StrcutSet
    // create StructSet which will contain root objects of all iterators in
    // from clause
    ObjectType elementType = null;
    SelectResults sr = null;
    List currentIterators = context.getCurrentIterators();
    if (projAttrs == null) {
        if (currentIterators.size() == 1) {
            RuntimeIterator iter = (RuntimeIterator) currentIterators.get(0);
            elementType = iter.getElementType();
        } else {
            elementType = createStructTypeForNullProjection(currentIterators, context);
        }
    } else {
        // Create StructType for projection attributes
        int projCount = projAttrs.size();
        String[] fieldNames = new String[projCount];
        ObjectType[] fieldTypes = new ObjectType[projCount];
        boolean createStructSet = false;
        String fldName = null;
        for (int i = 0; i < projCount; i++) {
            Object[] projDef = (Object[]) projAttrs.get(i);
            fldName = (String) projDef[0];
            if (!createStructSet) {
                if (fldName != null || projCount > 1) {
                    createStructSet = true;
                }
            }
            fieldNames[i] = (fldName == null && createStructSet) ? generateProjectionName((CompiledValue) projDef[1], context) : fldName;
            fieldTypes[i] = getFieldTypeOfProjAttrib(context, (CompiledValue) projDef[1]);
        // fieldTypes[i] = TypeUtils.OBJECT_TYPE;
        }
        if (createStructSet) {
            elementType = new StructTypeImpl(fieldNames, fieldTypes);
        } else {
            elementType = fieldTypes[0];
        }
    }
    return elementType;
}
Also used : PdxString(org.apache.geode.pdx.internal.PdxString) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with SelectResults

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

the class CompiledSelect method prepareEmptyResultSet.

private SelectResults prepareEmptyResultSet(ExecutionContext context, boolean ignoreOrderBy) throws TypeMismatchException, AmbiguousNameException {
    // if no projection attributes or '*'as projection attribute
    // & more than one/RunTimeIterator then create a StrcutSet.
    // If attribute is null or '*' & only one RuntimeIterator then create a
    // ResultSet.
    // If single attribute is present without alias name , then create
    // ResultSet
    // Else if more than on attribute or single attribute with alias is
    // present then return a StrcutSet
    // create StructSet which will contain root objects of all iterators in
    // from clause
    ObjectType elementType = this.cachedElementTypeForOrderBy != null ? this.cachedElementTypeForOrderBy : prepareResultType(context);
    SelectResults results;
    if (this.distinct || !this.count) {
        if (this.orderByAttrs != null) {
            boolean nullValuesAtStart = !((CompiledSortCriterion) orderByAttrs.get(0)).getCriterion();
            if (elementType.isStructType()) {
                if (ignoreOrderBy) {
                    results = this.distinct ? new LinkedStructSet((StructTypeImpl) elementType) : new SortedResultsBag(elementType, nullValuesAtStart);
                } else {
                    OrderByComparator comparator = this.hasUnmappedOrderByCols ? new OrderByComparatorUnmapped(this.orderByAttrs, (StructTypeImpl) elementType, context) : new OrderByComparator(this.orderByAttrs, (StructTypeImpl) elementType, context);
                    results = this.distinct ? new SortedStructSet(comparator, (StructTypeImpl) elementType) : new SortedStructBag(comparator, (StructType) elementType, nullValuesAtStart);
                }
            } else {
                if (ignoreOrderBy) {
                    results = this.distinct ? new LinkedResultSet() : new SortedResultsBag(nullValuesAtStart);
                } else {
                    OrderByComparator comparator = this.hasUnmappedOrderByCols ? new OrderByComparatorUnmapped(this.orderByAttrs, elementType, context) : new OrderByComparator(this.orderByAttrs, elementType, context);
                    results = this.distinct ? new SortedResultSet(comparator) : new SortedResultsBag(comparator, nullValuesAtStart);
                }
                results.setElementType(elementType);
            }
        } else {
            if (this.distinct) {
                if (elementType.isStructType()) {
                    results = new StructSet((StructType) elementType);
                } else {
                    results = new ResultsSet(elementType);
                }
            } else {
                if (elementType.isStructType()) {
                    results = new StructBag((StructType) elementType, context.getCachePerfStats());
                } else {
                    results = new ResultsBag(elementType, context.getCachePerfStats());
                }
            }
        }
    } else {
        // Shobhit: If its a 'COUNT' query and no End processing required Like for
        // 'DISTINCT'
        // we can directly keep count in ResultSet and ResultBag is good enough
        // for that.
        results = new ResultsBag(new ObjectTypeImpl(Integer.class), 1, /*
                                                               * initial capacity for count value
                                                               */
        context.getCachePerfStats());
        countStartQueryResult = 0;
    }
    return results;
}
Also used : StructType(org.apache.geode.cache.query.types.StructType) ObjectTypeImpl(org.apache.geode.cache.query.internal.types.ObjectTypeImpl) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl)

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