diff --git a/Microsoft.Azure.Cosmos/src/FeedRange/FeedRanges/FeedRangePartitionKey.cs b/Microsoft.Azure.Cosmos/src/FeedRange/FeedRanges/FeedRangePartitionKey.cs index 47f84fd280..1a04800b50 100644 --- a/Microsoft.Azure.Cosmos/src/FeedRange/FeedRanges/FeedRangePartitionKey.cs +++ b/Microsoft.Azure.Cosmos/src/FeedRange/FeedRanges/FeedRangePartitionKey.cs @@ -4,11 +4,13 @@ namespace Microsoft.Azure.Cosmos { + using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.Cosmos.Routing; using Microsoft.Azure.Cosmos.Tracing; + using Microsoft.Azure.Documents; /// /// FeedRange that represents an exact Partition Key value. @@ -31,8 +33,13 @@ public FeedRangePartitionKey(PartitionKey partitionKey) return Task.FromResult( new List> { - Documents.Routing.Range.GetPointRange( - this.PartitionKey.InternalKey.GetEffectivePartitionKeyString(partitionKeyDefinition)) + Documents.Routing.PartitionKeyInternal.GetEffectivePartitionKeyRange( + partitionKeyDefinition, + new Documents.Routing.Range( + min: this.PartitionKey.InternalKey, + max: this.PartitionKey.InternalKey, + isMinInclusive: true, + isMaxInclusive: true)) }); } diff --git a/Microsoft.Azure.Cosmos/src/Query/Core/QueryPlan/QueryPartitionProvider.cs b/Microsoft.Azure.Cosmos/src/Query/Core/QueryPlan/QueryPartitionProvider.cs index 6e7b0b4a8f..c62a65b27a 100644 --- a/Microsoft.Azure.Cosmos/src/Query/Core/QueryPlan/QueryPartitionProvider.cs +++ b/Microsoft.Azure.Cosmos/src/Query/Core/QueryPlan/QueryPartitionProvider.cs @@ -147,11 +147,7 @@ internal PartitionedQueryExecutionInfo ConvertPartitionedQueryExecutionInfo( List> effectiveRanges = new List>(queryInfoInternal.QueryRanges.Count); foreach (Documents.Routing.Range internalRange in queryInfoInternal.QueryRanges) { - effectiveRanges.Add(new Documents.Routing.Range( - internalRange.Min.GetEffectivePartitionKeyString(partitionKeyDefinition, false), - internalRange.Max.GetEffectivePartitionKeyString(partitionKeyDefinition, false), - internalRange.IsMinInclusive, - internalRange.IsMaxInclusive)); + effectiveRanges.Add(PartitionKeyInternal.GetEffectivePartitionKeyRange(partitionKeyDefinition, internalRange)); } effectiveRanges.Sort(Documents.Routing.Range.MinComparer.Instance); diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Routing/PartitionRoutingHelperTest.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Routing/PartitionRoutingHelperTest.cs index 35ffaa65bb..792621f54a 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Routing/PartitionRoutingHelperTest.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Routing/PartitionRoutingHelperTest.cs @@ -19,6 +19,8 @@ namespace Microsoft.Azure.Cosmos.Tests.Routing using Microsoft.Azure.Cosmos.Query.Core.QueryPlan; using System.Collections.ObjectModel; using System.Net; + using Microsoft.Azure.Cosmos.Query.Core; + using Microsoft.Azure.Cosmos.Query.Core.Monads; /// /// Tests for class. @@ -246,6 +248,521 @@ public async Task TestGetPartitionRoutingInfo() } } } + [TestMethod] + public async Task TestRoutingForPrefixedPartitionKeyQueriesAsync() + { + PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition() + { + Kind = PartitionKind.MultiHash, + Paths = new Collection() { "/path1", "/path2", "/path3" }, + Version = PartitionKeyDefinitionVersion.V2 + }; + + // Case 1: Query with 1 prefix path, split at 1st level. Should route to only one partition. + { + List partitionKeyRanges = new List() + { + new PartitionKeyRange() + { + Id = "0", + MinInclusive = string.Empty, + MaxExclusive = "07E4D14180A45153F00B44907886F856" //Seattle + }, + new PartitionKeyRange() + { + Id = "1", + MinInclusive = "07E4D14180A45153F00B44907886F856", //Seattle + MaxExclusive = "FF" + }, + }; + await PrefixPartitionKeyTestRunnerAsync( + partitionKeyDefinition, + $"SELECT VALUE r.id from r where r.path1 = \"Microsoft\"", + epkRanges => + { + return epkRanges.Count == 1; //Routes to only one pkRange. + }, + partitionKeyRanges); + } + + //Case 2: Query with 1 prefix path value which is split at 2nd level. Should route to two partitions. + //Case 3: Query with 2 prefix path values which is split at 2nd level. Should route to one partition. + { + List partitionKeyRanges = new List() + { + new PartitionKeyRange() + { + Id = "0", + MinInclusive = string.Empty, + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963" //[Seattle, Redmond] + }, + new PartitionKeyRange() + { + Id = "1", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963",//[Seattle, Redmond] + MaxExclusive = "FF" + }, + }; + await PrefixPartitionKeyTestRunnerAsync( + partitionKeyDefinition, + $"SELECT VALUE r.id from r where r.path1 = \"seattle\" or (r.path1 = \"seattle\" and r.path2 = \"bellevue\")", + epkRanges => + { + return epkRanges.Count == 2; //Since data is split at pkey [seattle, redmond], it should route to two pkRange. + }, + partitionKeyRanges); + await PrefixPartitionKeyTestRunnerAsync( + partitionKeyDefinition, + $"SELECT VALUE r.id from r where r.path1 = \"seattle\" and r.path2 =\"redmond\"", + epkRanges => + { + return epkRanges.Count == 1; //Since data is split at pkey [seattle, redmond], this query should route to one pkRange + }, + partitionKeyRanges); + } + + //Case 4: Query with 2 prefix path values split at the 3rd level. Should route to 2 paritions. + //Case 5: Query with 1 prefix path value split at 3rd level. Should route to 2 partitions. + { + List partitionKeyRanges = new List() + { + new PartitionKeyRange() + { + Id = "0", + MinInclusive = string.Empty, + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8"//[seattle, redmond, 5.12312419050912359123] + }, + new PartitionKeyRange() + { + Id = "1", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8",//[seattle, redmond, 5.12312419050912359123] + MaxExclusive = "FF" + }, + }; + await PrefixPartitionKeyTestRunnerAsync( + partitionKeyDefinition, + $"SELECT VALUE r.id from r where r.path1 = \"seattle\" and r.path2 =\"redmond\"", + epkRanges => + { + return epkRanges.Count == 2; //Since data is split at pkey [seattle, redmond, 5.12312419050912359123], it should route to two pkRange. + }, + partitionKeyRanges); + await PrefixPartitionKeyTestRunnerAsync( + partitionKeyDefinition, + $"SELECT VALUE r.id from r where r.path1 = \"seattle\" and r.path2 =\"redmond\" and r.path3=5.12312419050912359123", + epkRanges => + { + return epkRanges.Count == 1; + }, + partitionKeyRanges); + await PrefixPartitionKeyTestRunnerAsync( + partitionKeyDefinition, + $"SELECT VALUE r.id from r where r.path1 = \"seattle\"", + epkRanges => + { + return epkRanges.Count == 2; + }, + partitionKeyRanges); + } + + //Case 6: Query with 1 prefix path value split succesively at 2nd and then at the 3rd level. Should route to 3 partitions. + { + List partitionKeyRanges = new List() + { + new PartitionKeyRange() + { + Id = "0", + MinInclusive = string.Empty, + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963"//[seattle, redmond] + }, + new PartitionKeyRange() + { + Id = "1", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963",//[seattle, redmond] + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8"//[seattle, redmond, 5.12312419050912359123] + }, + new PartitionKeyRange() + { + Id = "2", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8",//[seattle, redmond, 5.12312419050912359123] + MaxExclusive = "FF" + }, + }; + await PrefixPartitionKeyTestRunnerAsync( + partitionKeyDefinition, + $"SELECT VALUE r.id from r where r.path1 = \"seattle\"", + epkRanges => + { + return epkRanges.Count == 3; + }, + partitionKeyRanges); + } + + //Case 7: Query with 1 prefix path value split succesively at 1st, 2nd and then at the 3rd level. Should route to 3 partitions. + //Case 8: Query with 2 prefix path value split succesively at 1st, 2nd and then at the 3rd level. Should route to 2 partitions. + //Case 9: Query with fully specfied pkey, split succesively at 1st, 2nd and then at the 3rd level. Should route to 1 partitions. + { + List partitionKeyRanges = new List() + { + new PartitionKeyRange() + { + Id = "0", + MinInclusive = string.Empty, + MaxExclusive = "07E4D14180A45153F00B44907886F856" //[seattle] + }, + new PartitionKeyRange() + { + Id = "1", + MinInclusive = "07E4D14180A45153F00B44907886F856", //[seattle] + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963" //[seattle, redmond] + }, + new PartitionKeyRange() + { + Id = "2", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963", //[seattle, redmond] + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8"//[seattle, redmond, 5.12312419050912359123] + }, + new PartitionKeyRange() + { + Id = "3", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8",//[seattle, redmond, 5.12312419050912359123] + MaxExclusive = "FF" + }, + }; + await PrefixPartitionKeyTestRunnerAsync( + partitionKeyDefinition, + $"SELECT VALUE r.id from r where r.path1 = \"seattle\"", + epkRanges => + { + return epkRanges.Count == 3; //Routes tp three pkRanges + }, + partitionKeyRanges); + await PrefixPartitionKeyTestRunnerAsync( + partitionKeyDefinition, + $"SELECT VALUE r.id from r where r.path1 = \"seattle\" and r.path2 = \"redmond\"", + epkRanges => + { + return epkRanges.Count == 2; //Routes to two pkRanges. + }, + partitionKeyRanges); + await PrefixPartitionKeyTestRunnerAsync( + partitionKeyDefinition, + $"SELECT VALUE r.id from r where r.path1 = \"seattle\" and r.path2 = \"redmond\" and r.path3 = \"98052\"", + epkRanges => + { + Assert.AreEqual(epkRanges.Count, 1); + + return epkRanges.Count == 1; //Routes to only one pkRanges. + }, + partitionKeyRanges); + } + } + + [TestMethod] + public async Task TestRoutingForPrefixedPartitionKeyChangeFeedAsync() + { + PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition() + { + Kind = PartitionKind.MultiHash, + Paths = new Collection() { "/path1", "/path2", "/path3" }, + Version = PartitionKeyDefinitionVersion.V2 + }; + + // Case 1: ChangeFeed with 1 prefix path, split at 1st level. Should route to only one partition. + { + List partitionKeyRanges = new List() + { + new PartitionKeyRange() + { + Id = "0", + MinInclusive = string.Empty, + MaxExclusive = "07E4D14180A45153F00B44907886F856" //Seattle + }, + new PartitionKeyRange() + { + Id = "1", + MinInclusive = "07E4D14180A45153F00B44907886F856", //Seattle + MaxExclusive = "FF" + }, + }; + await PrefixPartitionKeyChangeFeedTestRunnerAsync( + partitionKeyDefinition, + new PartitionKeyBuilder().Add("Microsoft").Build(), + epkRanges => + { + return epkRanges.Count == 1; //Routes to only one pkRange. + }, + partitionKeyRanges); + } + + //Case 2: ChangeFeed with 1 prefix path value which is split at 2nd level. Should route to two partitions. + //Case 3: ChangeFeed with 2 prefix path values which is split at 2nd level. Should route to one partition. + { + List partitionKeyRanges = new List() + { + new PartitionKeyRange() + { + Id = "0", + MinInclusive = string.Empty, + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963" //[Seattle, Redmond] + }, + new PartitionKeyRange() + { + Id = "1", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963", //[Seattle, Redmond] + MaxExclusive = "FF" + }, + }; + await PrefixPartitionKeyChangeFeedTestRunnerAsync( + partitionKeyDefinition, + new PartitionKeyBuilder().Add("seattle").Build(), + epkRanges => + { + return epkRanges.Count == 2; //Since data is split at pkey [seattle, redmond], it should route to two pkRange. + }, + partitionKeyRanges); + await PrefixPartitionKeyChangeFeedTestRunnerAsync( + partitionKeyDefinition, + new PartitionKeyBuilder().Add("seattle").Add("redmond").Build(), + epkRanges => + { + return epkRanges.Count == 1; //Since data is split at pkey [seattle, redmond], this query should route to one pkRange + }, + partitionKeyRanges); + } + + //Case 4: ChangeFeed with 2 prefix path values split at the 3rd level. Should route to 2 paritions. + //Case 5: ChangeFeed with 1 prefix path value split at 3rd level. Should route to 2 partitions. + { + List partitionKeyRanges = new List() + { + new PartitionKeyRange() + { + Id = "0", + MinInclusive = string.Empty, + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8"//[seattle, redmond, 5.12312419050912359123] + }, + new PartitionKeyRange() + { + Id = "1", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8", //[seattle, redmond, 5.12312419050912359123] + MaxExclusive = "FF" + }, + }; + await PrefixPartitionKeyChangeFeedTestRunnerAsync( + partitionKeyDefinition, + new PartitionKeyBuilder().Add("seattle").Add("redmond").Build(), + epkRanges => + { + return epkRanges.Count == 2; //Since data is split at pkey [seattle, redmond, 5.12312419050912359123], it should route to two pkRange. + }, + partitionKeyRanges); + await PrefixPartitionKeyChangeFeedTestRunnerAsync( + partitionKeyDefinition, + new PartitionKeyBuilder().Add("seattle").Add("redmond").Add(5.12312419050912359123).Build(), + epkRanges => + { + return epkRanges.Count == 1; + }, + partitionKeyRanges); + await PrefixPartitionKeyChangeFeedTestRunnerAsync( + partitionKeyDefinition, + new PartitionKeyBuilder().Add("seattle").Build(), + epkRanges => + { + return epkRanges.Count == 2; + }, + partitionKeyRanges); + } + + //Case 6: ChangeFeed with 1 prefix path value split succesively at 2nd and then at the 3rd level. Should route to 3 partitions. + { + List partitionKeyRanges = new List() + { + new PartitionKeyRange() + { + Id = "0", + MinInclusive = string.Empty, + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963" //[seattle, redmond] + }, + new PartitionKeyRange() + { + Id = "1", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963", //[seattle, redmond] + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8" //[seattle, redmond, 5.12312419050912359123] + }, + new PartitionKeyRange() + { + Id = "2", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8", //[seattle, redmond, 5.12312419050912359123] + MaxExclusive = "FF" + }, + }; + await PrefixPartitionKeyChangeFeedTestRunnerAsync( + partitionKeyDefinition, + new PartitionKeyBuilder().Add("seattle").Build(), + epkRanges => + { + return epkRanges.Count == 3; + }, + partitionKeyRanges); + } + + //Case 7: ChangeFeed with 1 prefix path value split succesively at 1st, 2nd and then at the 3rd level. Should route to 3 partitions. + //Case 8: ChangeFeed with 2 prefix path value split succesively at 1st, 2nd and then at the 3rd level. Should route to 2 partitions. + //Case 9: ChangeFeed with fully specfied pkey, split succesively at 1st, 2nd and then at the 3rd level. Should route to 1 partitions. + { + List partitionKeyRanges = new List() + { + new PartitionKeyRange() + { + Id = "0", + MinInclusive = string.Empty, + MaxExclusive = "07E4D14180A45153F00B44907886F856" //[seattle] + }, + new PartitionKeyRange() + { + Id = "1", + MinInclusive = "07E4D14180A45153F00B44907886F856", //[seattle] + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963" //[seattle, redmond] + }, + new PartitionKeyRange() + { + Id = "2", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A5963", //[seattle, redmond] + MaxExclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8"//[seattle, redmond, 5.12312419050912359123] + }, + new PartitionKeyRange() + { + Id = "3", + MinInclusive = "07E4D14180A45153F00B44907886F85622E342F38A486A088463DFF7838A59630EF2E2D82460884AF0F6440BE4F726A8", //[seattle, redmond, 5.12312419050912359123] + MaxExclusive = "FF" + }, + }; + await PrefixPartitionKeyChangeFeedTestRunnerAsync( + partitionKeyDefinition, + new PartitionKeyBuilder().Add("seattle").Build(), + epkRanges => + { + return epkRanges.Count == 3; //Routes tp three pkRanges + }, + partitionKeyRanges); + await PrefixPartitionKeyChangeFeedTestRunnerAsync( + partitionKeyDefinition, + new PartitionKeyBuilder().Add("seattle").Add("redmond").Build(), + epkRanges => + { + return epkRanges.Count == 2; //Routes to two pkRanges. + }, + partitionKeyRanges); + await PrefixPartitionKeyChangeFeedTestRunnerAsync( + partitionKeyDefinition, + new PartitionKeyBuilder().Add("seattle").Add("redmond").Add("98052").Build(), + epkRanges => + { + Assert.AreEqual(epkRanges.Count, 1); + + return epkRanges.Count == 1; //Routes to only one pkRanges. + }, + partitionKeyRanges); + } + } + + private static async Task PrefixPartitionKeyTestRunnerAsync( + PartitionKeyDefinition partitionKeyDefinition, + string queryText, + Predicate> validator, + List partitionKeyRanges) + { + IDictionary DefaultQueryengineConfiguration = new Dictionary() + { + {"maxSqlQueryInputLength", 30720}, + {"maxJoinsPerSqlQuery", 5}, + {"maxLogicalAndPerSqlQuery", 200}, + {"maxLogicalOrPerSqlQuery", 200}, + {"maxUdfRefPerSqlQuery", 2}, + {"maxInExpressionItemsCount", 8000}, + {"queryMaxInMemorySortDocumentCount", 500}, + {"maxQueryRequestTimeoutFraction", 0.90}, + {"sqlAllowNonFiniteNumbers", false}, + {"sqlAllowAggregateFunctions", true}, + {"sqlAllowSubQuery", true}, + {"sqlAllowScalarSubQuery", false}, + {"allowNewKeywords", true}, + {"sqlAllowLike", true}, + {"sqlAllowGroupByClause", false}, + {"queryEnableMongoNativeRegex", true}, + {"maxSpatialQueryCells", 12}, + {"spatialMaxGeometryPointCount", 256}, + {"sqlDisableOptimizationFlags", 0}, + {"sqlEnableParameterExpansionCheck", true} + }; + + QueryPartitionProvider QueryPartitionProvider = new QueryPartitionProvider(DefaultQueryengineConfiguration); + + IEnumerable> rangesAndServiceIdentity = partitionKeyRanges + .Select(range => Tuple.Create(range, (ServiceIdentity)null)); + string collectionRid = string.Empty; + CollectionRoutingMap routingMap = + CollectionRoutingMap.TryCreateCompleteRoutingMap( + rangesAndServiceIdentity, + collectionRid); + + RoutingMapProvider routingMapProvider = new RoutingMapProvider(routingMap); + TryCatch tryGetQueryPlan = + QueryPartitionProvider.TryGetPartitionedQueryExecutionInfo( + querySpecJsonString: JsonConvert.SerializeObject(new SqlQuerySpec(queryText)), + partitionKeyDefinition: partitionKeyDefinition, + requireFormattableOrderByQuery: true, + isContinuationExpected: true, + allowNonValueAggregateQuery: false, + hasLogicalPartitionKey: false, + allowDCount: true, + useSystemPrefix: false, + geospatialType: Cosmos.GeospatialType.Geography); + + HashSet resolvedPKRanges = new HashSet(); + foreach (Range range in tryGetQueryPlan.Result.QueryRanges) + { + resolvedPKRanges.UnionWith(await routingMapProvider.TryGetOverlappingRangesAsync( + collectionRid, + range, + NoOpTrace.Singleton)); + } + + Assert.IsTrue(validator(resolvedPKRanges)); + } + + private static async Task PrefixPartitionKeyChangeFeedTestRunnerAsync( + PartitionKeyDefinition partitionKeyDefinition, + Cosmos.PartitionKey partitionKey, + Predicate> validator, + List partitionKeyRanges) + { + IEnumerable> rangesAndServiceIdentity = partitionKeyRanges + .Select(range => Tuple.Create(range, (ServiceIdentity)null)); + string collectionRid = string.Empty; + CollectionRoutingMap routingMap = + CollectionRoutingMap.TryCreateCompleteRoutingMap( + rangesAndServiceIdentity, + collectionRid); + + RoutingMapProvider routingMapProvider = new RoutingMapProvider(routingMap); + + HashSet resolvedPKRanges = new HashSet(); + FeedRangePartitionKey feedRangePartitionKey = new FeedRangePartitionKey(partitionKey); + List> effectiveRanges = await feedRangePartitionKey.GetEffectiveRangesAsync(routingMapProvider, null, partitionKeyDefinition, null); + foreach (Range range in effectiveRanges) + { + resolvedPKRanges.UnionWith(await routingMapProvider.TryGetOverlappingRangesAsync( + collectionRid, + range, + NoOpTrace.Singleton)); + + } + + Assert.IsTrue(validator(resolvedPKRanges)); + } [TestMethod] public void TestCrossPartitionAggregateQueries()