Search in sources :

Example 26 with QueryInvalidException

use of org.apache.geode.cache.query.QueryInvalidException in project geode by apache.

the class OrderByComparatorJUnitTest method testUnsupportedOrderByForPR.

@Test
public void testUnsupportedOrderByForPR() throws Exception {
    String[] unsupportedQueries = { "select distinct p.status from /portfolio1 p order by p.status, p.ID" };
    Object[][] r = new Object[unsupportedQueries.length][2];
    QueryService qs;
    qs = CacheUtils.getQueryService();
    Position.resetCounter();
    // Create Regions
    PartitionAttributesFactory paf = new PartitionAttributesFactory();
    AttributesFactory af = new AttributesFactory();
    af.setPartitionAttributes(paf.create());
    Region r1 = CacheUtils.createRegion("portfolio1", af.create(), false);
    for (int i = 0; i < 50; i++) {
        r1.put(new Portfolio(i), new Portfolio(i));
    }
    for (int i = 0; i < unsupportedQueries.length; i++) {
        Query q = null;
        CacheUtils.getLogger().info("Executing query: " + unsupportedQueries[i]);
        q = CacheUtils.getQueryService().newQuery(unsupportedQueries[i]);
        try {
            r[i][0] = q.execute();
            fail("The query should have thrown exception");
        } catch (QueryInvalidException qe) {
        // ok
        }
    }
}
Also used : PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) Query(org.apache.geode.cache.query.Query) QueryService(org.apache.geode.cache.query.QueryService) Portfolio(org.apache.geode.cache.query.data.Portfolio) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) Region(org.apache.geode.cache.Region) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 27 with QueryInvalidException

use of org.apache.geode.cache.query.QueryInvalidException in project geode by apache.

the class QueryTraceJUnitTest method testQueryFailLocalRegionWithSmallTracePrefixNoSpace.

@Test
public void testQueryFailLocalRegionWithSmallTracePrefixNoSpace() throws Exception {
    String prefix = "<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(prefix + queryStr);
    } 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 28 with QueryInvalidException

use of org.apache.geode.cache.query.QueryInvalidException in project geode by apache.

the class PartitionedRegionDataStore method executeOnDataStore.

public void executeOnDataStore(final Set localKeys, final Function function, final Object object, final int prid, final Set<Integer> bucketSet, final boolean isReExecute, final PartitionedRegionFunctionStreamingMessage msg, long time, ServerConnection servConn, int transactionID) {
    if (!areAllBucketsHosted(bucketSet)) {
        throw new BucketMovedException(LocalizedStrings.FunctionService_BUCKET_MIGRATED_TO_ANOTHER_NODE.toLocalizedString());
    }
    final DM dm = this.partitionedRegion.getDistributionManager();
    ResultSender resultSender = new PartitionedRegionFunctionResultSender(dm, this.partitionedRegion, time, msg, function, bucketSet);
    final RegionFunctionContextImpl prContext = new RegionFunctionContextImpl(function.getId(), this.partitionedRegion, object, localKeys, ColocationHelper.constructAndGetAllColocatedLocalDataSet(this.partitionedRegion, bucketSet), bucketSet, resultSender, isReExecute);
    FunctionStats stats = FunctionStats.getFunctionStats(function.getId(), dm.getSystem());
    try {
        long start = stats.startTime();
        stats.startFunctionExecution(function.hasResult());
        if (logger.isDebugEnabled()) {
            logger.debug("Executing Function: {} on Remote Node with context: ", function.getId(), prContext);
        }
        function.execute(prContext);
        stats.endFunctionExecution(start, function.hasResult());
    } catch (FunctionException functionException) {
        if (logger.isDebugEnabled()) {
            logger.debug("FunctionException occurred on remote node while executing Function: {}", function.getId(), functionException);
        }
        stats.endFunctionExecutionWithException(function.hasResult());
        if (functionException.getCause() instanceof QueryInvalidException) {
            // create a new FunctionException on the original one's message (not cause).
            throw new FunctionException(functionException.getLocalizedMessage());
        }
        throw functionException;
    }
}
Also used : RegionFunctionContextImpl(org.apache.geode.internal.cache.execute.RegionFunctionContextImpl) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) FunctionException(org.apache.geode.cache.execute.FunctionException) BucketMovedException(org.apache.geode.internal.cache.execute.BucketMovedException) DM(org.apache.geode.distributed.internal.DM) FunctionStats(org.apache.geode.internal.cache.execute.FunctionStats) PartitionedRegionFunctionResultSender(org.apache.geode.internal.cache.execute.PartitionedRegionFunctionResultSender) ResultSender(org.apache.geode.cache.execute.ResultSender) PartitionedRegionFunctionResultSender(org.apache.geode.internal.cache.execute.PartitionedRegionFunctionResultSender)

Example 29 with QueryInvalidException

use of org.apache.geode.cache.query.QueryInvalidException in project geode by apache.

the class Query method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, InterruptedException {
    // Based on MessageType.DESTROY
    // Added by gregp 10/18/05
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    serverConnection.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
    // Retrieve the data from the message parts
    String queryString = clientMessage.getPart(0).getString();
    if (clientMessage.getNumberOfParts() == 3) {
        int timeout = clientMessage.getPart(2).getInt();
        serverConnection.setRequestSpecificTimeout(timeout);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Received query request from {} queryString: {}", serverConnection.getName(), serverConnection.getSocketString(), queryString);
    }
    try {
        // Create query
        QueryService queryService = serverConnection.getCachedRegionHelper().getCache().getLocalQueryService();
        org.apache.geode.cache.query.Query query = queryService.newQuery(queryString);
        Set regionNames = ((DefaultQuery) query).getRegionsInQuery(null);
        // Authorization check
        QueryOperationContext queryContext = null;
        AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
        if (authzRequest != null) {
            queryContext = authzRequest.queryAuthorize(queryString, regionNames);
            String newQueryString = queryContext.getQuery();
            if (queryString != null && !queryString.equals(newQueryString)) {
                query = queryService.newQuery(newQueryString);
                queryString = newQueryString;
                regionNames = queryContext.getRegionNames();
                if (regionNames == null) {
                    regionNames = ((DefaultQuery) query).getRegionsInQuery(null);
                }
            }
        }
        processQuery(clientMessage, query, queryString, regionNames, start, null, queryContext, serverConnection, true);
    } catch (QueryInvalidException e) {
        throw new QueryInvalidException(e.getMessage() + queryString);
    } catch (QueryExecutionLowMemoryException e) {
        writeQueryResponseException(clientMessage, e, serverConnection);
    }
}
Also used : Set(java.util.Set) DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) QueryExecutionLowMemoryException(org.apache.geode.cache.query.QueryExecutionLowMemoryException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) QueryOperationContext(org.apache.geode.cache.operations.QueryOperationContext) QueryService(org.apache.geode.cache.query.QueryService)

Example 30 with QueryInvalidException

use of org.apache.geode.cache.query.QueryInvalidException in project geode by apache.

the class CompiledSelect method transformGroupByIfPossible.

/**
   * Transforms the group by clause into distinct order by clause, if possible
   */
private void transformGroupByIfPossible(ExecutionContext context) throws AmbiguousNameException, TypeMismatchException, NameResolutionException {
    // for time being assume that the group by cols are explicitly mentioned in proj
    if (this.groupBy != null) {
        List projAttribs = this.projAttrs;
        if (projAttribs == null) {
            projAttribs = new ArrayList();
            List currentIters = context.getCurrentIterators();
            for (Object o : currentIters) {
                RuntimeIterator rIter = (RuntimeIterator) o;
                String name = rIter.getName();
                projAttribs.add(new Object[] { name, rIter });
            }
        }
        if (projAttribs != null && projAttribs.size() != this.groupBy.size()) {
            throw new QueryInvalidException(LocalizedStrings.DefaultQuery_PROJ_COL_ABSENT_IN_GROUP_BY.toLocalizedString() + " or " + LocalizedStrings.DefaultQuery_GROUP_BY_COL_ABSENT_IN_PROJ.toLocalizedString());
        }
        boolean shouldTransform = true;
        StringBuilder lhsBuffer = new StringBuilder();
        StringBuilder rhsBuffer = new StringBuilder();
        outer: for (int i = 0; i < projAttribs.size(); ++i) {
            Object[] prj = (Object[]) TypeUtils.checkCast(projAttribs.get(i), Object[].class);
            CompiledValue groupByAttr = this.groupBy.get(i);
            if (prj[0] != null) {
                if (groupByAttr instanceof CompiledID) {
                    if (prj[0].equals(((CompiledID) groupByAttr).getId())) {
                        lhsBuffer.delete(0, lhsBuffer.length());
                        rhsBuffer.delete(0, rhsBuffer.length());
                        continue;
                    }
                }
            }
            CompiledValue cvProj = (CompiledValue) TypeUtils.checkCast(prj[1], CompiledValue.class);
            cvProj.generateCanonicalizedExpression(lhsBuffer, context);
            groupByAttr.generateCanonicalizedExpression(rhsBuffer, context);
            if (lhsBuffer.length() == rhsBuffer.length()) {
                for (int indx = 0; indx < lhsBuffer.length(); ++indx) {
                    if (lhsBuffer.charAt(indx) != rhsBuffer.charAt(indx)) {
                        shouldTransform = false;
                        break outer;
                    }
                }
            } else {
                shouldTransform = false;
                break;
            }
            lhsBuffer.delete(0, lhsBuffer.length());
            rhsBuffer.delete(0, rhsBuffer.length());
        }
        // for now check if order by is null
        if (shouldTransform && this.orderByAttrs == null) {
            this.modifyGroupByToOrderBy(true, context);
        } else {
            throw new QueryInvalidException(LocalizedStrings.DefaultQuery_PROJ_COL_ABSENT_IN_GROUP_BY.toLocalizedString() + " or " + LocalizedStrings.DefaultQuery_GROUP_BY_COL_ABSENT_IN_PROJ.toLocalizedString());
        }
    }
}
Also used : QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) PdxString(org.apache.geode.pdx.internal.PdxString)

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