-
Notifications
You must be signed in to change notification settings - Fork 504
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
Query: Adds ArrayContains to CosmosLinqExtensions to allow partial matching on array fields in queries #4992
Open
bcrobinson
wants to merge
4
commits into
Azure:master
Choose a base branch
from
bcrobinson:feature/array_contains_builtin_function
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
786db81
Added ArrayContains to CosmosLinqExtensions to allow partial matching
d2e3e99
Merge branch 'master' into feature/array_contains_builtin_function
bcrobinson 9d99782
Merge branch 'master' into feature/array_contains_builtin_function
bcrobinson c981452
Added overload for ArrayContains with no 3rd paritalMatch parameter
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
namespace Microsoft.Azure.Cosmos.Linq | ||
{ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
|
@@ -237,6 +238,35 @@ public static bool RegexMatch(this object obj, string regularExpression, string | |
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented); | ||
} | ||
|
||
/// <summary> | ||
/// Returns a boolean indicating whether the array contains the specified value. | ||
/// You can check for a partial or full match of an object by using a boolean expression within the function. | ||
/// For more information, see https://learn.microsoft.com/en-gb/azure/cosmos-db/nosql/query/array-contains. | ||
/// This method is to be used in LINQ expressions only and will be evaluated on server. | ||
/// There's no implementation provided in the client library. | ||
/// </summary> | ||
/// <param name="obj"></param> | ||
/// <param name="itemToMatch">The value to search within the array.</param> | ||
/// <param name="partialMatch">Indicating whether the search should check for a partial match (true) or a full match (false).</param> | ||
/// <returns>Returns true if the array array contains the specified value; otherwise, false.</returns> | ||
/// <example> | ||
/// <code> | ||
/// <![CDATA[ | ||
/// var matched = documents.Where(document => document.Namess.ArrayContains(<itemToMatch>, <partialMatch>)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
/// // To do a partial match on an array of objects, pass in an anonymous object set partialMatch to true | ||
/// var matched = documents.Where(document => document.ObjectArray.ArrayContains(new { Name = <name> }, true)); | ||
/// ]]> | ||
/// </code> | ||
/// </example> | ||
public static bool ArrayContains(this IEnumerable obj, object itemToMatch, bool partialMatch) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
// The signature for this is not generic so the user can pass in anonymous type for the item to match | ||
// e.g documents.Where(document => document.FooItems.ArrayContains(new { Name = "Bar" }, true) | ||
// partialMatch could have a default values (bool partialMatch = false) but those are not valid in expressions | ||
// (see error CS0854) and this method will only be used in expressions, so not point adding it | ||
throw new NotImplementedException(ClientResources.TypeCheckExtensionFunctionsNotImplemented); | ||
} | ||
|
||
/// <summary> | ||
/// This method generate query definition from LINQ query. | ||
/// </summary> | ||
|
94 changes: 94 additions & 0 deletions
94
...selineTest/TestBaseline/LinqTranslationBaselineTests.TestArrayContainsBuiltinFunction.xml
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<Results> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Select clause with int value and match partial true]]></Description> | ||
<Expression><![CDATA[query.Select(doc => doc.ArrayField.ArrayContains(Convert(1, Object), True))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE ARRAY_CONTAINS(root["ArrayField"], 1, true) | ||
FROM root]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Filter clause with int value and match partial true]]></Description> | ||
<Expression><![CDATA[query.Where(doc => doc.ArrayField.ArrayContains(Convert(1, Object), True))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE ARRAY_CONTAINS(root["ArrayField"], 1, true)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Select clause with object value and match partial true]]></Description> | ||
<Expression><![CDATA[query.Select(doc => doc.ObjectArrayField.ArrayContains(new AnonymousType(Field = "abc"), True))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE ARRAY_CONTAINS(root["ObjectArrayField"], {"Field": "abc"}, true) | ||
FROM root]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Filter clause with object value and match partial true]]></Description> | ||
<Expression><![CDATA[query.Where(doc => doc.ObjectArrayField.ArrayContains(new AnonymousType(Field = "abc"), True))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE ARRAY_CONTAINS(root["ObjectArrayField"], {"Field": "abc"}, true)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Select clause with int value and match partial false]]></Description> | ||
<Expression><![CDATA[query.Select(doc => doc.ArrayField.ArrayContains(Convert(1, Object), False))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE ARRAY_CONTAINS(root["ArrayField"], 1, false) | ||
FROM root]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Filter clause with int value and match partial false]]></Description> | ||
<Expression><![CDATA[query.Where(doc => doc.ArrayField.ArrayContains(Convert(1, Object), False))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE ARRAY_CONTAINS(root["ArrayField"], 1, false)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Select clause with object value and match partial false]]></Description> | ||
<Expression><![CDATA[query.Select(doc => doc.ObjectArrayField.ArrayContains(new AnonymousType(Field = "abc"), False))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE ARRAY_CONTAINS(root["ObjectArrayField"], {"Field": "abc"}, false) | ||
FROM root]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
<Result> | ||
<Input> | ||
<Description><![CDATA[ArrayContains in Filter clause with object value and match partial false]]></Description> | ||
<Expression><![CDATA[query.Where(doc => doc.ObjectArrayField.ArrayContains(new AnonymousType(Field = "abc"), False))]]></Expression> | ||
</Input> | ||
<Output> | ||
<SqlQuery><![CDATA[ | ||
SELECT VALUE root | ||
FROM root | ||
WHERE ARRAY_CONTAINS(root["ObjectArrayField"], {"Field": "abc"}, false)]]></SqlQuery> | ||
</Output> | ||
</Result> | ||
</Results> |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if this is necessary