Search in sources :

Example 11 with QueryInvalidException

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

the class QCompiler method compileQuery.

/*
   * compile the string into a Query (returns the root CompiledValue)
   */
public CompiledValue compileQuery(String oqlSource) {
    try {
        OQLLexer lexer = new OQLLexer(new StringReader(oqlSource));
        OQLParser parser = new OQLParser(lexer);
        // by default use Unsupported AST class, overridden for supported
        // operators in the grammer proper
        parser.setASTNodeClass("org.apache.geode.cache.query.internal.parse.ASTUnsupported");
        parser.queryProgram();
        GemFireAST n = (GemFireAST) parser.getAST();
        n.compile(this);
    } catch (Exception ex) {
        // GemFire Exception.
        throw new QueryInvalidException(LocalizedStrings.QCompiler_SYNTAX_ERROR_IN_QUERY_0.toLocalizedString(ex.getMessage()), ex);
    }
    Assert.assertTrue(stackSize() == 1, "stack size = " + stackSize());
    return (CompiledValue) pop();
}
Also used : OQLLexer(org.apache.geode.cache.query.internal.parse.OQLLexer) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) StringReader(java.io.StringReader) GemFireAST(org.apache.geode.cache.query.internal.parse.GemFireAST) OQLParser(org.apache.geode.cache.query.internal.parse.OQLParser) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException)

Example 12 with QueryInvalidException

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

the class QCompiler method indexOp.

public void indexOp() {
    // find the List of index expressions and the receiver on the stack
    Object indexParams = pop();
    final CompiledValue rcvr = (CompiledValue) TypeUtils.checkCast(pop(), CompiledValue.class);
    CompiledValue indexExpr = CompiledValue.MAP_INDEX_ALL_KEYS;
    if (indexParams != null) {
        final List indexList = (List) TypeUtils.checkCast(indexParams, List.class);
        if (!isForIndexCompilation && indexList.size() != 1) {
            throw new UnsupportedOperationException(LocalizedStrings.QCompiler_ONLY_ONE_INDEX_EXPRESSION_SUPPORTED.toLocalizedString());
        }
        if (indexList.size() == 1) {
            indexExpr = (CompiledValue) TypeUtils.checkCast(indexList.get(0), CompiledValue.class);
            if (indexExpr.getType() == TOK_COLON) {
                throw new UnsupportedOperationException(LocalizedStrings.QCompiler_RANGES_NOT_SUPPORTED_IN_INDEX_OPERATORS.toLocalizedString());
            }
            indexExpr = (CompiledValue) TypeUtils.checkCast(indexList.get(0), CompiledValue.class);
            push(new CompiledIndexOperation(rcvr, indexExpr));
        } else {
            assert this.isForIndexCompilation;
            MapIndexable mi = new MapIndexOperation(rcvr, indexList);
            push(mi);
        }
    } else {
        if (!this.isForIndexCompilation) {
            throw new QueryInvalidException(LocalizedStrings.QCompiler_SYNTAX_ERROR_IN_QUERY_0.toLocalizedString("* use incorrect"));
        }
        push(new CompiledIndexOperation(rcvr, indexExpr));
    }
}
Also used : QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 13 with QueryInvalidException

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

the class QCompiler method select.

public void select(Map<Integer, Object> queryComponents) {
    CompiledValue limit = null;
    Object limitObject = queryComponents.remove(OQLLexerTokenTypes.LIMIT);
    if (limitObject instanceof Integer) {
        limit = new CompiledLiteral(limitObject);
    } else {
        limit = (CompiledBindArgument) limitObject;
    }
    List<CompiledSortCriterion> orderByAttrs = (List<CompiledSortCriterion>) queryComponents.remove(OQLLexerTokenTypes.LITERAL_order);
    List iterators = (List) queryComponents.remove(OQLLexerTokenTypes.LITERAL_from);
    List projAttrs = (List) queryComponents.remove(OQLLexerTokenTypes.PROJECTION_ATTRS);
    if (projAttrs == null) {
        // remove any * or all attribute
        queryComponents.remove(OQLLexerTokenTypes.TOK_STAR);
        queryComponents.remove(OQLLexerTokenTypes.LITERAL_all);
    }
    // "COUNT" or null
    /*
     * String aggrExpr = (String) queryComponents .remove(OQLLexerTokenTypes.LITERAL_count);
     */
    // "DISTINCT" or null
    String distinct = (String) queryComponents.remove(OQLLexerTokenTypes.LITERAL_distinct);
    List<String> hints = null;
    Object hintObject = queryComponents.remove(OQLLexerTokenTypes.LITERAL_hint);
    if (hintObject != null) {
        hints = (List<String>) hintObject;
    }
    List<CompiledValue> groupByClause = (List<CompiledValue>) queryComponents.remove(OQLLexerTokenTypes.LITERAL_group);
    // whatever remains , treat it as where
    // whereClause
    CompiledValue where = null;
    if (queryComponents.size() == 1) {
        where = (CompiledValue) queryComponents.values().iterator().next();
    } else if (queryComponents.size() > 1) {
        throw new QueryInvalidException("Unexpected/unsupported query clauses found");
    }
    LinkedHashMap<Integer, CompiledAggregateFunction> aggMap = identifyAggregateExpressions(projAttrs);
    boolean isCountOnly = checkForCountOnly(aggMap, projAttrs, groupByClause);
    if (isCountOnly) {
        projAttrs = null;
    }
    CompiledSelect select = createSelect(distinct != null, isCountOnly, where, iterators, projAttrs, orderByAttrs, limit, hints, groupByClause, aggMap);
    push(select);
}
Also used : QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 14 with QueryInvalidException

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

the class Query651 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();
    long compiledQueryId = 0;
    Object[] queryParams = null;
    try {
        if (clientMessage.getMessageType() == MessageType.QUERY_WITH_PARAMETERS) {
            // Query with parameters supported from 6.6 onwards.
            // Number of parameters.
            int params = clientMessage.getPart(1).getInt();
            // In case of native client there will be extra two parameters at 2 and 3 index.
            int paramStartIndex = 2;
            if (clientMessage.getNumberOfParts() > (1 + /* type */
            1 + /* query string */
            1 + /* params length */
            params)) {
                int timeout = clientMessage.getPart(3).getInt();
                serverConnection.setRequestSpecificTimeout(timeout);
                paramStartIndex = 4;
            }
            // Get the query execution parameters.
            queryParams = new Object[params];
            for (int i = 0; i < queryParams.length; i++) {
                queryParams[i] = clientMessage.getPart(i + paramStartIndex).getObject();
            }
        } else {
            // need to take care while adding new message
            if (clientMessage.getNumberOfParts() == 3) {
                int timeout = clientMessage.getPart(2).getInt();
                serverConnection.setRequestSpecificTimeout(timeout);
            }
        }
    } catch (ClassNotFoundException cne) {
        throw new QueryInvalidException(cne.getMessage() + queryString);
    }
    if (logger.isDebugEnabled()) {
        logger.debug("{}: Received query request from {} queryString: {}{}", serverConnection.getName(), serverConnection.getSocketString(), queryString, (queryParams != null ? (" with num query parameters :" + queryParams.length) : ""));
    }
    try {
        // Create query
        QueryService queryService = serverConnection.getCachedRegionHelper().getCache().getLocalQueryService();
        org.apache.geode.cache.query.Query query = null;
        if (queryParams != null) {
            // Its a compiled query.
            CacheClientNotifier ccn = serverConnection.getAcceptor().getCacheClientNotifier();
            query = ccn.getCompiledQuery(queryString);
            if (query == null) {
                // This is first time the query is seen by this server.
                query = queryService.newQuery(queryString);
                ccn.addCompiledQuery((DefaultQuery) query);
            }
            ccn.getStats().incCompiledQueryUsedCount(1);
            ((DefaultQuery) query).setLastUsed(true);
        } else {
            query = queryService.newQuery(queryString);
        }
        Set regionNames = ((DefaultQuery) query).getRegionsInQuery(queryParams);
        // Authorization check
        QueryOperationContext queryContext = null;
        AuthorizeRequest authzRequest = serverConnection.getAuthzRequest();
        if (authzRequest != null) {
            queryContext = authzRequest.queryAuthorize(queryString, regionNames, queryParams);
            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);
                }
            }
        }
        processQueryUsingParams(clientMessage, query, queryString, regionNames, start, null, queryContext, serverConnection, true, queryParams);
    } catch (QueryInvalidException e) {
        throw new QueryInvalidException(e.getMessage() + queryString);
    }
}
Also used : DefaultQuery(org.apache.geode.cache.query.internal.DefaultQuery) Set(java.util.Set) CacheClientNotifier(org.apache.geode.internal.cache.tier.sockets.CacheClientNotifier) AuthorizeRequest(org.apache.geode.internal.security.AuthorizeRequest) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) QueryOperationContext(org.apache.geode.cache.operations.QueryOperationContext) QueryService(org.apache.geode.cache.query.QueryService)

Example 15 with QueryInvalidException

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

the class QCompiler method compileImports.

/**
   * Processes import statements only. This compiler instance remembers the imports and can be used
   * to compile other strings with this context info
   */
public void compileImports(String imports) {
    try {
        OQLLexer lexer = new OQLLexer(new StringReader(imports));
        OQLParser parser = new OQLParser(lexer);
        // by default use Unsupported AST class, overridden for supported
        // operators in the grammer proper
        parser.setASTNodeClass("org.apache.geode.cache.query.internal.parse.ASTUnsupported");
        parser.loneImports();
        GemFireAST n = (GemFireAST) parser.getAST();
        n.compile(this);
    } catch (Exception ex) {
        // GemFire Exception.
        throw new QueryInvalidException(LocalizedStrings.QCompiler_SYNTAX_ERROR_IN_QUERY_0.toLocalizedString(ex.getMessage()), ex);
    }
    Assert.assertTrue(stackSize() == 0, "stack size = " + stackSize() + ";stack=" + this.stack);
}
Also used : OQLLexer(org.apache.geode.cache.query.internal.parse.OQLLexer) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) StringReader(java.io.StringReader) GemFireAST(org.apache.geode.cache.query.internal.parse.GemFireAST) OQLParser(org.apache.geode.cache.query.internal.parse.OQLParser) FunctionDomainException(org.apache.geode.cache.query.FunctionDomainException) NameResolutionException(org.apache.geode.cache.query.NameResolutionException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) TypeMismatchException(org.apache.geode.cache.query.TypeMismatchException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException)

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