use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class DistinctResultsWithDupValuesInRegionJUnitTest method testQueriesOnPartitionedRegionWithIndex.
/**
* Test on Partitioned Region data
*/
@Test
public void testQueriesOnPartitionedRegionWithIndex() {
Cache cache = CacheUtils.getCache();
createPartitionedRegion();
assertNotNull(cache.getRegion(regionName));
assertEquals(numElem * 2, cache.getRegion(regionName).size());
QueryService queryService = cache.getQueryService();
Query query1 = null;
try {
queryService.createIndex("idIndex", "p.ID", "/" + regionName + " p");
for (String queryStr : queries) {
query1 = queryService.newQuery(queryStr);
SelectResults result1 = (SelectResults) query1.execute();
assertEquals(queryStr, numElem * 2, result1.size());
verifyDistinctResults(result1);
}
} catch (Exception e) {
e.printStackTrace();
fail("Query " + query1 + " Execution Failed!");
}
// Destroy current Region for other tests
cache.getRegion(regionName).destroyRegion();
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class DistinctResultsWithDupValuesInRegionJUnitTest method testQueriesOnReplicatedRegionWithIndex.
/**
* Test on Replicated Region data
*/
@Test
public void testQueriesOnReplicatedRegionWithIndex() {
Cache cache = CacheUtils.getCache();
createReplicatedRegion();
assertNotNull(cache.getRegion(regionName));
assertEquals(numElem * 2, cache.getRegion(regionName).size());
QueryService queryService = cache.getQueryService();
Query query1 = null;
try {
queryService.createIndex("idIndex", "p.ID", "/" + regionName + " p");
for (String queryStr : queries) {
query1 = queryService.newQuery(queryStr);
SelectResults result1 = (SelectResults) query1.execute();
assertEquals(queryStr, numElem * 2, result1.size());
verifyDistinctResults(result1);
}
} catch (Exception e) {
e.printStackTrace();
fail("Query " + query1 + " Execution Failed!");
}
// Destroy current Region for other tests
cache.getRegion(regionName).destroyRegion();
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class NonDistinctOrderByReplicatedJUnitTest method testNonDistinctOrderbyResultSetForReplicatedRegion.
@Test
public void testNonDistinctOrderbyResultSetForReplicatedRegion() throws Exception {
final int numElements = 200;
CacheUtils.getCache();
Region region = this.createRegion("portfolios", Portfolio.class);
Short[] expectedArray = new Short[numElements - 1];
for (int i = 1; i < numElements; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i / 5);
region.put("" + i, pf);
expectedArray[i - 1] = pf.shortID;
}
Arrays.sort(expectedArray, new Comparator<Short>() {
@Override
public int compare(Short o1, Short o2) {
return o1.shortValue() - o2.shortValue();
}
});
String query = "select pf.shortID from /portfolios pf order by pf.shortID";
QueryService qs = CacheUtils.getQueryService();
SelectResults sr = (SelectResults) qs.newQuery(query).execute();
Object[] results = sr.toArray();
assertTrue(Arrays.equals(expectedArray, results));
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class NonDistinctOrderByReplicatedJUnitTest method testLimitAndOrderByApplicationOnPrimaryKeyIndexQuery.
@Test
public void testLimitAndOrderByApplicationOnPrimaryKeyIndexQuery() throws Exception {
String[] queries = { // from index
"SELECT ID, description, createTime FROM /portfolio1 pf1 where pf1.ID != '10' order by ID desc limit 5 ", "SELECT ID, description, createTime FROM /portfolio1 pf1 where pf1.ID != $1 order by ID " };
Object[][] r = new Object[queries.length][2];
QueryService qs;
qs = CacheUtils.getQueryService();
Position.resetCounter();
// Create Regions
Region r1 = this.createRegion("portfolio1", Portfolio.class);
for (int i = 0; i < 50; i++) {
r1.put(i + "", new Portfolio(i));
}
// Execute Queries without Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
r[i][0] = q.execute(new Object[] { new Integer(10) });
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
// Create Indexes
qs.createIndex("PKIDIndexPf1", IndexType.PRIMARY_KEY, "ID", "/portfolio1");
// Execute Queries with Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
r[i][1] = q.execute(new Object[] { "10" });
int indexLimit = queries[i].indexOf("limit");
int limit = -1;
boolean limitQuery = indexLimit != -1;
if (limitQuery) {
limit = Integer.parseInt(queries[i].substring(indexLimit + 5).trim());
}
boolean orderByQuery = queries[i].indexOf("order by") != -1;
SelectResults rcw = (SelectResults) r[i][1];
if (orderByQuery) {
assertTrue(rcw.getCollectionType().isOrdered());
}
if (!observer.isIndexesUsed) {
fail("Index is NOT uesd");
}
if (limitQuery) {
if (orderByQuery) {
assertFalse(observer.limitAppliedAtIndex);
} else {
assertTrue(observer.limitAppliedAtIndex);
}
} else {
assertFalse(observer.limitAppliedAtIndex);
}
Iterator itr = observer.indexesUsed.iterator();
while (itr.hasNext()) {
String indexUsed = itr.next().toString();
if (!(indexUsed).equals("PKIDIndexPf1")) {
fail("<PKIDIndexPf1> was expected but found " + indexUsed);
}
// assertIndexDetailsEquals("statusIndexPf1",itr.next().toString());
}
int indxs = observer.indexesUsed.size();
System.out.println("**************************************************Indexes Used :::::: " + indxs + " Index Name: " + observer.indexName);
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, true, queries);
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class NonDistinctOrderByTestImplementation method testLimitNotAppliedIfOrderByNotUsingIndex.
@Test
public void testLimitNotAppliedIfOrderByNotUsingIndex() throws Exception {
String[] queries = { // Test case No. IUMR021
"SELECT description, pkid FROM /portfolio1 pf1 where pkid = '12' and ID > 10 order by pkid asc ", "SELECT description, pkid FROM /portfolio1 pf1 where pkid > '1' and ID > 10 order by pkid desc ", "SELECT description, pkid FROM /portfolio1 pf1 where pkid = '13'and ID > 10 and ID < 20 order by pkid asc ", "SELECT description, pkid FROM /portfolio1 pf1 where pkid <'9' and ID > 10 and ID < 20 order by pkid desc", "SELECT description, pkid FROM /portfolio1 pf1 where pkid = '15' and ID >= 10 and ID <= 20 order by pkid desc ", "SELECT description, pkid FROM /portfolio1 pf1 where pkid > '1' and pkid <='9' and ID >= 10 and ID <= 20 order by pkid asc", "SELECT description, pkid FROM /portfolio1 pf1 where pkid > '1' and ID != 10 order by pkid asc ", "SELECT description, pkid FROM /portfolio1 pf1 where pkid > '1' and ID != 10 order by pkid desc ", "SELECT description, pkid FROM /portfolio1 pf1 where pkid = '17' and ID > 10 order by pkid asc limit 5", "SELECT description, pkid FROM /portfolio1 pf1 where pkid > '17' and ID > 10 order by pkid desc limit 5", "SELECT description, pkid FROM /portfolio1 pf1 where pkid < '7' and ID > 10 and ID < 20 order by pkid asc limit 5 ", "SELECT description, pkid FROM /portfolio1 pf1 where pkid = '18' and ID > 10 and ID < 20 order by pkid desc limit 5", "SELECT description, pkid FROM /portfolio1 pf1 where pkid > '1' and ID >= 10 and ID <= 20 order by pkid asc limit 5", "SELECT description, pkid FROM /portfolio1 pf1 where pkid != '17' and ID >= 10 and ID <= 20 order by pkid desc limit 5", "SELECT description, pkid FROM /portfolio1 pf1 where pkid > '0' and ID != 10 order by pkid asc limit 10", "SELECT description, createTime, pkid FROM /portfolio1 pf1 where pkid > '9' and ID != 10 order by pkid desc limit 10" };
Object[][] r = new Object[queries.length][2];
QueryService qs;
qs = CacheUtils.getQueryService();
Position.resetCounter();
// Create Regions
Region r1 = this.createRegion("portfolio1", Portfolio.class);
for (int i = 1; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.shortID = (short) ((short) i / 5);
r1.put("" + i, pf);
}
// Execute Queries without Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
r[i][0] = q.execute();
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
// Create Indexes
this.createIndex("PKIDIndexPf1", IndexType.FUNCTIONAL, "pkid", "/portfolio1");
// Execute Queries with Indexes
for (int i = 0; i < queries.length; i++) {
Query q = null;
try {
q = CacheUtils.getQueryService().newQuery(queries[i]);
CacheUtils.getLogger().info("Executing query: " + queries[i]);
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
r[i][1] = q.execute();
int indexLimit = queries[i].indexOf("limit");
int limit = -1;
boolean limitQuery = indexLimit != -1;
if (limitQuery) {
limit = Integer.parseInt(queries[i].substring(indexLimit + 5).trim());
}
SelectResults rcw = (SelectResults) r[i][1];
assertTrue(rcw.getCollectionType().isOrdered());
if (assertIndexUsedOnQueryNode() && !observer.isIndexesUsed) {
fail("Index is NOT uesd");
}
// assertTrue(!limitQuery || !observer.limitAppliedAtIndex);
Iterator itr = observer.indexesUsed.iterator();
while (itr.hasNext()) {
String indexUsed = itr.next().toString();
if (!(indexUsed).equals("PKIDIndexPf1")) {
fail("<PKIDIndexPf1> was expected but found " + indexUsed);
}
// assertIndexDetailsEquals("statusIndexPf1",itr.next().toString());
}
int indxs = observer.indexesUsed.size();
System.out.println("**************************************************Indexes Used :::::: " + indxs + " Index Name: " + observer.indexName);
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
StructSetOrResultsSet ssOrrs = new StructSetOrResultsSet();
ssOrrs.CompareQueryResultsWithoutAndWithIndexes(r, queries.length, true, queries);
ssOrrs.compareExternallySortedQueriesWithOrderBy(queries, r);
}
Aggregations