Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CosmosDB] Query fails when setting partition_key #13519

Closed
chrisyeh96 opened this issue Sep 2, 2020 · 7 comments
Closed

[CosmosDB] Query fails when setting partition_key #13519

chrisyeh96 opened this issue Sep 2, 2020 · 7 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. Cosmos customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.

Comments

@chrisyeh96
Copy link
Contributor

  • Package Name: azure-cosmos
  • Package Version: 4.1.0
  • Operating System: Ubuntu 18.04 LTS
  • Python Version: 3.8

Describe the bug
I created a test CosmosDB database using the Azure web portal with the examples from the CosmosDB Getting Started documentation.

{
  "id": "AndersenFamily",
  "children": ["..."],
  "..."
},
{
  "id": "WakefieldFamily",
  "children": ["..."],
  "..."
},

I set the partition_key on the container to be the "id" field. I can run the following queries (from the SQL Query Object Array page) via the "Data Explorer" on the Azure web portal just fine, but the Python SDK has issues when I set partition_key on the query.

The following two queries work fine in Data Explorer on the Azure web portal:

SELECT * FROM Families.children
SELECT * FROM c IN Families.children

Both queries throw errors in the Python SDK when I pass a partition_key parameter to query_items():

import os
from azure.cosmos.cosmos_client import CosmosClient

client = CosmosClient(os.environ['COSMOS_ENDPOINT'], credential=os.environ['COSMOS_KEY'])
container = client.get_database_client('testdb').get_container_client('testcontainer')
query1 = 'SELECT * FROM Families.children'
query2 = 'SELECT * FROM c IN Families.children'

# these work just fine
list(container.query_items(query=query1, enable_cross_partition_query=True))
list(container.query_items(query=query2, enable_cross_partition_query=True))

# both of these throw the same error
list(container.query_items(query=query1, partition_key='AndersenFamily'))
list(container.query_items(query=query2, partition_key='AndersenFamily'))
CosmosHttpResponseError: (BadRequest) Message: {"errors":[
  {"severity":"Error","location":{"start":4294967295,"end":4294967295},"code":"SC2001","message":"Identifier 'Families' could not be resolved."}]}
ActivityId: 5e53c5f6-43bc-43ee-90a7-4e39d48aa271, [...]

Expected behavior
Setting partition_key on query_items() should not cause an error.

Additional context
I suspect that this may be related to the bug reported in #11721. However, my example here is much simpler and identifies the issue with passing a partition_key argument to query_items().

@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Sep 2, 2020
@ghost
Copy link

ghost commented Sep 2, 2020

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @wmengmsft, @MehaKaushik, @shurd, @anfeldma-ms

@kaerm kaerm added Client This issue points to a problem in the data-plane of the library. Cosmos labels Sep 3, 2020
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Sep 3, 2020
@southpolesteve
Copy link
Contributor

@Rodrigossz Can you look into this? May be an issue with the samples for how to do query

@lmazuel lmazuel added the Service Attention Workflow: This issue is responsible by Azure service team. label Nov 2, 2020
@ghost
Copy link

ghost commented Nov 2, 2020

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @zfoster, @southpolesteve.

@Rodrigossz
Copy link
Contributor

Hello @chrisyeh96

Tks for the feedback. We will check it out. As a workaround you can use 'select children from Families'. BTW, you sample code has 'testcontainer' instead of Families, but I think that it's a copy/paste error. Tks for the contribution!

@Rodrigossz
Copy link
Contributor

Hello @chrisyeh96

I'm closing this issue because it's a documentation problem, what I will start to address today.

Basically what is happening is that your are narrowing down the scope with the FROM clause: instead of Families, and all of it's properties, your "dataset" only has the children property. And the partition key isn't there.

Tks for the help to improve the product.

@chrisyeh96
Copy link
Contributor Author

@Rodrigossz Thanks for looking into this issue. However, I'm not convinced that narrowing down the scope in the FROM clause is the problem. As explained in the official CosmosDB SQL syntax for the FROM clause, FROM Families.children is certainly valid syntax. It is a <container_expression> '.' property_name.

Plus, the queries work just fine in Data Explorer on the Azure web portal. This is clearly an issue with the Python SDK, not with my query.

@Rodrigossz
Copy link
Contributor

Rodrigossz commented Nov 4, 2020

@chrisyeh96

Data Explorer uses another SDK behind the interface and has different environment settings. As an example, it runs all queries as cross partition enabled. So you can expect absolutely different behavior between any SDK and Data Explorer. When you use partition_key='WakefieldFamily' with the SDK's query_items method, the SDK will add and id = 'WakefieldFamily' to your query string.

If you try the same in the Data Explorer you will get:

  • SELECT * FROM c IN Families.children where Familes.id = 'WakefieldFamily' => Will return an error
  • SELECT * FROM c IN Families.children where id = 'WakefieldFamily' => Will return an error
  • SELECT * FROM c IN Families.children where c.id = 'WakefieldFamily' => Will return no data
  • SELECT * FROM c IN Families.children where 'id' = 'WakefieldFamily' => Will return no data

Please note that for

query1 = 'SELECT * FROM Families.children'
list(container.query_items(query=query1, partition_key='AndersenFamily'))

The SDK will transform into SELECT * FROM Families.children where Families.children.id = 'AndersenFamily' , what doesn't work.

I am adding clarifications on that to the Python SDK README file.

openapi-sdkautomation bot pushed a commit to AzureSDKAutomation/azure-sdk-for-python that referenced this issue May 11, 2021
Network february release (Azure#14333)

* Adds base for updating Microsoft.Network from version stable/2020-11-01 to version 2021-02-01

* Updates readme

* Updates API version in new specs and examples

* init (Azure#13496)

Co-authored-by: matyang222 <[email protected]>

* Swagger change for CustomIpPrefix. Adding four new attributes.  (Azure#13456)

* update swagger

* fix apiversion

* fix

* add

Co-authored-by: Weiheng Li <[email protected]>

* typo: paramter in applicationGateway.json (Azure#13538)

* VPN NAT for Virtual Network Gateway feature changes(networkFeb) (Azure#13481)

* commit1

* commit2

* resolving comments

* pythonMd

Co-authored-by: Khushboo Baheti <[email protected]>

* fix virtual network resource (Azure#13570)

* Added a new feature FlowTimeoutInMinutes under Virtual Network Proper… (Azure#13519)

* Added a new feature FlowTimeoutInMinutes under Virtual Network Properties

* Updated the type from string to integer, added a non-null example

* Added missing format for 'integer' type

* Add new failedMessage property for CustomIpPrefix (Azure#13607)

* update swagger

* fix apiversion

* fix

* add

* add failedreason property

* update swagger

* fix apiversion

* fix

* add failedreason property

* update

Co-authored-by: Weiheng Li <[email protected]>

* Added Preferred Routing Gateway Support (Azure#13611)

* Feature: Address space update in peered vNets (Azure#13521)

* Adding new fields and operation to support the address space update in peered vNets

Adding new fields and operation to support the address space update in peered vNets

* Adding the new query param in the example

As per the review comment, adding the new query param in the example request response of swagger.

* Adding the new query param in examples

Adding the new query param in examples

* Restricting the sync param

Restricting the sync param to hold only true as value. We never need to send false.

Co-authored-by: Hari Prasad Perabattula <[email protected]>

* Remove max file size limit enforcement as it is done in NRP (Azure#13679)

* Tesha/fix waf policy examples crs version (Azure#13697)

* Remove max file size limit enforcement as it is done in NRP

* Update the CRS version in the examples to reflect latest

* Fix (Azure#13734)

Co-authored-by: Khushboo Baheti <[email protected]>

* Swagger for NRP's VipSwap operation (Azure#13639)

* Swagger for NRP's VipSwap operation

* Fixing validation errors

* minor fix

* Adding api version

* Remove required (Azure#13969)

Co-authored-by: Will Ehrich <[email protected]>

* Hotfix extended location parameter hierarchy (Azure#13864)

* add to feb branch

* delete project name reference

* expose two new client cert properties: validatedCertData, clientCertIssuerDN (Azure#13989)

* adding workloadType property for Baremetal scenarios (Azure#14101)

* Added bastion sku (Azure#14248)

* fix nrp resources based on s360 checks (Azure#14219)

* Adding Azure Network Manager association to the EffectiveNetworkSecurityGroups API (Azure#14265)

* Added Azure Network Manager association to the EffectiveNetworkSecurityGroupAssociation

* Adding example for networkManager response in the EffectiveNSG call

* Add deleteOption to PublicIPAddress (Azure#14343)

* Add deleteOption to PublicIPAddress

* run validators

Co-authored-by: Bashar Gharaibeh <[email protected]>

Co-authored-by: Matthew Yang <[email protected]>
Co-authored-by: matyang222 <[email protected]>
Co-authored-by: Tom Li <[email protected]>
Co-authored-by: Weiheng Li <[email protected]>
Co-authored-by: Nick Schonning <[email protected]>
Co-authored-by: Khushboo Baheti <[email protected]>
Co-authored-by: Khushboo Baheti <[email protected]>
Co-authored-by: guptas14 <[email protected]>
Co-authored-by: Satya-anshu <[email protected]>
Co-authored-by: arvenka <[email protected]>
Co-authored-by: Hari Prasad Perabattula <[email protected]>
Co-authored-by: Hari Prasad Perabattula <[email protected]>
Co-authored-by: tejasshah7 <[email protected]>
Co-authored-by: shnaya434 <[email protected]>
Co-authored-by: William Ehrich <[email protected]>
Co-authored-by: Will Ehrich <[email protected]>
Co-authored-by: litchiyangMSFT <[email protected]>
Co-authored-by: biaogao <[email protected]>
Co-authored-by: bhbhise <[email protected]>
Co-authored-by: mscorp-buchen <[email protected]>
Co-authored-by: Arpit Agarwal <[email protected]>
Co-authored-by: basharg <[email protected]>
Co-authored-by: Bashar Gharaibeh <[email protected]>
@github-actions github-actions bot locked and limited conversation to collaborators Apr 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. Cosmos customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

5 participants