Skip to content

Commit e4d4839

Browse files
authored
[Cosmos] query_items with feed_range and continuation bug fix (#43700)
* changes, changelog * Update CHANGELOG.md * pylint, mypy, tests, samples, comments * move logic * Update _read_items_helper.py * forgot to actually move the logic * Update _base.py
1 parent 98c340b commit e4d4839

14 files changed

+202
-18
lines changed

sdk/cosmos/azure-cosmos/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
### 4.14.1 (Unreleased)
44

55
#### Features Added
6+
* Added merge support. See [PR 42924](https://github.com/Azure/azure-sdk-for-python/pull/42924).
67

78
#### Breaking Changes
89

910
#### Bugs Fixed
1011
* Fixed bug where customer provided excluded region was not always being honored during certain transient failures. See [PR 43602](https://github.com/Azure/azure-sdk-for-python/pull/43602)
12+
* Fixed bug where queries using `feed_range` and `continuation` options would not work as expected. See [PR 43700](https://github.com/Azure/azure-sdk-for-python/pull/43700).
1113

1214
#### Other Changes
1315
* Further optimized health checks for sync and async clients. See [PR 43339](https://github.com/Azure/azure-sdk-for-python/pull/43339)
@@ -41,7 +43,6 @@ This version and all future versions will require Python 3.9+.
4143
#### Features Added
4244
* Added read_items API to provide an efficient method for retrieving multiple items in a single request. See [PR 42167](https://github.com/Azure/azure-sdk-for-python/pull/42167).
4345
* Added ability to replace a container's indexing policy if a vector embedding policy was present. See [PR 42810](https://github.com/Azure/azure-sdk-for-python/pull/42810).
44-
* Added merge support. See [PR 42924](https://github.com/Azure/azure-sdk-for-python/pull/42924).
4546

4647
#### Bugs Fixed
4748
* Improved the resilience of Database Account Read metadata operation against short-lived network issues by increasing number of retries. See [PR 42525](https://github.com/Azure/azure-sdk-for-python/pull/42525).

sdk/cosmos/azure-cosmos/azure/cosmos/_base.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -945,3 +945,18 @@ def _build_properties_cache(properties: dict[str, Any], container_link: str) ->
945945
"_self": properties.get("_self", None), "_rid": properties.get("_rid", None),
946946
"partitionKey": properties.get("partitionKey", None), "container_link": container_link
947947
}
948+
949+
def format_pk_range_options(query_options: Mapping[str, Any]) -> dict[str, Any]:
950+
"""Formats the partition key range options to be used internally from the query ones.
951+
952+
:param dict query_options: The query options being used.
953+
:return: The relevant partition key range options.
954+
:rtype: dict
955+
"""
956+
pk_range_options: dict[str, Any] = {}
957+
if query_options is not None:
958+
if "containerRID" in query_options:
959+
pk_range_options["containerRID"] = query_options["containerRID"]
960+
if "excludedLocations" in query_options:
961+
pk_range_options["excludedLocations"] = query_options["excludedLocations"]
962+
return pk_range_options

sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3258,7 +3258,6 @@ def __GetBodiesFromQueryResult(result: dict[str, Any]) -> list[dict[str, Any]]:
32583258

32593259
# If feed_range_epk exist, query with the range
32603260
if feed_range_epk is not None:
3261-
last_response_headers = CaseInsensitiveDict()
32623261
over_lapping_ranges = self._routing_map_provider.get_overlapping_ranges(resource_id, [feed_range_epk],
32633262
options)
32643263
# It is possible to get more than one over lapping range. We need to get the query results for each one

sdk/cosmos/azure-cosmos/azure/cosmos/_execution_context/aio/hybrid_search_aggregator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""Internal class for multi execution context aggregator implementation in the Azure Cosmos database service.
55
"""
6-
76
from azure.cosmos._execution_context.aio.base_execution_context import _QueryExecutionContextBase
87
from azure.cosmos._execution_context.aio import document_producer
98
from azure.cosmos._execution_context.hybrid_search_aggregator import _retrieve_component_scores, _rewrite_query_infos, \

sdk/cosmos/azure-cosmos/azure/cosmos/_execution_context/aio/multi_execution_aggregator.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
"""Internal class for multi execution context aggregator implementation in the Azure Cosmos database service.
2323
"""
24-
2524
from azure.cosmos._execution_context.aio.base_execution_context import _QueryExecutionContextBase
2625
from azure.cosmos._execution_context.aio import document_producer, _queue_async_helper
2726
from azure.cosmos._routing import routing_range
@@ -162,7 +161,6 @@ def _createTargetPartitionQueryExecutionContext(self, partition_key_target_range
162161
)
163162

164163
async def _get_target_partition_key_range(self):
165-
166164
query_ranges = self._partitioned_query_ex_info.get_query_ranges()
167165
return await self._routing_provider.get_overlapping_ranges(
168166
self._resource_link,

sdk/cosmos/azure-cosmos/azure/cosmos/_execution_context/aio/non_streaming_order_by_aggregator.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""Internal class for multi execution context aggregator implementation in the Azure Cosmos database service.
55
"""
6-
76
from azure.cosmos._execution_context.aio.base_execution_context import _QueryExecutionContextBase
87
from azure.cosmos._execution_context.aio.multi_execution_aggregator import _MultiExecutionContextAggregator
98
from azure.cosmos._execution_context.aio import document_producer
@@ -107,7 +106,6 @@ def _createTargetPartitionQueryExecutionContext(self, partition_key_target_range
107106
)
108107

109108
async def _get_target_partition_key_range(self):
110-
111109
query_ranges = self._partitioned_query_ex_info.get_query_ranges()
112110
return await self._routing_provider.get_overlapping_ranges(
113111
self._resource_link,

sdk/cosmos/azure-cosmos/azure/cosmos/_execution_context/multi_execution_aggregator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ def _createTargetPartitionQueryExecutionContext(self, partition_key_target_range
195195
)
196196

197197
def _get_target_partition_key_range(self):
198-
199198
query_ranges = self._partitioned_query_ex_info.get_query_ranges()
200199
return self._routing_provider.get_overlapping_ranges(
201200
self._resource_link,

sdk/cosmos/azure-cosmos/azure/cosmos/_execution_context/non_streaming_order_by_aggregator.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""Internal class for multi execution context aggregator implementation in the Azure Cosmos database service.
55
"""
6-
76
from azure.cosmos._execution_context.base_execution_context import _QueryExecutionContextBase
87
from azure.cosmos._execution_context.multi_execution_aggregator import _MultiExecutionContextAggregator
98
from azure.cosmos._execution_context import document_producer

sdk/cosmos/azure-cosmos/azure/cosmos/_routing/aio/routing_map_provider.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ async def get_overlapping_ranges(self, collection_link, partition_key_ranges, fe
6161
:rtype: list
6262
"""
6363
collection_id = _base.GetResourceIdOrFullNameFromLink(collection_link)
64-
await self.init_collection_routing_map_if_needed(collection_link, collection_id, feed_options, **kwargs)
64+
pk_range_options = _base.format_pk_range_options(feed_options)
65+
await self.init_collection_routing_map_if_needed(collection_link, collection_id, pk_range_options, **kwargs)
6566

6667
return self._collection_routing_map_by_item[collection_id].get_overlapping_ranges(partition_key_ranges)
6768

@@ -95,7 +96,8 @@ async def get_range_by_partition_key_range_id(
9596
**kwargs: dict[str, Any]
9697
) -> Optional[dict[str, Any]]:
9798
collection_id = _base.GetResourceIdOrFullNameFromLink(collection_link)
98-
await self.init_collection_routing_map_if_needed(collection_link, collection_id, feed_options, **kwargs)
99+
pk_range_options = _base.format_pk_range_options(feed_options)
100+
await self.init_collection_routing_map_if_needed(collection_link, collection_id, pk_range_options, **kwargs)
99101

100102
return self._collection_routing_map_by_item[collection_id].get_range_by_partition_key_range_id(
101103
partition_key_range_id)

sdk/cosmos/azure-cosmos/azure/cosmos/_routing/routing_map_provider.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ def get_overlapping_ranges(self, collection_link, partition_key_ranges, feed_opt
8484
:rtype: list
8585
"""
8686
collection_id = _base.GetResourceIdOrFullNameFromLink(collection_link)
87-
self.init_collection_routing_map_if_needed(collection_link, collection_id, feed_options, **kwargs)
87+
pk_range_options = _base.format_pk_range_options(feed_options)
88+
self.init_collection_routing_map_if_needed(collection_link, collection_id, pk_range_options, **kwargs)
8889

8990
return self._collection_routing_map_by_item[collection_id].get_overlapping_ranges(partition_key_ranges)
9091

@@ -96,7 +97,8 @@ def get_range_by_partition_key_range_id(
9697
**kwargs: dict[str, Any]
9798
) -> Optional[dict[str, Any]]:
9899
collection_id = _base.GetResourceIdOrFullNameFromLink(collection_link)
99-
self.init_collection_routing_map_if_needed(collection_link, collection_id, feed_options, **kwargs)
100+
pk_range_options = _base.format_pk_range_options(feed_options)
101+
self.init_collection_routing_map_if_needed(collection_link, collection_id, pk_range_options, **kwargs)
100102

101103
return (self._collection_routing_map_by_item[collection_id]
102104
.get_range_by_partition_key_range_id(partition_key_range_id))

0 commit comments

Comments
 (0)