use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class GroupByTestImpl method testAggregateFuncSum.
@Test
public void testAggregateFuncSum() throws Exception {
Region region = this.createRegion("portfolio", Portfolio.class);
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i / 5);
region.put("" + i, pf);
}
String queryStr = "select p.status as status, Sum(p.ID) 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");
int activeSum = 0;
int inactiveSum = 0;
for (Object o : rgn.values()) {
Portfolio pf = (Portfolio) o;
if (pf.status.equals("active")) {
activeSum += pf.ID;
} else if (pf.status.equals("inactive")) {
inactiveSum += pf.ID;
} else {
fail("unexpected value of status");
}
}
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(activeSum, ((Integer) struct.get("summ")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(inactiveSum, ((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());
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class GroupByTestImpl method testAggregateFuncCountStar.
@Test
public void testAggregateFuncCountStar() throws Exception {
Region region = this.createRegion("portfolio", Portfolio.class);
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i % 5);
region.put("" + i, pf);
}
String queryStr = "select p.status as status, Count(*) 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");
int activeCount = 0;
int inactiveCount = 0;
for (Object o : rgn.values()) {
Portfolio pf = (Portfolio) o;
if (pf.status.equals("active")) {
++activeCount;
} else if (pf.status.equals("inactive")) {
++inactiveCount;
} 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(activeCount, ((Integer) struct.get("countt")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(inactiveCount, ((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.internal.DefaultQuery in project geode by apache.
the class GroupByTestImpl method testSumWithMultiColumnGroupBy.
@Test
public void testSumWithMultiColumnGroupBy() throws Exception {
Region region = this.createRegion("portfolio", Portfolio.class);
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i % 5);
region.put("" + i, pf);
}
Map<String, Integer> expectedData = new HashMap<String, Integer>();
for (Object o : region.values()) {
Portfolio pf = (Portfolio) o;
String key = pf.status + "_" + pf.shortID;
if (expectedData.containsKey(key)) {
int sum = expectedData.get(key).intValue() + pf.ID;
expectedData.put(key, sum);
} else {
expectedData.put(key, pf.ID);
}
}
String queryStr = "select p.status as status, p.shortID as shortID, sum(p.ID) as summ from /portfolio p" + " where p.ID > 0 group by status, shortID ";
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(expectedData.size(), sr.size());
Iterator iter = sr.iterator();
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
StructType structType = struct.getStructType();
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Short", fieldTypes[1].getSimpleClassName());
assertEquals("Number", fieldTypes[2].getSimpleClassName());
String key = struct.get("status") + "_" + struct.get("shortID");
int sum = ((Integer) struct.get("summ")).intValue();
assertTrue(expectedData.containsKey(key));
assertEquals(expectedData.get(key).intValue(), sum);
}
ObjectType elementType = sr.getCollectionType().getElementType();
assertTrue(elementType.isStructType());
StructType structType = (StructType) elementType;
ObjectType[] fieldTypes = structType.getFieldTypes();
assertEquals("String", fieldTypes[0].getSimpleClassName());
assertEquals("Short", fieldTypes[1].getSimpleClassName());
assertEquals("Number", fieldTypes[2].getSimpleClassName());
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class GroupByTestImpl method testAggregateFuncMin.
@Test
public void testAggregateFuncMin() throws Exception {
Region region = this.createRegion("portfolio", Portfolio.class);
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i / 5);
region.put("" + i, pf);
}
String queryStr = "select p.status as status, Min(p.ID) as Minn 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");
int activeMinID = Integer.MAX_VALUE;
int inactiveMinID = Integer.MAX_VALUE;
for (Object o : rgn.values()) {
Portfolio pf = (Portfolio) o;
if (pf.status.equals("active")) {
if (pf.ID < activeMinID) {
activeMinID = pf.ID;
}
} else if (pf.status.equals("inactive")) {
if (pf.ID < inactiveMinID) {
inactiveMinID = pf.ID;
}
} else {
fail("unexpected value of status");
}
}
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(activeMinID, ((Integer) struct.get("Minn")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(inactiveMinID, ((Integer) struct.get("Minn")).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());
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class RegionValue method testLocalDataSetIndexing.
@Test
public void testLocalDataSetIndexing() {
final CacheSerializableRunnable createPRs = new CacheSerializableRunnable("create prs ") {
public void run2() {
AttributesFactory<Integer, RegionValue> factory = new AttributesFactory<Integer, RegionValue>();
factory.setPartitionAttributes(new PartitionAttributesFactory<Integer, RegionValue>().setRedundantCopies(1).setTotalNumBuckets(8).create());
final PartitionedRegion pr1 = (PartitionedRegion) createRootRegion("pr1", factory.create());
factory = new AttributesFactory<Integer, RegionValue>();
factory.setPartitionAttributes(new PartitionAttributesFactory<Integer, RegionValue>().setRedundantCopies(1).setTotalNumBuckets(8).setColocatedWith(pr1.getName()).create());
final PartitionedRegion pr2 = (PartitionedRegion) createRootRegion("pr2", factory.create());
}
};
final CacheSerializableRunnable createIndexesOnPRs = new CacheSerializableRunnable("create prs ") {
public void run2() {
try {
QueryService qs = getCache().getQueryService();
qs.createIndex("valueIndex1", IndexType.FUNCTIONAL, "e1.value", "/pr1 e1");
qs.createIndex("valueIndex2", IndexType.FUNCTIONAL, "e2.value", "/pr2 e2");
} catch (Exception e) {
org.apache.geode.test.dunit.Assert.fail("Test failed due to Exception in index creation ", e);
}
}
};
final CacheSerializableRunnable execute = new CacheSerializableRunnable("execute function") {
public void run2() {
final PartitionedRegion pr1 = (PartitionedRegion) getRootRegion("pr1");
final PartitionedRegion pr2 = (PartitionedRegion) getRootRegion("pr2");
final Set<Integer> filter = new HashSet<Integer>();
for (int i = 1; i <= 80; i++) {
pr1.put(i, new RegionValue(i));
if (i <= 20) {
pr2.put(i, new RegionValue(i));
if ((i % 5) == 0) {
filter.add(i);
}
}
}
ArrayList<List> result = (ArrayList<List>) FunctionService.onRegion(pr1).withFilter(filter).execute(new FunctionAdapter() {
public void execute(FunctionContext context) {
try {
RegionFunctionContext rContext = (RegionFunctionContext) context;
Region pr1 = rContext.getDataSet();
LocalDataSet localCust = (LocalDataSet) PartitionRegionHelper.getLocalDataForContext(rContext);
Map<String, Region<?, ?>> colocatedRegions = PartitionRegionHelper.getColocatedRegions(pr1);
Map<String, Region<?, ?>> localColocatedRegions = PartitionRegionHelper.getLocalColocatedRegions(rContext);
Region pr2 = colocatedRegions.get("/pr2");
LocalDataSet localOrd = (LocalDataSet) localColocatedRegions.get("/pr2");
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
QueryService qs = pr1.getCache().getQueryService();
DefaultQuery query = (DefaultQuery) qs.newQuery("select distinct e1.value from /pr1 e1, /pr2 e2 where e1.value=e2.value");
GemFireCacheImpl.getInstance().getLogger().fine(" Num BUCKET SET: " + localCust.getBucketSet());
GemFireCacheImpl.getInstance().getLogger().fine("VALUES FROM PR1 bucket:");
for (Integer bId : localCust.getBucketSet()) {
BucketRegion br = ((PartitionedRegion) pr1).getDataStore().getLocalBucketById(bId);
String val = "";
for (Object e : br.values()) {
val += (e + ",");
}
GemFireCacheImpl.getInstance().getLogger().fine(": " + val);
}
GemFireCacheImpl.getInstance().getLogger().fine("VALUES FROM PR2 bucket:");
for (Integer bId : localCust.getBucketSet()) {
BucketRegion br = ((PartitionedRegion) pr2).getDataStore().getLocalBucketById(bId);
String val = "";
for (Object e : br.values()) {
val += (e + ",");
}
GemFireCacheImpl.getInstance().getLogger().fine(": " + val);
}
SelectResults r = (SelectResults) localCust.executeQuery(query, null, localCust.getBucketSet());
GemFireCacheImpl.getInstance().getLogger().fine("Result :" + r.asList());
Assert.assertTrue(observer.isIndexesUsed);
pr1.getCache().getLogger().fine("Index Used: " + observer.numIndexesUsed());
Assert.assertTrue(2 == observer.numIndexesUsed());
context.getResultSender().lastResult((Serializable) r.asList());
} catch (Exception e) {
context.getResultSender().lastResult(Boolean.TRUE);
}
}
@Override
public String getId() {
return "ok";
}
@Override
public boolean optimizeForWrite() {
return false;
}
}).getResult();
int numResults = 0;
for (List oneNodeResult : result) {
GemFireCacheImpl.getInstance().getLogger().fine("Result :" + numResults + " oneNodeResult.size(): " + oneNodeResult.size() + " oneNodeResult :" + oneNodeResult);
numResults = +oneNodeResult.size();
}
Assert.assertTrue(10 == numResults);
}
};
dataStore1.invoke(createPRs);
dataStore2.invoke(createPRs);
dataStore1.invoke(createIndexesOnPRs);
dataStore1.invoke(execute);
}
Aggregations