Search in sources :

Example 21 with StructTypeImpl

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

the class CompiledComparison method singleBaseCollectionFilterEvaluate.

/** **************** PRIVATE METHODS ************************** */
/**
   * evaluate as a filter, involving a single iterator. Use an index if possible.
   */
// Invariant: the receiver is dependent on the current iterator.
private SelectResults singleBaseCollectionFilterEvaluate(ExecutionContext context, SelectResults intermediateResults, final boolean completeExpansionNeeded, @Retained CompiledValue iterOperands, IndexInfo indexInfo, RuntimeIterator[] indpndntItr, boolean isIntersection, boolean conditioningNeeded, boolean evaluateProj) throws TypeMismatchException, AmbiguousNameException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
    ObjectType resultType = indexInfo._index.getResultSetType();
    int indexFieldsSize = -1;
    SelectResults set = null;
    boolean createEmptySet = false;
    // Direct comparison with UNDEFINED will always return empty set
    Object key = indexInfo.evaluateIndexKey(context);
    createEmptySet = (key != null && key.equals(QueryService.UNDEFINED));
    if (resultType instanceof StructType) {
        indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
    } else {
        indexFieldsSize = 1;
    }
    // if the key is the LEFT operand, then reflect the operator
    // before the index lookup
    int op = reflectOnOperator(indexInfo._key());
    // actual index lookup
    QueryObserver observer = QueryObserverHolder.getInstance();
    List projAttrib = null;
    try {
        if (!createEmptySet) {
            observer.beforeIndexLookup(indexInfo._index, op, key);
            context.cachePut(CompiledValue.INDEX_INFO, indexInfo);
        }
        // //////////////////////////////////////////////////////////
        // Asif:Create an instance of IndexConditionHelper to see , if the match
        // level is zero & expansion is to Group level & that no reshuffling is
        // needed.
        // If this holds true , then we will try to evaluate iter operand while
        // collecting the results itself. Pass the iter operand as null so that
        // we get the right idea. Also right now we will assume that only single
        // iterator cases will be candidates for this oprtmization.
        // dependent iterators will come later.
        boolean useLinkedDataStructure = false;
        boolean nullValuesAtStart = true;
        Boolean orderByClause = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
        if (orderByClause != null && orderByClause.booleanValue()) {
            List orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
            useLinkedDataStructure = orderByAttrs.size() == 1;
            nullValuesAtStart = !((CompiledSortCriterion) orderByAttrs.get(0)).getCriterion();
        }
        // ////////////////////////////////////////////////////////////////
        if (!conditioningNeeded) {
            ObjectType projResultType = evaluateProj ? (ObjectType) context.cacheGet(RESULT_TYPE) : null;
            if (projResultType != null) {
                resultType = projResultType;
                projAttrib = (List) context.cacheGet(PROJ_ATTRIB);
                context.cachePut(RESULT_TYPE, Boolean.TRUE);
            }
            if (isIntersection) {
                if (resultType instanceof StructType) {
                    context.getCache().getLogger().fine("StructType resultType.class=" + resultType.getClass().getName());
                    if (useLinkedDataStructure) {
                        set = context.isDistinct() ? new LinkedStructSet((StructTypeImpl) resultType) : new SortedResultsBag<Struct>((StructTypeImpl) resultType, nullValuesAtStart);
                    } else {
                        set = QueryUtils.createStructCollection(context, (StructTypeImpl) resultType);
                    }
                    indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
                } else {
                    context.getCache().getLogger().fine("non-StructType resultType.class=" + resultType.getClass().getName());
                    if (useLinkedDataStructure) {
                        set = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType, nullValuesAtStart);
                    } else {
                        set = QueryUtils.createResultCollection(context, resultType);
                    }
                    indexFieldsSize = 1;
                }
            } else {
                if (intermediateResults != null && context.getQuery() != null && ((DefaultQuery) context.getQuery()).getSelect().isDistinct()) {
                    set = intermediateResults;
                    intermediateResults = null;
                } else {
                    if (resultType instanceof StructType) {
                        context.getCache().getLogger().fine("StructType resultType.class=" + resultType.getClass().getName());
                        if (useLinkedDataStructure) {
                            set = context.isDistinct() ? new LinkedStructSet((StructTypeImpl) resultType) : new SortedResultsBag<Struct>((StructTypeImpl) resultType, nullValuesAtStart);
                        } else {
                            set = QueryUtils.createStructCollection(context, (StructTypeImpl) resultType);
                        }
                        indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
                    } else {
                        context.getCache().getLogger().fine("non-StructType resultType.class=" + resultType.getClass().getName());
                        if (useLinkedDataStructure) {
                            set = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType, nullValuesAtStart);
                        } else {
                            set = QueryUtils.createResultCollection(context, resultType);
                        }
                        indexFieldsSize = 1;
                    }
                }
            }
            if (!createEmptySet) {
                indexInfo._index.query(key, op, set, iterOperands, indpndntItr != null ? indpndntItr[0] : null, context, projAttrib, intermediateResults, isIntersection);
            }
        } else {
            if (resultType instanceof StructType) {
                context.getCache().getLogger().fine("StructType resultType.class=" + resultType.getClass().getName());
                if (useLinkedDataStructure) {
                    set = context.isDistinct() ? new LinkedStructSet((StructTypeImpl) resultType) : new SortedResultsBag<Struct>((StructTypeImpl) resultType, nullValuesAtStart);
                } else {
                    set = QueryUtils.createStructCollection(context, (StructTypeImpl) resultType);
                }
                indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
            } else {
                context.getCache().getLogger().fine("non-StructType resultType.class=" + resultType.getClass().getName());
                if (useLinkedDataStructure) {
                    set = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType, nullValuesAtStart);
                } else {
                    set = QueryUtils.createResultCollection(context, resultType);
                }
                indexFieldsSize = 1;
            }
            if (!createEmptySet) {
                // tow rows qualify from index
                indexInfo._index.query(key, op, set, context);
            }
        // look up. rahul
        }
    } finally {
        if (!createEmptySet) {
            observer.afterIndexLookup(set);
        }
    }
    if (conditioningNeeded) {
        return QueryUtils.getConditionedIndexResults(set, indexInfo, context, indexFieldsSize, completeExpansionNeeded, iterOperands, indpndntItr);
    } else {
        return set;
    }
}
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) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) ArrayList(java.util.ArrayList) List(java.util.List)

Example 22 with StructTypeImpl

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

the class CompiledComparison method isConditioningNeededForIndex.

public boolean isConditioningNeededForIndex(RuntimeIterator independentIter, ExecutionContext context, boolean completeExpnsNeeded) throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
    IndexConditioningHelper ich = null;
    IndexInfo[] idxInfo = getIndexInfo(context);
    int indexFieldsSize = -1;
    boolean conditioningNeeded = true;
    if (idxInfo == null || idxInfo.length > 1) {
        return conditioningNeeded;
    }
    // assert idxInfo.length == 1;
    ObjectType indexRsltType = idxInfo[0]._index.getResultSetType();
    if (indexRsltType instanceof StructType) {
        indexFieldsSize = ((StructTypeImpl) indexRsltType).getFieldNames().length;
    } else {
        indexFieldsSize = 1;
    }
    if (independentIter != null && indexFieldsSize == 1) {
        ich = new IndexConditioningHelper(idxInfo[0], context, indexFieldsSize, completeExpnsNeeded, null, independentIter);
    }
    return ich == null || ich.shufflingNeeded;
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) StructType(org.apache.geode.cache.query.types.StructType) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl)

Example 23 with StructTypeImpl

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

the class CompiledGroupBySelect method createNewElementType.

private ObjectType createNewElementType(ObjectType elementType, boolean isStruct) {
    if (isStruct) {
        StructType oldType = (StructType) elementType;
        if (this.aggregateFunctions.length > 0) {
            ObjectType[] oldFieldTypes = oldType.getFieldTypes();
            ObjectType[] newFieldTypes = new ObjectType[oldFieldTypes.length];
            int i = 0;
            int aggFuncIndex = 0;
            for (ObjectType oldFieldType : oldFieldTypes) {
                if (this.aggregateColsPos.get(i)) {
                    newFieldTypes[i] = this.aggregateFunctions[aggFuncIndex++].getObjectType();
                } else {
                    newFieldTypes[i] = oldFieldType;
                }
                ++i;
            }
            return new StructTypeImpl(oldType.getFieldNames(), newFieldTypes);
        } else {
            return oldType;
        }
    } else {
        return this.aggregateFunctions.length > 0 ? this.aggregateFunctions[0].getObjectType() : elementType;
    }
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) StructType(org.apache.geode.cache.query.types.StructType) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl)

Example 24 with StructTypeImpl

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

the class AbstractGroupOrRangeJunction method auxIterateEvaluate.

/** invariant: the operand is known to be evaluated by iteration */
private SelectResults auxIterateEvaluate(CompiledValue operand, ExecutionContext context, SelectResults intermediateResults) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    // cannot be evaluated as a filter.
    if (intermediateResults == null)
        throw new RuntimeException(LocalizedStrings.AbstractGroupOrRangeJunction_INTERMEDIATERESULTS_CAN_NOT_BE_NULL.toLocalizedString());
    if (// short circuit
    intermediateResults.isEmpty())
        return intermediateResults;
    List currentIters = (this.completeExpansion) ? context.getCurrentIterators() : QueryUtils.getDependentItrChainForIndpndntItrs(this.indpndntItr, context);
    SelectResults resultSet = null;
    RuntimeIterator[] rIters = new RuntimeIterator[currentIters.size()];
    currentIters.toArray(rIters);
    ObjectType elementType = intermediateResults.getCollectionType().getElementType();
    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("AND/OR operands must be of type boolean, not type '" + result.getClass().getName() + "'");
        }
    } finally {
        observer.endIteration(resultSet);
    }
    return resultSet;
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl)

Example 25 with StructTypeImpl

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

the class CompiledUndefined method filterEvaluate.

/**
   * Asif : Evaluates as a filter taking advantage of indexes if appropriate. This function has a
   * meaningful implementation only in CompiledComparison & CompiledUndefined . It is unsupported in
   * other classes. The additional parameters which it takes are a boolean which is used to indicate
   * whether the index result set needs to be expanded to the top level or not. The second is a
   * CompiledValue representing the operands which are only iter evaluatable. The CompiledValue
   * passed will be null except if a GroupJunction has only one filter evaluatable condition & rest
   * are iter operands. In such cases , the iter operands will be evaluated while expanding/cutting
   * down the index resultset
   * 
   * @return SelectResults
   */
@Override
public SelectResults filterEvaluate(ExecutionContext context, SelectResults intermediateResults, boolean completeExpansionNeeded, CompiledValue iterOperands, RuntimeIterator[] indpndntItrs, boolean isIntersection, boolean conditioningNeeded, boolean evaluateProjAttrib) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    // this method is called if we are independent of the iterator,
    // or if we can use an index.
    // if we are independent, then we should not have been here in the first
    // place
    Support.Assert(this._value.isDependentOnCurrentScope(context), "For a condition which does not depend on any RuntimeIterator of current scope , we should not have been in this function");
    IndexInfo[] idxInfo = getIndexInfo(context);
    ObjectType resultType = idxInfo[0]._index.getResultSetType();
    int indexFieldsSize = -1;
    SelectResults set = null;
    if (resultType instanceof StructType) {
        set = QueryUtils.createStructCollection(context, (StructTypeImpl) resultType);
        indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
    } else {
        set = QueryUtils.createResultCollection(context, resultType);
        indexFieldsSize = 1;
    }
    int op = _is_defined ? TOK_NE : TOK_EQ;
    Object key = QueryService.UNDEFINED;
    QueryObserver observer = QueryObserverHolder.getInstance();
    try {
        observer.beforeIndexLookup(idxInfo[0]._index, op, key);
        context.cachePut(CompiledValue.INDEX_INFO, idxInfo[0]);
        idxInfo[0]._index.query(key, op, set, context);
    } finally {
        observer.afterIndexLookup(set);
    }
    return QueryUtils.getConditionedIndexResults(set, idxInfo[0], context, indexFieldsSize, completeExpansionNeeded, iterOperands, indpndntItrs);
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) StructType(org.apache.geode.cache.query.types.StructType) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl)

Aggregations

StructTypeImpl (org.apache.geode.cache.query.internal.types.StructTypeImpl)38 ObjectType (org.apache.geode.cache.query.types.ObjectType)29 SelectResults (org.apache.geode.cache.query.SelectResults)21 ObjectTypeImpl (org.apache.geode.cache.query.internal.types.ObjectTypeImpl)14 StructType (org.apache.geode.cache.query.types.StructType)14 ArrayList (java.util.ArrayList)12 List (java.util.List)12 Test (org.junit.Test)12 Iterator (java.util.Iterator)9 Portfolio (org.apache.geode.cache.query.data.Portfolio)9 QueryService (org.apache.geode.cache.query.QueryService)8 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 Query (org.apache.geode.cache.query.Query)7 QueryObserverAdapter (org.apache.geode.cache.query.internal.QueryObserverAdapter)7 Region (org.apache.geode.cache.Region)6 Index (org.apache.geode.cache.query.Index)6 Struct (org.apache.geode.cache.query.Struct)6 ListIterator (java.util.ListIterator)5 UnitTest (org.apache.geode.test.junit.categories.UnitTest)4 Collection (java.util.Collection)3