use of org.apache.geode.cache.query.internal.types.StructTypeImpl 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.internal.types.StructTypeImpl in project geode by apache.
the class AbstractCompiledValue method filterEvaluate.
public SelectResults filterEvaluate(ExecutionContext context, SelectResults intermediateResults) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
// for the general case of filter evaluation, most compiled values
// can only be evaluated in this way if they are independent of the current
// iterator,
// This method is also only applicable in places where a boolean value is
// expected.
// RuntimeIterator itr = context.getCurrentIterator();
// Support.Assert(itr != null);
// if it is possible for the following assertion to be false,
// then this method as well as protGetPlanInfo must be overridden
Support.Assert(!isDependentOnCurrentScope(context));
Object result = evaluate(context);
if (result == null || result == QueryService.UNDEFINED)
return new ResultsBag(intermediateResults.getCollectionType().getElementType(), 0, context.getCachePerfStats());
if (!(result instanceof Boolean))
throw new TypeMismatchException(LocalizedStrings.AbstractCompiledValue_BOOLEAN_VALUE_EXPECTED_NOT_TYPE_0.toLocalizedString(result.getClass().getName()));
boolean b = ((Boolean) result).booleanValue();
// indicated by null value. A false means an empty ResultSet
if (b) {
return null;
} else {
// Asif : We need to return either an empty ResultSet or an empty
// StructSet based
// on the number of iterators in the current scope.
SelectResults emptySet = null;
List iterators = context.getCurrentIterators();
int len = iterators.size();
if (len == 1) {
ObjectType elementType = ((RuntimeIterator) iterators.get(0)).getElementType();
emptySet = context.isDistinct() ? new ResultsSet(elementType) : new ResultsBag(elementType, 0, context.getCachePerfStats());
} else {
String[] fieldNames = new String[len];
ObjectType[] fieldTypes = new ObjectType[len];
for (int i = 0; i < len; i++) {
RuntimeIterator iter = (RuntimeIterator) iterators.get(i);
fieldNames[i] = iter.getInternalId();
fieldTypes[i] = iter.getElementType();
}
emptySet = context.isDistinct() ? new StructSet(new StructTypeImpl(fieldNames, fieldTypes)) : new StructBag(0, new StructTypeImpl(fieldNames, fieldTypes), context.getCachePerfStats());
}
return emptySet;
}
}
use of org.apache.geode.cache.query.internal.types.StructTypeImpl in project geode by apache.
the class QueryUtils method cartesian.
/**
* This util function does a cartesian of the array of SelectResults object , expanding the
* resultant set to the number of iterators passed in expansionList. The position of the iterator
* fields in the final result is governed by the order of RuntimeIterators present in the
* finalList. If any condition needs to be evaluated during cartesian , it can be passed as
* operand
*
* @param results Array of SelectResults object which are to be cartesianed
* @param itrsForResultFields A two dimensional array of RuntimeIterator. Each row of this two
* dimensional RuntimeIterator array , maps to a SelectResults object in the results array.
* Thus the 0th row of two dimensional RuntimeIterator array will map to the 0th element of
* the SelectResults array. The order of RuntimeIterator in a row will map to the fields in
* the SelectResults object. The 0th RuntimeIterator will map to the 0th field of the
* corresponding SelectResults object. The number of rows in the two dimensional array of
* RuntimeIterator should be equal to the size of array of SelectResults object passed and
* the number of RuntimeIterators in each row should be equal to the number of fields in
* the SelectResults object . The SelectResults object itself may be a ResultBag object or
* a StructBag object.
*
* @param expansionList List containing RunimeIterators to which the final Results should be
* expanded to.
* @param finalList List containing RuntimeIterators which define the number of fields to be
* present in the resultant SelectResults and their relative positions. The Runtime
* Iterators present in the List should be either available in the expansion List or should
* be present in each row of the two dimensional RuntimeIterator array.
*
* @param context ExecutionContext object
* @param operand The CompiledValue which needs to be iter evaluated during cartesian. Only those
* tuples will be selected in the final Result for which oerand evaluates to true.
* @return SelectResults object representing the final result of the cartesian
*/
public static SelectResults cartesian(SelectResults[] results, RuntimeIterator[][] itrsForResultFields, List expansionList, List finalList, ExecutionContext context, CompiledValue operand) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
SelectResults returnSet = null;
if (finalList.size() == 1) {
ObjectType type = ((RuntimeIterator) finalList.iterator().next()).getElementType();
if (type instanceof StructType) {
returnSet = QueryUtils.createStructCollection(context, (StructTypeImpl) type);
} else {
return QueryUtils.createResultCollection(context, type);
}
} else {
StructType structType = createStructTypeForRuntimeIterators(finalList);
returnSet = QueryUtils.createStructCollection(context, structType);
}
ListIterator expnItr = expansionList.listIterator();
doNestedIterations(0, returnSet, results, itrsForResultFields, finalList, expnItr, results.length + expansionList.size(), context, operand);
return returnSet;
}
use of org.apache.geode.cache.query.internal.types.StructTypeImpl in project geode by apache.
the class QueryUtils method doNestedIterationsForIndex.
private static void doNestedIterationsForIndex(boolean continueRecursion, SelectResults resultSet, List finalItrs, ListIterator expansionItrs, ExecutionContext context, CompiledValue iterOps, int limit, Map<String, SelectResults> derivedResults) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
if (!continueRecursion) {
// end recusrion
Iterator itr = finalItrs.iterator();
int len = finalItrs.size();
boolean select = true;
if (iterOps != null) {
select = applyCondition(iterOps, context);
}
if (len > 1) {
boolean isOrdered = resultSet.getCollectionType().isOrdered();
StructTypeImpl elementType = (StructTypeImpl) resultSet.getCollectionType().getElementType();
// TODO: Optimize the LinkedStructSet implementation so that Object[] can be added rather
// than Struct
Object[] values = new Object[len];
int j = 0;
// creates tuple
while (itr.hasNext()) {
// Check if query execution on this thread is canceled.
QueryMonitor.isQueryExecutionCanceled();
values[j++] = ((RuntimeIterator) itr.next()).evaluate(context);
}
if (select) {
if (isOrdered) {
// ((LinkedStructSet) resultSet).add(new StructImpl(elementType, values));
// Can be LinkedStructSet or SortedResultsBag ( containing underlying LinkedHashMap)
resultSet.add(new StructImpl(elementType, values));
} else {
((StructFields) resultSet).addFieldValues(values);
}
}
} else {
if (select)
resultSet.add(((RuntimeIterator) itr.next()).evaluate(context));
}
} else {
RuntimeIterator currentLevel = (RuntimeIterator) expansionItrs.next();
// Calculate the key to find the derived join results. If we are a non nested lookup it will
// be a Compiled Region otherwise it will be a CompiledPath that
// we can extract the id from. In the end the result will be the alias which is used as a
// prefix
CompiledValue collectionExpression = currentLevel.getCmpIteratorDefn().getCollectionExpr();
String key = null;
boolean useDerivedResults = true;
if (currentLevel.getCmpIteratorDefn().getCollectionExpr().getType() == OQLLexerTokenTypes.RegionPath) {
key = currentLevel.getCmpIteratorDefn().getName() + ':' + currentLevel.getDefinition();
} else if (currentLevel.getCmpIteratorDefn().getCollectionExpr().getType() == OQLLexerTokenTypes.LITERAL_select) {
useDerivedResults = false;
} else {
key = getCompiledIdFromPath(currentLevel.getCmpIteratorDefn().getCollectionExpr()).getId() + ':' + currentLevel.getDefinition();
}
SelectResults c;
if (useDerivedResults && derivedResults != null && derivedResults.containsKey(key)) {
c = derivedResults.get(key);
} else {
c = currentLevel.evaluateCollection(context);
}
if (c == null) {
expansionItrs.previous();
return;
}
for (Object aC : c) {
// Check if query execution on this thread is canceled.
QueryMonitor.isQueryExecutionCanceled();
currentLevel.setCurrent(aC);
doNestedIterationsForIndex(expansionItrs.hasNext(), resultSet, finalItrs, expansionItrs, context, iterOps, limit, derivedResults);
if (limit != -1 && resultSet.size() >= limit) {
break;
}
}
expansionItrs.previous();
}
}
use of org.apache.geode.cache.query.internal.types.StructTypeImpl in project geode by apache.
the class QueryUtils method getConditionedRelationshipIndexResultsExpandedToTopOrCGJLevel.
/**
* This function is used to evaluate a filter evaluatable composite condition. It gets invoked
* either from a CompositeGroupJunction of "OR" type or a where clause containing single composite
* condition. In the later case the boolean completeExpansion flag is always true. While in the
* former case it may be true or false. If it is false, the array of independent iterators passed
* is not null.
*
* @param data A List object whose elements are two dimensional object array. Each element of the
* List represent a value which satisfies the equi-join condition. Since there may be more
* than one tuples on either side of the equality condition which meet the criteria for a
* given value, we require a 2 dimensional Object array. The cartesian of the two rows will
* give us the set of tuples satisfying the join criteria. Each element of the row of
* Object Array may be either an Object or a Struct object.
* @param indxInfo An array of IndexInfo objects of size 2 , representing the range indexes of the
* two operands. The other Index maps to the 0th Object array row of the List object ( data
* ) & so on.
* @param context ExecutionContext object
* @param completeExpansionNeeded boolean if true indicates that the CGJ needs to be expanded to
* the query from clause ( top level )
* @param iterOperands This will be null as for OR junction we cannot have iter operand
* @param indpdntItrs Array of independent iterators representing the various Groups forming the
* composite group junction. It will be null, if complete expansion flag is true
* @return SelectResults objet representing the result obtained by evaluating a filter evaluatable
* composite condition in an OR junction. The returned Result is expanded either to the
* CompositeGroupJunction level or to the top level as the case may be
*/
static SelectResults getConditionedRelationshipIndexResultsExpandedToTopOrCGJLevel(List data, IndexInfo[] indxInfo, ExecutionContext context, boolean completeExpansionNeeded, CompiledValue iterOperands, RuntimeIterator[] indpdntItrs) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
ObjectType resultType1 = indxInfo[0]._index.getResultSetType();
int indexFieldsSize1 = resultType1 instanceof StructType ? ((StructTypeImpl) resultType1).getFieldNames().length : 1;
ObjectType resultType2 = indxInfo[1]._index.getResultSetType();
int indexFieldsSize2 = resultType2 instanceof StructType ? ((StructTypeImpl) resultType2).getFieldNames().length : 1;
/*
* even if th complete expansion is needed pass the flag of complete expansion as false. Thus
* for LHS & RHS we will get the expansionList for that individual group. Thus the total
* expansion List wil contain sum of the individual expansion lists plus all the iterators of
* the current scope which are dependent on any other groups or are composite iterators ( i.e
* dependent on both the independent groups currently under consideration
*/
// pass completeExpansion as false, irrespective of actual value
IndexConditioningHelper ich1 = new IndexConditioningHelper(indxInfo[0], context, indexFieldsSize1, false, iterOperands, null);
// pass completeExpansion as false, irrespective of actual value
IndexConditioningHelper ich2 = new IndexConditioningHelper(indxInfo[1], context, indexFieldsSize2, false, iterOperands, null);
List totalExpList = new ArrayList();
totalExpList.addAll(ich1.expansionList);
totalExpList.addAll(ich2.expansionList);
List totalFinalList = null;
if (completeExpansionNeeded) {
totalFinalList = context.getCurrentIterators();
Set expnItrsAlreadyAccounted = new HashSet();
expnItrsAlreadyAccounted.addAll(ich1.finalList);
expnItrsAlreadyAccounted.addAll(ich2.finalList);
int size = totalFinalList.size();
for (int i = 0; i < size; ++i) {
RuntimeIterator currItr = (RuntimeIterator) totalFinalList.get(i);
// If the runtimeIterators of scope not present in CheckSet add it to the expansion list
if (!expnItrsAlreadyAccounted.contains(currItr)) {
totalExpList.add(currItr);
}
}
} else {
totalFinalList = new ArrayList();
for (int i = 0; i < indpdntItrs.length; ++i) {
RuntimeIterator indpndntItr = indpdntItrs[i];
if (indpndntItr == ich1.finalList.get(0)) {
totalFinalList.addAll(ich1.finalList);
} else if (indpndntItr == ich2.finalList.get(0)) {
totalFinalList.addAll(ich2.finalList);
} else {
List temp = context.getCurrScopeDpndntItrsBasedOnSingleIndpndntItr(indpndntItr);
totalFinalList.addAll(temp);
totalExpList.addAll(temp);
}
}
}
SelectResults returnSet;
StructType stype = createStructTypeForRuntimeIterators(totalFinalList);
if (totalFinalList.size() == 1) {
returnSet = QueryUtils.createResultCollection(context, new ObjectTypeImpl(stype.getClass()));
} else {
returnSet = QueryUtils.createStructCollection(context, stype);
}
RuntimeIterator[][] mappings = new RuntimeIterator[2][];
mappings[0] = ich1.indexFieldToItrsMapping;
mappings[1] = ich2.indexFieldToItrsMapping;
List[] totalCheckList = new List[] { ich1.checkList, ich2.checkList };
Iterator dataItr = data.iterator();
IndexCutDownExpansionHelper[] icdeh = new IndexCutDownExpansionHelper[] { new IndexCutDownExpansionHelper(ich1.checkList, context), new IndexCutDownExpansionHelper(ich2.checkList, context) };
ListIterator expansionListIterator = totalExpList.listIterator();
if (dataItr.hasNext()) {
QueryObserver observer = QueryObserverHolder.getInstance();
try {
observer.beforeMergeJoinOfDoubleIndexResults(ich1.indxInfo._index, ich2.indxInfo._index, data);
while (dataItr.hasNext()) {
// TODO: Change the code in range Index so that while collecting data instead of creating
// two dimensional object array , we create one dimensional Object array of size 2, & each
// elemnt stores an Object array
Object[][] values = (Object[][]) dataItr.next();
// Before doing the cartesian of the Results , we need to clear the CheckSet of
// IndexCutDownExpansionHelper. This is needed because for a new key , the row of sets
// needs to be considered fresh as presence of old row in checkset may cause us to wrongly
// skip the similar row of a set , even when the row in its entirety is unique ( made by
// different data in the other set)
mergeAndExpandCutDownRelationshipIndexResults(values, returnSet, mappings, expansionListIterator, totalFinalList, context, totalCheckList, iterOperands, icdeh, 0);
if (icdeh[0].cutDownNeeded)
icdeh[0].checkSet.clear();
}
} finally {
observer.afterMergeJoinOfDoubleIndexResults(returnSet);
}
}
return returnSet;
}
Aggregations