use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class QueryUsingFunctionContextDUnitTest method runQueryOnServerLDS.
protected ArrayList runQueryOnServerLDS(String queryStr, Set filter) {
Set buckets = getBucketsForFilter(filter);
Region localDataSet = new LocalDataSet((PartitionedRegion) CacheFactory.getAnyInstance().getRegion(PartitionedRegionName1), buckets);
QueryService qservice = CacheFactory.getAnyInstance().getQueryService();
Query query = qservice.newQuery(queryStr);
SelectResults results;
try {
results = (SelectResults) ((LocalDataSet) localDataSet).executeQuery((DefaultQuery) query, null, buckets);
return (ArrayList) results.asList();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class OrderByTestImplementation method testOrderByWithColumnAlias_Bug52041_2.
@Test
public void testOrderByWithColumnAlias_Bug52041_2() throws Exception {
Region region = createRegion("portfolio", Portfolio.class);
for (int i = 0; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i / 5);
pf.status = "active";
region.put("" + i, pf);
}
String queryStr = "select distinct p.ID as _id, p.shortID as short_id from /portfolio p where p.ID >= 0 " + "order by short_id asc, p.ID desc";
QueryService qs = CacheUtils.getQueryService();
Query query = qs.newQuery(queryStr);
SelectResults<Struct> results = (SelectResults<Struct>) query.execute();
Iterator<Struct> iter = results.asList().iterator();
int counter = 0;
int k = 0;
while (iter.hasNext()) {
k = ((counter) / 5 + 1) * 5 - 1;
Struct str = iter.next();
assertEquals(counter / 5, ((Short) str.get("short_id")).intValue());
assertEquals(k - (counter) % 5, ((Integer) str.get("_id")).intValue());
++counter;
}
CompiledSelect cs = ((DefaultQuery) query).getSimpleSelect();
List<CompiledSortCriterion> orderbyAtts = cs.getOrderByAttrs();
assertEquals(orderbyAtts.get(0).getColumnIndex(), 1);
assertEquals(orderbyAtts.get(1).getColumnIndex(), 0);
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class PdxGroupByTestImpl method testAggregateFuncMax.
@Override
@Test
public void testAggregateFuncMax() 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, Max(p.ID) as Maxx 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 activeMaxID = 0;
int inactiveMaxID = 0;
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
if (pf.status.equals("active")) {
if (pf.getID() > activeMaxID) {
activeMaxID = pf.getID();
}
} else if (pf.status.equals("inactive")) {
if (pf.getID() > inactiveMaxID) {
inactiveMaxID = pf.getID();
}
} 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(activeMaxID, ((Integer) struct.get("Maxx")).intValue());
} else if (struct.get("status").equals("inactive")) {
assertEquals(inactiveMaxID, ((Integer) struct.get("Maxx")).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 PdxGroupByTestImpl method testSumWithMultiColumnGroupBy.
@Override
@Test
public void testSumWithMultiColumnGroupBy() 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);
}
Map<String, Integer> expectedData = new HashMap<String, Integer>();
for (Object o : region.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
String key = pf.status + "_" + pf.shortID;
if (expectedData.containsKey(key)) {
int sum = expectedData.get(key).intValue() + pf.getID();
expectedData.put(key, sum);
} else {
expectedData.put(key, pf.getID());
}
}
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 PdxGroupByTestImpl method testAggregateFuncNoGroupBy.
@Override
@Test
public void testAggregateFuncNoGroupBy() 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 sum(p.ID) as summ , Max(p.ID) as maxx, min(p.ID) as minn," + " avg(p.ID) as average from /portfolio p where p.ID > 0 ";
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(1, sr.size());
Iterator iter = sr.iterator();
Region rgn = CacheUtils.getRegion("portfolio");
int sum = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (Object o : rgn.values()) {
PortfolioPdx pf = (PortfolioPdx) o;
sum += pf.getID();
if (pf.getID() > max) {
max = pf.getID();
}
if (pf.getID() < min) {
min = pf.getID();
}
}
float avg = sum / rgn.size();
while (iter.hasNext()) {
Struct struct = (Struct) iter.next();
assertEquals(sum, ((Integer) struct.get("summ")).intValue());
assertEquals(max, ((Integer) struct.get("maxx")).intValue());
assertEquals(min, ((Integer) struct.get("minn")).intValue());
assertEquals(avg, ((Number) struct.get("average")).floatValue(), 0.0f);
}
}
Aggregations