use of org.apache.geode.cache.query.internal.ResultsBag in project geode by apache.
the class LikePredicateJUnitTest method likePercentageTerminated_2.
/**
* Tests a pattern which just contains a single % indicating all match
*
* @throws Exception
*/
private void likePercentageTerminated_2(boolean useBindParam) throws Exception {
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
char ch = 'd';
String base = "abc";
for (int i = 1; i < 6; ++i) {
Portfolio pf = new Portfolio(i);
pf.status = base + ch;
ch += 1;
region.put(new Integer(i), pf);
}
base = "abd";
ch = 'd';
for (int i = 6; i < 11; ++i) {
Portfolio pf = new Portfolio(i);
pf.status = base + ch;
ch += 1;
region.put(new Integer(i), pf);
}
QueryService qs = cache.getQueryService();
Query q;
SelectResults results;
SelectResults expectedResults;
String predicate = "";
if (useBindParam) {
predicate = "$1";
} else {
predicate = " '%'";
}
q = qs.newQuery("SELECT distinct * FROM /pos ps WHERE ps.status like " + predicate);
if (useBindParam) {
results = (SelectResults) q.execute(new Object[] { "%" });
} else {
results = (SelectResults) q.execute();
}
ResultsBag bag = new ResultsBag(null);
for (int i = 1; i < 11; ++i) {
bag.add(region.get(new Integer(i)));
}
expectedResults = new ResultsCollectionWrapper(new ObjectTypeImpl(Object.class), bag.asSet());
SelectResults[][] rs = new SelectResults[][] { { results, expectedResults } };
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
// Create Index
qs.createIndex("status", IndexType.FUNCTIONAL, "ps.status", "/pos ps");
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
if (useBindParam) {
results = (SelectResults) q.execute(new Object[] { "%" });
} else {
results = (SelectResults) q.execute();
}
rs[0][0] = results;
rs[0][1] = expectedResults;
CacheUtils.compareResultsOfWithAndWithoutIndex(rs, this);
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.internal.ResultsBag in project geode by apache.
the class LikePredicateJUnitTest method testLikePredicateOnNullValues.
@Test
public void testLikePredicateOnNullValues() throws Exception {
Cache cache = CacheUtils.getCache();
AttributesFactory attributesFactory = new AttributesFactory();
RegionAttributes regionAttributes = attributesFactory.create();
Region region = cache.createRegion("pos", regionAttributes);
char ch = 'd';
String base = "abc";
for (int i = 1; i < 6; i++) {
Portfolio pf = new Portfolio(i);
pf.pkid = "abc";
if (i % 2 == 0) {
pf.pkid = null;
pf.status = "like";
} else if (i == 3) {
pf.status = null;
}
region.put(new Integer(i), pf);
}
QueryService qs = cache.getQueryService();
Query q;
SelectResults results;
String[] query = new String[] { "SELECT distinct * FROM /pos ps WHERE ps.pkid like '%b%'", "SELECT * FROM /pos ps WHERE ps.pkid like '%b%' and ps.status like '%ctiv%'", "SELECT * FROM /pos ps WHERE ps.pkid like '_bc'", "SELECT pkid FROM /pos ps WHERE ps.pkid like 'abc%'", "SELECT pkid FROM /pos ps WHERE ps.pkid = 'abc'", "SELECT pkid FROM /pos ps WHERE ps.pkid like '%b%' and ps.status = 'like'", "SELECT pkid FROM /pos ps WHERE ps.pkid like '%b%' and ps.status like '%ike'", "SELECT pkid FROM /pos ps WHERE ps.pkid like '%b%' and ps.pkid like '_bc'", "SELECT pkid FROM /pos ps WHERE ps.pkid like 'ml%' or ps.status = 'like'" };
// null check
for (int i = 0; i < query.length; i++) {
q = qs.newQuery(query[i]);
// No NPE.
results = (SelectResults) q.execute();
}
// validate results
q = qs.newQuery(query[0]);
results = (SelectResults) q.execute();
assertEquals("Result size is not as expected", 3, results.size());
q = qs.newQuery(query[1]);
results = (SelectResults) q.execute();
assertEquals("Result size is not as expected", 2, results.size());
// Create index.
qs.createIndex("pkid", IndexType.FUNCTIONAL, "ps.pkid", "/pos ps");
qs.createIndex("status", IndexType.FUNCTIONAL, "ps.status", "/pos ps");
for (int i = 0; i < query.length; i++) {
q = qs.newQuery(query[i]);
// No NPE.
results = (SelectResults) q.execute();
}
ResultsBag bag = new ResultsBag(null);
for (int i = 1; i < 6; ++i) {
bag.add(region.get(new Integer(i)));
}
}
Aggregations