use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CompiledIn method singleBaseCollectionFilterEvaluate.
/**
* 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, boolean completeExpansionNeeded, CompiledValue iterOperands, IndexInfo indexInfo, RuntimeIterator[] indpndntItr, boolean isIntersection, boolean conditioningNeeded, boolean evalProj) throws TypeMismatchException, AmbiguousNameException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
ObjectType resultType = indexInfo._index.getResultSetType();
int indexFieldsSize = -1;
SelectResults results = null;
if (resultType instanceof StructType) {
indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
} else {
indexFieldsSize = 1;
}
boolean useLinkedDataStructure = false;
boolean nullValuesAtStart = true;
Boolean orderByClause = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
if (orderByClause != null && orderByClause) {
List orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
useLinkedDataStructure = orderByAttrs.size() == 1;
nullValuesAtStart = !((CompiledSortCriterion) orderByAttrs.get(0)).getCriterion();
}
List projAttrib = null;
ObjectType projResultType = null;
if (!conditioningNeeded) {
projResultType = evalProj ? (ObjectType) context.cacheGet(RESULT_TYPE) : null;
if (projResultType != null) {
resultType = projResultType;
context.cachePut(RESULT_TYPE, Boolean.TRUE);
projAttrib = (List) context.cacheGet(PROJ_ATTRIB);
}
if (isIntersection) {
if (resultType instanceof StructType) {
context.getCache().getLogger().fine("StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
results = context.isDistinct() ? new LinkedStructSet((StructTypeImpl) resultType) : new SortedResultsBag<Struct>((StructTypeImpl) resultType, nullValuesAtStart);
} else {
results = QueryUtils.createStructCollection(context, (StructTypeImpl) resultType);
}
indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
} else {
context.getCache().getLogger().fine("non-StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
results = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType, nullValuesAtStart);
} else {
results = QueryUtils.createResultCollection(context, resultType);
}
indexFieldsSize = 1;
}
} else {
if (intermediateResults != null && !completeExpansionNeeded) {
results = intermediateResults;
} else {
if (resultType instanceof StructType) {
context.getCache().getLogger().fine("StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
results = context.isDistinct() ? new LinkedStructSet((StructTypeImpl) resultType) : new SortedResultsBag<Struct>((StructTypeImpl) resultType, nullValuesAtStart);
} else {
results = QueryUtils.createStructCollection(context, (StructTypeImpl) resultType);
}
indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
} else {
context.getCache().getLogger().fine("non-StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
results = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType, nullValuesAtStart);
} else {
results = QueryUtils.createResultCollection(context, resultType);
}
indexFieldsSize = 1;
}
}
}
} else {
if (resultType instanceof StructType) {
context.getCache().getLogger().fine("StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
results = context.isDistinct() ? new LinkedStructSet((StructTypeImpl) resultType) : new SortedResultsBag<Struct>((StructTypeImpl) resultType, nullValuesAtStart);
} else {
results = QueryUtils.createStructCollection(context, (StructTypeImpl) resultType);
}
indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
} else {
context.getCache().getLogger().fine("non-StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
results = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType, nullValuesAtStart);
} else {
results = QueryUtils.createResultCollection(context, resultType);
}
indexFieldsSize = 1;
}
}
QueryObserver observer = QueryObserverHolder.getInstance();
try {
Object evalColln = evaluateColln(context);
observer.beforeIndexLookup(indexInfo._index, TOK_EQ, evalColln);
// unexpected values overwriting expected values
if (!conditioningNeeded) {
if (projResultType != null) {
resultType = projResultType;
context.cachePut(RESULT_TYPE, Boolean.TRUE);
}
}
// handle each type of collection that we support
if (evalColln instanceof Map) {
Iterator itr = ((Map) evalColln).entrySet().iterator();
while (itr.hasNext()) {
this.queryIndex(itr.next(), indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
} else if (evalColln instanceof Collection) {
Object key = indexInfo.evaluateIndexKey(context);
// and the evalColln in the [0] position
if (key instanceof Object[]) {
Iterator iterator = ((Iterable) ((Object[]) key)[0]).iterator();
while (iterator.hasNext()) {
this.queryIndex(new Object[] { iterator.next(), ((Object[]) key)[1] }, indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
} else {
// Removing duplicates from the collection
HashSet set = new HashSet((Collection) evalColln);
Iterator itr = set.iterator();
while (itr.hasNext()) {
this.queryIndex(itr.next(), indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
}
} else {
if (!evalColln.getClass().isArray()) {
throw new TypeMismatchException("Operand of IN cannot be interpreted as a Collection. " + "Is instance of " + evalColln.getClass().getName());
}
if (evalColln instanceof Object[]) {
Object[] arr = (Object[]) evalColln;
for (int i = 0; i < arr.length; ++i) {
this.queryIndex(arr[i], indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
} else if (evalColln instanceof long[]) {
long[] a = (long[]) evalColln;
for (int i = 0; i < a.length; i++) {
this.queryIndex(a[i], indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
} else if (evalColln instanceof double[]) {
double[] a = (double[]) evalColln;
for (int i = 0; i < a.length; i++) {
this.queryIndex(a[i], indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
} else if (evalColln instanceof float[]) {
float[] a = (float[]) evalColln;
for (int i = 0; i < a.length; i++) {
this.queryIndex(a[i], indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
} else if (evalColln instanceof int[]) {
int[] a = (int[]) evalColln;
for (int i = 0; i < a.length; i++) {
this.queryIndex(a[i], indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
} else if (evalColln instanceof short[]) {
short[] a = (short[]) evalColln;
for (int i = 0; i < a.length; i++) {
this.queryIndex(a[i], indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
} else if (evalColln instanceof char[]) {
char[] a = (char[]) evalColln;
for (int i = 0; i < a.length; i++) {
this.queryIndex(a[i], indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
} else if (evalColln instanceof byte[]) {
byte[] a = (byte[]) evalColln;
for (int i = 0; i < a.length; i++) {
this.queryIndex(a[i], indexInfo, results, iterOperands, indpndntItr, context, projAttrib, conditioningNeeded);
}
} else {
throw new TypeMismatchException("Operand of IN cannot be interpreted as a Comparable Object. Operand is of type =" + evalColln.getClass());
}
}
if (conditioningNeeded) {
results = QueryUtils.getConditionedIndexResults(results, indexInfo, context, indexFieldsSize, completeExpansionNeeded, iterOperands, indpndntItr);
} else {
if (isIntersection && intermediateResults != null) {
results = QueryUtils.intersection(intermediateResults, results, context);
}
}
return results;
} finally {
observer.afterIndexLookup(results);
}
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CompiledSelect method evaluate.
public SelectResults evaluate(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
context.newScope((Integer) context.cacheGet(scopeID));
context.pushExecCache((Integer) context.cacheGet(scopeID));
context.setDistinct(this.distinct);
if (this.hasUnmappedOrderByCols && context.getBucketList() != null) {
throw new QueryInvalidException(LocalizedStrings.DefaultQuery_ORDER_BY_ATTRIBS_NOT_PRESENT_IN_PROJ.toLocalizedString());
}
if (hints != null) {
context.cachePut(QUERY_INDEX_HINTS, hints);
}
try {
// set flag to keep objects serialized for "select *" queries
if ((DefaultQuery) context.getQuery() != null) {
((DefaultQuery) context.getQuery()).keepResultsSerialized(this, context);
}
Iterator iter = iterators.iterator();
while (iter.hasNext()) {
CompiledIteratorDef iterDef = (CompiledIteratorDef) iter.next();
RuntimeIterator rIter = iterDef.getRuntimeIterator(context);
context.bindIterator(rIter);
// Ideally the function below should always be called after binding has occurred
// So that the internal ID gets set during binding to the scope. If not so then chances
// are that internal_id is still null causing index_internal_id to be null.
// Though in our case it may not be an issue as the compute dependency phase must have
// already set the index id
}
Integer limitValue = evaluateLimitValue(context, this.limit);
SelectResults result = null;
boolean evalAsFilters = false;
if (this.whereClause == null) {
result = doIterationEvaluate(context, false);
} else {
if (!this.whereClause.isDependentOnCurrentScope(context)) {
// independent
// where
// @todo
// check
// for
// dependency
// on
// current
// scope
// only?
// clause
Object b = this.whereClause.evaluate(context);
if (b == null || b == QueryService.UNDEFINED) {
// treat as if all elements are undefined
result = prepareEmptyResultSet(context, false);
// ResultsSet.emptyResultsSet(resultSet, 0);
// return result;
} else if (!(b instanceof Boolean)) {
throw new TypeMismatchException(LocalizedStrings.CompiledSelect_THE_WHERE_CLAUSE_WAS_TYPE_0_INSTEAD_OF_BOOLEAN.toLocalizedString(b.getClass().getName()));
} else if ((Boolean) b) {
result = doIterationEvaluate(context, false);
} else {
result = prepareEmptyResultSet(context, false);
// ResultsSet.emptyResultsSet(resultSet, 0);
// return result;
}
} else {
// Check the numer of independent iterators
int numInd = context.getAllIndependentIteratorsOfCurrentScope().size();
// If order by clause is defined, then the first column should be the preferred index
if (this.orderByAttrs != null && numInd == 1) {
CompiledSortCriterion csc = (CompiledSortCriterion) orderByAttrs.get(0);
StringBuilder preferredIndexCondn = new StringBuilder();
this.evalCanonicalizedExpressionForCSC(csc, context, preferredIndexCondn);
context.cachePut(PREF_INDEX_COND, preferredIndexCondn.toString());
}
boolean unlock = true;
Object obj = context.cacheGet(this.whereClause);
if (obj != null && (obj instanceof IndexInfo[] || obj.equals(CLAUSE_EVALUATED))) {
// if indexinfo is cached means the read lock
// is not being taken this time, so releasing
// the lock is not required
unlock = false;
}
// see if we should evaluate as filters,
// and count how many actual index lookups will be performed
PlanInfo planInfo = this.whereClause.getPlanInfo(context);
if (context.cacheGet(this.whereClause) == null) {
context.cachePut(this.whereClause, CLAUSE_EVALUATED);
}
try {
evalAsFilters = planInfo.evalAsFilter;
// let context know if there is exactly one index lookup
context.setOneIndexLookup(planInfo.indexes.size() == 1);
if (evalAsFilters) {
((QueryExecutionContext) context).setIndexUsed(true);
// Ignore order by attribs for a while
boolean canApplyOrderByAtIndex = false;
if (limitValue >= 0 && numInd == 1 && ((Filter) this.whereClause).isLimitApplicableAtIndexLevel(context)) {
context.cachePut(CAN_APPLY_LIMIT_AT_INDEX, Boolean.TRUE);
}
StringBuilder temp = null;
if (this.orderByAttrs != null) {
temp = new StringBuilder();
CompiledSortCriterion csc = (CompiledSortCriterion) this.orderByAttrs.get(0);
this.evalCanonicalizedExpressionForCSC(csc, context, temp);
}
boolean needsTopLevelOrdering = true;
if (temp != null && numInd == 1 && ((Filter) this.whereClause).isOrderByApplicableAtIndexLevel(context, temp.toString())) {
context.cachePut(CAN_APPLY_ORDER_BY_AT_INDEX, Boolean.TRUE);
context.cachePut(ORDERBY_ATTRIB, this.orderByAttrs);
canApplyOrderByAtIndex = true;
if (this.orderByAttrs.size() == 1) {
needsTopLevelOrdering = false;
// we should use a sorted set
if (this.limit != null) {
// Currently check bucket list to determine if it's a pr query
if (context.getBucketList() != null && context.getBucketList().size() > 0) {
needsTopLevelOrdering = true;
}
}
}
} else if (temp != null) {
// If order by is present but cannot be applied at index level,
// then limit also cannot be applied
// at index level
context.cachePut(CAN_APPLY_LIMIT_AT_INDEX, Boolean.FALSE);
}
context.cachePut(RESULT_LIMIT, limitValue);
if (numInd == 1 && ((Filter) this.whereClause).isProjectionEvaluationAPossibility(context) && (this.orderByAttrs == null || (canApplyOrderByAtIndex && !needsTopLevelOrdering)) && this.projAttrs != null) {
// Possibility of evaluating the resultset as filter itself
ObjectType resultType = this.cachedElementTypeForOrderBy != null ? this.cachedElementTypeForOrderBy : this.prepareResultType(context);
context.cachePut(RESULT_TYPE, resultType);
context.cachePut(PROJ_ATTRIB, this.projAttrs);
}
result = ((Filter) this.whereClause).filterEvaluate(context, null);
if (!(context.cacheGet(RESULT_TYPE) instanceof Boolean)) {
QueryObserverHolder.getInstance().beforeApplyingProjectionOnFilterEvaluatedResults(result);
result = applyProjectionOnCollection(result, context, !needsTopLevelOrdering);
}
} else {
// otherwise iterate over the single from var to evaluate
result = doIterationEvaluate(context, true);
}
} finally {
// because we need to select index which can be read-locked.
if (unlock) {
releaseReadLockOnUsedIndex(planInfo);
}
}
}
}
// if (result == null) { return QueryService.UNDEFINED; }
assert result != null;
// drop duplicates if this is DISTINCT
if (result instanceof SelectResults) {
SelectResults sr = (SelectResults) result;
CollectionType colnType = sr.getCollectionType();
// if (this.distinct && colnType.allowsDuplicates()) {
if (this.distinct) {
Collection r;
// Set s = sr.asSet();
if (colnType.allowsDuplicates()) {
// don't just convert to a ResultsSet (or StructSet), since
// the bags can convert themselves to a Set more efficiently
r = sr.asSet();
} else {
r = sr;
}
result = new ResultsCollectionWrapper(colnType.getElementType(), r, limitValue);
if (r instanceof Bag.SetView) {
((ResultsCollectionWrapper) result).setModifiable(false);
}
} else {
// SelectResults is of type
if (limitValue > -1) {
((Bag) sr).applyLimit(limitValue);
}
}
/*
* We still have to get size of SelectResults in some cases like, if index was used OR query
* is a distinct query.
*
* If SelectResult size is zero then we need to put Integer for 0 count.
*/
if (this.count) {
SelectResults res = (SelectResults) result;
if ((this.distinct || evalAsFilters || countStartQueryResult == 0)) {
// at coordinator node for PR queries.
if (context.getBucketList() != null && this.distinct) {
return result;
}
// Take size and empty the results
int resultCount = res.size();
res.clear();
ResultsBag countResult = new ResultsBag(new ObjectTypeImpl(Integer.class), context.getCachePerfStats());
countResult.addAndGetOccurence(resultCount);
result = countResult;
} else {
((Bag) res).addAndGetOccurence(countStartQueryResult);
}
}
}
return result;
} finally {
context.popScope();
context.popExecCache();
}
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CompiledSelect method createStructTypeForNullProjection.
/**
* This function should be used to create a StructType for those queries which have * as
* projection attribute (implying null projection attribute) & multiple from clauses
*/
private StructTypeImpl createStructTypeForNullProjection(List currentIterators, ExecutionContext context) {
int len = currentIterators.size();
String[] fieldNames = new String[len];
ObjectType[] fieldTypes = new ObjectType[len];
String fldName = null;
for (int i = 0; i < len; i++) {
RuntimeIterator iter = (RuntimeIterator) currentIterators.get(i);
// fieldNames[i] = iter.getName();
if ((fldName = iter.getName()) == null) {
fldName = generateProjectionName(iter, context);
}
fieldNames[i] = fldName;
fieldTypes[i] = iter.getElementType();
}
return new StructTypeImpl(fieldNames, fieldTypes);
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CumulativeNonDistinctResults method fromData.
@Override
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
ObjectType elementType = (ObjectType) DataSerializer.readObject(in);
this.collectionType = new CollectionTypeImpl(CumulativeNonDistinctResults.class, elementType);
boolean isStruct = elementType.isStructType();
long size = in.readLong();
this.data = new ArrayList<E>((int) size);
long numLeft = size;
while (numLeft > 0) {
if (isStruct) {
Object[] fields = DataSerializer.readObjectArray(in);
this.data.add((E) new StructImpl((StructTypeImpl) elementType, fields));
} else {
E element = DataSerializer.readObject(in);
this.data.add(element);
}
--numLeft;
}
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class QueryUtils method cutDownAndExpandIndexResults.
// creates the returned set and then calls other methods to do actual work
private static SelectResults cutDownAndExpandIndexResults(SelectResults result, RuntimeIterator[] indexFieldToItrsMapping, List expansionList, List finalItrs, ExecutionContext context, List checkList, CompiledValue iterOps, IndexInfo theFilteringIndex) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
boolean useLinkedDataStructure = false;
boolean nullValuesAtStart = true;
Boolean orderByClause = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
if (orderByClause != null && orderByClause) {
List orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
useLinkedDataStructure = orderByAttrs.size() == 1;
nullValuesAtStart = !((CompiledSortCriterion) orderByAttrs.get(0)).getCriterion();
}
SelectResults returnSet = null;
if (finalItrs.size() == 1) {
ObjectType resultType = ((RuntimeIterator) finalItrs.iterator().next()).getElementType();
if (useLinkedDataStructure) {
returnSet = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType, nullValuesAtStart);
} else {
returnSet = QueryUtils.createResultCollection(context, resultType);
}
} else {
StructTypeImpl resultType = (StructTypeImpl) createStructTypeForRuntimeIterators(finalItrs);
if (useLinkedDataStructure) {
returnSet = context.isDistinct() ? new LinkedStructSet(resultType) : new SortedResultsBag<Struct>((StructTypeImpl) resultType, nullValuesAtStart);
} else {
returnSet = QueryUtils.createStructCollection(context, resultType);
}
}
cutDownAndExpandIndexResults(returnSet, result, indexFieldToItrsMapping, expansionList, finalItrs, context, checkList, iterOps, theFilteringIndex);
return returnSet;
}
Aggregations