azure-webjobs-sdk-extensions icon indicating copy to clipboard operation
azure-webjobs-sdk-extensions copied to clipboard

CosmosDB: Can't get an IEnumerable response on just PartitionKey

Open aaronpowell opened this issue 5 years ago • 6 comments

With the CosmosDB input binding for Functions I want to get back all the records for a partition key, and I saw you can get an IEnumerable<T> result and thought that since you can specify the PartitionKey property on the attribute it could be used as the query, but it didn't work.

I'm wondering if that's a by-design feature or if it's something that could be added, because looking into the code the extension context doesn't warn of it (the rules only checks that you don't have an Id) and the implementation expects a SQL query.

I did a quick test to see if it is possible to query Cosmos with just the partition key, and this works:

	CosmosDBConnectionString connection = new CosmosDBConnectionString("");
	
	var client = new DocumentClient(connection.ServiceEndpoint, connection.AuthKey);
	
	var url = UriFactory.CreateDocumentCollectionUri("blazingPizza", "pizza");

	var query = client.CreateDocumentQuery<Pizza>(url, new FeedOptions { PartitionKey = new PartitionKey("specials") }).AsDocumentQuery();
	
	var response = await query.ExecuteNextAsync();

In summary, the extension doesn't prevent you from getting an IEnumerable<T> input binding where you set the PartitionKey, but the PartitionKey attribute property doesn't do anything in this case, which isn't obvious initially.

Would this be useful to include in the extension? I'm happy to do a PR for it.

@brettsam @ealsur

aaronpowell avatar Jul 01 '20 12:07 aaronpowell

If I'm understanding correctly, this is likely related to #647.

techfg avatar Nov 12 '20 06:11 techfg

Hi @aaronpowell , Do we have any updates here, kindly let us know if this still requires an implementation or can we close the issue

v-anvari avatar Dec 07 '20 14:12 v-anvari

@v-anvari I believe there's some form of requirement here, either the rule for IEnumerable<T> bindings should raise an error if you only try and get a partition key query or you should be able to get an IEnumerable<T> using just the partition key.

aaronpowell avatar Dec 07 '20 21:12 aaronpowell

As of 3.0.8, the following code should return results filtered by partitionkey:

var query = client.CreateDocumentQuery<Pizza>(url, new FeedOptions { PartitionKey = new PartitionKey("specials") }).AsDocumentQuery();

In 3.0.7, it would return results but it would not be filtered (ignored the partition key due to bugs identified in underlying dependencies in #647).

techfg avatar Dec 07 '20 23:12 techfg

cc @brettsam , @ealsur , Kindly suggest if this requirement could be included in further releases.

v-anvari avatar Dec 15 '20 10:12 v-anvari

@v-anvari The user can pull the DocumentClient instance using the input binding and execute the query as @techfg showed, so it should not be blocked.

Adding the PartitionKey support for bindings would be interesting for the next version though as a new feature.

ealsur avatar Feb 11 '21 22:02 ealsur

This was already done as part of the 4.X extension

ealsur avatar Dec 14 '22 12:12 ealsur