use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class PartitionedRegionQueryEvaluator method sortIncomingData.
// TODO Asif: optimize it by creating a Sorted SelectResults Object at the time of fromData , so
// that processData already recieves ordered data.
private List sortIncomingData(List objects, List<CompiledSortCriterion> orderByAttribs) {
ObjectType resultType = cumulativeResults.getCollectionType().getElementType();
ExecutionContext local = new ExecutionContext(null, this.pr.cache);
Comparator comparator = new OrderByComparator(orderByAttribs, resultType, local);
boolean nullAtStart = !orderByAttribs.get(0).getCriterion();
final SelectResults newResults;
// of adding the struct objects as is, add fieldValues.
if (resultType != null && resultType.isStructType()) {
SortedStructBag sortedStructBag = new SortedStructBag(comparator, (StructType) resultType, nullAtStart);
for (Object o : objects) {
Struct s = (Struct) o;
sortedStructBag.addFieldValues(s.getFieldValues());
}
newResults = sortedStructBag;
} else {
newResults = new SortedResultsBag(comparator, resultType, nullAtStart);
newResults.addAll(objects);
}
objects = newResults.asList();
return objects;
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class StructSetOrResultsSet method getOrderByComparatorAndLimitForQuery.
public Wrapper getOrderByComparatorAndLimitForQuery(String orderByQuery, int unorderedResultSize) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
DefaultQuery q = (DefaultQuery) CacheUtils.getQueryService().newQuery(orderByQuery);
CompiledSelect cs = q.getSimpleSelect();
List<CompiledSortCriterion> orderByAttribs = null;
if (cs.getType() == CompiledValue.GROUP_BY_SELECT) {
Field originalOrderByMethod = CompiledGroupBySelect.class.getDeclaredField("originalOrderByClause");
originalOrderByMethod.setAccessible(true);
orderByAttribs = (List<CompiledSortCriterion>) originalOrderByMethod.get(cs);
} else {
orderByAttribs = cs.getOrderByAttrs();
}
ObjectType resultType = cs.getElementTypeForOrderByQueries();
ExecutionContext context = new ExecutionContext(null, CacheUtils.getCache());
final OrderByComparator obc = new OrderByComparator(orderByAttribs, resultType, context);
Comparator baseComparator = obc;
if (resultType.isStructType()) {
baseComparator = new Comparator<Struct>() {
@Override
public int compare(Struct o1, Struct o2) {
return obc.compare(o1.getFieldValues(), o2.getFieldValues());
}
};
}
final Comparator secondLevelComparator = baseComparator;
final Comparator finalComparator = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
final boolean[] orderByColsEqual = new boolean[] { false };
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
@Override
public void orderByColumnsEqual() {
orderByColsEqual[0] = true;
}
});
int result = secondLevelComparator.compare(o1, o2);
if (result != 0 && orderByColsEqual[0]) {
result = 0;
}
return result;
}
};
Field hasUnmappedOrderByColsField = CompiledSelect.class.getDeclaredField("hasUnmappedOrderByCols");
hasUnmappedOrderByColsField.setAccessible(true);
boolean skip = ((Boolean) hasUnmappedOrderByColsField.get(cs)).booleanValue();
ValidationLevel validationLevel = ValidationLevel.ALL;
int limit;
if (cs.getType() == CompiledValue.GROUP_BY_SELECT) {
Field limitCVField = CompiledGroupBySelect.class.getDeclaredField("limit");
limitCVField.setAccessible(true);
CompiledValue limitCV = (CompiledValue) limitCVField.get(cs);
Method evaluateLimitMethod = CompiledSelect.class.getDeclaredMethod("evaluateLimitValue", ExecutionContext.class, CompiledValue.class);
evaluateLimitMethod.setAccessible(true);
limit = ((Integer) evaluateLimitMethod.invoke(null, context, limitCV)).intValue();
} else {
limit = cs.getLimitValue(null);
}
if (limit != -1 && limit < unorderedResultSize) {
// chances are that results will not match
if (skip) {
validationLevel = ValidationLevel.NONE;
} else {
validationLevel = ValidationLevel.ORDER_BY_ONLY;
}
} else {
if (skip) {
validationLevel = ValidationLevel.MATCH_ONLY;
}
}
return new Wrapper(finalComparator, limit, validationLevel);
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncCountDistinctStar_1.
@Override
@Test
public void testAggregateFuncCountDistinctStar_1() 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, count(distinct p.shortID) as countt 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<Object> distinctShortIDActive = new HashSet<Object>();
Set<Object> distinctShortIDInactive = new HashSet<Object>();
int inactiveCount = 0;
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.status.equals("active")) {
distinctShortIDActive.add(pf.shortID);
} else if (pf.status.equals("inactive")) {
distinctShortIDInactive.add(pf.shortID);
} else {
fail("unexpected status");
}
}
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
StructType structType = struct.getStructType();
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Integer", fieldTypes[1].getSimpleClassName());
if (struct.get("status").equals("active")) {
assertEquals(distinctShortIDActive.size(), ((Integer) struct.get("countt")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(distinctShortIDInactive.size(), ((Integer) struct.get("countt")).intValue());
} 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("Integer", fieldTypes[1].getSimpleClassName());
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncCountDistinctStar_2.
@Override
@Test
public void testAggregateFuncCountDistinctStar_2() 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, COUNT(distinct p.shortID) as countt 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<Object> distinctShortIDActive = new HashSet<Object>();
Set<Object> distinctShortIDInactive = new HashSet<Object>();
int inactiveCount = 0;
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.status.equals("active")) {
distinctShortIDActive.add(pf.shortID);
} else if (pf.status.equals("inactive")) {
distinctShortIDInactive.add(pf.shortID);
} else {
fail("unexpected status");
}
}
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
StructType structType = struct.getStructType();
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Integer", fieldTypes[1].getSimpleClassName());
if (struct.get("status").equals("active")) {
assertEquals(distinctShortIDActive.size(), ((Integer) struct.get("countt")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(distinctShortIDInactive.size(), ((Integer) struct.get("countt")).intValue());
} 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("Integer", fieldTypes[1].getSimpleClassName());
}
use of org.apache.geode.cache.query.types.ObjectType in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncSumDistinct.
@Override
@Test
public void testAggregateFuncSumDistinct() 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, SUM (distinct p.shortID) as summ 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> activeSum = new HashSet<Short>();
Set<Short> inactiveSum = new HashSet<Short>();
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.status.equals("active")) {
activeSum.add(pf.shortID);
} else if (pf.status.equals("inactive")) {
inactiveSum.add(pf.shortID);
} else {
fail("unexpected value of status");
}
}
int activeSumm = 0, inactiveSumm = 0;
for (Short val : activeSum) {
activeSumm += val.intValue();
}
for (Short val : inactiveSum) {
inactiveSumm += val.intValue();
}
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(activeSumm, ((Integer) struct.get("summ")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(inactiveSumm, ((Integer) struct.get("summ")).intValue());
} 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());
}
Aggregations