-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathGroupJoinMethodToPipelineTranslator.cs
119 lines (104 loc) · 5.57 KB
/
GroupJoinMethodToPipelineTranslator.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
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Linq.Expressions;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Linq.Linq3Implementation.Ast;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Stages;
using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods;
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
using MongoDB.Driver.Linq.Linq3Implementation.Reflection;
using MongoDB.Driver.Linq.Linq3Implementation.Serializers;
using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators;
namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators
{
internal static class GroupJoinMethodToPipelineTranslator
{
// public static methods
public static AstPipeline Translate(TranslationContext context, MethodCallExpression expression)
{
var method = expression.Method;
var arguments = expression.Arguments;
if (method.Is(QueryableMethod.GroupJoin))
{
var outerExpression = arguments[0];
var pipeline = ExpressionToPipelineTranslator.Translate(context, outerExpression);
ClientSideProjectionHelper.ThrowIfClientSideProjection(expression, pipeline, method);
AstExpression outerAst;
var rootVar = AstExpression.Var("ROOT", isCurrent: true);
var outerSerializer = pipeline.OutputSerializer;
if (outerSerializer is IWrappedValueSerializer wrappedSerializer)
{
outerAst = AstExpression.GetField(rootVar, wrappedSerializer.FieldName);
outerSerializer = wrappedSerializer.ValueSerializer;
}
else
{
outerAst = rootVar;
}
var wrapOuterStage = AstStage.Project(
AstProject.Set("_outer", outerAst),
AstProject.ExcludeId());
var wrappedOuterSerializer = WrappedValueSerializer.Create("_outer", outerSerializer);
var innerExpression = arguments[1];
var (queryProvider, isRawCollectionExpression) = innerExpression.FindMongoQueryProvider(containerExpression: expression);
var outerKeySelectorLambda = ExpressionHelper.UnquoteLambda(arguments[2]);
var localField = outerKeySelectorLambda.TranslateToDottedFieldName(context, wrappedOuterSerializer);
var innerKeySelectorLambda = ExpressionHelper.UnquoteLambda(arguments[3]);
var foreignField = innerKeySelectorLambda.TranslateToDottedFieldName(context, queryProvider.PipelineInputSerializer);
AstStage lookupStage;
if (isRawCollectionExpression)
{
lookupStage = AstStage.Lookup(
from: queryProvider.CollectionNamespace.CollectionName,
localField,
foreignField,
@as: "_inner");
}
else
{
var lookupPipeline = ExpressionToPipelineTranslator.Translate(context, innerExpression);
lookupStage = AstStage.Lookup(
from: queryProvider.CollectionNamespace.CollectionName,
localField,
foreignField,
Array.Empty<AstComputedField>(),
lookupPipeline,
@as: "_inner");
}
var resultSelectorLambda = ExpressionHelper.UnquoteLambda(arguments[4]);
var root = AstExpression.Var("ROOT", isCurrent: true);
var outerParameter = resultSelectorLambda.Parameters[0];
var outerField = AstExpression.GetField(root, "_outer");
var outerSymbol = context.CreateSymbol(outerParameter, outerField, outerSerializer);
var innerParameter = resultSelectorLambda.Parameters[1];
var innerField = AstExpression.GetField(root, "_inner");
var ienumerableInnerSerializer = IEnumerableSerializer.Create(queryProvider.PipelineInputSerializer);
var innerSymbol = context.CreateSymbol(innerParameter, innerField, ienumerableInnerSerializer);
var resultSelectorContext = context.WithSymbols(outerSymbol, innerSymbol);
var resultSelectorTranslation = ExpressionToAggregationExpressionTranslator.Translate(resultSelectorContext, resultSelectorLambda.Body);
var (projectStage, newOutputSerializer) = ProjectionHelper.CreateProjectStage(resultSelectorTranslation);
pipeline = pipeline.AddStages(
newOutputSerializer,
wrapOuterStage,
lookupStage,
projectStage);
return pipeline;
}
throw new ExpressionNotSupportedException(expression);
}
}
}