use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class OrderByPartitionedJUnitTest method testOrderByWithNullValues.
@Test
public void testOrderByWithNullValues() throws Exception {
// IN ORDER BY NULL values are treated as smallest. E.g For an ascending order by field
// its null values are reported first and then the values in ascending order.
String[] queries = getQueriesForOrderByWithNullValues();
Object[][] r = new Object[queries.length][2];
QueryService qs;
qs = CacheUtils.getQueryService();
// Create Regions
final int size = 9;
final int numNullValues = 3;
Region r1 = createRegion("portfolio1", Portfolio.class);
for (int i = 1; i <= size; i++) {
Portfolio pf = new Portfolio(i);
// Add numNullValues null values.
if (i <= numNullValues) {
pf.pkid = null;
pf.status = "a" + i;
}
r1.put(i + "", pf);
}
Query q = null;
SelectResults results = null;
List list = null;
String str = "";
try {
// Query 0 - null values are first in the order.
str = queries[0];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
r[0][0] = results;
list = results.asList();
for (int i = 1; i <= size; i++) {
Portfolio p = (Portfolio) list.get((i - 1));
if (i <= numNullValues) {
assertNull("Expected null value for pkid, p: " + p, p.pkid);
} else {
assertNotNull("Expected not null value for pkid", p.pkid);
if (!p.pkid.equals("" + i)) {
fail(" Value of pkid is not in expected order.");
}
}
}
// Query 1 - null values are first in the order.
str = queries[1];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
list = results.asList();
for (int i = 1; i <= size; i++) {
Portfolio p = (Portfolio) list.get((i - 1));
if (i <= numNullValues) {
assertNull("Expected null value for pkid", p.pkid);
} else {
assertNotNull("Expected not null value for pkid", p.pkid);
if (!p.pkid.equals("" + i)) {
fail(" Value of pkid is not in expected order.");
}
}
}
// Query 2 - null values are last in the order.
str = queries[2];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
list = results.asList();
for (int i = 1; i <= size; i++) {
Portfolio p = (Portfolio) list.get((i - 1));
if (i > (size - numNullValues)) {
assertNull("Expected null value for pkid", p.pkid);
} else {
assertNotNull("Expected not null value for pkid", p.pkid);
if (!p.pkid.equals("" + (size - (i - 1)))) {
fail(" Value of pkid is not in expected order.");
}
}
}
// Query 3 - 1 distinct null value with pkid.
str = queries[3];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
list = results.asList();
for (int i = 1; i <= list.size(); i++) {
String pkid = (String) list.get((i - 1));
if (i == 1) {
assertNull("Expected null value for pkid", pkid);
} else {
assertNotNull("Expected not null value for pkid", pkid);
if (!pkid.equals("" + (numNullValues + (i - 1)))) {
fail(" Value of pkid is not in expected order.");
}
}
}
// Query 4 - 1 distinct null value with pkid.
str = queries[4];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
list = results.asList();
for (int i = 1; i <= list.size(); i++) {
String pkid = (String) list.get((i - 1));
if (i == 1) {
assertNull("Expected null value for pkid", pkid);
} else {
assertNotNull("Expected not null value for pkid", pkid);
if (!pkid.equals("" + (numNullValues + (i - 1)))) {
fail(" Value of pkid is not in expected order.");
}
}
}
// Query 5 - 1 distinct null value with pkid at the end.
str = queries[5];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
list = results.asList();
for (int i = 1; i <= list.size(); i++) {
String pkid = (String) list.get((i - 1));
if (i == (list.size())) {
assertNull("Expected null value for pkid", pkid);
} else {
assertNotNull("Expected not null value for pkid", pkid);
if (!pkid.equals("" + (size - (i - 1)))) {
fail(" Value of pkid is not in expected order.");
}
}
}
// Query 6 - ID field values should be in the same order.
str = queries[6];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
list = results.asList();
for (int i = 1; i <= size; i++) {
int id = (Integer) ((Struct) list.get((i - 1))).getFieldValues()[0];
// ID should be one of 1, 2, 3 because of distinct
if (i <= numNullValues) {
if (!(id == 1 || id == 2 || id == 3)) {
fail(" Value of ID is not as expected " + id);
}
} else {
if (id != i) {
fail(" Value of ID is not as expected " + id);
}
}
}
// Query 7 - ID, pkid field values should be in the same order.
str = queries[7];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
list = results.asList();
for (int i = 1; i <= list.size(); i++) {
Struct vals = (Struct) list.get((i - 1));
int id = ((Integer) vals.get("ID")).intValue();
String pkid = (String) vals.get("pkid");
if (i <= numNullValues) {
assertNull("Expected null value for pkid, " + pkid, pkid);
if (!(id == 1 || id == 2 || id == 3)) {
fail(" Value of ID is not as expected " + id);
}
} else {
if (!pkid.equals("" + i)) {
fail(" Value of pkid is not as expected, " + pkid);
}
if (id != i) {
fail(" Value of ID is not as expected, " + id);
}
}
}
// Query 8 - ID asc, pkid field values should be in the same order.
str = queries[8];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
list = results.asList();
for (int i = 1; i <= list.size(); i++) {
Struct vals = (Struct) list.get((i - 1));
int id = ((Integer) vals.get("ID")).intValue();
String pkid = (String) vals.get("pkid");
if (i <= numNullValues) {
assertNull("Expected null value for pkid, " + pkid, pkid);
if (id != i) {
fail(" Value of ID is not as expected, it is: " + id + " expected :" + i);
}
} else {
if (!pkid.equals("" + i)) {
fail(" Value of pkid is not as expected, " + pkid);
}
if (id != i) {
fail(" Value of ID is not as expected, " + id);
}
}
}
// Query 9 - ID desc, pkid field values should be in the same order.
str = queries[9];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
list = results.asList();
for (int i = 1; i <= list.size(); i++) {
Struct vals = (Struct) list.get((i - 1));
int id = ((Integer) vals.get("ID")).intValue();
String pkid = (String) vals.get("pkid");
if (i <= numNullValues) {
assertNull("Expected null value for pkid, " + pkid, pkid);
if (id != (numNullValues - (i - 1))) {
fail(" Value of ID is not as expected " + id);
}
} else {
if (!pkid.equals("" + i)) {
fail(" Value of pkid is not as expected, " + pkid);
}
if (id != i) {
fail(" Value of ID is not as expected, " + id);
}
}
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class MultiIndexCreationJUnitTest method testMultiIndexCreationOnlyDefine.
@Test
public void testMultiIndexCreationOnlyDefine() throws Exception {
Region r = CacheUtils.getRegion(regionName);
for (int i = 0; i < 10; i++) {
r.put("" + i, new Portfolio(i));
}
QueryService qs = CacheUtils.getQueryService();
qs.defineIndex("statusIndex", "status", r.getFullPath());
qs.defineIndex("IDIndex", "ID", r.getFullPath());
Index ind = qs.getIndex(r, "statusIndex");
assertNull("Index should not have been created", ind);
ind = qs.getIndex(r, "IDIndex");
assertNull("Index should not have been created", ind);
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertFalse(indexCalled);
}
});
String[] queries = { "select * from " + r.getFullPath() + " where status = 'active'", "select * from " + r.getFullPath() + " where ID > 4" };
for (int i = 0; i < queries.length; i++) {
SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
assertEquals(5, sr.size());
}
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class MultiIndexCreationJUnitTest method testBasicMultiIndexCreationDifferentTypes.
@Test
public void testBasicMultiIndexCreationDifferentTypes() throws Exception {
Region r = CacheUtils.getRegion(regionName);
for (int i = 0; i < 10; i++) {
r.put("" + i, new Portfolio(i));
}
QueryService qs = CacheUtils.getQueryService();
qs.defineIndex("statusIndex", "status", r.getFullPath());
qs.defineHashIndex("IDIndex", "ID", r.getFullPath());
qs.defineKeyIndex("keyIDIndex", "ID", r.getFullPath());
List<Index> indexes = qs.createDefinedIndexes();
assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
Index ind = qs.getIndex(r, "statusIndex");
assertTrue(ind instanceof CompactRangeIndex);
assertEquals(2, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(r, "IDIndex");
assertTrue(ind instanceof HashIndex);
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(r, "keyIDIndex");
assertTrue(ind instanceof PrimaryKeyIndex);
assertEquals(10, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
String[] queries = { "select * from " + r.getFullPath() + " where status = 'active'", "select * from " + r.getFullPath() + " where ID > 4" };
for (int i = 0; i < queries.length; i++) {
SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
assertEquals(5, sr.size());
}
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class MultiIndexCreationJUnitTest method testIndexCreationOnMultipleRegions.
@Test
public void testIndexCreationOnMultipleRegions() throws Exception {
Region pr = CacheUtils.getCache().createRegionFactory(RegionShortcut.PARTITION).create(prRegionName);
for (int i = 0; i < 10; i++) {
pr.put("" + i, new Portfolio(i));
}
Region overflow = CacheUtils.getCache().createRegionFactory(RegionShortcut.REPLICATE_OVERFLOW).create(overflowRegionName);
for (int i = 0; i < 10; i++) {
overflow.put("" + i, new Portfolio(i));
}
Region r = CacheUtils.getRegion(regionName);
for (int i = 0; i < 10; i++) {
r.put("" + i, new Portfolio(i));
}
QueryService qs = CacheUtils.getQueryService();
qs.defineIndex("IDIndex", "ID", pr.getFullPath());
qs.defineIndex("secIDIndex", "pos.secId", r.getFullPath() + " p, p.positions.values pos ");
qs.defineIndex("statusIndex", "status", overflow.getFullPath());
List<Index> indexes = qs.createDefinedIndexes();
assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
Index ind = qs.getIndex(overflow, "statusIndex");
assertEquals(2, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(pr, "IDIndex");
assertEquals(10, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(r, "secIDIndex");
assertEquals(12, ind.getStatistics().getNumberOfKeys());
assertEquals(20, ind.getStatistics().getNumberOfValues());
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
String[] queries = { "select * from " + overflow.getFullPath() + " where status = 'active'", "select * from " + pr.getFullPath() + " where ID > 4", "select * from " + r.getFullPath() + " p, p.positions.values pos where pos.secId != NULL" };
for (int i = 0; i < queries.length; i++) {
SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
if (i == 2) {
assertEquals("Incorrect results for query: " + queries[i], 20, sr.size());
} else {
assertEquals("Incorrect results for query: " + queries[i], 5, sr.size());
}
}
QueryObserverHolder.setInstance(old);
}
use of org.apache.geode.cache.query.SelectResults in project geode by apache.
the class MultiIndexCreationJUnitTest method testIndexCreationOnMultipleRegionsBeforePuts.
@Test
public void testIndexCreationOnMultipleRegionsBeforePuts() throws Exception {
Region pr = CacheUtils.getCache().createRegionFactory(RegionShortcut.PARTITION).create(prRegionName);
Region overflow = CacheUtils.getCache().createRegionFactory(RegionShortcut.REPLICATE_OVERFLOW).create(overflowRegionName);
Region r = CacheUtils.getRegion(regionName);
QueryService qs = CacheUtils.getQueryService();
qs.defineIndex("IDIndex", "ID", pr.getFullPath());
qs.defineIndex("secIDIndex", "pos.secId", r.getFullPath() + " p, p.positions.values pos ");
qs.defineIndex("statusIndex", "status", overflow.getFullPath());
List<Index> indexes = qs.createDefinedIndexes();
for (int i = 0; i < 10; i++) {
r.put("" + i, new Portfolio(i));
}
for (int i = 0; i < 10; i++) {
pr.put("" + i, new Portfolio(i));
}
for (int i = 0; i < 10; i++) {
overflow.put("" + i, new Portfolio(i));
}
assertEquals("Only 3 indexes should have been created. ", 3, indexes.size());
Index ind = qs.getIndex(overflow, "statusIndex");
assertEquals(2, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(pr, "IDIndex");
assertEquals(10, ind.getStatistics().getNumberOfKeys());
assertEquals(10, ind.getStatistics().getNumberOfValues());
ind = qs.getIndex(r, "secIDIndex");
assertEquals(12, ind.getStatistics().getNumberOfKeys());
assertEquals(20, ind.getStatistics().getNumberOfValues());
QueryObserver old = QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private boolean indexCalled = false;
public void afterIndexLookup(Collection results) {
indexCalled = true;
}
public void endQuery() {
assertTrue(indexCalled);
}
});
String[] queries = { "select * from " + overflow.getFullPath() + " where status = 'active'", "select * from " + pr.getFullPath() + " where ID > 4", "select * from " + r.getFullPath() + " p, p.positions.values pos where pos.secId != NULL" };
for (int i = 0; i < queries.length; i++) {
SelectResults sr = (SelectResults) qs.newQuery(queries[i]).execute();
if (i == 2) {
assertEquals("Incorrect results for query: " + queries[i], 20, sr.size());
} else {
assertEquals("Incorrect results for query: " + queries[i], 5, sr.size());
}
}
QueryObserverHolder.setInstance(old);
}
Aggregations