use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CompiledComparison method singleBaseCollectionFilterEvaluate.
/** **************** PRIVATE METHODS ************************** */
/**
* 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, final boolean completeExpansionNeeded, @Retained CompiledValue iterOperands, IndexInfo indexInfo, RuntimeIterator[] indpndntItr, boolean isIntersection, boolean conditioningNeeded, boolean evaluateProj) throws TypeMismatchException, AmbiguousNameException, FunctionDomainException, NameResolutionException, QueryInvocationTargetException {
ObjectType resultType = indexInfo._index.getResultSetType();
int indexFieldsSize = -1;
SelectResults set = null;
boolean createEmptySet = false;
// Direct comparison with UNDEFINED will always return empty set
Object key = indexInfo.evaluateIndexKey(context);
createEmptySet = (key != null && key.equals(QueryService.UNDEFINED));
if (resultType instanceof StructType) {
indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
} else {
indexFieldsSize = 1;
}
// if the key is the LEFT operand, then reflect the operator
// before the index lookup
int op = reflectOnOperator(indexInfo._key());
// actual index lookup
QueryObserver observer = QueryObserverHolder.getInstance();
List projAttrib = null;
try {
if (!createEmptySet) {
observer.beforeIndexLookup(indexInfo._index, op, key);
context.cachePut(CompiledValue.INDEX_INFO, indexInfo);
}
// //////////////////////////////////////////////////////////
// Asif:Create an instance of IndexConditionHelper to see , if the match
// level is zero & expansion is to Group level & that no reshuffling is
// needed.
// If this holds true , then we will try to evaluate iter operand while
// collecting the results itself. Pass the iter operand as null so that
// we get the right idea. Also right now we will assume that only single
// iterator cases will be candidates for this oprtmization.
// dependent iterators will come later.
boolean useLinkedDataStructure = false;
boolean nullValuesAtStart = true;
Boolean orderByClause = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
if (orderByClause != null && orderByClause.booleanValue()) {
List orderByAttrs = (List) context.cacheGet(CompiledValue.ORDERBY_ATTRIB);
useLinkedDataStructure = orderByAttrs.size() == 1;
nullValuesAtStart = !((CompiledSortCriterion) orderByAttrs.get(0)).getCriterion();
}
// ////////////////////////////////////////////////////////////////
if (!conditioningNeeded) {
ObjectType projResultType = evaluateProj ? (ObjectType) context.cacheGet(RESULT_TYPE) : null;
if (projResultType != null) {
resultType = projResultType;
projAttrib = (List) context.cacheGet(PROJ_ATTRIB);
context.cachePut(RESULT_TYPE, Boolean.TRUE);
}
if (isIntersection) {
if (resultType instanceof StructType) {
context.getCache().getLogger().fine("StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
set = context.isDistinct() ? new LinkedStructSet((StructTypeImpl) resultType) : new SortedResultsBag<Struct>((StructTypeImpl) resultType, nullValuesAtStart);
} else {
set = QueryUtils.createStructCollection(context, (StructTypeImpl) resultType);
}
indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
} else {
context.getCache().getLogger().fine("non-StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
set = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType, nullValuesAtStart);
} else {
set = QueryUtils.createResultCollection(context, resultType);
}
indexFieldsSize = 1;
}
} else {
if (intermediateResults != null && context.getQuery() != null && ((DefaultQuery) context.getQuery()).getSelect().isDistinct()) {
set = intermediateResults;
intermediateResults = null;
} else {
if (resultType instanceof StructType) {
context.getCache().getLogger().fine("StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
set = context.isDistinct() ? new LinkedStructSet((StructTypeImpl) resultType) : new SortedResultsBag<Struct>((StructTypeImpl) resultType, nullValuesAtStart);
} else {
set = QueryUtils.createStructCollection(context, (StructTypeImpl) resultType);
}
indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
} else {
context.getCache().getLogger().fine("non-StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
set = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType, nullValuesAtStart);
} else {
set = QueryUtils.createResultCollection(context, resultType);
}
indexFieldsSize = 1;
}
}
}
if (!createEmptySet) {
indexInfo._index.query(key, op, set, iterOperands, indpndntItr != null ? indpndntItr[0] : null, context, projAttrib, intermediateResults, isIntersection);
}
} else {
if (resultType instanceof StructType) {
context.getCache().getLogger().fine("StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
set = context.isDistinct() ? new LinkedStructSet((StructTypeImpl) resultType) : new SortedResultsBag<Struct>((StructTypeImpl) resultType, nullValuesAtStart);
} else {
set = QueryUtils.createStructCollection(context, (StructTypeImpl) resultType);
}
indexFieldsSize = ((StructTypeImpl) resultType).getFieldNames().length;
} else {
context.getCache().getLogger().fine("non-StructType resultType.class=" + resultType.getClass().getName());
if (useLinkedDataStructure) {
set = context.isDistinct() ? new LinkedResultSet(resultType) : new SortedResultsBag(resultType, nullValuesAtStart);
} else {
set = QueryUtils.createResultCollection(context, resultType);
}
indexFieldsSize = 1;
}
if (!createEmptySet) {
// tow rows qualify from index
indexInfo._index.query(key, op, set, context);
}
// look up. rahul
}
} finally {
if (!createEmptySet) {
observer.afterIndexLookup(set);
}
}
if (conditioningNeeded) {
return QueryUtils.getConditionedIndexResults(set, indexInfo, context, indexFieldsSize, completeExpansionNeeded, iterOperands, indpndntItr);
} else {
return set;
}
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CompiledComparison method isConditioningNeededForIndex.
public boolean isConditioningNeededForIndex(RuntimeIterator independentIter, ExecutionContext context, boolean completeExpnsNeeded) throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
IndexConditioningHelper ich = null;
IndexInfo[] idxInfo = getIndexInfo(context);
int indexFieldsSize = -1;
boolean conditioningNeeded = true;
if (idxInfo == null || idxInfo.length > 1) {
return conditioningNeeded;
}
// assert idxInfo.length == 1;
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);
}
return ich == null || ich.shufflingNeeded;
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CompiledGroupBySelect method createNewElementType.
private ObjectType createNewElementType(ObjectType elementType, boolean isStruct) {
if (isStruct) {
StructType oldType = (StructType) elementType;
if (this.aggregateFunctions.length > 0) {
ObjectType[] oldFieldTypes = oldType.getFieldTypes();
ObjectType[] newFieldTypes = new ObjectType[oldFieldTypes.length];
int i = 0;
int aggFuncIndex = 0;
for (ObjectType oldFieldType : oldFieldTypes) {
if (this.aggregateColsPos.get(i)) {
newFieldTypes[i] = this.aggregateFunctions[aggFuncIndex++].getObjectType();
} else {
newFieldTypes[i] = oldFieldType;
}
++i;
}
return new StructTypeImpl(oldType.getFieldNames(), newFieldTypes);
} else {
return oldType;
}
} else {
return this.aggregateFunctions.length > 0 ? this.aggregateFunctions[0].getObjectType() : elementType;
}
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class AbstractGroupOrRangeJunction method auxIterateEvaluate.
/** invariant: the operand is known to be evaluated by iteration */
private SelectResults auxIterateEvaluate(CompiledValue operand, ExecutionContext context, SelectResults intermediateResults) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
// cannot be evaluated as a filter.
if (intermediateResults == null)
throw new RuntimeException(LocalizedStrings.AbstractGroupOrRangeJunction_INTERMEDIATERESULTS_CAN_NOT_BE_NULL.toLocalizedString());
if (// short circuit
intermediateResults.isEmpty())
return intermediateResults;
List currentIters = (this.completeExpansion) ? context.getCurrentIterators() : QueryUtils.getDependentItrChainForIndpndntItrs(this.indpndntItr, context);
SelectResults resultSet = null;
RuntimeIterator[] rIters = new RuntimeIterator[currentIters.size()];
currentIters.toArray(rIters);
ObjectType elementType = intermediateResults.getCollectionType().getElementType();
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("AND/OR operands must be of type boolean, not type '" + result.getClass().getName() + "'");
}
} finally {
observer.endIteration(resultSet);
}
return resultSet;
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class PartitionedRegionQueryEvaluator method buildCumulativeResults.
private SelectResults buildCumulativeResults(boolean isDistinct, int limit) {
// Indicates whether to check for PdxInstance and convert them to
// domain object.
// In case of local queries the domain objects are stored in the result set
// for client/server queries PdxInstance are stored in result set.
boolean getDomainObjectForPdx;
// indicated results from remote nodes need to be deserialized
// for local queries
boolean getDeserializedObject = false;
int numElementsInResult = 0;
ObjectType elementType = this.cumulativeResults.getCollectionType().getElementType();
boolean isStruct = elementType != null && elementType.isStructType();
final DistributedMember me = this.pr.getMyId();
if (DefaultQuery.testHook != null) {
DefaultQuery.testHook.doTestHook(4);
}
boolean localResults = false;
List<CumulativeNonDistinctResults.Metadata> collectionsMetadata = null;
List<Collection> results = null;
if (isDistinct) {
if (isStruct) {
StructType stype = (StructType) elementType;
this.cumulativeResults = new StructSet(stype);
} else {
this.cumulativeResults = new ResultsSet(elementType);
}
} else {
collectionsMetadata = new ArrayList<CumulativeNonDistinctResults.Metadata>();
results = new ArrayList<Collection>();
}
for (Map.Entry<InternalDistributedMember, Collection<Collection>> e : this.resultsPerMember.entrySet()) {
checkLowMemory();
// retrieved on the client side.
if (e.getKey().equals(me)) {
// In case of local node query results, the objects are already in
// domain object form
getDomainObjectForPdx = false;
localResults = true;
// for select * queries on local node return deserialized objects
} else {
// In case of remote nodes, the result objects are in PdxInstance form
// get domain objects for local queries.
getDomainObjectForPdx = !(this.pr.getCache().getPdxReadSerializedByAnyGemFireServices());
// deserialize the value
if (!getDeserializedObject && !((DefaultQuery) this.query).isKeepSerialized()) {
getDeserializedObject = true;
}
}
final boolean isDebugEnabled = logger.isDebugEnabled();
if (!isDistinct) {
CumulativeNonDistinctResults.Metadata wrapper = CumulativeNonDistinctResults.getCollectionMetadata(getDomainObjectForPdx, getDeserializedObject, localResults);
for (Collection res : e.getValue()) {
results.add(res);
collectionsMetadata.add(wrapper);
}
} else {
for (Collection res : e.getValue()) {
checkLowMemory();
// final TaintableArrayList res = (TaintableArrayList) e.getValue();
if (res != null) {
if (isDebugEnabled) {
logger.debug("Query Result from member :{}: {}", e.getKey(), res.size());
}
if (numElementsInResult == limit) {
break;
}
boolean[] objectChangedMarker = new boolean[1];
for (Object obj : res) {
checkLowMemory();
int occurence = 0;
obj = PDXUtils.convertPDX(obj, isStruct, getDomainObjectForPdx, getDeserializedObject, localResults, objectChangedMarker, true);
boolean elementGotAdded = isStruct ? ((StructSet) this.cumulativeResults).addFieldValues((Object[]) obj) : this.cumulativeResults.add(obj);
occurence = elementGotAdded ? 1 : 0;
// for non distinct query
if (occurence == 1) {
++numElementsInResult;
// time and then exit. It will exit immediately on this return
if (numElementsInResult == limit) {
break;
}
}
}
}
}
}
}
if (prQueryTraceInfoList != null && this.query.isTraced() && logger.isInfoEnabled()) {
if (DefaultQuery.testHook != null) {
DefaultQuery.testHook.doTestHook("Create PR Query Trace String");
}
StringBuilder sb = new StringBuilder();
sb.append(LocalizedStrings.PartitionedRegion_QUERY_TRACE_LOG.toLocalizedString(this.query.getQueryString())).append("\n");
for (PRQueryTraceInfo queryTraceInfo : prQueryTraceInfoList) {
sb.append(queryTraceInfo.createLogLine(me)).append("\n");
}
logger.info(sb.toString());
;
}
if (!isDistinct) {
this.cumulativeResults = new CumulativeNonDistinctResults(results, limit, this.cumulativeResults.getCollectionType().getElementType(), collectionsMetadata);
}
return this.cumulativeResults;
}
Aggregations