use of org.apache.geode.cache.query.QueryInvalidException in project geode by apache.
the class CompiledGroupBySelectJUnitTest method testUnsupportedQuery.
@Test
public void testUnsupportedQuery() throws Exception {
String queryStr = "select count(*) from /portfolio pf where pf.ID > 0 group by pf.shortID";
QueryService qs = CacheUtils.getQueryService();
try {
DefaultQuery query = (DefaultQuery) qs.newQuery(queryStr);
fail("query creation should have failed");
} catch (QueryInvalidException qie) {
assertTrue(qie.toString().indexOf(LocalizedStrings.DefaultQuery_GROUP_BY_COL_ABSENT_IN_PROJ.toLocalizedString()) != -1);
}
}
use of org.apache.geode.cache.query.QueryInvalidException in project geode by apache.
the class QueryTraceJUnitTest method testQueryFailLocalRegionWithSmallTraceSuffixNoComments.
@Test
public void testQueryFailLocalRegionWithSmallTraceSuffixNoComments() throws Exception {
String suffix = "<trace> ";
// Create Partition Region
AttributesFactory af = new AttributesFactory();
af.setScope(Scope.LOCAL);
region = CacheUtils.createRegion("portfolio", af.create(), false);
if (region.size() == 0) {
for (int i = 1; i <= 100; i++) {
region.put(Integer.toString(i), new Portfolio(i, i));
}
}
assertEquals(100, region.size());
qs = CacheUtils.getQueryService();
keyIndex1 = (IndexProtocol) qs.createIndex(INDEX_NAME, IndexType.FUNCTIONAL, "ID", "/portfolio ");
assertTrue(keyIndex1 instanceof CompactRangeIndex);
try {
Query query = qs.newQuery(queryStr + suffix);
} catch (Exception e) {
if (!(e instanceof QueryInvalidException)) {
fail("Test Failed: Query is invalid but exception was not thrown!");
}
}
}
use of org.apache.geode.cache.query.QueryInvalidException in project geode by apache.
the class CompiledGroupBySelect method replaceAggregateFunctionInProjection.
private void replaceAggregateFunctionInProjection() {
// Extract the parameter compiledValues out of aggregate functions &
// modify the projection
// attributes to have that instead. Empty out the groupByList
// Create orderby attribute out of group by
int bitStart = 0;
for (CompiledAggregateFunction aggFunc : this.aggregateFunctions) {
int index = this.aggregateColsPos.nextSetBit(bitStart);
bitStart = index + 1;
CompiledValue param = aggFunc.getParameter();
if (param == null && aggFunc.getFunctionType() == OQLLexerTokenTypes.COUNT) {
// * case of *, substitue a dummy parameter of compiled literal = 0 to
// satisfy the code
param = new CompiledLiteral(0);
} else if (param == null) {
throw new QueryInvalidException("aggregate function passed invalid parameter");
}
Object[] projAtt = (Object[]) this.projAttrs.get(index);
projAtt[1] = param;
}
}
use of org.apache.geode.cache.query.QueryInvalidException in project geode by apache.
the class QueryDataFunction method queryData.
public static Object queryData(final String query, final String members, final int limit, final boolean zipResult, final int queryResultSetLimit, final int queryCollectionsDepth) throws Exception {
if (query == null || query.isEmpty()) {
return new JsonisedErrorMessage(ManagementStrings.QUERY__MSG__QUERY_EMPTY.toLocalizedString()).toString();
}
Set<DistributedMember> inputMembers = null;
if (StringUtils.isNotBlank(members)) {
inputMembers = new HashSet<>();
StringTokenizer st = new StringTokenizer(members, ",");
while (st.hasMoreTokens()) {
String member = st.nextToken();
DistributedMember distributedMember = BeanUtilFuncs.getDistributedMemberByNameOrId(member);
inputMembers.add(distributedMember);
if (distributedMember == null) {
return new JsonisedErrorMessage(ManagementStrings.QUERY__MSG__INVALID_MEMBER.toLocalizedString(member)).toString();
}
}
}
InternalCache cache = (InternalCache) CacheFactory.getAnyInstance();
try {
SystemManagementService service = (SystemManagementService) ManagementService.getExistingManagementService(cache);
Set<String> regionsInQuery = compileQuery(cache, query);
// Validate region existence
if (regionsInQuery.size() > 0) {
for (String regionPath : regionsInQuery) {
DistributedRegionMXBean regionMBean = service.getDistributedRegionMXBean(regionPath);
if (regionMBean == null) {
return new JsonisedErrorMessage(ManagementStrings.QUERY__MSG__REGIONS_NOT_FOUND.toLocalizedString(regionPath)).toString();
} else {
Set<DistributedMember> associatedMembers = DataCommands.getRegionAssociatedMembers(regionPath, cache, true);
if (inputMembers != null && inputMembers.size() > 0) {
if (!associatedMembers.containsAll(inputMembers)) {
return new JsonisedErrorMessage(ManagementStrings.QUERY__MSG__REGIONS_NOT_FOUND_ON_MEMBERS.toLocalizedString(regionPath)).toString();
}
}
}
}
} else {
return new JsonisedErrorMessage(ManagementStrings.QUERY__MSG__INVALID_QUERY.toLocalizedString("Region mentioned in query probably missing /")).toString();
}
// Validate
if (regionsInQuery.size() > 1 && inputMembers == null) {
for (String regionPath : regionsInQuery) {
DistributedRegionMXBean regionMBean = service.getDistributedRegionMXBean(regionPath);
if (regionMBean.getRegionType().equals(DataPolicy.PARTITION.toString()) || regionMBean.getRegionType().equals(DataPolicy.PERSISTENT_PARTITION.toString())) {
return new JsonisedErrorMessage(ManagementStrings.QUERY__MSG__JOIN_OP_EX.toLocalizedString()).toString();
}
}
}
String randomRegion = regionsInQuery.iterator().next();
Set<DistributedMember> associatedMembers = // First
DataCommands.getQueryRegionsAssociatedMembers(regionsInQuery, cache, false);
if (associatedMembers != null && associatedMembers.size() > 0) {
Object[] functionArgs = new Object[6];
if (inputMembers != null && inputMembers.size() > 0) {
// on input
// members
functionArgs[DISPLAY_MEMBERWISE] = true;
functionArgs[QUERY] = query;
functionArgs[REGION] = randomRegion;
functionArgs[LIMIT] = limit;
functionArgs[QUERY_RESULTSET_LIMIT] = queryResultSetLimit;
functionArgs[QUERY_COLLECTIONS_DEPTH] = queryCollectionsDepth;
return callFunction(functionArgs, inputMembers, zipResult);
} else {
// Query on any random member
functionArgs[DISPLAY_MEMBERWISE] = false;
functionArgs[QUERY] = query;
functionArgs[REGION] = randomRegion;
functionArgs[LIMIT] = limit;
functionArgs[QUERY_RESULTSET_LIMIT] = queryResultSetLimit;
functionArgs[QUERY_COLLECTIONS_DEPTH] = queryCollectionsDepth;
return callFunction(functionArgs, associatedMembers, zipResult);
}
} else {
return new JsonisedErrorMessage(ManagementStrings.QUERY__MSG__REGIONS_NOT_FOUND.toLocalizedString(regionsInQuery.toString())).toString();
}
} catch (QueryInvalidException qe) {
return new JsonisedErrorMessage(ManagementStrings.QUERY__MSG__INVALID_QUERY.toLocalizedString(qe.getMessage())).toString();
}
}
use of org.apache.geode.cache.query.QueryInvalidException in project geode by apache.
the class QueryDataFunction method compileQuery.
/**
* Compile the query and return a set of regions involved in the query It throws an
* QueryInvalidException if the query is not proper
*
* @param cache current cache
* @param query input query
*
* @return a set of regions involved in the query
*/
private static Set<String> compileQuery(final InternalCache cache, final String query) throws QueryInvalidException {
QCompiler compiler = new QCompiler();
Set<String> regionsInQuery;
try {
CompiledValue compiledQuery = compiler.compileQuery(query);
Set<String> regions = new HashSet<>();
compiledQuery.getRegionsInQuery(regions, null);
regionsInQuery = Collections.unmodifiableSet(regions);
return regionsInQuery;
} catch (QueryInvalidException qe) {
logger.error("{} Failed, Error {}", query, qe.getMessage(), qe);
throw qe;
}
}
Aggregations