Azure / azure-cosmosdb-java

Java Async SDK for SQL API of Azure Cosmos DB
MIT License
54 stars 61 forks source link

Offset and limit in query return wrong result in Driver V3 #265

Closed apescione closed 4 years ago

apescione commented 5 years ago

Using OFFSET and LIMIT in query, Driver V3 returns wrong result, instead the V2 returns correct one. I've tested the following version: V3 version 3.2.1 V2 version 2.6.1

The example query is: SELECT c.id FROM c order by c.id offset 7 limit 4

Both V2 and Azure portal reply with the same correct result:

{"id":"ac0c5110-4913-4f4a-8c51-104913cf4a8c"}
{"id":"c821d14f-bca6-4a6b-a1d1-4fbca6da6b78"}
{"id":"dfcead67-3818-4f8b-8ead-673818cf8b1b"}
{"id":"f55ea967-493e-4f66-9ea9-67493e8f664f"}

Instead, SDK V3 returns this RECORDS plus other 7 records before:

{"id":"1a38568d-4e81-4381-b856-8d4e818381c6"}
{"id":"28ebc33a-38f0-4b45-abc3-3a38f0bb4581"}
{"id":"4bc01d05-eb89-48aa-801d-05eb8928aa64"}
{"id":"70df8949-2f9a-46d5-9f89-492f9af6d5eb"}
{"id":"8d5fe800-e51a-4bcd-9fe8-00e51adbcd45"}
{"id":"9731de50-a983-49bd-b1de-50a983d9bd14"}
{"id":"9e0a9981-b821-4790-8a99-81b821c790c0"}
{"id":"ac0c5110-4913-4f4a-8c51-104913cf4a8c"}
{"id":"c821d14f-bca6-4a6b-a1d1-4fbca6da6b78"}
{"id":"dfcead67-3818-4f8b-8ead-673818cf8b1b"}
{"id":"f55ea967-493e-4f66-9ea9-67493e8f664f"}

Here the Code snipped for V2:

 AsyncDocumentClient client = new AsyncDocumentClient.Builder()
        .withServiceEndpoint(uri)
        .withMasterKeyOrResourceToken(key)
        .build();
    try {
      FeedOptions options = new FeedOptions();
      options.setEnableCrossPartitionQuery(true);
      client
          .queryDocuments("/dbs/dbtest/colls/colletest", "SELECT c.id FROM c order by c.id offset 7 limit 4", options)
          .toBlocking().single().getResults()
          .forEach(System.out::println);

    } finally {
      client.close();
    }

And Here the code of V3 (That probably there is a bug):

 try (CosmosClient client = CosmosClient.builder()
        .endpoint(uri)
        .key(key)
        .build()) {
      FeedOptions options = new FeedOptions();
      options.enableCrossPartitionQuery(true);
      client.getDatabase(dbName).getContainer(collectionId)
          .queryItems("SELECT c.id FROM c order by c.id offset 7 limit 4", options).blockFirst().results()
          .stream()
           .collect(Collectors.toList())
          .forEach(System.out::println);
    }

At the moment a workaround is to get the LAST N results (where the N is limit number)

Could these issues be linking to #256?

kushagraThapar commented 5 years ago

@apescione , thanks for reporting this, we are aware of the issue and trying to fix it as soon as possible. Will update this thread once its fixed.

mbhaskar commented 5 years ago

Offset limit is not totally supported in V3. Is related to this https://github.com/Azure/azure-cosmosdb-java/issues/126

mbhaskar commented 4 years ago

Support added in this PR https://github.com/Azure/azure-sdk-for-java/pull/5365