Search in sources :

Example 66 with ObjectType

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

the class QueryObjectSerializationJUnitTest method testSerializationOfQueryResults.

/**
   * Tests the serialization of many, but not all of the possible ResultSets
   */
@Test
public void testSerializationOfQueryResults() throws IOException, ClassNotFoundException {
    Collection data = new java.util.ArrayList();
    data.add(null);
    data.add(null);
    data.add("some string");
    data.add(Long.MAX_VALUE);
    data.add(45);
    data.add(QueryService.UNDEFINED);
    ObjectType elementType = new SimpleObjectType();
    // Undefined
    checkRoundTrip(QueryService.UNDEFINED);
    // ResultsBag
    ResultsBag rbWithoutData = new ResultsBag();
    // avoid NPE in equals
    rbWithoutData.setElementType(elementType);
    checkRoundTrip(rbWithoutData);
    ResultsBag rbWithData = new ResultsBag(data, (CachePerfStats) null);
    // avoid NPE in equals
    rbWithData.setElementType(elementType);
    checkRoundTrip(rbWithData);
    /*
     * Set rbWithoutDataAsSet = new ResultsBag().asSet(); ResultsCollectionWrapper rcw = new
     * ResultsCollectionWrapper(elementType, rbWithoutDataAsSet, -1); checkRoundTrip(rcw); Set
     * rbWithDataAsSet = new ResultsBag(data, (CachePerfStats)null).asSet();
     * ResultsCollectionWrapper rcwWithData = new ResultsCollectionWrapper(elementType,
     * rbWithDataAsSet, -1); checkRoundTrip(rcwWithData);
     */
    // SortedResultSet
    SortedResultSet srsWithoutData = new SortedResultSet();
    // avoid NPE in equals
    srsWithoutData.setElementType(elementType);
    checkRoundTrip(srsWithoutData);
    SortedResultSet srsWithData = new SortedResultSet();
    // avoid NPE in equals
    srsWithData.setElementType(elementType);
    checkRoundTrip(srsWithData);
// SortedStructSet
// SortedStructSet sssWithoutData = new SortedStructSet();
// checkRoundTrip(sssWithoutData);
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) Collection(java.util.Collection) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 67 with ObjectType

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

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

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

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

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