Search in sources :

Example 1 with StructTypeImpl

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

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

use of org.apache.geode.cache.query.internal.types.StructTypeImpl 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 4 with StructTypeImpl

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

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

the class CompiledGroupBySelect method terminateAndAddToResults.

private boolean terminateAndAddToResults(boolean isStruct, SelectResults newResults, Aggregator[] aggregators, Object prev, ExecutionContext context, boolean isStrucFields, int limitValue) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
    Object[] newRowArray = isStruct ? copyStruct((Struct) prev) : null;
    Object newObject = null;
    int bitstart = 0;
    if (limitValue == 0) {
        return false;
    }
    for (Aggregator aggregator : aggregators) {
        if (isStruct) {
            int pos = this.aggregateColsPos.nextSetBit(bitstart);
            bitstart = pos + 1;
            Object scalarResult = aggregator.terminate();
            newRowArray[pos] = scalarResult;
        } else {
            newObject = aggregator.terminate();
        }
    }
    if (isStruct) {
        if (isStrucFields) {
            ((StructFields) newResults).addFieldValues(newRowArray);
        } else {
            newResults.add(new StructImpl((StructTypeImpl) ((Struct) prev).getStructType(), newRowArray));
        }
    } else {
        newResults.add(newObject);
    }
    boolean keepAdding = true;
    if (this.originalOrderByClause == null && limitValue > 0 && (context.getIsPRQueryNode() || context.getBucketList() == null) && newResults.size() == limitValue) {
        keepAdding = false;
    }
    // rfresh the aggregators
    refreshAggregators(aggregators, context);
    return keepAdding;
}
Also used : StructTypeImpl(org.apache.geode.cache.query.internal.types.StructTypeImpl) Aggregator(org.apache.geode.cache.query.Aggregator) Struct(org.apache.geode.cache.query.Struct)

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