Azure / azure-sdk-for-python

This repository is for active development of the Azure SDK for Python. For consumers of the SDK we recommend visiting our public developer docs at https://learn.microsoft.com/python/azure/ or our versioned developer docs at https://azure.github.io/azure-sdk-for-python.
MIT License
4.65k stars 2.84k forks source link

'NoneType' object has no attribute 'clear_route_to_location' when querying an Azure CosmosDB table using azure-cosmos python SDK #37214

Closed abhimanyusingh-sigfyn closed 3 weeks ago

abhimanyusingh-sigfyn commented 3 months ago

Describe the bug I am encountering the following error while querying data from CosmosDB using the azure-cosmos Python SDK: AttributeError("'NoneType' object has no attribute 'clear_route_to_location'").

Sample Query "SELECT C.Col1, C.Col2, C.Col3 FROM C WHERE C.ID IN (<Str1>, <Str2>, <Str3>....) AND (C.Date IN (<Date1>, <Date2>, <Date3>...))"

Table Details

Upon further investigation, I came across 3 files in azure.cosmos library, that are referencing clear_route_to_location function:

Relevant code for initializing Azure CosmosClient for querying the table

from azure.cosmos import CosmosClient

def get_client():
    global client

    if not client:
        cosmodb_logger = getLogger("azure.core.pipeline.policies.http_logging_policy")
        cosmodb_logger.setLevel(WARNING)

        response_string = get_secret(COSMOS_DB_SECRET)
        key = response_string["key"]

        client = CosmosClient(environ.get(COSMOS_DB_ENDPOINT), credential=key, logger=cosmodb_logger)

    return client

def get_container(database_name, container_name):
    database = get_client().get_database_client(database=database_name)
    container = database.get_container_client(container_name)
    return container

def query_items(database_name, container_name, query, max_item_count=100):
    container = get_container(database_name, container_name)
    query_items_response = container.query_items(query=query, enable_cross_partition_query=True, max_item_count=max_item_count)
    items = [item for item in query_items_response]
    return items

To Reproduce The error is not consistently reproducible.

Expected behavior The query runs successfully and data is returned back.

github-actions[bot] commented 3 months ago

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @AbhinavTrips @bambriz @pilchie @pjohari-ms @simorenoh.

kristapratico commented 2 months ago

@abhimanyusingh-sigfyn thanks for your issue, the team will take a look and get back to you as soon as possible.

bambriz commented 2 months ago

Hello @abhimanyusingh-sigfyn Thank you for bringing this to our attention. It seems this error is happening because the request object for the query is None while a retryable error occured. (One of the retryable errors you mentioned). One other question I have is how are you currently using the query_items() method you showed? Could you provide an example of how it is used?

abhimanyusingh-sigfyn commented 2 months ago

Hello @bambriz. Here's an example of how query_items() is being used:

def get_data_from_db(container_name, query):
    data = query_items("db_name", container_name, query, max_item_count=10000)
    data_df = pd.Dataframe(data)
    return data_df

def my_func():
    query = "SELECT C.Col1, C.Col2, C.Col3 FROM C WHERE C.ID IN (<Str1>, <Str2>, <Str3>....) AND (C.Date IN (<Date1>, <Date2>, <Date3>...))"
    data_df = get_data_from_db("db_container_name", query)
bambriz commented 2 months ago

@abhimanyusingh-sigfyn This is currently being fixed with PR 37578.

bambriz commented 2 months ago

@abhimanyusingh-sigfyn The PR with the fix for this issue has been merged.

github-actions[bot] commented 1 month ago

Hi @abhimanyusingh-sigfyn. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.

github-actions[bot] commented 3 weeks ago

Hi @abhimanyusingh-sigfyn, since you haven’t asked that we /unresolve the issue, we’ll close this out. If you believe further discussion is needed, please add a comment /unresolve to reopen the issue.