Search in sources :

Example 1 with ObjectType

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

the class CompiledSelect method applyProjectionAndAddToResultSet.

// resultSet could be a set or a bag (we have set constructor, or there
// could be a distinct subquery)
// in future, it would be good to simplify this to always work with a bag
// (converting all sets to bags) until the end when we enforce distinct
// The number returned indicates the occurence of the data in the SelectResults
// Thus if the SelectResults is of type ResultsSet or StructSet
// then 1 will indicate that data was added to the results & that was the
// first occurence. For this 0 will indicate that the data was not added
// because it was a duplicate
// If the SelectResults is an instance ResultsBag or StructsBag , the number will
// indicate the occurence. Thus 1 will indicate it being added for first time
// Currently orderBy is present only for StructSet & ResultSet which are
// unique object holders. So the occurence for them can be either 0 or 1 only
private int applyProjectionAndAddToResultSet(ExecutionContext context, SelectResults resultSet, boolean ignoreOrderBy) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    List currrentRuntimeIters = context.getCurrentIterators();
    int occurence = 0;
    ObjectType elementType = resultSet.getCollectionType().getElementType();
    boolean isStruct = elementType != null && elementType.isStructType();
    // TODO: Optimize this condition in some clean way
    boolean isLinkedStructure = resultSet instanceof Ordered && ((Ordered) resultSet).dataPreordered();
    ArrayList evaluatedOrderByClause = null;
    OrderByComparator comparator = null;
    boolean applyOrderBy = false;
    if (this.orderByAttrs != null && !ignoreOrderBy) {
        // In case PR order-by will get applied on the coordinator node
        // on the cumulative results. Apply the order-by on PR only if
        // limit is specified.
        Integer limitValue = evaluateLimitValue(context, this.limit);
        if (context.getPartitionedRegion() != null && limitValue < 0) {
            applyOrderBy = false;
        }
        applyOrderBy = true;
    }
    if (this.orderByAttrs != null && !ignoreOrderBy) {
        comparator = (OrderByComparator) ((Ordered) resultSet).comparator();
    }
    if (projAttrs == null) {
        int len = currrentRuntimeIters.size();
        Object[] values = new Object[len];
        for (int i = 0; i < len; i++) {
            RuntimeIterator iter = (RuntimeIterator) currrentRuntimeIters.get(i);
            values[i] = iter.evaluate(context);
            // case of all Pdx objects in cache
            if (this.distinct && !((DefaultQuery) context.getQuery()).isRemoteQuery() && !context.getCache().getPdxReadSerialized() && (values[i] instanceof PdxInstance)) {
                values[i] = ((PdxInstance) values[i]).getObject();
            }
        }
        // Don't care about Order By for count(*).
        if (isCount() && !this.distinct) {
            // Counter is local to CompileSelect and not available in ResultSet
            // until
            // the end of evaluate call to this CompiledSelect object.
            this.countStartQueryResult++;
            occurence = 1;
        } else {
            // if order by is present
            if (applyOrderBy) {
                StructImpl structImpl;
                if (this.distinct) {
                    if (isStruct) {
                        if (values.length == 1 && values[0] instanceof StructImpl) {
                            structImpl = (StructImpl) values[0];
                            comparator.addEvaluatedSortCriteria(structImpl.getFieldValues(), context);
                            occurence = resultSet.add(structImpl) ? 1 : 0;
                        } else {
                            comparator.addEvaluatedSortCriteria(values, context);
                            occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
                        }
                    // TODO:Instead of a normal Map containing which holds
                    // StructImpl object
                    // use a THashObject with Object[] array hashing stragtegy as we
                    // are unnnecessarily
                    // creating objects of type Object[]
                    } else {
                        comparator.addEvaluatedSortCriteria(values[0], context);
                        occurence = resultSet.add(values[0]) ? 1 : 0;
                    }
                } else {
                    if (isStruct) {
                        if (values.length == 1 && values[0] instanceof StructImpl) {
                            structImpl = (StructImpl) values[0];
                            comparator.addEvaluatedSortCriteria(structImpl.getFieldValues(), context);
                            occurence = ((Bag) resultSet).addAndGetOccurence(structImpl.getFieldValues());
                        } else {
                            comparator.addEvaluatedSortCriteria(values, context);
                            occurence = ((Bag) resultSet).addAndGetOccurence(values);
                        }
                    } else {
                        comparator.addEvaluatedSortCriteria(values[0], context);
                        occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                    }
                }
            } else {
                if (isLinkedStructure) {
                    if (isStruct) {
                        StructImpl structImpl;
                        if (values.length == 1 && values[0] instanceof StructImpl) {
                            structImpl = (StructImpl) values[0];
                        } else {
                            structImpl = new StructImpl((StructTypeImpl) elementType, values);
                        }
                        if (this.distinct) {
                            occurence = resultSet.add(structImpl) ? 1 : 0;
                        } else {
                            occurence = ((Bag) resultSet).addAndGetOccurence(structImpl);
                        }
                    } else {
                        if (this.distinct) {
                            occurence = resultSet.add(values[0]) ? 1 : 0;
                        } else {
                            occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                        }
                    }
                } else {
                    if (this.distinct) {
                        if (isStruct) {
                            occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
                        } else {
                            occurence = resultSet.add(values[0]) ? 1 : 0;
                        }
                    } else {
                        if (isStruct) {
                            occurence = ((Bag) resultSet).addAndGetOccurence(values);
                        } else {
                            boolean add = true;
                            if (context.isCqQueryContext()) {
                                if (values[0] instanceof Region.Entry) {
                                    Region.Entry e = (Region.Entry) values[0];
                                    if (!e.isDestroyed()) {
                                        try {
                                            values[0] = new CqEntry(e.getKey(), e.getValue());
                                        } catch (EntryDestroyedException ignore) {
                                            // Even though isDestory() check is made, the entry could throw
                                            // EntryDestroyedException if the value becomes null.
                                            add = false;
                                        }
                                    } else {
                                        add = false;
                                    }
                                }
                            }
                            if (add) {
                                occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                            }
                        }
                    }
                }
            }
        }
    } else {
        // One or more projection attributes
        int projCount = projAttrs.size();
        Object[] values = new Object[projCount];
        for (int i = 0; i < projCount; i++) {
            Object[] projDef = (Object[]) projAttrs.get(i);
            values[i] = ((CompiledValue) projDef[1]).evaluate(context);
            // for remote queries
            if (!((DefaultQuery) context.getQuery()).isRemoteQuery()) {
                if (this.distinct && values[i] instanceof PdxInstance && !context.getCache().getPdxReadSerialized()) {
                    values[i] = ((PdxInstance) values[i]).getObject();
                } else if (values[i] instanceof PdxString) {
                    values[i] = ((PdxString) values[i]).toString();
                }
            }
        }
        // if order by is present
        if (applyOrderBy) {
            if (distinct) {
                if (isStruct) {
                    comparator.addEvaluatedSortCriteria(values, context);
                    // Occurence field is used to identify the corrcet number of
                    // iterations
                    // required to implement the limit based on the presence or absence
                    // of distinct clause
                    occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
                } else {
                    comparator.addEvaluatedSortCriteria(values[0], context);
                    occurence = resultSet.add(values[0]) ? 1 : 0;
                }
            } else {
                if (isStruct) {
                    comparator.addEvaluatedSortCriteria(values, context);
                    occurence = ((Bag) resultSet).addAndGetOccurence(values);
                } else {
                    comparator.addEvaluatedSortCriteria(values[0], context);
                    occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                }
            }
        } else {
            if (isLinkedStructure) {
                if (isStruct) {
                    StructImpl structImpl = new StructImpl((StructTypeImpl) elementType, values);
                    if (this.distinct) {
                        occurence = resultSet.add(structImpl) ? 1 : 0;
                    } else {
                        occurence = ((Bag) resultSet).addAndGetOccurence(structImpl);
                    }
                } else {
                    if (this.distinct) {
                        occurence = resultSet.add(values[0]) ? 1 : 0;
                    } else {
                        occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                    }
                }
            } else {
                if (this.distinct) {
                    if (isStruct) {
                        occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
                    } else {
                        occurence = resultSet.add(values[0]) ? 1 : 0;
                    }
                } else {
                    if (isStruct) {
                        occurence = ((Bag) resultSet).addAndGetOccurence(values);
                    } else {
                        occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
                    }
                }
            }
        }
    }
    return occurence;
}
Also used : EntryDestroyedException(org.apache.geode.cache.EntryDestroyedException) ArrayList(java.util.ArrayList) PdxString(org.apache.geode.pdx.internal.PdxString) ObjectType(org.apache.geode.cache.query.types.ObjectType) PdxInstance(org.apache.geode.pdx.PdxInstance) StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) Region(org.apache.geode.cache.Region) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ObjectType

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

the class CompiledIteratorDef method getRuntimeIterator.

public RuntimeIterator getRuntimeIterator(ExecutionContext context) throws TypeMismatchException, AmbiguousNameException, NameResolutionException {
    RuntimeIterator rIter = null;
    // check cached in context
    rIter = (RuntimeIterator) context.cacheGet(this);
    if (rIter != null) {
        return rIter;
    }
    ObjectType type = this.elementType;
    if (type.equals(TypeUtils.OBJECT_TYPE)) {
        // check to see if there's a typecast for this collection
        ObjectType typc = getCollectionElementTypeCast();
        if (typc != null) {
            type = typc;
        } else {
            // Does not determine type of nested query
            if (!(this.collectionExpr instanceof CompiledSelect)) {
                type = computeElementType(context);
            }
        }
    }
    rIter = new RuntimeIterator(this, type);
    // generate from clause should take care of bucket region substitution if
    // necessary and then set the definition.
    String fromClause = genFromClause(context);
    rIter.setDefinition(fromClause);
    /*
     * If the type of RunTimeIterator is still ObjectType & if the RuneTimeIterator is independent
     * of any iterator of the scopes less than or equal to its own scope, we can evaluate the
     * collection via RuntimeIterator. This will initialize the Collection of RuntimeIterator ,
     * which is OK. The code in RuntimeIterator will be rectified such that the ElementType of that
     * RuntimeIterator is taken from the collection
     */
    if (type.equals(TypeUtils.OBJECT_TYPE) && !this.isDependentOnAnyIteratorOfScopeLessThanItsOwn(context)) {
        // the collection
        try {
            rIter.evaluateCollection(context);
        } catch (QueryExecutionTimeoutException qet) {
            throw qet;
        } catch (RegionNotFoundException re) {
            throw re;
        } catch (Exception e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Exception while getting runtime iterator.", e);
            }
            throw new TypeMismatchException(LocalizedStrings.CompiledIteratorDef_EXCEPTION_IN_EVALUATING_THE_COLLECTION_EXPRESSION_IN_GETRUNTIMEITERATOR_EVEN_THOUGH_THE_COLLECTION_IS_INDEPENDENT_OF_ANY_RUNTIMEITERATOR.toLocalizedString(), e);
        }
    }
    // cache in context
    context.cachePut(this, rIter);
    return rIter;
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryExecutionTimeoutException(org.apache.geode.cache.query.QueryExecutionTimeoutException) AmbiguousNameException(org.apache.geode.cache.query.AmbiguousNameException) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) QueryExecutionTimeoutException(org.apache.geode.cache.query.QueryExecutionTimeoutException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException)

Example 3 with ObjectType

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

the class CompiledIteratorDef method evaluateCollection.

/**
   * Evaluate just the collectionExpr
   * 
   * @param stopAtIter the RuntimeIterator associated with this iterator defn -- don't use this or
   *        any subsequent runtime iterators to evaluate.
   */
SelectResults evaluateCollection(ExecutionContext context, RuntimeIterator stopAtIter) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    Object coll;
    // Check if query execution on this thread is Canceled.
    // QueryMonitor.isQueryExecutionCanceled();
    context.currentScope().setLimit(stopAtIter);
    try {
        coll = this.collectionExpr.evaluate(context);
    } finally {
        context.currentScope().setLimit(null);
    }
    // element type here
    if (TypeUtils.OBJECT_TYPE.equals(this.elementType)) {
        ObjectType elmTypc = getCollectionElementTypeCast();
        if (elmTypc != null) {
            this.elementType = elmTypc;
        }
    }
    return prepareIteratorDef(coll, this.elementType, context);
}
Also used : ObjectType(org.apache.geode.cache.query.types.ObjectType)

Example 4 with ObjectType

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

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

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