-
-
Notifications
You must be signed in to change notification settings - Fork 102
fix(cosmos): support quoted bracket property access in queries #297
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
Open
thomhurst
wants to merge
3
commits into
floci-io:main
Choose a base branch
from
thomhurst:fix/cosmos-bracket-property-access
base: main
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
64 changes: 64 additions & 0 deletions
64
compatibility-tests/sdk-test-dotnet/CosmosBracketPropertyCompatibilityTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| using Microsoft.Azure.Cosmos; | ||
| using Newtonsoft.Json.Linq; | ||
|
|
||
| namespace FlociAz.Compatibility; | ||
|
|
||
| [NotInParallel] | ||
| public sealed class CosmosBracketPropertyCompatibilityTests | ||
| { | ||
| [Test] | ||
| [Timeout(60_000)] | ||
| public async Task ErasureQueryFindsBracketIndexedUserReferences(CancellationToken cancellationToken) | ||
| { | ||
| string endpoint = Environment.GetEnvironmentVariable("FLOCI_AZ_ENDPOINT") ?? "http://localhost:4577"; | ||
| using var client = new CosmosClient($"{endpoint}/devstoreaccount1-cosmos/", | ||
| "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", | ||
| new CosmosClientOptions { ConnectionMode = ConnectionMode.Gateway, LimitToEndpoint = true }); | ||
| Database database = await client.CreateDatabaseAsync( | ||
| $"dotnet-bracket-{Guid.NewGuid():N}", cancellationToken: cancellationToken); | ||
| try | ||
| { | ||
| Container container = await database.CreateContainerAsync( | ||
| "items", "/pk", cancellationToken: cancellationToken); | ||
| string userId = Guid.NewGuid().ToString(); | ||
| await container.CreateItemAsync(new | ||
| { | ||
| id = "declined", pk = "event-a", goingUserIds = Array.Empty<string>(), | ||
| rsvpVersionsByUserId = new Dictionary<string, int> { [userId] = 4 } | ||
| }, new PartitionKey("event-a"), cancellationToken: cancellationToken); | ||
| await container.CreateItemAsync(new | ||
| { | ||
| id = "unrelated", pk = "event-b", goingUserIds = Array.Empty<string>(), | ||
| rsvpVersionsByUserId = new Dictionary<string, int> { ["another-user"] = 2 } | ||
| }, new PartitionKey("event-b"), cancellationToken: cancellationToken); | ||
|
|
||
| var query = new QueryDefinition( | ||
| "SELECT * FROM c WHERE ARRAY_CONTAINS(c.goingUserIds, @userId) " | ||
| + $"OR IS_DEFINED(c.rsvpVersionsByUserId[\"{userId}\"])") | ||
| .WithParameter("@userId", userId); | ||
| using FeedIterator<JObject> iterator = container.GetItemQueryIterator<JObject>(query, | ||
| requestOptions: new QueryRequestOptions { MaxItemCount = 1 }); | ||
| var ids = new List<string>(); | ||
| while (iterator.HasMoreResults) | ||
| { | ||
| FeedResponse<JObject> page = await iterator.ReadNextAsync(cancellationToken); | ||
| ids.AddRange(page.Select(item => item.Value<string>("id")!)); | ||
| } | ||
| await Assert.That(ids).IsEquivalentTo(["declined"]); | ||
| await container.CreateItemAsync(new JObject | ||
| { | ||
| ["id"] = "quoted", ["pk"] = "event-c", ["a\"b"] = 7, ["a.b"] = 8 | ||
| }, new PartitionKey("event-c"), cancellationToken: cancellationToken); | ||
| using FeedIterator<JObject> quoted = container.GetItemQueryIterator<JObject>(new QueryDefinition( | ||
| "SELECT c[\"a\\\"b\"], c[\"a.b\"] FROM c WHERE c.id = @id") | ||
| .WithParameter("@id", "quoted")); | ||
| FeedResponse<JObject> quotedPage = await quoted.ReadNextAsync(cancellationToken); | ||
| await Assert.That(quotedPage.Single().Value<int>("a\"b")).IsEqualTo(7); | ||
| await Assert.That(quotedPage.Single().Value<int>("a.b")).IsEqualTo(8); | ||
| } | ||
| finally | ||
| { | ||
| await database.DeleteAsync(cancellationToken: cancellationToken); | ||
| } | ||
| } | ||
| } |
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
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
98 changes: 98 additions & 0 deletions
98
src/test/java/io/floci/az/services/cosmos/CosmosBracketPropertyTest.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| package io.floci.az.services.cosmos; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| class CosmosBracketPropertyTest { | ||
| private final CosmosQueryEngine engine = new CosmosQueryEngine(); | ||
|
|
||
| @Test | ||
| void escapedKeyQueriesPreserveBackslashesInParameters() { | ||
| String value = "C:\\folder\\"; | ||
| var document = Map.<String, Object>of("a\"b", 7, "path", value); | ||
| assertEquals(List.of(7), engine.execute( | ||
| "SELECT VALUE c[\"a\\\"b\"] FROM c WHERE c.path = @path ORDER BY c.path", | ||
| List.of(Map.of("name", "@path", "value", value)), List.of(document)).items()); | ||
| } | ||
|
|
||
| @Test | ||
| void bracketProjectionUsesTheDecodedPropertyName() { | ||
| var document = Map.<String, Object>of("a.b", 7, "map", Map.of("user-id", 8)); | ||
| assertEquals(List.of(Map.of("a.b", 7, "user-id", 8)), engine.execute( | ||
| "SELECT c[\"a.b\"], c.map[\"user-id\"] FROM c", List.of(), List.of(document)).items()); | ||
| } | ||
|
|
||
| @Test | ||
| void escapedBracketKeysSurviveFullQueryScanning() { | ||
| var document = Map.<String, Object>of("a\"b", 7, "id", "item"); | ||
| assertEquals(List.of(7), engine.execute( | ||
| "SELECT VALUE c[\"a\\\"b\"] FROM c WHERE c.id = @id", | ||
| List.of(Map.of("name", "@id", "value", "item")), List.of(document)).items()); | ||
| assertEquals(List.of(Map.of("value", 7, "id", "item")), engine.execute( | ||
| "SELECT c[\"a\\\"b\"] AS value, c.id AS id FROM c WHERE c.id = 'item'", | ||
| List.of(), List.of(document)).items()); | ||
| } | ||
|
|
||
| @Test | ||
| void correlatedAliasLongerThanTenCharactersKeepsItsBinding() { | ||
| var documents = List.of(Map.<String, Object>of("actions", List.of(Map.of("type", "rsvp")))); | ||
| assertEquals(documents, engine.execute( | ||
| "SELECT * FROM c WHERE EXISTS(SELECT VALUE actionEntry FROM actionEntry IN c.actions " | ||
| + "WHERE actionEntry[\"type\"] = 'rsvp')", List.of(), documents).items()); | ||
| } | ||
|
|
||
| @Test | ||
| void orderByIndexPathsPreserveQuotedDots() { | ||
| assertEquals("/a.b", CosmosIndexingPolicy.normalizeOrderByPath("c[\"a.b\"]")); | ||
| assertEquals("/map/a.b/value", CosmosIndexingPolicy.normalizeOrderByPath("c.map['a.b'].value")); | ||
| } | ||
|
|
||
| @Test | ||
| void erasureQueryFindsUsersPresentOnlyInTheRsvpVersionMap() { | ||
| String userId = "eec725e5-b24c-48c1-b336-e599da1e6711"; | ||
| var documents = List.of( | ||
| Map.<String, Object>of("id", "declined", "goingUserIds", List.of(), | ||
| "rsvpVersionsByUserId", Map.of(userId, 4)), | ||
| Map.<String, Object>of("id", "going", "goingUserIds", List.of(userId)), | ||
| Map.<String, Object>of("id", "unrelated", "rsvpVersionsByUserId", Map.of("other-user", 2))); | ||
|
|
||
| assertEquals(List.of("declined", "going"), engine.execute( | ||
| "SELECT VALUE c.id FROM c WHERE ARRAY_CONTAINS(c.goingUserIds, @userId) " | ||
| + "OR IS_DEFINED(c.rsvpVersionsByUserId[\"" + userId + "\"])", | ||
| List.of(Map.of("name", "@userId", "value", userId)), documents).items()); | ||
| } | ||
|
|
||
| @Test | ||
| void bracketMembersPreserveDotsSpacesAndEmptyKeys() { | ||
| var document = Map.<String, Object>of("map", Map.of("a.b", Map.of("", 7), "two spaces", 8)); | ||
| assertEquals(7, engine.resolve(document, "c[\"map\"][\"a.b\"][\"\"]")); | ||
| assertEquals(7, engine.resolve(document, "c.map['a.b']['']")); | ||
| assertEquals(List.of(8), engine.execute( | ||
| "SELECT VALUE c.map[\"two spaces\"] FROM c", List.of(), List.of(document)).items()); | ||
| } | ||
|
|
||
| @Test | ||
| void mixedMemberAccessWorksInPredicatesAndProjections() { | ||
| var documents = List.of(Map.<String, Object>of("map", Map.of("user-id", Map.of("version", 4)))); | ||
| assertEquals(List.of(4), engine.execute( | ||
| "SELECT VALUE c[\"map\"]['user-id'].version FROM c " | ||
| + "WHERE c.map[\"user-id\"].version = 4", List.of(), documents).items()); | ||
| assertEquals(List.of(), engine.execute( | ||
| "SELECT * FROM c WHERE IS_DEFINED(c.map[\"missing\"])", List.of(), documents).items()); | ||
| } | ||
|
|
||
| @Test | ||
| void bracketStringsDecodeEscapesWithoutSplittingTheMember() { | ||
| var document = Map.<String, Object>of("map", Map.of("a\"b", 1, "a\\b", 2, "Alice's", 3)); | ||
| assertEquals(1, engine.resolve(document, "c.map[\"a\\\"b\"]")); | ||
| assertEquals(2, engine.resolve(document, "c.map[\"a\\\\b\"]")); | ||
| assertEquals(3, engine.resolve(document, "c.map['Alice''s']")); | ||
| assertNull(engine.resolve(document, "c.map[\"missing\"].value")); | ||
| assertNull(engine.resolve(document, "c.map[\"a\"b\"]")); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.