use of org.apache.geode.cache.query.internal.types.StructTypeImpl in project geode by apache.
the class AbstractIndex method evaluateLastColl.
private List evaluateLastColl(Object value, ExecutionContext context, List itrs, int level) throws FunctionDomainException, TypeMismatchException, NameResolutionException, QueryInvocationTargetException {
// A tuple is a value generated from RegionEntry value which could be a StructType (Multiple
// Dependent Iterators) or ObjectType (Single Iterator) value.
List tuples = new ArrayList(1);
RuntimeIterator currItrator = (RuntimeIterator) itrs.get(level);
currItrator.setCurrent(value);
// If its last iterator then just evaluate final struct.
if (itrs.size() - 1 == level) {
if (itrs.size() > 1) {
Object[] tuple = new Object[itrs.size()];
for (int i = 0; i < itrs.size(); i++) {
RuntimeIterator iter = (RuntimeIterator) itrs.get(i);
tuple[i] = iter.evaluate(context);
}
// Its ok to pass type as null as we are only interested in values.
tuples.add(new StructImpl(new StructTypeImpl(), tuple));
} else {
tuples.add(currItrator.evaluate(context));
}
} else {
// Not the last iterator.
RuntimeIterator nextItr = (RuntimeIterator) itrs.get(level + 1);
Collection nextLevelValues = nextItr.evaluateCollection(context);
// If value is null or INVALID then the evaluated collection would be Null.
if (nextLevelValues != null) {
for (Object nextLevelValue : nextLevelValues) {
tuples.addAll(evaluateLastColl(nextLevelValue, context, itrs, level + 1));
}
}
}
return tuples;
}
use of org.apache.geode.cache.query.internal.types.StructTypeImpl in project geode by apache.
the class BaseCommandQuery method processQueryUsingParams.
/**
* Process the give query and sends the resulset back to the client.
*
* @param msg
* @param query
* @param queryString
* @param regionNames
* @param start
* @param cqQuery
* @param queryContext
* @param servConn
* @return true if successful execution false in case of failure.
* @throws IOException
*/
protected boolean processQueryUsingParams(Message msg, Query query, String queryString, Set regionNames, long start, ServerCQ cqQuery, QueryOperationContext queryContext, ServerConnection servConn, boolean sendResults, Object[] params) throws IOException, InterruptedException {
ChunkedMessage queryResponseMsg = servConn.getQueryResponseMessage();
CacheServerStats stats = servConn.getCacheServerStats();
CachedRegionHelper crHelper = servConn.getCachedRegionHelper();
{
long oldStart = start;
start = DistributionStats.getStatTime();
stats.incReadQueryRequestTime(start - oldStart);
}
// object type
if (servConn.getClientVersion().compareTo(Version.GFE_70) >= 0) {
((DefaultQuery) query).setRemoteQuery(true);
}
// Process the query request
try {
// integrated security
for (Object regionName : regionNames) {
this.securityService.authorizeRegionRead(regionName.toString());
}
// Execute query
// startTime = GenericStats.getTime();
// startTime = System.currentTimeMillis();
// For now we assume the results are a SelectResults
// which is the only possibility now, but this may change
// in the future if we support arbitrary queries
Object result = null;
if (params != null) {
result = query.execute(params);
} else {
result = query.execute();
}
// Asif : Before conditioning the results check if any
// of the regions involved in the query have been destroyed
// or not. If yes, throw an Exception.
// This is a workaround/fix for Bug 36969
Iterator itr = regionNames.iterator();
while (itr.hasNext()) {
String regionName = (String) itr.next();
if (crHelper.getRegion(regionName) == null) {
throw new RegionDestroyedException(LocalizedStrings.BaseCommand_REGION_DESTROYED_DURING_THE_EXECUTION_OF_THE_QUERY.toLocalizedString(), regionName);
}
}
AuthorizeRequestPP postAuthzRequest = servConn.getPostAuthzRequest();
if (postAuthzRequest != null) {
if (cqQuery == null) {
queryContext = postAuthzRequest.queryAuthorize(queryString, regionNames, result, queryContext, params);
} else {
queryContext = postAuthzRequest.executeCQAuthorize(cqQuery.getName(), queryString, regionNames, result, queryContext);
}
result = queryContext.getQueryResult();
}
if (result instanceof SelectResults) {
SelectResults selectResults = (SelectResults) result;
if (logger.isDebugEnabled()) {
logger.debug("Query Result size for : {} is {}", query.getQueryString(), selectResults.size());
}
CollectionType collectionType = null;
boolean sendCqResultsWithKey = true;
boolean isStructs = false;
// check if resultset has serialized objects, so that they could be sent
// as ObjectPartList
boolean hasSerializedObjects = ((DefaultQuery) query).isKeepSerialized();
if (logger.isDebugEnabled()) {
logger.debug("Query Result for :{} has serialized objects: {}", query.getQueryString(), hasSerializedObjects);
}
// Don't convert to a Set, there might be duplicates now
// The results in a StructSet are stored in Object[]s
// Get them as Object[]s for the objs[] in order to avoid duplicating
// the StructTypes
// Object[] objs = new Object[selectResults.size()];
// Get the collection type (which includes the element type)
// (used to generate the appropriate instance on the client)
// Get the collection type (which includes the element type)
// (used to generate the appropriate instance on the client)
collectionType = getCollectionType(selectResults);
isStructs = collectionType.getElementType().isStructType();
// Check if the Query is from CQ execution.
if (cqQuery != null) {
// Check if the key can be sent to the client based on its version.
sendCqResultsWithKey = sendCqResultsWithKey(servConn);
if (sendCqResultsWithKey) {
// Update the collection type to include key info.
collectionType = new CollectionTypeImpl(Collection.class, new StructTypeImpl(new String[] { "key", "value" }));
isStructs = collectionType.getElementType().isStructType();
}
}
int numberOfChunks = (int) Math.ceil(selectResults.size() * 1.0 / MAXIMUM_CHUNK_SIZE);
if (logger.isTraceEnabled()) {
logger.trace("{}: Query results size: {}: Entries in chunk: {}: Number of chunks: {}", servConn.getName(), selectResults.size(), MAXIMUM_CHUNK_SIZE, numberOfChunks);
}
long oldStart = start;
start = DistributionStats.getStatTime();
stats.incProcessQueryTime(start - oldStart);
if (sendResults) {
queryResponseMsg.setMessageType(MessageType.RESPONSE);
queryResponseMsg.setTransactionId(msg.getTransactionId());
queryResponseMsg.sendHeader();
}
if (sendResults && numberOfChunks == 0) {
// Send 1 empty chunk
if (logger.isTraceEnabled()) {
logger.trace("{}: Creating chunk: 0", servConn.getName());
}
writeQueryResponseChunk(new Object[0], collectionType, true, servConn);
if (logger.isDebugEnabled()) {
logger.debug("{}: Sent chunk (1 of 1) of query response for query {}", servConn.getName(), queryString);
}
} else {
// send it as a part of ObjectPartList
if (hasSerializedObjects) {
sendResultsAsObjectPartList(numberOfChunks, servConn, selectResults.asList(), isStructs, collectionType, queryString, cqQuery, sendCqResultsWithKey, sendResults);
} else {
sendResultsAsObjectArray(selectResults, numberOfChunks, servConn, isStructs, collectionType, queryString, cqQuery, sendCqResultsWithKey, sendResults);
}
}
if (cqQuery != null) {
// Set the CQ query result cache initialized flag.
cqQuery.setCqResultsCacheInitialized();
}
} else if (result instanceof Integer) {
if (sendResults) {
queryResponseMsg.setMessageType(MessageType.RESPONSE);
queryResponseMsg.setTransactionId(msg.getTransactionId());
queryResponseMsg.sendHeader();
writeQueryResponseChunk(result, null, true, servConn);
}
} else {
throw new QueryInvalidException(LocalizedStrings.BaseCommand_UNKNOWN_RESULT_TYPE_0.toLocalizedString(result.getClass()));
}
msg.clearParts();
} catch (QueryInvalidException e) {
// Handle this exception differently since it can contain
// non-serializable objects.
// java.io.NotSerializableException: antlr.CommonToken
// Log a warning to show stack trace and create a new
// QueryInvalidEsception on the original one's message (not cause).
logger.warn(LocalizedMessage.create(LocalizedStrings.BaseCommand_UNEXPECTED_QUERYINVALIDEXCEPTION_WHILE_PROCESSING_QUERY_0, queryString), e);
QueryInvalidException qie = new QueryInvalidException(LocalizedStrings.BaseCommand_0_QUERYSTRING_IS_1.toLocalizedString(new Object[] { e.getLocalizedMessage(), queryString }));
writeQueryResponseException(msg, qie, servConn);
return false;
} catch (DistributedSystemDisconnectedException se) {
if (msg != null && logger.isDebugEnabled()) {
logger.debug("{}: ignoring message of type {} from client {} because shutdown occurred during message processing.", servConn.getName(), MessageType.getString(msg.getMessageType()), servConn.getProxyID());
}
servConn.setFlagProcessMessagesAsFalse();
servConn.setClientDisconnectedException(se);
return false;
} catch (Exception e) {
// If an interrupted exception is thrown , rethrow it
checkForInterrupt(servConn, e);
// Otherwise, write a query response and continue
// Check if query got canceled from QueryMonitor.
DefaultQuery defaultQuery = (DefaultQuery) query;
if ((defaultQuery).isCanceled()) {
e = new QueryException(defaultQuery.getQueryCanceledException().getMessage(), e.getCause());
}
writeQueryResponseException(msg, e, servConn);
return false;
} finally {
// Since the query object is being shared in case of bind queries,
// resetting the flag may cause inconsistency.
// Also since this flag is only being set in code path executed by
// remote query execution, resetting it is not required.
// ((DefaultQuery)query).setRemoteQuery(false);
}
if (logger.isDebugEnabled()) {
logger.debug("{}: Sent query response for query {}", servConn.getName(), queryString);
}
stats.incWriteQueryResponseTime(DistributionStats.getStatTime() - start);
return true;
}
use of org.apache.geode.cache.query.internal.types.StructTypeImpl in project geode by apache.
the class CustomerOptimizationsJUnitTest method testProjectionEvaluationDuringIndexResults.
@Test
public void testProjectionEvaluationDuringIndexResults() throws QueryException {
QueryService qs = CacheUtils.getQueryService();
String[] queries = new String[] { "select p.status from /pos p where p.ID > 0 ", "select p.status from /pos p, p.positions pos where p.ID > 0 ", "select p.status from /pos p where p.ID > 0 and p.createTime > 0", "select p.status as sts, p as pos from /pos p where p.ID > 0 and p.createTime > 0", "select p.status as sts, p as pos from /pos p where p.ID IN SET( 0,1,2,3) and p.createTime > 0", "select p.status as sts, p as pos from /pos p where ( p.ID IN SET( 0,1,2,3) and p.createTime > 0L) OR (p.ID IN SET( 2,3) and p.createTime > 5L)" };
SelectResults[][] sr = new SelectResults[queries.length][2];
for (int i = 0; i < queries.length; ++i) {
Query q = qs.newQuery(queries[i]);
sr[i][0] = (SelectResults) q.execute();
}
qs.createIndex("PortFolioID", IndexType.FUNCTIONAL, "ID", "/pos");
ObjectType[] expectedTypes = new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(String.class), new ObjectTypeImpl(String.class), new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }), new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }), new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }), new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }) };
final boolean[] expectedCallback = { false, true, false, false, false, true };
final boolean[] actualCallback = new boolean[queries.length];
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private int i = 0;
public void beforeApplyingProjectionOnFilterEvaluatedResults(Object preProjectionApplied) {
actualCallback[i] = true;
}
public void afterQueryEvaluation(Object result) {
++i;
}
});
for (int i = 0; i < queries.length; ++i) {
Query q = qs.newQuery(queries[i]);
sr[i][1] = (SelectResults) q.execute();
assertEquals(expectedCallback[i], actualCallback[i]);
assertEquals(expectedTypes[i], sr[i][1].getCollectionType().getElementType());
}
CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
use of org.apache.geode.cache.query.internal.types.StructTypeImpl in project geode by apache.
the class CustomerOptimizationsJUnitTest method testProjectionEvaluationDuringIndexResultsWithComplexWhereClause_UNIMPLEMENTED_2.
@Ignore
@Test
public void testProjectionEvaluationDuringIndexResultsWithComplexWhereClause_UNIMPLEMENTED_2() throws QueryException {
QueryService qs = CacheUtils.getQueryService();
Region rgn = CacheUtils.getRegion("/pos");
for (int i = 100; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.setCreateTime(10l);
rgn.put("" + i, pf);
}
String[] queries = new String[] { "select p.status as sts, p as pos from /pos p where (p.ID IN SET( 0,1,2,3,4,5,101,102,103,104,105) AND p.createTime > 9l) OR (p.ID IN SET( 20,30,110,120) AND p.createTime > 7l)" };
SelectResults[][] sr = new SelectResults[queries.length][2];
for (int i = 0; i < queries.length; ++i) {
Query q = qs.newQuery(queries[i]);
sr[i][0] = (SelectResults) q.execute();
}
qs.createIndex("PortFolioID", IndexType.FUNCTIONAL, "ID", "/pos");
qs.createIndex("CreateTime", IndexType.FUNCTIONAL, "createTime", "/pos");
final boolean[] expectedIndexUsed = new boolean[] { true };
final boolean[] actualIndexUsed = new boolean[] { false };
final boolean[] expectedProjectionCallabck = new boolean[] { false };
final boolean[] actualProjectionCallback = new boolean[] { false };
final boolean[] expectedUnionCallback = { true };
final boolean[] actualUnionCallback = new boolean[queries.length];
final boolean[] expectedIntersectionCallback = { false };
final boolean[] actualIntersectionCallback = new boolean[queries.length];
ObjectType[] expectedTypes = new ObjectType[] { new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }) };
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private int i = 0;
public void invokedQueryUtilsUnion(SelectResults r1, SelectResults r2) {
actualUnionCallback[i] = true;
}
public void invokedQueryUtilsIntersection(SelectResults r1, SelectResults r2) {
actualIntersectionCallback[i] = true;
}
public void beforeIndexLookup(Index index, int oper, Object key) {
actualIndexUsed[i] = true;
}
public void beforeApplyingProjectionOnFilterEvaluatedResults(Object preProjectionApplied) {
actualProjectionCallback[i] = true;
}
public void afterQueryEvaluation(Object result) {
++i;
}
});
for (int i = 0; i < queries.length; ++i) {
Query q = qs.newQuery(queries[i]);
sr[i][1] = (SelectResults) q.execute();
assertEquals(expectedUnionCallback[i], actualUnionCallback[i]);
assertEquals(expectedTypes[i], sr[i][1].getCollectionType().getElementType());
assertEquals(expectedIndexUsed[i], actualIndexUsed[i]);
assertEquals(expectedIntersectionCallback[i], actualIntersectionCallback[i]);
assertEquals(expectedProjectionCallabck[i], actualProjectionCallback[i]);
}
CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
use of org.apache.geode.cache.query.internal.types.StructTypeImpl in project geode by apache.
the class CustomerOptimizationsJUnitTest method testBug39851.
@Test
public void testBug39851() throws QueryException {
QueryService qs = CacheUtils.getQueryService();
Region rgn = CacheUtils.getRegion("/pos");
for (int i = 100; i < 200; ++i) {
Portfolio pf = new Portfolio(i);
pf.setCreateTime(10l);
rgn.put("" + i, pf);
}
String[] queries = new String[] { "select p.status as sts, p as pos from /pos p where p.ID IN ( Select x.ID from /pos x where x.ID > 10) " };
SelectResults[][] sr = new SelectResults[queries.length][2];
for (int i = 0; i < queries.length; ++i) {
Query q = qs.newQuery(queries[i]);
sr[i][0] = (SelectResults) q.execute();
}
qs.createIndex("PortFolioID", IndexType.FUNCTIONAL, "ID", "/pos");
final boolean[] expectedIndexUsed = new boolean[] { true, true };
final boolean[] actualIndexUsed = new boolean[] { false, false };
final boolean[] expectedProjectionCallabck = new boolean[] { false, false };
final boolean[] actualProjectionCallback = new boolean[] { false, false };
final boolean[] expectedUnionCallback = { false };
final boolean[] actualUnionCallback = new boolean[queries.length];
final boolean[] expectedIntersectionCallback = { false };
final boolean[] actualIntersectionCallback = new boolean[queries.length];
ObjectType[] expectedTypes = new ObjectType[] { new StructTypeImpl(new String[] { "sts", "pos" }, new ObjectType[] { new ObjectTypeImpl(String.class), new ObjectTypeImpl(Portfolio.class) }) };
QueryObserverHolder.setInstance(new QueryObserverAdapter() {
private int i = 0;
public void invokedQueryUtilsUnion(SelectResults r1, SelectResults r2) {
actualUnionCallback[i] = true;
}
public void invokedQueryUtilsIntersection(SelectResults r1, SelectResults r2) {
actualIntersectionCallback[i] = true;
}
public void beforeIndexLookup(Index index, int oper, Object key) {
actualIndexUsed[i] = true;
}
public void beforeApplyingProjectionOnFilterEvaluatedResults(Object preProjectionApplied) {
actualProjectionCallback[i] = true;
}
public void afterQueryEvaluation(Object result) {
++i;
}
});
for (int i = 0; i < queries.length; ++i) {
Query q = qs.newQuery(queries[i]);
sr[i][1] = (SelectResults) q.execute();
assertEquals(expectedUnionCallback[i], actualUnionCallback[i]);
assertEquals(expectedTypes[i], sr[i][1].getCollectionType().getElementType());
assertEquals(expectedIndexUsed[i], actualIndexUsed[i]);
assertEquals(expectedProjectionCallabck[i], actualProjectionCallback[i]);
assertEquals(expectedIntersectionCallback[i], actualIntersectionCallback[i]);
}
CacheUtils.compareResultsOfWithAndWithoutIndex(sr, this);
}
Aggregations