Search in sources :

Example 6 with QueryInvalidException

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);
    }
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 7 with QueryInvalidException

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!");
        }
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CompactRangeIndex(org.apache.geode.cache.query.internal.index.CompactRangeIndex) Query(org.apache.geode.cache.query.Query) Portfolio(org.apache.geode.cache.query.data.Portfolio) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 8 with QueryInvalidException

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;
    }
}
Also used : QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException)

Example 9 with QueryInvalidException

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();
    }
}
Also used : QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) InternalCache(org.apache.geode.internal.cache.InternalCache) DistributedRegionMXBean(org.apache.geode.management.DistributedRegionMXBean) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) StringTokenizer(java.util.StringTokenizer) DistributedMember(org.apache.geode.distributed.DistributedMember) GfJsonObject(org.apache.geode.management.internal.cli.json.GfJsonObject)

Example 10 with QueryInvalidException

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;
    }
}
Also used : QCompiler(org.apache.geode.cache.query.internal.QCompiler) CompiledValue(org.apache.geode.cache.query.internal.CompiledValue) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) HashSet(java.util.HashSet)

Aggregations

QueryInvalidException (org.apache.geode.cache.query.QueryInvalidException)36 Test (org.junit.Test)12 QueryService (org.apache.geode.cache.query.QueryService)11 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)11 List (java.util.List)7 TypeMismatchException (org.apache.geode.cache.query.TypeMismatchException)7 ArrayList (java.util.ArrayList)6 FunctionDomainException (org.apache.geode.cache.query.FunctionDomainException)6 NameResolutionException (org.apache.geode.cache.query.NameResolutionException)6 Query (org.apache.geode.cache.query.Query)6 AttributesFactory (org.apache.geode.cache.AttributesFactory)5 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)5 DefaultQuery (org.apache.geode.cache.query.internal.DefaultQuery)5 StringReader (java.io.StringReader)4 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)4 Region (org.apache.geode.cache.Region)4 SelectResults (org.apache.geode.cache.query.SelectResults)4 Portfolio (org.apache.geode.cache.query.data.Portfolio)4 Set (java.util.Set)3 RegionDestroyedException (org.apache.geode.cache.RegionDestroyedException)3