Search in sources :

Example 6 with ObjectType

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

the class CompiledSelect method getFieldTypeOfProjAttrib.

private ObjectType getFieldTypeOfProjAttrib(ExecutionContext context, CompiledValue cv) throws TypeMismatchException, AmbiguousNameException {
    // Identify the RuntimeIterator for the compiled value
    ObjectType retType = TypeUtils.OBJECT_TYPE;
    try {
        RuntimeIterator rit = context.findRuntimeIterator(cv);
        List pathOnItr = cv.getPathOnIterator(rit, context);
        if (pathOnItr != null) {
            String[] path = (String[]) pathOnItr.toArray(new String[pathOnItr.size()]);
            ObjectType[] ot = PathUtils.calculateTypesAlongPath(rit.getElementType(), path);
            retType = ot[ot.length - 1];
        }
    } catch (NameNotFoundException ignore) {
    // Unable to determine the type Of attribute.It will default to
    // ObjectType
    }
    return retType;
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) NameNotFoundException(org.apache.geode.cache.query.NameNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) PdxString(org.apache.geode.pdx.internal.PdxString)

Example 7 with ObjectType

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

Example 8 with ObjectType

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

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

the class AllGroupJunction method evaluateAndJunction.

/**
   * Asif:Evaluates the individual GroupJunctions and CompositeGroupJunctions and does a cartesian
   * of the results so obtained and simultaneously expanding it to the query from clause level as
   * well as evaluating any iter evaluatable conditions. The evaluated result of an AllGroupJunction
   * will always be of the query from clause level which can be ORed or ANDd with filter evaluatable
   * subtree CompiledJunction
   * 
   * @param context ExecutionContext object
   * @return SelectResults
   * @throws FunctionDomainException
   * @throws TypeMismatchException
   * @throws NameResolutionException
   * @throws QueryInvocationTargetException
   */
// Asif : For doing the Cartesian first evaluate the result of all Group
// Junction. Doing Cartesian of all the Results together is better than doing
// in pair
private SelectResults evaluateAndJunction(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    int len = this.abstractGroupOrRangeJunctions.size();
    // 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[] results = new SelectResults[len];
    List finalList = context.getCurrentIterators();
    List expansionList = new LinkedList(finalList);
    RuntimeIterator[][] itrsForResultFields = new RuntimeIterator[len][];
    CompiledValue gj = null;
    Iterator junctionItr = this.abstractGroupOrRangeJunctions.iterator();
    List grpItrs = null;
    int j = 0;
    RuntimeIterator tempItr = null;
    while (junctionItr.hasNext()) {
        gj = (CompiledValue) junctionItr.next();
        SelectResults filterResults = ((Filter) gj).filterEvaluate(context, null);
        Support.Assert(filterResults != null, "FilterResults cannot be null here");
        if (filterResults.isEmpty()) {
            if (finalList.size() > 1) {
                StructType type = QueryUtils.createStructTypeForRuntimeIterators(finalList);
                return QueryUtils.createStructCollection(context, type);
            } else {
                ObjectType type = ((RuntimeIterator) finalList.iterator().next()).getElementType();
                if (type instanceof StructType) {
                    return QueryUtils.createStructCollection(context, (StructTypeImpl) type);
                } else {
                    return QueryUtils.createResultCollection(context, type);
                }
            }
        } else {
            results[j] = filterResults;
            grpItrs = (gj instanceof CompositeGroupJunction) ? QueryUtils.getDependentItrChainForIndpndntItrs(((CompositeGroupJunction) gj).getIndependentIteratorsOfCJ(), context) : context.getCurrScopeDpndntItrsBasedOnSingleIndpndntItr(((AbstractGroupOrRangeJunction) gj).getIndependentIteratorForGroup()[0]);
            itrsForResultFields[j] = new RuntimeIterator[grpItrs.size()];
            Iterator grpItr = grpItrs.iterator();
            int k = 0;
            while (grpItr.hasNext()) {
                tempItr = (RuntimeIterator) grpItr.next();
                itrsForResultFields[j][k++] = tempItr;
                expansionList.remove(tempItr);
            }
            ++j;
        }
    }
    SelectResults resultsSet = null;
    // Asif : Do the Cartesian of the different group junction results.
    CompiledValue iterOperandsToSend = null;
    if (!iterOperands.isEmpty()) {
        // TODO ASIF : Avoid creation of CompiledJunction by providing
        // functionality in AllGroupJunction for evaluating condition
        int size = iterOperands.size();
        CompiledValue[] cv = new CompiledValue[size];
        for (int k = 0; k < size; ++k) {
            cv[k] = (CompiledValue) this.iterOperands.get(k);
        }
        if (cv.length == 1) {
            iterOperandsToSend = cv[0];
        } else {
            iterOperandsToSend = new CompiledJunction(cv, this.operator);
        }
    }
    QueryObserver observer = QueryObserverHolder.getInstance();
    observer.beforeCartesianOfGroupJunctionsInAnAllGroupJunctionOfType_AND(results);
    resultsSet = QueryUtils.cartesian(results, itrsForResultFields, expansionList, finalList, context, iterOperandsToSend);
    observer.afterCartesianOfGroupJunctionsInAnAllGroupJunctionOfType_AND();
    Support.Assert(resultsSet != null, "ResultsSet obtained was NULL in AllGroupJunction");
    return resultsSet;
}
Also used : StructType(org.apache.geode.cache.query.types.StructType) ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults)

Example 10 with ObjectType

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

the class QueryUtils method cartesian.

/**
   * This util function does a cartesian of the array of SelectResults object , expanding the
   * resultant set to the number of iterators passed in expansionList. The position of the iterator
   * fields in the final result is governed by the order of RuntimeIterators present in the
   * finalList. If any condition needs to be evaluated during cartesian , it can be passed as
   * operand
   * 
   * @param results Array of SelectResults object which are to be cartesianed
   * @param itrsForResultFields A two dimensional array of RuntimeIterator. Each row of this two
   *        dimensional RuntimeIterator array , maps to a SelectResults object in the results array.
   *        Thus the 0th row of two dimensional RuntimeIterator array will map to the 0th element of
   *        the SelectResults array. The order of RuntimeIterator in a row will map to the fields in
   *        the SelectResults object. The 0th RuntimeIterator will map to the 0th field of the
   *        corresponding SelectResults object. The number of rows in the two dimensional array of
   *        RuntimeIterator should be equal to the size of array of SelectResults object passed and
   *        the number of RuntimeIterators in each row should be equal to the number of fields in
   *        the SelectResults object . The SelectResults object itself may be a ResultBag object or
   *        a StructBag object.
   * 
   * @param expansionList List containing RunimeIterators to which the final Results should be
   *        expanded to.
   * @param finalList List containing RuntimeIterators which define the number of fields to be
   *        present in the resultant SelectResults and their relative positions. The Runtime
   *        Iterators present in the List should be either available in the expansion List or should
   *        be present in each row of the two dimensional RuntimeIterator array.
   * 
   * @param context ExecutionContext object
   * @param operand The CompiledValue which needs to be iter evaluated during cartesian. Only those
   *        tuples will be selected in the final Result for which oerand evaluates to true.
   * @return SelectResults object representing the final result of the cartesian
   */
public static SelectResults cartesian(SelectResults[] results, RuntimeIterator[][] itrsForResultFields, List expansionList, List finalList, ExecutionContext context, CompiledValue operand) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    SelectResults returnSet = null;
    if (finalList.size() == 1) {
        ObjectType type = ((RuntimeIterator) finalList.iterator().next()).getElementType();
        if (type instanceof StructType) {
            returnSet = QueryUtils.createStructCollection(context, (StructTypeImpl) type);
        } else {
            return QueryUtils.createResultCollection(context, type);
        }
    } else {
        StructType structType = createStructTypeForRuntimeIterators(finalList);
        returnSet = QueryUtils.createStructCollection(context, structType);
    }
    ListIterator expnItr = expansionList.listIterator();
    doNestedIterations(0, returnSet, results, itrsForResultFields, finalList, expnItr, results.length + expansionList.size(), context, operand);
    return returnSet;
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) SelectResults(org.apache.geode.cache.query.SelectResults) StructType(org.apache.geode.cache.query.types.StructType) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) ListIterator(java.util.ListIterator)

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