-
Notifications
You must be signed in to change notification settings - Fork 536
Expand file tree
/
Copy pathDocumentQueryExecutionContextFactory.cs
More file actions
281 lines (255 loc) · 13.4 KB
/
DocumentQueryExecutionContextFactory.cs
File metadata and controls
281 lines (255 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Query
{
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Common;
using Microsoft.Azure.Cosmos.Query.Core.QueryPlan;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Documents;
/// <summary>
/// Factory class for creating the appropriate DocumentQueryExecutionContext for the provided type of query.
/// </summary>
internal static class DocumentQueryExecutionContextFactory
{
private const int PageSizeFactorForTop = 5;
public static async Task<IDocumentQueryExecutionContext> CreateDocumentQueryExecutionContextAsync(
IDocumentQueryClient client,
ResourceType resourceTypeEnum,
Type resourceType,
Expression expression,
FeedOptions feedOptions,
string resourceLink,
bool isContinuationExpected,
CancellationToken token,
Guid correlatedActivityId)
{
ContainerProperties collection = null;
if (resourceTypeEnum.IsCollectionChild())
{
CollectionCache collectionCache = await client.GetCollectionCacheAsync();
using (
DocumentServiceRequest request = DocumentServiceRequest.Create(
OperationType.Query,
resourceTypeEnum,
resourceLink,
AuthorizationTokenType.Invalid)) //this request doesnt actually go to server
{
collection = await collectionCache.ResolveCollectionAsync(request, token, NoOpTrace.Singleton);
}
if (feedOptions?.PartitionKey != null && feedOptions.PartitionKey.Equals(Documents.PartitionKey.None))
{
feedOptions.PartitionKey = Documents.PartitionKey.FromInternalKey(collection.GetNoneValue());
}
}
DocumentQueryExecutionContextBase.InitParams constructorParams = new DocumentQueryExecutionContextBase.InitParams(
client,
resourceTypeEnum,
resourceType,
expression,
feedOptions,
resourceLink,
false,
correlatedActivityId);
// For non-Windows platforms(like Linux and OSX) in .NET Core SDK, we cannot use ServiceInterop, so need to bypass in that case.
// We are also now bypassing this for 32 bit host process running even on Windows as there are many 32 bit apps that will not work without this
if (QueryPlanRetriever.BypassQueryParsing())
{
// We create a ProxyDocumentQueryExecutionContext that will be initialized with DefaultDocumentQueryExecutionContext
// which will be used to send the query to Gateway and on getting 400(bad request) with 1004(cross partition query not servable), we initialize it with
// PipelinedDocumentQueryExecutionContext by providing the partition query execution info that's needed(which we get from the exception returned from Gateway).
ProxyDocumentQueryExecutionContext proxyQueryExecutionContext =
ProxyDocumentQueryExecutionContext.Create(
client,
resourceTypeEnum,
resourceType,
expression,
feedOptions,
resourceLink,
token,
collection,
isContinuationExpected,
correlatedActivityId);
return proxyQueryExecutionContext;
}
DefaultDocumentQueryExecutionContext queryExecutionContext = await DefaultDocumentQueryExecutionContext.CreateAsync(
constructorParams, isContinuationExpected, token);
// If isContinuationExpected is false, we want to check if there are aggregates.
if (
resourceTypeEnum.IsCollectionChild()
&& resourceTypeEnum.IsPartitioned()
&& (feedOptions.EnableCrossPartitionQuery || !isContinuationExpected))
{
//todo:elasticcollections this may rely on information from collection cache which is outdated
//if collection is deleted/created with same name.
//need to make it not rely on information from collection cache.
PartitionedQueryExecutionInfo partitionedQueryExecutionInfo = await queryExecutionContext.GetPartitionedQueryExecutionInfoAsync(
partitionKeyDefinition: collection.PartitionKey,
vectorEmbeddingPolicy: collection.VectorEmbeddingPolicy,
requireFormattableOrderByQuery: true,
isContinuationExpected: isContinuationExpected,
allowNonValueAggregateQuery: true,
hasLogicalPartitionKey: feedOptions.PartitionKey != null,
allowDCount: true,
geospatialType: collection.GeospatialConfig.GeospatialType,
cancellationToken: token);
if (DocumentQueryExecutionContextFactory.ShouldCreateSpecializedDocumentQueryExecutionContext(
resourceTypeEnum,
feedOptions,
partitionedQueryExecutionInfo,
collection.PartitionKey,
isContinuationExpected))
{
List<PartitionKeyRange> targetRanges = await GetTargetPartitionKeyRangesAsync(
queryExecutionContext,
partitionedQueryExecutionInfo,
collection,
feedOptions);
// Devnote this will get replace by the new v3 to v2 logic
throw new NotSupportedException("v2 query excution context is currently not supported.");
}
}
return queryExecutionContext;
}
/// <summary>
/// Gets the list of partition key ranges.
/// 1. Check partition key range id
/// 2. Check Partition key
/// 3. Check the effective partition key
/// 4. Get the range from the PartitionedQueryExecutionInfo
/// </summary>
internal static async Task<List<PartitionKeyRange>> GetTargetPartitionKeyRangesAsync(
DefaultDocumentQueryExecutionContext queryExecutionContext,
PartitionedQueryExecutionInfo partitionedQueryExecutionInfo,
ContainerProperties collection,
FeedOptions feedOptions)
{
List<PartitionKeyRange> targetRanges = null;
if (!string.IsNullOrEmpty(feedOptions.PartitionKeyRangeId))
{
targetRanges = new List<PartitionKeyRange>()
{
await queryExecutionContext.GetTargetPartitionKeyRangeByIdAsync(
collection.ResourceId,
feedOptions.PartitionKeyRangeId)
};
}
else if (feedOptions.PartitionKey != null)
{
targetRanges = await queryExecutionContext.GetTargetPartitionKeyRangesByEpkStringAsync(
collection.ResourceId,
feedOptions.PartitionKey.InternalKey.GetEffectivePartitionKeyString(collection.PartitionKey));
}
else if (TryGetEpkProperty(feedOptions, out string effectivePartitionKeyString))
{
targetRanges = await queryExecutionContext.GetTargetPartitionKeyRangesByEpkStringAsync(
collection.ResourceId,
effectivePartitionKeyString);
}
else
{
targetRanges = await queryExecutionContext.GetTargetPartitionKeyRangesAsync(
collection.ResourceId,
partitionedQueryExecutionInfo.QueryRanges);
}
return targetRanges;
}
private static bool ShouldCreateSpecializedDocumentQueryExecutionContext(
ResourceType resourceTypeEnum,
FeedOptions feedOptions,
PartitionedQueryExecutionInfo partitionedQueryExecutionInfo,
PartitionKeyDefinition partitionKeyDefinition,
bool isContinuationExpected)
{
// We need to aggregate the total results with Pipelined~Context if isContinuationExpected is false.
return
(DocumentQueryExecutionContextFactory.IsCrossPartitionQuery(
resourceTypeEnum,
feedOptions,
partitionKeyDefinition,
partitionedQueryExecutionInfo) &&
(DocumentQueryExecutionContextFactory.IsTopOrderByQuery(partitionedQueryExecutionInfo) ||
DocumentQueryExecutionContextFactory.IsAggregateQuery(partitionedQueryExecutionInfo) ||
DocumentQueryExecutionContextFactory.IsOffsetLimitQuery(partitionedQueryExecutionInfo) ||
DocumentQueryExecutionContextFactory.IsParallelQuery(feedOptions)) ||
!string.IsNullOrEmpty(feedOptions.PartitionKeyRangeId)) ||
// Even if it's single partition query we create a specialized context to aggregate the aggregates and distinct of distinct.
DocumentQueryExecutionContextFactory.IsAggregateQueryWithoutContinuation(
partitionedQueryExecutionInfo,
isContinuationExpected) ||
DocumentQueryExecutionContextFactory.IsDistinctQuery(partitionedQueryExecutionInfo) ||
DocumentQueryExecutionContextFactory.IsGroupByQuery(partitionedQueryExecutionInfo) ||
DocumentQueryExecutionContextFactory.IsDCountQuery(partitionedQueryExecutionInfo);
}
private static bool IsCrossPartitionQuery(
ResourceType resourceTypeEnum,
FeedOptions feedOptions,
PartitionKeyDefinition partitionKeyDefinition,
PartitionedQueryExecutionInfo partitionedQueryExecutionInfo)
{
return resourceTypeEnum.IsPartitioned()
&& (feedOptions.PartitionKey == null && feedOptions.EnableCrossPartitionQuery)
&& (partitionKeyDefinition.Paths.Count > 0)
&& !(partitionedQueryExecutionInfo.QueryRanges.Count == 1 && partitionedQueryExecutionInfo.QueryRanges[0].IsSingleValue);
}
private static bool IsParallelQuery(FeedOptions feedOptions)
{
return (feedOptions.MaxDegreeOfParallelism != 0);
}
private static bool IsTopOrderByQuery(PartitionedQueryExecutionInfo partitionedQueryExecutionInfo)
{
return (partitionedQueryExecutionInfo.QueryInfo != null)
&& (partitionedQueryExecutionInfo.QueryInfo.HasOrderBy || partitionedQueryExecutionInfo.QueryInfo.HasTop);
}
private static bool IsAggregateQuery(PartitionedQueryExecutionInfo partitionedQueryExecutionInfo)
{
return (partitionedQueryExecutionInfo.QueryInfo != null)
&& (partitionedQueryExecutionInfo.QueryInfo.HasAggregates);
}
private static bool IsAggregateQueryWithoutContinuation(PartitionedQueryExecutionInfo partitionedQueryExecutionInfo, bool isContinuationExpected)
{
return IsAggregateQuery(partitionedQueryExecutionInfo) && !isContinuationExpected;
}
private static bool IsOffsetLimitQuery(PartitionedQueryExecutionInfo partitionedQueryExecutionInfo)
{
return partitionedQueryExecutionInfo.QueryInfo.HasOffset && partitionedQueryExecutionInfo.QueryInfo.HasLimit;
}
private static bool IsDistinctQuery(PartitionedQueryExecutionInfo partitionedQueryExecutionInfo)
{
return partitionedQueryExecutionInfo.QueryInfo.HasDistinct;
}
private static bool IsGroupByQuery(PartitionedQueryExecutionInfo partitionedQueryExecutionInfo)
{
return partitionedQueryExecutionInfo.QueryInfo.HasGroupBy;
}
private static bool IsDCountQuery(PartitionedQueryExecutionInfo partitionedQueryExecutionInfo)
{
return partitionedQueryExecutionInfo.QueryInfo.HasDCount;
}
private static bool TryGetEpkProperty(
FeedOptions feedOptions,
out string effectivePartitionKeyString)
{
if (feedOptions?.Properties != null
&& feedOptions.Properties.TryGetValue(
WFConstants.BackendHeaders.EffectivePartitionKeyString,
out object effectivePartitionKeyStringObject))
{
effectivePartitionKeyString = effectivePartitionKeyStringObject as string;
if (string.IsNullOrEmpty(effectivePartitionKeyString))
{
throw new ArgumentOutOfRangeException(nameof(effectivePartitionKeyString));
}
return true;
}
effectivePartitionKeyString = null;
return false;
}
}
}