use of org.apache.geode.cache.query.internal.types.StructTypeImpl in project geode by apache.
the class CompositeGroupJunction method evaluateAndJunction.
private SelectResults evaluateAndJunction(ExecutionContext context) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
// Asif: We will evaluate the iter operand while getting the result of
// composite condition iff there exists only one Filterable Composite
// Condition & no GroupJunction
CompiledValue iterOp = null;
SelectResults intermediateResults = null;
// TODO: Check if null can come for the iter operand List
if (this.iterOperands != null && !this.iterOperands.isEmpty()) {
int len = this.iterOperands.size();
if (len == 1) {
iterOp = (CompiledValue) this.iterOperands.get(0);
} else {
CompiledValue[] newOperands = new CompiledValue[len];
newOperands = (CompiledValue[]) this.iterOperands.toArray(newOperands);
iterOp = new CompiledJunction(newOperands, this.operator);
}
}
SortedSet intersectionSet = new TreeSet(new SelectResultsComparator());
// TODO:Asif: Clean this logic. If the iter operand is not null when should
// we evaluate it.? Ideally if we were to stick to our logic of delaying
// evaluation of condition , then we shoudl ideally first do intersection of
// results obtained using indexes on conditions & then apply iter operand on
// it. But some how it appears that the final iteration will be more
// expensive then evaluation. So we use for time being following idea. If
// the iter condition is not null & there exists atleast one Group Junction
// ( then we are sure it will need expansion to CompositeGroupJunction
// level, so the iter operand can be evaluated at that point, else we will
// evaluate it with the first available filterable cc
// Asif : Obtain results of Filter evaluatable composite conditions
// Support.Assert( iterOp == null || ( iterOp !=null &&
// this.filterableCC.size() == 1 && this.groupJunctions == null), "The Iter
// operand can be not null only if there exists single filterable CC & no
// group junction");
boolean delayIterOpEval = (this.groupJunctions != null && this.groupJunctions.size() != 0);
Iterator itr = this.filterableCC.iterator();
int filterableCCSize = this.filterableCC.size();
if (filterableCCSize > 1) {
for (int i = 0; i < (filterableCCSize - 1); i++) {
CompiledValue cc = (CompiledValue) itr.next();
intermediateResults = ((Filter) cc).filterEvaluate(context, intermediateResults, false, null, /* iterOpn = null */
null, /* send independent itrs null */
false, true, false);
if (intermediateResults.isEmpty()) {
StructType structType = QueryUtils.createStructTypeForRuntimeIterators(this.completeExpansion ? context.getCurrentIterators() : QueryUtils.getDependentItrChainForIndpndntItrs(this.indpndnts, context));
return QueryUtils.createStructCollection(context, structType);
}
}
}
CompiledValue cc = (CompiledValue) itr.next();
intermediateResults = ((Filter) cc).filterEvaluate(context, intermediateResults, this.completeExpansion, delayIterOpEval ? null : iterOp, this.indpndnts, /*
* Since this is the last condition pass the indpndt. grp of itrs so that
* result structset with correct placement of itrs can be formed
*/
false, true, false);
intersectionSet.add(intermediateResults);
intermediateResults = null;
if (iterOp != null && !delayIterOpEval) {
iterOp = null;
}
Support.Assert(iterOp == null || (this.groupJunctions != null), "The Iter operand can be not null only if there exists atleast one group junction");
// can use it. Identify a cleaner approach to it other than null check
if (this.groupJunctions != null) {
int len = this.groupJunctions.size();
if (len > 1) {
// Asif : Create an array of SelectResults for each of the GroupJunction
// For each Group Junction there will be a corresponding array of
// RuntimeIterators
// which will map to the fields of the ResultSet obtained from
// Group Junction
SelectResults[] results = new SelectResults[len];
// Asif : the final list will be some of all the iterators depending on
// each of indpendent group if complete expansion is not needed
List finalList = null;
if (this.completeExpansion) {
finalList = context.getCurrentIterators();
} else {
finalList = QueryUtils.getDependentItrChainForIndpndntItrs(this.indpndnts, context);
}
List expansionList = new LinkedList(finalList);
RuntimeIterator[][] itrsForResultFields = new RuntimeIterator[len][];
AbstractGroupOrRangeJunction gj = null;
Iterator junctionItr = this.groupJunctions.iterator();
List grpItrs = null;
int j = 0;
RuntimeIterator tempItr = null;
while (junctionItr.hasNext()) {
gj = (AbstractGroupOrRangeJunction) junctionItr.next();
SelectResults filterResults = ((Filter) gj).filterEvaluate(context, null);
Support.Assert(filterResults != null, "FilterResults cannot be null here");
if (filterResults.isEmpty()) {
// TODO Asif : Compact the code of creation of empty set.
// Asif: Create an empty resultset of the required type & return
// it. e cannot use the existing Resultset as it may not be expanded
// to the required level.
SelectResults empty = null;
if (finalList.size() == 1) {
ObjectType type = ((RuntimeIterator) finalList.iterator().next()).getElementType();
if (type instanceof StructType) {
empty = QueryUtils.createStructCollection(context, (StructTypeImpl) type);
} else {
empty = QueryUtils.createResultCollection(context, type);
}
} else {
StructType strucType = QueryUtils.createStructTypeForRuntimeIterators(finalList);
empty = QueryUtils.createStructCollection(context, strucType);
}
return empty;
} else {
results[j] = filterResults;
grpItrs = context.getCurrScopeDpndntItrsBasedOnSingleIndpndntItr(gj.getIndependentIteratorForGroup()[0]);
itrsForResultFields[j] = new RuntimeIterator[grpItrs.size()];
Iterator grpItr = grpItrs.iterator();
int k = 0;
while (grpItr.hasNext()) {
tempItr = (RuntimeIterator) grpItr.next();
itrsForResultFields[j][k++] = tempItr;
expansionList.remove(tempItr);
}
++j;
}
}
// Do the cartesian of the different group junction results.
// TODO:Asif Remove the time
QueryObserver observer = QueryObserverHolder.getInstance();
observer.beforeCartesianOfGroupJunctionsInCompositeGroupJunctionOfType_AND(results);
SelectResults grpCartRs = QueryUtils.cartesian(results, itrsForResultFields, expansionList, finalList, context, iterOp);
observer.afterCartesianOfGroupJunctionsInCompositeGroupJunctionOfType_AND();
Support.Assert(grpCartRs != null, "ResultsSet obtained was NULL in CompositeGroupJunction");
intersectionSet.add(grpCartRs);
} else {
// TODO:Asif : Examine this logic as expansion of a GroupJunction to a a
// CompositeGroupJunction level may not be a good idea, as each filter
// evaluatable condition in a GroupJunction will have to be
// unnecessarily expanded to the Composite GroupJunction level . This
// will be a heavy operation & also the size of the sets to be
// intersected will also be quite large. Ideally we should expand the
// GroupJunction to CompositeGroupJunction level only if there exists a
// single filter evaluatable condition in a GroupJunction. Asif : If
// there exists only one GroupJunction in a CompsoiteGroupJunction ,
// then we can afford to expand the result of GroupJunction to the level
// of CompositeGroupJunction directly within the GroupJunction Thus
// create a new GroupJunction with independent group of iterators same
// as that of CompositeGroupJunction if complete expansion is false. If
// complete expansion is true then create a new GroupJunction only if
// there is residual iter operand. Thus this is the only special case (&
// iff completeExpansion boolean false) where in a GroupJunction will
// contain more than one independent iterators TODO:ASIF: CHECK IT
// OUT..................
AbstractGroupOrRangeJunction newGJ = (AbstractGroupOrRangeJunction) this.groupJunctions.get(0);
if (!this.completeExpansion) {
newGJ = newGJ.recreateFromOld(this.completeExpansion, this.indpndnts, iterOp);
} else if (iterOp != null) {
// Asif :Complete expansion is true. In this case we should not pass
// the Group of iterators belonging to the CompositeGroupJunction but
// stick to independent iterator for the group. Also we are creating a
// new GroupJunction below only bcoz of extra iter operand. For
// toggling the complete xpansion flag of Grp Junction to on , that
// task is alreday done. in setCompleteExpansionOn of CGJ
newGJ = newGJ.recreateFromOld(this.completeExpansion, newGJ.getIndependentIteratorForGroup(), iterOp);
}
SelectResults rs = newGJ.filterEvaluate(context, null);
if (rs.isEmpty()) {
return rs;
} else {
intersectionSet.add(rs);
}
}
}
Iterator iter = intersectionSet.iterator();
while (iter.hasNext()) {
SelectResults sr = (SelectResults) iter.next();
intermediateResults = (intermediateResults == null) ? sr : QueryUtils.intersection(intermediateResults, sr, context);
}
return intermediateResults;
}
use of org.apache.geode.cache.query.internal.types.StructTypeImpl in project geode by apache.
the class CompiledIn method isConditioningNeededForIndex.
public boolean isConditioningNeededForIndex(RuntimeIterator independentIter, ExecutionContext context, boolean completeExpnsNeeded) throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
IndexConditioningHelper ich = null;
IndexInfo[] idxInfo = getIndexInfo(context);
assert idxInfo.length == 1;
int indexFieldsSize = -1;
boolean conditioningNeeded = true;
ObjectType indexRsltType = idxInfo[0]._index.getResultSetType();
if (indexRsltType instanceof StructType) {
indexFieldsSize = ((StructTypeImpl) indexRsltType).getFieldNames().length;
} else {
indexFieldsSize = 1;
}
if (independentIter != null && indexFieldsSize == 1) {
ich = new IndexConditioningHelper(idxInfo[0], context, indexFieldsSize, completeExpnsNeeded, null, independentIter);
}
conditioningNeeded = ich == null || ich.shufflingNeeded;
return conditioningNeeded;
}
use of org.apache.geode.cache.query.internal.types.StructTypeImpl 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.internal.types.StructTypeImpl 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.internal.types.StructTypeImpl 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