use of org.apache.geode.cache.query.types.StructType 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.StructType 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.StructType in project geode by apache.
the class LimitClauseJUnitTest method testLimitDistinctIterEvaluatedQueryForStructBagWithProjectionAttribute.
/**
* Tests the limit functionality for Iter evaluated query with distinct clause This tests the
* basic limit functionality for StructBag wrapped by a SelectResults . This test contains
* projection attributes. Since the attribute is unique every time, the limit will be satisfied
* with first 5 iterations
*
* Tests StructBag behaviour
*
*/
@Test
public void testLimitDistinctIterEvaluatedQueryForStructBagWithProjectionAttribute() {
try {
Query query;
SelectResults result;
query = qs.newQuery("SELECT DISTINCT pf.ID, pf.createTime FROM /portfolios pf WHERE pf.ID > 0 limit 5");
final int[] num = new int[1];
num[0] = 0;
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
public void afterIterationEvaluation(Object result) {
num[0] += 1;
}
});
result = (SelectResults) query.execute();
assertEquals(5, num[0]);
assertTrue(result instanceof SelectResults);
assertEquals(5, result.size());
SelectResults wrapper = (SelectResults) result;
assertEquals(5, wrapper.asSet().size());
assertTrue(wrapper.getCollectionType().getElementType() instanceof StructType);
} catch (Exception e) {
CacheUtils.getLogger().error(e);
fail(e.toString());
} finally {
QueryObserverHolder.setInstance(new QueryObserverAdapter());
}
}
use of org.apache.geode.cache.query.types.StructType in project geode by apache.
the class ProjectionAttributeJUnitTest method checkNames.
private void checkNames(SelectResults results, String query) {
int i1 = query.indexOf(" distinct ");
int i2 = query.indexOf(" from ");
if (i1 < 0 || i2 < 0)
fail(query);
String projStr = query.substring(i1 + " distinct ".length(), i2);
// CacheUtils.log("projStr = "+projStr);
QCompiler compiler = new QCompiler();
List projAttrs = compiler.compileProjectionAttributes(projStr);
StructType stype = (StructType) results.getCollectionType().getElementType();
String[] names = stype.getFieldNames();
for (int i = 0; i < names.length; i++) {
String name = names[i];
Object[] arr = (Object[]) projAttrs.get(i);
String nameToMatch = "";
if (arr[0] != null)
nameToMatch = (String) arr[0];
else {
if (arr[1] instanceof CompiledID)
nameToMatch = ((CompiledID) arr[1]).getId();
if (arr[1] instanceof CompiledPath)
nameToMatch = ((CompiledPath) arr[1]).getTailID();
if (arr[1] instanceof CompiledLiteral)
nameToMatch = (((CompiledLiteral) arr[1])._obj).toString();
}
if (!nameToMatch.equals(name))
fail(query);
}
}
use of org.apache.geode.cache.query.types.StructType 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;
}
}
Aggregations