use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncAvg.
@Override
@Test
public void testAggregateFuncAvg() throws Exception {
Region region = this.createRegion("portfolio", PortfolioPdx.class);
for (int i = 1; i < 200; ++i) {
PortfolioPdx pf = new PortfolioPdx(i);
pf.shortID = (short) ((short) i / 5);
region.put("key-" + i, pf);
}
String queryStr = "select p.status as status, Avg(p.ID) as average from " + "/portfolio p where p.ID > 0 group by status";
QueryService qs = CacheUtils.getQueryService();
Query query = qs.newQuery(queryStr);
CompiledSelect cs = ((DefaultQuery) query).getSelect();
SelectResults sr = (SelectResults) query.execute();
assertTrue(sr.getCollectionType().getElementType().isStructType());
assertEquals(2, sr.size());
Iterator iter = sr.iterator();
Region rgn = CacheUtils.getRegion("portfolio");
double sumIDActive = 0, sumIDInactive = 0;
int numActive = 0, numInactive = 0;
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.getID() > 0) {
if (pf.status.equals("active")) {
sumIDActive += pf.getID();
++numActive;
} else if (pf.status.equals("inactive")) {
sumIDInactive += pf.getID();
++numInactive;
}
}
}
Number avgActive = AbstractAggregator.downCast(sumIDActive / numActive);
Number avgInactive = AbstractAggregator.downCast(sumIDInactive / numInactive);
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
StructType structType = struct.getStructType();
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Number", fieldTypes[1].getSimpleClassName());
if (struct.get("status").equals("active")) {
assertEquals(avgActive, struct.get("average"));
} else if (struct.get("status").equals("inactive")) {
assertEquals(avgInactive, struct.get("average"));
} else {
fail("unexpected value of status");
}
}
ObjectType elementType = sr.getCollectionType().getElementType();
assertTrue(elementType.isStructType());
StructType structType = (StructType) elementType;
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Number", fieldTypes[1].getSimpleClassName());
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class PdxGroupByTestImpl method testComplexValueAggregateFuncAvgDistinct.
@Override
@Test
public void testComplexValueAggregateFuncAvgDistinct() throws Exception {
Region region = this.createRegion("portfolio", PortfolioPdx.class);
for (int i = 1; i < 200; ++i) {
PortfolioPdx pf = new PortfolioPdx(i);
pf.shortID = (short) ((short) i / 5);
region.put("key-" + i, pf);
}
String queryStr = "select p.status as status, avg(distinct element(select iter.shortID from /portfolio iter where iter.ID = p.ID) ) as average from " + "/portfolio p where p.ID > 0 group by status";
QueryService qs = CacheUtils.getQueryService();
Query query = qs.newQuery(queryStr);
CompiledSelect cs = ((DefaultQuery) query).getSelect();
SelectResults sr = (SelectResults) query.execute();
assertTrue(sr.getCollectionType().getElementType().isStructType());
assertEquals(2, sr.size());
Iterator iter = sr.iterator();
Region rgn = CacheUtils.getRegion("portfolio");
Set<Short> sumIDActiveSet = new HashSet<Short>(), sumIDInactiveSet = new HashSet<Short>();
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.getID() > 0) {
if (pf.status.equals("active")) {
sumIDActiveSet.add(pf.shortID);
} else if (pf.status.equals("inactive")) {
sumIDInactiveSet.add(pf.shortID);
}
}
}
double sumActive = 0, sumInactive = 0;
for (Short shortt : sumIDActiveSet) {
sumActive += shortt.doubleValue();
}
for (Short shortt : sumIDInactiveSet) {
sumInactive += shortt.doubleValue();
}
Number avgActive = AbstractAggregator.downCast(sumActive / sumIDActiveSet.size());
Number avgInactive = AbstractAggregator.downCast(sumInactive / sumIDInactiveSet.size());
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
StructType structType = struct.getStructType();
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Number", fieldTypes[1].getSimpleClassName());
if (struct.get("status").equals("active")) {
assertEquals(avgActive, struct.get("average"));
} else if (struct.get("status").equals("inactive")) {
assertEquals(avgInactive, struct.get("average"));
} else {
fail("unexpected value of status");
}
}
ObjectType elementType = sr.getCollectionType().getElementType();
assertTrue(elementType.isStructType());
StructType structType = (StructType) elementType;
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Number", fieldTypes[1].getSimpleClassName());
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class QueryUtils method createStructTypeForRuntimeIterators.
/**
* This function creates a StructType using Internal IDs of the iterators as the field names for
* the StructType. It should be invoked iff the iterators size is greater than 1
*
* @param runTimeIterators List of RuntimeIterator objects
* @return StructType object
*
*/
static StructType createStructTypeForRuntimeIterators(List runTimeIterators) {
int len = runTimeIterators.size();
String[] fieldNames = new String[len];
String[] indexAlternativeFieldNames = new String[len];
ObjectType[] fieldTypes = new ObjectType[len];
// use an Iterator as the chances are that we will be sending
// LinkedList rather than ArrayList
Iterator itr = runTimeIterators.iterator();
int i = 0;
while (itr.hasNext()) {
RuntimeIterator iter = (RuntimeIterator) itr.next();
fieldNames[i] = iter.getInternalId();
indexAlternativeFieldNames[i] = iter.getIndexInternalID();
fieldTypes[i++] = iter.getElementType();
}
return new StructTypeImpl(fieldNames, indexAlternativeFieldNames, fieldTypes);
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class DerivedInfo method populateDerivedResultsFromDerivedJoin.
/*
* Does the evaluation/execution of the cc and stores them into our map We prevent limit and order
* by to be conducted by the index at this time as we do not those applied We have no idea what
* the other operands are and do not want to limit results as the first X results may not fulfill
* all operands.
*/
private void populateDerivedResultsFromDerivedJoin(ExecutionContext context, CompiledComparison dcc, IndexInfo indexInfo) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
// overwrite context values to disable limit, order by etc that should not be done by a derived
// join
// If we apply limit at this point, we cannot guarantee that after we iterate, the we do not
// continue to
// reduce the count below the limited amount
Boolean originalCanApplyLimit = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX);
context.cachePut(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX, Boolean.FALSE);
Boolean originalCanApplyOrderBy = (Boolean) context.cacheGet(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX);
context.cachePut(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX, Boolean.FALSE);
SelectResults sr = dcc.filterEvaluate(context, null, false, null, null, false, false, false);
context.cachePut(CompiledValue.CAN_APPLY_LIMIT_AT_INDEX, originalCanApplyLimit);
context.cachePut(CompiledValue.CAN_APPLY_ORDER_BY_AT_INDEX, originalCanApplyOrderBy);
ObjectType ot = indexInfo._index.getResultSetType();
// duplicate results or incorrect tupling
if (ot.isStructType()) {
// createObjectResultsFromStructResults(indexInfo, sr);
} else if (ot.isMapType()) {
} else if (ot.isCollectionType()) {
} else {
this.addDerivedResults(dcc.getIndexInfo(context)[0], sr);
}
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class NWayMergeResults method fromData.
@Override
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
ObjectType elementType = (ObjectType) DataSerializer.readObject(in);
this.collectionType = new CollectionTypeImpl(NWayMergeResults.class, elementType);
boolean isStruct = elementType.isStructType();
this.isDistinct = DataSerializer.readPrimitiveBoolean(in);
long size = in.readLong();
this.data = new ArrayList<E>((int) size);
long numLeft = size;
while (numLeft > 0) {
if (isStruct) {
Object[] fields = DataSerializer.readObjectArray(in);
this.data.add((E) new StructImpl((StructTypeImpl) elementType, fields));
} else {
E element = DataSerializer.readObject(in);
this.data.add(element);
}
--numLeft;
}
}
Aggregations