use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CompiledSelect method applyProjectionAndAddToResultSet.
// resultSet could be a set or a bag (we have set constructor, or there
// could be a distinct subquery)
// in future, it would be good to simplify this to always work with a bag
// (converting all sets to bags) until the end when we enforce distinct
// The number returned indicates the occurence of the data in the SelectResults
// Thus if the SelectResults is of type ResultsSet or StructSet
// then 1 will indicate that data was added to the results & that was the
// first occurence. For this 0 will indicate that the data was not added
// because it was a duplicate
// If the SelectResults is an instance ResultsBag or StructsBag , the number will
// indicate the occurence. Thus 1 will indicate it being added for first time
// Currently orderBy is present only for StructSet & ResultSet which are
// unique object holders. So the occurence for them can be either 0 or 1 only
private int applyProjectionAndAddToResultSet(ExecutionContext context, SelectResults resultSet, boolean ignoreOrderBy) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
List currrentRuntimeIters = context.getCurrentIterators();
int occurence = 0;
ObjectType elementType = resultSet.getCollectionType().getElementType();
boolean isStruct = elementType != null && elementType.isStructType();
// TODO: Optimize this condition in some clean way
boolean isLinkedStructure = resultSet instanceof Ordered && ((Ordered) resultSet).dataPreordered();
ArrayList evaluatedOrderByClause = null;
OrderByComparator comparator = null;
boolean applyOrderBy = false;
if (this.orderByAttrs != null && !ignoreOrderBy) {
// In case PR order-by will get applied on the coordinator node
// on the cumulative results. Apply the order-by on PR only if
// limit is specified.
Integer limitValue = evaluateLimitValue(context, this.limit);
if (context.getPartitionedRegion() != null && limitValue < 0) {
applyOrderBy = false;
}
applyOrderBy = true;
}
if (this.orderByAttrs != null && !ignoreOrderBy) {
comparator = (OrderByComparator) ((Ordered) resultSet).comparator();
}
if (projAttrs == null) {
int len = currrentRuntimeIters.size();
Object[] values = new Object[len];
for (int i = 0; i < len; i++) {
RuntimeIterator iter = (RuntimeIterator) currrentRuntimeIters.get(i);
values[i] = iter.evaluate(context);
// case of all Pdx objects in cache
if (this.distinct && !((DefaultQuery) context.getQuery()).isRemoteQuery() && !context.getCache().getPdxReadSerialized() && (values[i] instanceof PdxInstance)) {
values[i] = ((PdxInstance) values[i]).getObject();
}
}
// Don't care about Order By for count(*).
if (isCount() && !this.distinct) {
// Counter is local to CompileSelect and not available in ResultSet
// until
// the end of evaluate call to this CompiledSelect object.
this.countStartQueryResult++;
occurence = 1;
} else {
// if order by is present
if (applyOrderBy) {
StructImpl structImpl;
if (this.distinct) {
if (isStruct) {
if (values.length == 1 && values[0] instanceof StructImpl) {
structImpl = (StructImpl) values[0];
comparator.addEvaluatedSortCriteria(structImpl.getFieldValues(), context);
occurence = resultSet.add(structImpl) ? 1 : 0;
} else {
comparator.addEvaluatedSortCriteria(values, context);
occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
}
// TODO:Instead of a normal Map containing which holds
// StructImpl object
// use a THashObject with Object[] array hashing stragtegy as we
// are unnnecessarily
// creating objects of type Object[]
} else {
comparator.addEvaluatedSortCriteria(values[0], context);
occurence = resultSet.add(values[0]) ? 1 : 0;
}
} else {
if (isStruct) {
if (values.length == 1 && values[0] instanceof StructImpl) {
structImpl = (StructImpl) values[0];
comparator.addEvaluatedSortCriteria(structImpl.getFieldValues(), context);
occurence = ((Bag) resultSet).addAndGetOccurence(structImpl.getFieldValues());
} else {
comparator.addEvaluatedSortCriteria(values, context);
occurence = ((Bag) resultSet).addAndGetOccurence(values);
}
} else {
comparator.addEvaluatedSortCriteria(values[0], context);
occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
}
}
} else {
if (isLinkedStructure) {
if (isStruct) {
StructImpl structImpl;
if (values.length == 1 && values[0] instanceof StructImpl) {
structImpl = (StructImpl) values[0];
} else {
structImpl = new StructImpl((StructTypeImpl) elementType, values);
}
if (this.distinct) {
occurence = resultSet.add(structImpl) ? 1 : 0;
} else {
occurence = ((Bag) resultSet).addAndGetOccurence(structImpl);
}
} else {
if (this.distinct) {
occurence = resultSet.add(values[0]) ? 1 : 0;
} else {
occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
}
}
} else {
if (this.distinct) {
if (isStruct) {
occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
} else {
occurence = resultSet.add(values[0]) ? 1 : 0;
}
} else {
if (isStruct) {
occurence = ((Bag) resultSet).addAndGetOccurence(values);
} else {
boolean add = true;
if (context.isCqQueryContext()) {
if (values[0] instanceof Region.Entry) {
Region.Entry e = (Region.Entry) values[0];
if (!e.isDestroyed()) {
try {
values[0] = new CqEntry(e.getKey(), e.getValue());
} catch (EntryDestroyedException ignore) {
// Even though isDestory() check is made, the entry could throw
// EntryDestroyedException if the value becomes null.
add = false;
}
} else {
add = false;
}
}
}
if (add) {
occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
}
}
}
}
}
}
} else {
// One or more projection attributes
int projCount = projAttrs.size();
Object[] values = new Object[projCount];
for (int i = 0; i < projCount; i++) {
Object[] projDef = (Object[]) projAttrs.get(i);
values[i] = ((CompiledValue) projDef[1]).evaluate(context);
// for remote queries
if (!((DefaultQuery) context.getQuery()).isRemoteQuery()) {
if (this.distinct && values[i] instanceof PdxInstance && !context.getCache().getPdxReadSerialized()) {
values[i] = ((PdxInstance) values[i]).getObject();
} else if (values[i] instanceof PdxString) {
values[i] = ((PdxString) values[i]).toString();
}
}
}
// if order by is present
if (applyOrderBy) {
if (distinct) {
if (isStruct) {
comparator.addEvaluatedSortCriteria(values, context);
// Occurence field is used to identify the corrcet number of
// iterations
// required to implement the limit based on the presence or absence
// of distinct clause
occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
} else {
comparator.addEvaluatedSortCriteria(values[0], context);
occurence = resultSet.add(values[0]) ? 1 : 0;
}
} else {
if (isStruct) {
comparator.addEvaluatedSortCriteria(values, context);
occurence = ((Bag) resultSet).addAndGetOccurence(values);
} else {
comparator.addEvaluatedSortCriteria(values[0], context);
occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
}
}
} else {
if (isLinkedStructure) {
if (isStruct) {
StructImpl structImpl = new StructImpl((StructTypeImpl) elementType, values);
if (this.distinct) {
occurence = resultSet.add(structImpl) ? 1 : 0;
} else {
occurence = ((Bag) resultSet).addAndGetOccurence(structImpl);
}
} else {
if (this.distinct) {
occurence = resultSet.add(values[0]) ? 1 : 0;
} else {
occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
}
}
} else {
if (this.distinct) {
if (isStruct) {
occurence = ((StructFields) resultSet).addFieldValues(values) ? 1 : 0;
} else {
occurence = resultSet.add(values[0]) ? 1 : 0;
}
} else {
if (isStruct) {
occurence = ((Bag) resultSet).addAndGetOccurence(values);
} else {
occurence = ((Bag) resultSet).addAndGetOccurence(values[0]);
}
}
}
}
}
return occurence;
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CompiledIteratorDef method getRuntimeIterator.
public RuntimeIterator getRuntimeIterator(ExecutionContext context) throws TypeMismatchException, AmbiguousNameException, NameResolutionException {
RuntimeIterator rIter = null;
// check cached in context
rIter = (RuntimeIterator) context.cacheGet(this);
if (rIter != null) {
return rIter;
}
ObjectType type = this.elementType;
if (type.equals(TypeUtils.OBJECT_TYPE)) {
// check to see if there's a typecast for this collection
ObjectType typc = getCollectionElementTypeCast();
if (typc != null) {
type = typc;
} else {
// Does not determine type of nested query
if (!(this.collectionExpr instanceof CompiledSelect)) {
type = computeElementType(context);
}
}
}
rIter = new RuntimeIterator(this, type);
// generate from clause should take care of bucket region substitution if
// necessary and then set the definition.
String fromClause = genFromClause(context);
rIter.setDefinition(fromClause);
/*
* If the type of RunTimeIterator is still ObjectType & if the RuneTimeIterator is independent
* of any iterator of the scopes less than or equal to its own scope, we can evaluate the
* collection via RuntimeIterator. This will initialize the Collection of RuntimeIterator ,
* which is OK. The code in RuntimeIterator will be rectified such that the ElementType of that
* RuntimeIterator is taken from the collection
*/
if (type.equals(TypeUtils.OBJECT_TYPE) && !this.isDependentOnAnyIteratorOfScopeLessThanItsOwn(context)) {
// the collection
try {
rIter.evaluateCollection(context);
} catch (QueryExecutionTimeoutException qet) {
throw qet;
} catch (RegionNotFoundException re) {
throw re;
} catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Exception while getting runtime iterator.", e);
}
throw new TypeMismatchException(LocalizedStrings.CompiledIteratorDef_EXCEPTION_IN_EVALUATING_THE_COLLECTION_EXPRESSION_IN_GETRUNTIMEITERATOR_EVEN_THOUGH_THE_COLLECTION_IS_INDEPENDENT_OF_ANY_RUNTIMEITERATOR.toLocalizedString(), e);
}
}
// cache in context
context.cachePut(this, rIter);
return rIter;
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CompiledIteratorDef method evaluateCollection.
/**
* Evaluate just the collectionExpr
*
* @param stopAtIter the RuntimeIterator associated with this iterator defn -- don't use this or
* any subsequent runtime iterators to evaluate.
*/
SelectResults evaluateCollection(ExecutionContext context, RuntimeIterator stopAtIter) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
Object coll;
// Check if query execution on this thread is Canceled.
// QueryMonitor.isQueryExecutionCanceled();
context.currentScope().setLimit(stopAtIter);
try {
coll = this.collectionExpr.evaluate(context);
} finally {
context.currentScope().setLimit(null);
}
// element type here
if (TypeUtils.OBJECT_TYPE.equals(this.elementType)) {
ObjectType elmTypc = getCollectionElementTypeCast();
if (elmTypc != null) {
this.elementType = elmTypc;
}
}
return prepareIteratorDef(coll, this.elementType, context);
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class CompiledJunction method auxIterateEvaluate.
/** invariant: the operand is known to be evaluated by iteration */
SelectResults auxIterateEvaluate(CompiledValue operand, ExecutionContext context, SelectResults intermediateResults) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
// signifies that the junction cannot be evaluated as a filter.
if (intermediateResults == null)
throw new RuntimeException(LocalizedStrings.CompiledJunction_INTERMEDIATERESULTS_CAN_NOT_BE_NULL.toLocalizedString());
if (// short circuit
intermediateResults.isEmpty())
return intermediateResults;
List currentIters = context.getCurrentIterators();
RuntimeIterator[] rIters = new RuntimeIterator[currentIters.size()];
currentIters.toArray(rIters);
ObjectType elementType = intermediateResults.getCollectionType().getElementType();
SelectResults resultSet;
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(LocalizedStrings.CompiledJunction_ANDOR_OPERANDS_MUST_BE_OF_TYPE_BOOLEAN_NOT_TYPE_0.toLocalizedString(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 CompiledSelect method prepareResultType.
protected ObjectType prepareResultType(ExecutionContext context) throws TypeMismatchException, AmbiguousNameException {
// if no projection attributes or '*'as projection attribute
// & more than one/RunTimeIterator then create a StrcutSet.
// If attribute is null or '*' & only one RuntimeIterator then create a
// ResultSet.
// If single attribute is present without alias name , then create
// ResultSet
// Else if more than on attribute or single attribute with alias is
// present then return a StrcutSet
// create StructSet which will contain root objects of all iterators in
// from clause
ObjectType elementType = null;
SelectResults sr = null;
List currentIterators = context.getCurrentIterators();
if (projAttrs == null) {
if (currentIterators.size() == 1) {
RuntimeIterator iter = (RuntimeIterator) currentIterators.get(0);
elementType = iter.getElementType();
} else {
elementType = createStructTypeForNullProjection(currentIterators, context);
}
} else {
// Create StructType for projection attributes
int projCount = projAttrs.size();
String[] fieldNames = new String[projCount];
ObjectType[] fieldTypes = new ObjectType[projCount];
boolean createStructSet = false;
String fldName = null;
for (int i = 0; i < projCount; i++) {
Object[] projDef = (Object[]) projAttrs.get(i);
fldName = (String) projDef[0];
if (!createStructSet) {
if (fldName != null || projCount > 1) {
createStructSet = true;
}
}
fieldNames[i] = (fldName == null && createStructSet) ? generateProjectionName((CompiledValue) projDef[1], context) : fldName;
fieldTypes[i] = getFieldTypeOfProjAttrib(context, (CompiledValue) projDef[1]);
// fieldTypes[i] = TypeUtils.OBJECT_TYPE;
}
if (createStructSet) {
elementType = new StructTypeImpl(fieldNames, fieldTypes);
} else {
elementType = fieldTypes[0];
}
}
return elementType;
}
Aggregations