question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cosmos async query_items and query_conflicts break if partition_key is specified

See original GitHub issue
  • Package Name: azure-cosmos
  • Package Version: 4.3.0b2
  • Operating System: Linux
  • Python Version: 3.9.7

Describe the bug

azure.cosmos.aio.container.query_items(sql, partition_key=x) fails with the error

RuntimeWarning: coroutine 'ContainerProxy._set_partition_key' was never awaited

when run on a single partition.

It runs correctly when no partition key is given and instead enable_cross_partition_query is set.

The same error is present in query_conflicts(), though I haven’t tested it.

To reproduce

Run the example for async container.query_items() in <https://github.com/Azure/azure-sdk-for-python/blob/main/sdk/cosmos/azure-cosmos/samples/nonpartitioned_container_operations_async.py`.

This executes the query with:

[item async for item in container.query_items(sql, enable_cross_partition_query=True)]

and returns the list of items, as expected.

However when trying the same query for a single partition:

[item async for item in container.query_items(sql, partition_key="test")]

the result is an async error while setting the partition key:

RuntimeWarning: coroutine 'ContainerProxy._set_partition_key' was never awaited

Root cause

The problem arises in lines 348-349 of azure/cosmos/aio/container.py, in the function query_items:

        if partition_key is not None:
            feed_options["partitionKey"] = self._set_partition_key(partition_key)

This _set_partition_key() function is actually an async function. So the two lines should be:

        if partition_key is not None:
            feed_options["partitionKey"] = await self._set_partition_key(partition_key)

This version of the code is already present in read_item() and delete_item(). It is wrong in query_items() and also query_conflicts().

This in turn makes the query_items() function async. Thus line 285 needs the async keyword added:

    async def query_items(

The same changes need to be made to query_conflicts() at lines 619 and 644.

Finally, that means the examples now need an await as well:

# From
[item async for item in container.query_items(sql, partition_key="test")]
[item async for item in container.query_items(sql, enable_cross_partition_query=True)]
# To
[item async for item in await container.query_items(sql, partition_key="test")]
[item async for item in await container.query_items(sql, enable_cross_partition_query=True)]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Rodrigosszcommented, Feb 1, 2022

@swathipil , I’m not the Python SDK anymore. @gahl-levy is the new PM and @simorenoh is the new developer.

0reactions
simorenohcommented, Mar 10, 2022

@stevesimmons The change has been released today with the new beta version, thank you again for pointing this out and for using our SDK!

Read more comments on GitHub >

github_iconTop Results From Across the Web

CosmosAsyncContainer Class | Microsoft Learn
Deletes all items in the Container with the specified partitionKey value. ... Get the id of the CosmosAsyncContainer. ... queryConflicts(String query).
Read more >
azure-sdk-for-java/CosmosAsyncContainer.java at main
* Deletes all items in the Container with the specified partitionKey value. * Starts an asynchronous Cosmos DB background operation which deletes all...
Read more >
Index (Azure SDK for Java Reference Documentation) - NET
Builds a cosmos async client with the provided properties ... Builds a new instance of the ChangeFeedProcessor with the specified configuration.
Read more >
CosmosAsyncContainer (Azure SDK for Java ... - javadoc.io
Gets a CosmosAsyncScripts using the current container as context. CosmosPagedFlux<CosmosConflictProperties>, queryConflicts(String query). Queries all the ...
Read more >
CosmosContainer (Async SDK for SQL API of Azure Cosmos ...
reactor.core.publisher.Flux<FeedResponse<CosmosItemProperties>>, queryItems(SqlQuerySpec querySpec). Query for documents in a items in a container After ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found