use of org.apache.geode.cache.query.Struct in project geode by apache.
the class CumulativeNonDistinctResults method toData.
// TODO : optimize for struct elements , by directly writing the fields
// instead
// of struct
@Override
public void toData(DataOutput out) throws IOException {
boolean isStruct = this.collectionType.getElementType().isStructType();
DataSerializer.writeObject(this.collectionType.getElementType(), out);
HeapDataOutputStream hdos = new HeapDataOutputStream(1024, null);
LongUpdater lu = hdos.reserveLong();
Iterator<E> iter = this.iterator();
int numElements = 0;
while (iter.hasNext()) {
E data = iter.next();
if (isStruct) {
Object[] fields = ((Struct) data).getFieldValues();
DataSerializer.writeObjectArray(fields, out);
} else {
DataSerializer.writeObject(data, hdos);
}
++numElements;
}
lu.update(numElements);
hdos.sendTo(out);
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class QueryMessage method getNextReplyObject.
/**
* Provide results to send back to requestor. terminate by returning END_OF_STREAM token object
*/
@Override
protected Object getNextReplyObject(PartitionedRegion pr) throws CacheException, ForceReattemptException, InterruptedException {
final boolean isDebugEnabled = logger.isDebugEnabled();
if (QueryMonitor.isLowMemory()) {
String reason = LocalizedStrings.QueryMonitor_LOW_MEMORY_CANCELED_QUERY.toLocalizedString(QueryMonitor.getMemoryUsedDuringLowMemory());
throw new QueryExecutionLowMemoryException(reason);
}
if (Thread.interrupted()) {
throw new InterruptedException();
}
while (this.currentResultIterator == null || !this.currentResultIterator.hasNext()) {
if (this.currentSelectResultIterator.hasNext()) {
if (this.isTraceInfoIteration && this.currentResultIterator != null) {
this.isTraceInfoIteration = false;
}
Collection results = this.currentSelectResultIterator.next();
if (isDebugEnabled) {
logger.debug("Query result size: {}", results.size());
}
this.currentResultIterator = results.iterator();
} else {
return Token.END_OF_STREAM;
}
}
Object data = this.currentResultIterator.next();
boolean isPostGFE_8_1 = this.getSender().getVersionObject().compareTo(Version.GFE_81) > 0;
// inaccurate struct type for backward compatibility.
if (this.isStructType && !this.isTraceInfoIteration && isPostGFE_8_1) {
return ((Struct) data).getFieldValues();
} else if (this.isStructType && !this.isTraceInfoIteration) {
Struct struct = (Struct) data;
ObjectType[] fieldTypes = struct.getStructType().getFieldTypes();
for (int i = 0; i < fieldTypes.length; ++i) {
fieldTypes[i] = new ObjectTypeImpl(Object.class);
}
return data;
} else {
return data;
}
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class NonDistinctOrderByReplicatedJUnitTest method testLimitApplicationOnPrimaryKeyIndex.
@Test
public void testLimitApplicationOnPrimaryKeyIndex() throws Exception {
String[] queries = { // from index
"SELECT ID, description, createTime FROM /portfolio1 pf1 where pf1.ID != $1 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 = 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) {
assertEquals("Ordered", rcw.getCollectionType().getSimpleClassName());
}
if (!observer.isIndexesUsed) {
fail("Index is NOT uesd");
}
int indexDistinct = queries[i].indexOf("distinct");
boolean distinctQuery = indexDistinct != -1;
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());
}
}
// Result set verification
Collection coll1 = null;
Collection coll2 = null;
Iterator itert1 = null;
Iterator itert2 = null;
ObjectType type1, type2;
type1 = ((SelectResults) r[0][0]).getCollectionType().getElementType();
type2 = ((SelectResults) r[0][1]).getCollectionType().getElementType();
if ((type1.getClass().getName()).equals(type2.getClass().getName())) {
CacheUtils.log("Both SelectResults are of the same Type i.e.--> " + ((SelectResults) r[0][0]).getCollectionType().getElementType());
} else {
CacheUtils.log("Classes are : " + type1.getClass().getName() + " " + type2.getClass().getName());
fail("FAILED:Select result Type is different in both the cases." + "; failed query=" + queries[0]);
}
if (((SelectResults) r[0][0]).size() == ((SelectResults) r[0][1]).size()) {
CacheUtils.log("Both SelectResults are of Same Size i.e. Size= " + ((SelectResults) r[0][1]).size());
} else {
fail("FAILED:SelectResults size is different in both the cases. Size1=" + ((SelectResults) r[0][0]).size() + " Size2 = " + ((SelectResults) r[0][1]).size() + "; failed query=" + queries[0]);
}
coll2 = (((SelectResults) r[0][1]).asSet());
coll1 = (((SelectResults) r[0][0]).asSet());
itert1 = coll1.iterator();
itert2 = coll2.iterator();
while (itert1.hasNext()) {
Object[] values1 = ((Struct) itert1.next()).getFieldValues();
Object[] values2 = ((Struct) itert2.next()).getFieldValues();
assertEquals(values1.length, values2.length);
assertTrue((((Integer) values1[0]).intValue() != 10));
assertTrue((((Integer) values2[0]).intValue() != 10));
}
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class OrderByReplicatedJUnitTest method testLimitApplicationOnPrimaryKeyIndex.
@Test
public void testLimitApplicationOnPrimaryKeyIndex() throws Exception {
String[] queries = { // from index
"SELECT distinct ID, description, createTime FROM /portfolio1 pf1 where pf1.ID != $1 limit 10" };
Object[][] r = new Object[queries.length][2];
QueryService qs;
qs = CacheUtils.getQueryService();
Position.resetCounter();
// Create Regions
Region r1 = 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
this.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) {
assertEquals("Ordered", rcw.getCollectionType().getSimpleClassName());
}
if (assertIndexUsedOnQueryNode() && !observer.isIndexesUsed) {
fail("Index is NOT uesd");
}
int indexDistinct = queries[i].indexOf("distinct");
boolean distinctQuery = indexDistinct != -1;
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());
}
}
// Result set verification
Collection coll1 = null;
Collection coll2 = null;
Iterator itert1 = null;
Iterator itert2 = null;
ObjectType type1, type2;
type1 = ((SelectResults) r[0][0]).getCollectionType().getElementType();
type2 = ((SelectResults) r[0][1]).getCollectionType().getElementType();
if ((type1.getClass().getName()).equals(type2.getClass().getName())) {
CacheUtils.log("Both SelectResults are of the same Type i.e.--> " + ((SelectResults) r[0][0]).getCollectionType().getElementType());
} else {
CacheUtils.log("Classes are : " + type1.getClass().getName() + " " + type2.getClass().getName());
fail("FAILED:Select result Type is different in both the cases." + "; failed query=" + queries[0]);
}
if (((SelectResults) r[0][0]).size() == ((SelectResults) r[0][1]).size()) {
CacheUtils.log("Both SelectResults are of Same Size i.e. Size= " + ((SelectResults) r[0][1]).size());
} else {
fail("FAILED:SelectResults size is different in both the cases. Size1=" + ((SelectResults) r[0][0]).size() + " Size2 = " + ((SelectResults) r[0][1]).size() + "; failed query=" + queries[0]);
}
coll2 = (((SelectResults) r[0][1]).asSet());
coll1 = (((SelectResults) r[0][0]).asSet());
itert1 = coll1.iterator();
itert2 = coll2.iterator();
while (itert1.hasNext()) {
Object[] values1 = ((Struct) itert1.next()).getFieldValues();
Object[] values2 = ((Struct) itert2.next()).getFieldValues();
assertEquals(values1.length, values2.length);
assertTrue((((Integer) values1[0]).intValue() != 10));
assertTrue((((Integer) values2[0]).intValue() != 10));
}
}
use of org.apache.geode.cache.query.Struct in project geode by apache.
the class NonDistinctOrderByTestImplementation method testOrderByWithNullValuesUseIndex.
@Test
public void testOrderByWithNullValuesUseIndex() 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 = { // 0
"SELECT * FROM /portfolio1 pf1 where ID > 0 order by pkid", // 1
"SELECT * FROM /portfolio1 pf1 where ID > 0 order by pkid asc", // 2
"SELECT * FROM /portfolio1 where ID > 0 order by pkid desc", // 3
"SELECT pkid FROM /portfolio1 pf1 where ID > 0 order by pkid", // 4
"SELECT pkid FROM /portfolio1 pf1 where ID > 0 order by pkid asc", // 5
"SELECT pkid FROM /portfolio1 pf1 where ID > 0 order by pkid desc", // 6
"SELECT ID, pkid FROM /portfolio1 pf1 where ID < 1000 order by pkid", // 7
"SELECT ID, pkid FROM /portfolio1 pf1 where ID > 3 order by pkid", // 8
"SELECT ID, pkid FROM /portfolio1 pf1 where ID < 1000 order by pkid", // 9
"SELECT ID, pkid FROM /portfolio1 pf1 where ID > 0 order by pkid" };
Object[][] r = new Object[queries.length][2];
QueryService qs;
qs = CacheUtils.getQueryService();
// Create Regions
final int size = 9;
final int numNullValues = 3;
Region r1 = this.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);
}
// Create Indexes
this.createIndex("IDIndexPf1", IndexType.FUNCTIONAL, "ID", "/portfolio1");
this.createIndex("PKIDIndexPf1", IndexType.FUNCTIONAL, "pkid", "/portfolio1");
Query q = null;
SelectResults results = null;
List list = null;
String str = "";
try {
// Query 0 - null values are first in the order.
QueryObserverImpl observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
str = queries[0];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
if (assertIndexUsedOnQueryNode() && !observer.isIndexesUsed) {
fail("Index is NOT uesd");
}
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 <= numNullValues) {
assertNull("Expected null value for pkid", pkid);
} else {
assertNotNull("Expected not null value for pkid", pkid);
if (!pkid.equals("" + i)) {
fail(" Value of pkid is not in expected order.");
}
}
}
// Query 4 - 1 distinct null value with pkid.
observer = new QueryObserverImpl();
QueryObserverHolder.setInstance(observer);
str = queries[4];
q = CacheUtils.getQueryService().newQuery(str);
CacheUtils.getLogger().info("Executing query: " + str);
results = (SelectResults) q.execute();
if (assertIndexUsedOnQueryNode() && !observer.isIndexesUsed) {
fail("Index is NOT uesd");
}
list = results.asList();
for (int i = 1; i <= list.size(); i++) {
String pkid = (String) list.get((i - 1));
if (i <= numNullValues) {
assertNull("Expected null value for pkid", pkid);
} else {
assertNotNull("Expected not null value for pkid", pkid);
if (!pkid.equals("" + i)) {
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() - numNullValues) {
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++) {
Struct strct = (Struct) list.get(i - 1);
int id = ((Integer) strct.getFieldValues()[0]).intValue();
// 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 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 strct = (Struct) list.get(i - 1);
int id = ((Integer) strct.getFieldValues()[0]).intValue();
if (id != (numNullValues + i)) {
fail(" Value of ID is not as expected, " + id);
}
}
// Query 8 - ID, 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 <= size; i++) {
Struct vals = (Struct) list.get((i - 1));
int id = ((Integer) vals.get("ID")).intValue();
String pkid = (String) vals.get("pkid");
// 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);
}
assertNull("Expected null value for pkid", pkid);
} else {
if (id != i) {
fail(" Value of ID is not as expected " + id);
}
assertNotNull("Expected not null value for pkid", pkid);
if (!pkid.equals("" + i)) {
fail(" Value of pkid is not in expected order.");
}
}
}
// Query 9 - ID, 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 == 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);
}
}
}
} catch (Exception e) {
e.printStackTrace();
fail(q.getQueryString());
}
}
Aggregations