-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
CosmosProjectionBindingExpressionVisitor.cs
377 lines (323 loc) · 16.8 KB
/
CosmosProjectionBindingExpressionVisitor.cs
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Storage;
namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public class CosmosProjectionBindingExpressionVisitor : ExpressionVisitor
{
private readonly CosmosSqlTranslatingExpressionVisitor _sqlTranslator;
private SelectExpression _selectExpression;
private bool _clientEval;
private readonly IDictionary<ProjectionMember, Expression> _projectionMapping
= new Dictionary<ProjectionMember, Expression>();
private readonly Stack<ProjectionMember> _projectionMembers = new Stack<ProjectionMember>();
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public CosmosProjectionBindingExpressionVisitor(CosmosSqlTranslatingExpressionVisitor sqlTranslator)
{
_sqlTranslator = sqlTranslator;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Expression Translate(SelectExpression selectExpression, Expression expression)
{
_selectExpression = selectExpression;
_clientEval = false;
_projectionMembers.Push(new ProjectionMember());
var result = Visit(expression);
if (result == null)
{
_clientEval = true;
result = Visit(expression);
_projectionMapping.Clear();
}
_selectExpression.ReplaceProjectionMapping(_projectionMapping);
_selectExpression = null;
_projectionMembers.Clear();
_projectionMapping.Clear();
return result;
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public override Expression Visit(Expression expression)
{
if (expression == null)
{
return null;
}
if (expression is NewExpression
|| expression is MemberInitExpression
|| expression is EntityShaperExpression)
{
return base.Visit(expression);
}
// This skips the group parameter from GroupJoin
if (expression is ParameterExpression parameter
&& parameter.Type.IsGenericType
&& parameter.Type.GetGenericTypeDefinition() == typeof(IEnumerable<>))
{
return parameter;
}
if (_clientEval)
{
switch (expression)
{
case ConstantExpression _:
return expression;
case ParameterExpression parameterExpression:
return Expression.Call(
_getParameterValueMethodInfo.MakeGenericMethod(parameterExpression.Type),
QueryCompilationContext.QueryContextParameter,
Expression.Constant(parameterExpression.Name));
case MaterializeCollectionNavigationExpression materializeCollectionNavigationExpression:
return base.Visit(expression);
//return _selectExpression.AddCollectionProjection(
// _queryableMethodTranslatingExpressionVisitor.TranslateSubquery(
// materializeCollectionNavigationExpression.Subquery),
// materializeCollectionNavigationExpression.Navigation, null);
}
var translation = _sqlTranslator.Translate(expression);
if (translation == null)
{
return base.Visit(expression);
}
return new ProjectionBindingExpression(
_selectExpression, _selectExpression.AddToProjection(translation), expression.Type);
}
else
{
var translation = _sqlTranslator.Translate(expression);
if (translation == null)
{
return null;
}
_projectionMapping[_projectionMembers.Peek()] = translation;
return new ProjectionBindingExpression(_selectExpression, _projectionMembers.Peek(), expression.Type);
}
}
private static readonly MethodInfo _getParameterValueMethodInfo
= typeof(CosmosProjectionBindingExpressionVisitor)
.GetTypeInfo().GetDeclaredMethod(nameof(GetParameterValue));
#pragma warning disable IDE0052 // Remove unread private members
private static T GetParameterValue<T>(QueryContext queryContext, string parameterName)
#pragma warning restore IDE0052 // Remove unread private members
=> (T)queryContext.ParameterValues[parameterName];
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitMember(MemberExpression memberExpression)
{
if (!_clientEval)
{
return null;
}
var innerExpression = Visit(memberExpression.Expression);
EntityShaperExpression shaperExpression;
switch (innerExpression)
{
case EntityShaperExpression shaper:
shaperExpression = shaper;
break;
case UnaryExpression unaryExpression:
shaperExpression = unaryExpression.Operand as EntityShaperExpression;
if (shaperExpression == null)
{
return memberExpression.Update(innerExpression);
}
break;
default:
return memberExpression.Update(innerExpression);
}
EntityProjectionExpression innerEntityProjection;
switch (shaperExpression.ValueBufferExpression)
{
case ProjectionBindingExpression innerProjectionBindingExpression:
innerEntityProjection = (EntityProjectionExpression)_selectExpression.Projection[
innerProjectionBindingExpression.Index.Value].Expression;
break;
case UnaryExpression unaryExpression:
innerEntityProjection = (EntityProjectionExpression)((UnaryExpression)unaryExpression.Operand).Operand;
break;
default:
throw new InvalidOperationException();
}
var navigationProjection = innerEntityProjection.BindMember(
memberExpression.Member, innerExpression.Type, clientEval: true, out var propertyBase);
if (!(propertyBase is INavigation navigation)
|| !navigation.IsEmbedded())
{
return memberExpression.Update(innerExpression);
}
switch (navigationProjection)
{
case EntityProjectionExpression entityProjection:
return new EntityShaperExpression(
navigation.GetTargetType(),
Expression.Convert(Expression.Convert(entityProjection, typeof(object)), typeof(ValueBuffer)),
nullable: true);
case ObjectArrayProjectionExpression objectArrayProjectionExpression:
{
var innerShaperExpression = new EntityShaperExpression(
navigation.GetTargetType(),
Expression.Convert(
Expression.Convert(objectArrayProjectionExpression.InnerProjection, typeof(object)), typeof(ValueBuffer)),
nullable: true);
return new CollectionShaperExpression(
objectArrayProjectionExpression,
innerShaperExpression,
navigation,
innerShaperExpression.EntityType.ClrType);
}
default:
throw new InvalidOperationException();
}
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitExtension(Expression extensionExpression)
{
switch (extensionExpression)
{
case EntityShaperExpression entityShaperExpression:
{
var projectionBindingExpression = (ProjectionBindingExpression)entityShaperExpression.ValueBufferExpression;
VerifySelectExpression(projectionBindingExpression);
if (_clientEval)
{
var entityProjection = (EntityProjectionExpression)_selectExpression.GetMappedProjection(
projectionBindingExpression.ProjectionMember);
return entityShaperExpression.Update(
new ProjectionBindingExpression(
_selectExpression, _selectExpression.AddToProjection(entityProjection), typeof(ValueBuffer)));
}
_projectionMapping[_projectionMembers.Peek()]
= _selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember);
return entityShaperExpression.Update(
new ProjectionBindingExpression(_selectExpression, _projectionMembers.Peek(), typeof(ValueBuffer)));
}
case MaterializeCollectionNavigationExpression materializeCollectionNavigationExpression:
return materializeCollectionNavigationExpression.Navigation.IsEmbedded()
? base.Visit(materializeCollectionNavigationExpression.Subquery)
: base.VisitExtension(materializeCollectionNavigationExpression);
case IncludeExpression includeExpression:
return _clientEval ? base.VisitExtension(includeExpression) : null;
default:
throw new InvalidOperationException(new ExpressionPrinter().Print(extensionExpression));
}
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitNew(NewExpression newExpression)
{
if (newExpression.Arguments.Count == 0)
{
return newExpression;
}
if (!_clientEval
&& newExpression.Members == null)
{
return null;
}
var newArguments = new Expression[newExpression.Arguments.Count];
for (var i = 0; i < newArguments.Length; i++)
{
if (_clientEval)
{
newArguments[i] = Visit(newExpression.Arguments[i]);
}
else
{
var projectionMember = _projectionMembers.Peek().Append(newExpression.Members[i]);
_projectionMembers.Push(projectionMember);
newArguments[i] = Visit(newExpression.Arguments[i]);
if (newArguments[i] == null)
{
return null;
}
_projectionMembers.Pop();
}
}
return newExpression.Update(newArguments);
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
protected override Expression VisitMemberInit(MemberInitExpression memberInitExpression)
{
var newExpression = (NewExpression)Visit(memberInitExpression.NewExpression);
if (newExpression == null)
{
return null;
}
var newBindings = new MemberAssignment[memberInitExpression.Bindings.Count];
for (var i = 0; i < newBindings.Length; i++)
{
var memberAssignment = (MemberAssignment)memberInitExpression.Bindings[i];
if (_clientEval)
{
newBindings[i] = memberAssignment.Update(Visit(memberAssignment.Expression));
}
else
{
var projectionMember = _projectionMembers.Peek().Append(memberAssignment.Member);
_projectionMembers.Push(projectionMember);
var visitedExpression = Visit(memberAssignment.Expression);
if (visitedExpression == null)
{
return null;
}
newBindings[i] = memberAssignment.Update(visitedExpression);
_projectionMembers.Pop();
}
}
return memberInitExpression.Update(newExpression, newBindings);
}
// TODO: Debugging
private void VerifySelectExpression(ProjectionBindingExpression projectionBindingExpression)
{
if (projectionBindingExpression.QueryExpression != _selectExpression)
{
throw new InvalidOperationException();
}
}
}
}