use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class CompiledSelect method applyProjectionOnCollection.
private SelectResults applyProjectionOnCollection(SelectResults resultSet, ExecutionContext context, boolean ignoreOrderBy) throws TypeMismatchException, AmbiguousNameException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
List iterators = context.getCurrentIterators();
if (projAttrs == null && (this.orderByAttrs == null || ignoreOrderBy)) {
// as it is.
if (iterators.size() > 1) {
StructType type = createStructTypeForNullProjection(iterators, context);
((SelectResults) resultSet).setElementType(type);
}
return resultSet;
} else {
int numElementsAdded = 0;
SelectResults pResultSet = prepareEmptyResultSet(context, ignoreOrderBy);
boolean isStructType = resultSet.getCollectionType().getElementType() != null && resultSet.getCollectionType().getElementType().isStructType();
if (isStructType) {
Iterator resultsIter = resultSet.iterator();
// Apply limit if there is no order by.
Integer limitValue = evaluateLimitValue(context, this.limit);
while (((this.orderByAttrs != null && !ignoreOrderBy) || limitValue < 0 || (numElementsAdded < limitValue)) && resultsIter.hasNext()) {
// Check if query execution on this thread is canceled.
QueryMonitor.isQueryExecutionCanceled();
Object[] values = ((Struct) resultsIter.next()).getFieldValues();
for (int i = 0; i < values.length; i++) {
((RuntimeIterator) iterators.get(i)).setCurrent(values[i]);
}
int occurence = applyProjectionAndAddToResultSet(context, pResultSet, ignoreOrderBy);
if (occurence == 1 || (occurence > 1 && !this.distinct)) {
// (Unique i.e first time occurence) or subsequent occurence
// for non distinct query
++numElementsAdded;
}
}
// return pResultSet;
} else if (iterators.size() == 1) {
RuntimeIterator rIter = (RuntimeIterator) iterators.get(0);
Iterator resultsIter = ((SelectResults) resultSet).iterator();
// Apply limit if there is no order by.
Integer limitValue = evaluateLimitValue(context, this.limit);
while (((this.orderByAttrs != null && !ignoreOrderBy) || limitValue < 0 || (numElementsAdded < limitValue)) && resultsIter.hasNext()) {
rIter.setCurrent(resultsIter.next());
int occurence = applyProjectionAndAddToResultSet(context, pResultSet, ignoreOrderBy);
if (occurence == 1 || (occurence > 1 && !this.distinct)) {
// (Unique i.e first time occurence) or subsequent occurence
// for non distinct query
++numElementsAdded;
}
}
} else {
throw new RuntimeException(LocalizedStrings.CompiledSelect_RESULT_SET_DOES_NOT_MATCH_WITH_ITERATOR_DEFINITIONS_IN_FROM_CLAUSE.toLocalizedString());
}
return pResultSet;
}
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class CompiledSelect method getEmptyResultSet.
/*
* Gets the appropriate empty results set when outside of actual query evalutaion.
*
* @param parameters the parameters that will be passed into the query when evaluated
*
* @param cache the cache the query will be executed in the context of
*
* @return the empty result set of the appropriate type
*/
public SelectResults getEmptyResultSet(Object[] parameters, InternalCache cache, Query query) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
ExecutionContext context = new QueryExecutionContext(parameters, cache, query);
computeDependencies(context);
context.newScope((Integer) context.cacheGet(scopeID));
context.pushExecCache((Integer) context.cacheGet(scopeID));
SelectResults results = null;
try {
Iterator iter = iterators.iterator();
while (iter.hasNext()) {
CompiledIteratorDef iterDef = (CompiledIteratorDef) iter.next();
RuntimeIterator rIter = iterDef.getRuntimeIterator(context);
context.bindIterator(rIter);
}
results = prepareEmptyResultSet(context, false);
} finally {
context.popScope();
context.popExecCache();
}
return results;
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class CompiledGroupBySelect method applyAggregateAndGroupBy.
public SelectResults applyAggregateAndGroupBy(SelectResults baseResults, ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
ObjectType elementType = baseResults.getCollectionType().getElementType();
boolean isStruct = elementType != null && elementType.isStructType();
boolean isBucketNodes = context.getBucketList() != null;
boolean createOrderedResultSet = isBucketNodes && this.orderByAttrs != null;
boolean[] objectChangedMarker = new boolean[] { false };
int limitValue = evaluateLimitValue(context, limit);
SelectResults newResults = createResultSet(context, elementType, isStruct, createOrderedResultSet);
Aggregator[] aggregators = new Aggregator[this.aggregateFunctions.length];
refreshAggregators(aggregators, context);
if (this.orderByAttrs != null) {
applyGroupBy(baseResults, context, isStruct, newResults, aggregators, !createOrderedResultSet, objectChangedMarker, limitValue);
} else {
Iterator iter = baseResults.iterator();
Object current = null;
boolean unterminated = iter.hasNext();
while (iter.hasNext()) {
current = iter.next();
accumulate(isStruct, aggregators, current, objectChangedMarker);
}
if (unterminated) {
this.terminateAndAddToResults(isStruct, newResults, aggregators, current, context, !createOrderedResultSet, limitValue);
}
}
return newResults;
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class CompiledGroupBySelect method createResultSet.
private SelectResults createResultSet(ExecutionContext context, ObjectType elementType, boolean isStruct, boolean createOrderedResults) {
elementType = createNewElementType(elementType, isStruct);
SelectResults newResults;
// boolean isBucketNodes = context.getBucketList() != null;
boolean isPrQueryNode = context.getIsPRQueryNode();
// If it is bucket nodes query, we need to return ordered data
if (isStruct) {
if (createOrderedResults) {
newResults = new SortedResultsBag((StructTypeImpl) elementType, true);
} else {
if (this.originalOrderByClause != null) {
Comparator comparator = new OrderByComparator(this.originalOrderByClause, elementType, context);
newResults = new SortedStructBag(comparator, (StructType) elementType, !this.originalOrderByClause.get(0).getCriterion());
} else {
newResults = QueryUtils.createStructCollection(this.isDistinct, (StructType) elementType, context);
}
}
} else {
if (createOrderedResults) {
newResults = new SortedResultsBag(elementType, true);
} else {
if (this.originalOrderByClause != null) {
Comparator comparator = new OrderByComparator(this.originalOrderByClause, elementType, context);
newResults = new SortedResultsBag(comparator, elementType, !this.originalOrderByClause.get(0).getCriterion());
} else {
newResults = QueryUtils.createResultCollection(this.isDistinct, elementType, context);
}
}
}
return newResults;
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class CompiledLike method filterEvaluate.
@Override
public SelectResults filterEvaluate(ExecutionContext context, SelectResults intermediateResults) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
RuntimeIterator grpItr = (RuntimeIterator) QueryUtils.getCurrentScopeUltimateRuntimeIteratorsIfAny(this, context).iterator().next();
OrganizedOperands newOperands = organizeOperands(context, true, new RuntimeIterator[] { grpItr });
assert newOperands.iterateOperand == null;
SelectResults result = intermediateResults;
result = (newOperands.filterOperand).filterEvaluate(context, intermediateResults);
return result;
}
Aggregations