use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class GroupByTestImpl method testAggregateFuncCountDistinctStar_2.
@Test
public void testAggregateFuncCountDistinctStar_2() 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(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()) {
Portfolio pf = (Portfolio) 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.internal.DefaultQuery in project geode by apache.
the class RangeIndexAPIJUnitTest method testQueryMethod_4.
/**
* Tests the query method of RangeIndex which just contains not equal keys. That is if the where
* clause looks like a != 7 and a != 8 & a!=9
*/
@Test
public void testQueryMethod_4() throws Exception {
QueryService qs;
qs = CacheUtils.getQueryService();
AbstractIndex i1 = (AbstractIndex) qs.createIndex("idIndex", IndexType.FUNCTIONAL, "ID", "/portfolios");
AbstractIndex i2 = (AbstractIndex) qs.createIndex("statusIndex", IndexType.FUNCTIONAL, "status", "/portfolios");
AbstractIndex i3 = (AbstractIndex) qs.createIndex("status.toString()", IndexType.FUNCTIONAL, "status.toString", "/portfolios");
Set results = new HashSet();
DefaultQuery q = new DefaultQuery("select * from /portfolios", CacheUtils.getCache(), false);
q.setRemoteQuery(false);
ExecutionContext context = new QueryExecutionContext(null, CacheUtils.getCache(), q);
bindIterators(context, "/portfolios");
Set keysToRemove = new HashSet();
keysToRemove.add(new Integer(5));
i1.query(results, keysToRemove, context);
assertEquals(11, results.size());
for (int i = 0; i < 12; ) {
if (i != 5) {
assertTrue(results.contains(region.get(new Integer(i))));
}
++i;
}
results.clear();
keysToRemove.clear();
keysToRemove.add(new Integer(5));
keysToRemove.add(new Integer(8));
i1.query(results, keysToRemove, context);
assertEquals(10, results.size());
for (int i = 0; i < 12; ) {
if (i != 5 && i != 8) {
assertTrue(results.contains(region.get(new Integer(i))));
}
++i;
}
results.clear();
keysToRemove.clear();
keysToRemove.add("active");
keysToRemove.add("inactive");
i2.query(results, keysToRemove, context);
assertEquals(2, results.size());
for (int i = 10; i < 12; ) {
assertTrue(results.contains(region.get(new Integer(i))));
++i;
}
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class BugJUnitTest method testEquijoinPRColocatedQuery_2.
@Test
public void testEquijoinPRColocatedQuery_2() throws Exception {
AttributesFactory factory = new AttributesFactory();
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {
public String getName() {
return "blah";
}
public Serializable getRoutingObject(EntryOperation opDetails) {
return (Serializable) opDetails.getKey();
}
public void close() {
}
}).create());
PartitionedRegion pr1 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr1", factory.create());
factory = new AttributesFactory();
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {
public String getName() {
return "blah";
}
public Serializable getRoutingObject(EntryOperation opDetails) {
return (Serializable) opDetails.getKey();
}
public void close() {
}
}).setColocatedWith(pr1.getName()).create());
final PartitionedRegion pr2 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr2", factory.create());
createAllNumPRAndEvenNumPR(pr1, pr2, 80);
Set<Integer> set = createAndPopulateSet(15);
LocalDataSet lds = new LocalDataSet(pr1, set);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
QueryService qs = pr1.getCache().getQueryService();
qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr1.entries e");
qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr2.entries e");
String query = "select distinct e1.key from /pr1.entries e1,/pr2.entries e2" + " where e1.value=e2.value";
DefaultQuery cury = (DefaultQuery) CacheUtils.getQueryService().newQuery(query);
SelectResults r = (SelectResults) lds.executeQuery(cury, null, set);
if (!observer.isIndexesUsed) {
fail("Indexes should have been used");
}
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class BugJUnitTest method testEquijoinPRColocatedQuery_1.
@Test
public void testEquijoinPRColocatedQuery_1() throws Exception {
AttributesFactory factory = new AttributesFactory();
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {
public String getName() {
return "blah";
}
public Serializable getRoutingObject(EntryOperation opDetails) {
return (Serializable) opDetails.getKey();
}
public void close() {
}
}).create());
PartitionedRegion pr1 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr1", factory.create());
factory = new AttributesFactory();
factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(1).setTotalNumBuckets(40).setPartitionResolver(new PartitionResolver() {
public String getName() {
return "blah";
}
public Serializable getRoutingObject(EntryOperation opDetails) {
return (Serializable) opDetails.getKey();
}
public void close() {
}
}).setColocatedWith(pr1.getName()).create());
final PartitionedRegion pr2 = (PartitionedRegion) CacheUtils.getCache().createRegion("pr2", factory.create());
createAllNumPRAndEvenNumPR(pr1, pr2, 80);
Set<Integer> set = createAndPopulateSet(15);
LocalDataSet lds = new LocalDataSet(pr1, set);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
QueryService qs = pr1.getCache().getQueryService();
qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr1 e");
qs.createIndex("valueIndex", IndexType.FUNCTIONAL, "e.value", "/pr2 e");
String query = "select distinct e1.value from /pr1 e1, " + "/pr2 e2" + " where e1.value=e2.value";
DefaultQuery cury = (DefaultQuery) CacheUtils.getQueryService().newQuery(query);
SelectResults r = (SelectResults) lds.executeQuery(cury, null, set);
if (!observer.isIndexesUsed) {
fail("Indexes should have been used");
}
}
use of org.apache.geode.cache.query.internal.DefaultQuery in project geode by apache.
the class ServerCQImpl method constructServerSideQuery.
/**
* Returns parameterized query used by the server. This method replaces Region name with $1 and if
* type is not specified in the query, looks for type from cqattributes and appends into the
* query.
*
* @return String modified query.
*/
private Query constructServerSideQuery() throws QueryException {
InternalCache cache = cqService.getInternalCache();
DefaultQuery locQuery = (DefaultQuery) cache.getLocalQueryService().newQuery(this.queryString);
CompiledSelect select = locQuery.getSimpleSelect();
CompiledIteratorDef from = (CompiledIteratorDef) select.getIterators().get(0);
// WARNING: ASSUMES QUERY WAS ALREADY VALIDATED FOR PROPER "FORM" ON CLIENT;
// THIS VALIDATION WILL NEED TO BE DONE ON THE SERVER FOR NATIVE CLIENTS,
// BUT IS NOT DONE HERE FOR JAVA CLIENTS.
// The query was already checked on the client that the sole iterator is a
// CompiledRegion
this.regionName = ((CompiledRegion) from.getCollectionExpr()).getRegionPath();
from.setCollectionExpr(new CompiledBindArgument(1));
return locQuery;
}
Aggregations