Query: Adds Query Advisor SDK capabilities#45331
Open
aayush3011 wants to merge 8 commits intoAzure:mainfrom
Open
Query: Adds Query Advisor SDK capabilities#45331aayush3011 wants to merge 8 commits intoAzure:mainfrom
aayush3011 wants to merge 8 commits intoAzure:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds Query Advisor support to the Azure Cosmos DB Python SDK by letting callers request query advice via query_items(..., populate_query_advice=True) and surfacing formatted advice from the x-ms-cosmos-query-advice response header.
Changes:
- Adds new request/response headers (
PopulateQueryAdvice,QueryAdvice) and plumbspopulate_query_advicethrough sync/asyncquery_items. - Introduces a
_query_advisorhelper module to decode URL-encoded JSON advice and format it using a local rules catalog. - Adds unit/integration tests and extends existing “None options” tests to cover the new parameter.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| sdk/cosmos/azure-cosmos/azure/cosmos/http_constants.py | Adds header constants for Query Advice request/response. |
| sdk/cosmos/azure-cosmos/azure/cosmos/container.py | Adds populate_query_advice kwarg + docstrings and maps it into feed options. |
| sdk/cosmos/azure-cosmos/azure/cosmos/aio/_container.py | Async query_items support for populate_query_advice. |
| sdk/cosmos/azure-cosmos/azure/cosmos/_base.py | Emits the x-ms-cosmos-populatequeryadvice request header when enabled. |
| sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py | Decodes and formats x-ms-cosmos-query-advice in last response headers (sync). |
| sdk/cosmos/azure-cosmos/azure/cosmos/aio/_cosmos_client_connection_async.py | Decodes and formats x-ms-cosmos-query-advice in last response headers (async). |
| sdk/cosmos/azure-cosmos/azure/cosmos/_query_advisor/init.py | Exposes internal Query Advisor helpers. |
| sdk/cosmos/azure-cosmos/azure/cosmos/_query_advisor/_get_query_advice_info.py | Entry point for turning the header value into a formatted string. |
| sdk/cosmos/azure-cosmos/azure/cosmos/_query_advisor/_query_advice.py | Implements parsing/formatting for advice entries and collections. |
| sdk/cosmos/azure-cosmos/azure/cosmos/_query_advisor/_rule_directory.py | Loads the local rules catalog and provides message lookup. |
| sdk/cosmos/azure-cosmos/azure/cosmos/_query_advisor/query_advice_rules.json | Adds the local rule message catalog + documentation URL prefix. |
| sdk/cosmos/azure-cosmos/tests/test_query_advisor.py | Adds unit tests for parsing/formatting Query Advisor data. |
| sdk/cosmos/azure-cosmos/tests/test_query.py | Adds (currently skipped) emulator integration coverage for Query Advice (sync). |
| sdk/cosmos/azure-cosmos/tests/test_query_async.py | Adds (currently skipped) emulator integration coverage for Query Advice (async). |
| sdk/cosmos/azure-cosmos/tests/test_query_cross_partition.py | Adds (currently skipped) cross-partition integration coverage for Query Advice (sync). |
| sdk/cosmos/azure-cosmos/tests/test_query_cross_partition_async.py | Adds live cross-partition async integration coverage for Query Advice. |
| sdk/cosmos/azure-cosmos/tests/test_none_options.py | Ensures populate_query_advice=None is accepted in sync query paths. |
| sdk/cosmos/azure-cosmos/tests/test_none_options_async.py | Ensures populate_query_advice=None is accepted in async query paths. |
sdk/cosmos/azure-cosmos/azure/cosmos/_query_advisor/_get_query_advice_info.py
Outdated
Show resolved
Hide resolved
sdk/cosmos/azure-cosmos/azure/cosmos/_query_advisor/query_advice_rules.json
Outdated
Show resolved
Hide resolved
sdk/cosmos/azure-cosmos/azure/cosmos/_query_advisor/_query_advice.py
Outdated
Show resolved
Hide resolved
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR adds the capability to request and receive Query Advice in QueryRequests. The response from BE is an URL-encoded JSON string that will get decoded to UTF8 text.
When
populate_query_advice=Trueis passed toquery_items(), the backend analyzes the query and returns optimization hints (e.g., replaceCONTAINSwithSTARTSWITH, use computed properties, prefer static datetime functions). The formatted advice is surfaced in response headers underHttpHeaders.QueryAdvice, following the same pattern aspopulate_index_metrics..NET SDK (PR #4714)
Usage
All SDK Contribution checklist:
General Guidelines and Best Practices
Testing Guidelines