microsoft / spring-data-cosmosdb

Access data with Azure Cosmos DB
MIT License
94 stars 64 forks source link

Major bug in AbstractQueryGenerator #537

Closed mkarczewski85 closed 4 years ago

mkarczewski85 commented 4 years ago

Method generateBetween() of AbstractQueryGenerator class gives the same parameters names ("start", "end") for every Criteria of type BETWEEN in the query. Their names aren't unique, so when trying to execute query you get:

com.azure.data.cosmos.CosmosClientException: Gateway Failed to Retrieve Query Plan: Message: {"Errors":["Invalid query. Specified duplicate parameter name '@start'."]}

You cannot perform more complex queries which includes more than one Criteria of type BEETWEEN. You can easily fix by concatenating value of criteria.getSubject() with "start" nad "end". After that parameters for query will be unique. Effect should look like this:

private String generateBetween(@NonNull Criteria criteria, @NonNull List<Pair<String, Object>> parameters) {
        final String subject = criteria.getSubject();
        final Object value1 = toCosmosDbValue(criteria.getSubjectValues().get(0));
        final Object value2 = toCosmosDbValue(criteria.getSubjectValues().get(1));
        final String subject1 = subject + "start"; // names of parameters should be unique 
        final String subject2 = subject + "end"; // 
        final String parameter1 = generateQueryParameter(subject1);
        final String parameter2 = generateQueryParameter(subject2);
        final String keyword = criteria.getType().getSqlKeyword();

        parameters.add(Pair.with(parameter1, value1));
        parameters.add(Pair.with(parameter2, value2));

        return String.format("(r.%s %s @%s AND @%s)", subject, keyword, parameter1, parameter2);
    }
kushagraThapar commented 4 years ago

@mkarczewski85 - thanks for reporting this issue. We highly appreciate these fixes by developers like you from open source community. Would you care to create a PR for this fix ? Currently master branch points to v2.3.x release train. If you are using v2.2.x or v2.1.x versions, please checkout from branch which represents these versions.

mkarczewski85 commented 4 years ago

@kushagraThapar thank you for replying. I've just created PR.