use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class CountStarJUnitTest method testCountStarWithDuplicateValues.
@Test
public void testCountStarWithDuplicateValues() throws Exception {
Cache cache = CacheUtils.getCache();
createLocalRegion();
assertNotNull(cache.getRegion(regionName));
assertEquals(numElem, cache.getRegion(regionName).size());
HashMap<String, Integer> countStarDistinctQueries = new HashMap<String, Integer>();
countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName, 100);
countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0", 100);
countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID < 0", 0);
countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0 LIMIT 50", 50);
countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0 AND status='active'", 50);
countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0 OR status='active'", 100);
countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0 AND status LIKE 'act%'", 50);
countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID > 0 OR status LIKE 'ina%'", 100);
countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where ID IN SET(1, 2, 3, 4, 5)", 5);
countStarDistinctQueries.put("select distinct COUNT(*) from /" + regionName + " where NOT (ID > 5)", 5);
QueryService queryService = cache.getQueryService();
// Run queries
Query query1 = null;
Query query2 = null;
// Populate the region.
Region region = cache.getRegion(regionName);
for (int i = 1; i <= 100; i++) {
region.put(i, new Portfolio(i, i));
}
// Duplicate values
for (int i = 1; i <= 100; i++) {
region.put(i + 100, new Portfolio(i, i));
}
// Without Indexes
for (String queryStr : countStarDistinctQueries.keySet()) {
query1 = queryService.newQuery(queryStr);
query2 = queryService.newQuery(queryStr.replace("COUNT(*)", "*"));
SelectResults result1 = (SelectResults) query1.execute();
SelectResults result2 = (SelectResults) query2.execute();
assertEquals(queryStr, 1, result1.size());
assertTrue(result1.asList().get(0) instanceof Integer);
int count = ((Integer) result1.asList().get(0)).intValue();
// Also verify with size of result2 to count
assertEquals("COUNT(*) query result is wrong for query: " + queryStr, result2.size(), count);
// assertIndexDetailsEquals("Query: "+ queryStr,
// countStarDistinctQueries.get(queryStr).intValue(), count);
}
// CReate Index on status and ID
queryService.createIndex("sampleIndex-1", IndexType.FUNCTIONAL, "p.ID", "/" + regionName + " p");
queryService.createIndex("sampleIndex-2", IndexType.FUNCTIONAL, "p.status", "/" + regionName + " p");
queryService.createIndex("sampleIndex-3", IndexType.FUNCTIONAL, "pos.secId", "/" + regionName + " p, p.positions.values pos");
// Verify Index Creation
assertNotNull(queryService.getIndex(region, "sampleIndex-1"));
assertNotNull(queryService.getIndex(region, "sampleIndex-2"));
assertEquals(3, queryService.getIndexes().size());
// Without Indexes
for (String queryStr : countStarDistinctQueries.keySet()) {
query1 = queryService.newQuery(queryStr);
query2 = queryService.newQuery(queryStr.replace("COUNT(*)", "*"));
SelectResults result1 = (SelectResults) query1.execute();
SelectResults result2 = (SelectResults) query2.execute();
assertEquals(queryStr, 1, result1.size());
assertTrue(result1.asList().get(0) instanceof Integer);
int count = ((Integer) result1.asList().get(0)).intValue();
// Also verify with size of result2 to count
assertEquals("COUNT(*) query result is wrong for query: " + queryStr, result2.size(), count);
// assertIndexDetailsEquals("Query: "+ queryStr,
// countStarDistinctQueries.get(queryStr).intValue(), count);
}
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class CountStarJUnitTest method testCountStartQueriesOnLocalRegion.
// Queries without indexes.
/**
* Test on Local Region data
*/
@Test
public void testCountStartQueriesOnLocalRegion() throws Exception {
Cache cache = CacheUtils.getCache();
createLocalRegion();
assertNotNull(cache.getRegion(regionName));
assertEquals(numElem, cache.getRegion(regionName).size());
QueryService queryService = cache.getQueryService();
Query query1 = null;
Query query2 = null;
for (String queryStr : countStarQueries.keySet()) {
query1 = queryService.newQuery(queryStr);
query2 = queryService.newQuery(queryStr.replace("COUNT(*)", "*"));
SelectResults result1 = (SelectResults) query1.execute();
SelectResults result2 = (SelectResults) query2.execute();
assertEquals(queryStr, 1, result1.size());
assertTrue(result1.asList().get(0) instanceof Integer);
int count = ((Integer) result1.asList().get(0)).intValue();
// Also verify with size of result2 to count
assertEquals("COUNT(*) query result is wrong for query: " + queryStr, result2.size(), count);
assertEquals("Query: " + queryStr, countStarQueries.get(queryStr).intValue(), count);
}
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class ComparisonOperatorsJUnitTest method testCompareWithInt.
@Test
public void testCompareWithInt() throws Exception {
String var = "ID";
int value = 2;
QueryService qs = CacheUtils.getQueryService();
for (int i = 0; i < operators.length; i++) {
Query query = qs.newQuery("SELECT DISTINCT * FROM /Portfolios where " + var + operators[i] + value);
Object result = query.execute();
if (result instanceof Collection) {
Iterator iter = ((Collection) result).iterator();
while (iter.hasNext()) {
boolean isPassed = false;
Portfolio p = (Portfolio) iter.next();
switch(i) {
case 0:
isPassed = (p.getID() == value);
break;
case 1:
isPassed = (p.getID() != value);
break;
case 2:
isPassed = (p.getID() != value);
break;
case 3:
isPassed = (p.getID() < value);
break;
case 4:
isPassed = (p.getID() <= value);
break;
case 5:
isPassed = (p.getID() > value);
break;
case 6:
isPassed = (p.getID() >= value);
break;
}
if (!isPassed)
fail(this.getName() + " failed for operator " + operators[i]);
}
} else {
fail(this.getName() + " failed for operator " + operators[i]);
}
}
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class SelectStarQueryDUnitTest method functionWithStructTypeInInnerQueryShouldNotThrowExceptionWhenRunOnMultipleNodes.
@Test
public void functionWithStructTypeInInnerQueryShouldNotThrowExceptionWhenRunOnMultipleNodes() throws Exception {
final Host host = Host.getHost(0);
final VM server1 = host.getVM(0);
final VM server2 = host.getVM(1);
final VM server3 = host.getVM(2);
final VM client = host.getVM(3);
PortfolioPdx[] portfolios = new PortfolioPdx[10];
for (int i = 0; i < portfolios.length; i++) {
portfolios[i] = new PortfolioPdx(i);
}
// create servers and regions
final int port1 = startPartitionedCacheServer(server1, portfolios);
final int port2 = startPartitionedCacheServer(server2, portfolios);
final int port3 = startPartitionedCacheServer(server3, portfolios);
// create client
client.invoke(new SerializableCallable("Create client") {
@Override
public Object call() throws Exception {
ClientCacheFactory cf = new ClientCacheFactory();
cf.addPoolServer(getServerHostName(server1.getHost()), port1);
cf.addPoolServer(getServerHostName(server2.getHost()), port2);
cf.addPoolServer(getServerHostName(server3.getHost()), port3);
ClientCache cache = getClientCache(cf);
cache.createClientRegionFactory(ClientRegionShortcut.PROXY).create(regName);
return null;
}
});
// put serialized PortfolioPdx objects
client.invoke(new SerializableCallable("Put objects") {
@Override
public Object call() throws Exception {
Region r1 = getRootRegion(regName);
for (int i = 10; i < 100; i++) {
r1.put("key-" + i, new PortfolioPdx(i));
}
return null;
}
});
// query remotely from client
client.invoke(new SerializableCallable("Query") {
@Override
public Object call() throws Exception {
getLogWriter().info("Querying remotely from client");
QueryService remoteQS = null;
try {
remoteQS = ((ClientCache) getCache()).getQueryService();
SelectResults sr = (SelectResults) remoteQS.newQuery("select distinct oP.ID, oP.status, oP.getType from /" + regName + " oP where element(select distinct p.ID, p.status, p.getType from /" + regName + " p where p.ID = oP.ID).status = 'inactive'").execute();
assertEquals(50, sr.size());
} catch (Exception e) {
fail("Exception getting query service ", e);
}
return null;
}
});
closeCache(client);
closeCache(server1);
closeCache(server2);
closeCache(server3);
}
use of org.apache.geode.cache.query.QueryService in project geode by apache.
the class PdxOrderByJUnitTest method testPartitionRangeIndex.
@Test
public void testPartitionRangeIndex() throws Exception {
final int numberOfEntries = 10;
Region pr = this.configurePR();
// create a local query service
QueryService localQueryService = null;
try {
localQueryService = CacheUtils.getQueryService();
} catch (Exception e) {
fail(e.toString());
}
for (int i = 0; i < numberOfEntries; i++) {
pr.put("key-" + i, new PortfolioPdx(i));
}
localQueryService = CacheUtils.getCache().getQueryService();
SelectResults[][] rs = new SelectResults[queryString.length][2];
for (int i = 0; i < queryString.length; i++) {
try {
Query query = localQueryService.newQuery(queryString[i]);
rs[i][0] = (SelectResults) query.execute();
checkForPdxString(rs[i][0].asList(), queryString[i]);
} catch (Exception e) {
fail("Failed executing " + queryString[i]);
}
}
Index index = null;
try {
index = localQueryService.createIndex("secIdIndex", "pos.secId", regName + " p, p.positions.values pos");
if (index instanceof PartitionedIndex) {
for (Object o : ((PartitionedIndex) index).getBucketIndexes()) {
if (!(o instanceof RangeIndex)) {
fail("Range Index should have been created instead of " + index.getClass());
}
}
} else {
fail("Partitioned index expected");
}
} catch (Exception ex) {
fail("Failed to create index." + ex.getMessage());
}
for (int i = 0; i < queryString.length; i++) {
try {
Query query = localQueryService.newQuery(queryString[i]);
rs[i][1] = (SelectResults) query.execute();
checkForPdxString(rs[i][1].asList(), queryString[i]);
} catch (Exception e) {
fail("Failed executing " + queryString[i]);
}
}
for (int i = 0; i < queryString.length; i++) {
try {
if (i < 7) {
// Compare local and remote query results.
if (!compareResultsOfWithAndWithoutIndex(rs[i])) {
fail("Local and Remote Query Results are not matching for query :" + queryString[i]);
}
} else {
// compare the order of results returned
compareResultsOrder(rs[i], true);
}
} catch (Exception e) {
fail("Failed executing " + queryString[i]);
}
}
}
Aggregations