ORDER BY in Rust Azure SDK (v. 0.19.0) doesn't work

60 Views Asked by At

Query with ORDER BY clause doesn't work when using SDK

  • Azure_sdk version: 0.19.0
  • Azure_core version: 0.19.0
  • Rust version: 1.71
  • MacOS: 14.1

My goal is to get sorted items from NoSQL Cosmos DB after my request.

Here is the part of my Rust code:

let mut query_builder = get_collection_client(collection_name)?
        .query_documents(Query::new(
            "SELECT * FROM dwellings c ORDER BY c.price".to_string())
        )
        .max_item_count(item_count);

But result of the query: server returned error status which will not be retried: 400. If I run this query exactly in Azure CosmosDB Data Explorer, query returns expected result.

1

There are 1 best solutions below

0
Matias Quaranta On

Assuming that the library you are using is this: https://github.com/Azure/azure-sdk-for-rust/tree/main/sdk/data_cosmos

Your query is probably a cross partition query. The HTTP 400 error you are getting has a body and a message and it probably says that the query cannot be executed.

Based on the examples: https://github.com/Azure/azure-sdk-for-rust/blob/main/sdk/data_cosmos/examples/query_document.rs

You can add .query_cross_partition(true) to the query_documents operation, but this would not work for queries with ORDER BY: https://learn.microsoft.com/rest/api/cosmos-db/querying-cosmosdb-resources-using-the-rest-api#queries-that-cannot-be-served-by-gateway

In short, in order for ORDER BY cross-partition queries to work, the client library needs to do the orchestration work, which this library does not seem to have it.

The query should have the scope of a Partition Key Value in order to execute.