|
| 1 | +/* Copyright 2010-present MongoDB Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using System.Linq.Expressions; |
| 19 | +using MongoDB.Bson.Serialization; |
| 20 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast; |
| 21 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; |
| 22 | +using MongoDB.Driver.Linq.Linq3Implementation.Ast.Stages; |
| 23 | +using MongoDB.Driver.Linq.Linq3Implementation.ExtensionMethods; |
| 24 | +using MongoDB.Driver.Linq.Linq3Implementation.Misc; |
| 25 | +using MongoDB.Driver.Linq.Linq3Implementation.Reflection; |
| 26 | +using MongoDB.Driver.Linq.Linq3Implementation.Serializers; |
| 27 | +using MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToAggregationExpressionTranslators; |
| 28 | + |
| 29 | +namespace MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToPipelineTranslators |
| 30 | +{ |
| 31 | + internal static class LeftJoinMethodToPipelineTranslator |
| 32 | + { |
| 33 | + // public static methods |
| 34 | + public static TranslatedPipeline Translate(TranslationContext context, MethodCallExpression expression) |
| 35 | + { |
| 36 | + var method = expression.Method; |
| 37 | + var arguments = expression.Arguments; |
| 38 | + |
| 39 | + if (method.IsOneOf(MongoQueryableMethod.LeftJoin, QueryableMethod.LeftJoin)) |
| 40 | + { |
| 41 | + var outerExpression = arguments[0]; |
| 42 | + var innerExpression = arguments[1]; |
| 43 | + var outerKeySelectorLambda = ExpressionHelper.UnquoteLambda(arguments[2]); |
| 44 | + var innerKeySelectorLambda = ExpressionHelper.UnquoteLambda(arguments[3]); |
| 45 | + var resultSelectorLambda = ExpressionHelper.UnquoteLambda(arguments[4]); |
| 46 | + |
| 47 | + var pipeline = ExpressionToPipelineTranslator.Translate(context, outerExpression); |
| 48 | + ClientSideProjectionHelper.ThrowIfClientSideProjection(expression, pipeline, method); |
| 49 | + |
| 50 | + AstExpression outerAst; |
| 51 | + IBsonSerializer outerSerializer; |
| 52 | + if (pipeline.OutputSerializer is IWrappedValueSerializer pipelineOutputWrappedSerializer) |
| 53 | + { |
| 54 | + outerAst = AstExpression.GetField(AstExpression.RootVar, pipelineOutputWrappedSerializer.FieldName); |
| 55 | + outerSerializer = pipelineOutputWrappedSerializer.ValueSerializer; |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + outerAst = AstExpression.RootVar; |
| 60 | + outerSerializer = pipeline.OutputSerializer; |
| 61 | + } |
| 62 | + |
| 63 | + ThrowIfReservedFieldNames(expression, outerSerializer); |
| 64 | + |
| 65 | + var wrapOuterStage = AstStage.Project( |
| 66 | + AstProject.Set("_outer", outerAst), |
| 67 | + AstProject.Exclude("_id")); |
| 68 | + var wrappedOuterSerializer = WrappedValueSerializer.Create("_outer", outerSerializer); |
| 69 | + |
| 70 | + var (innerCollectionName, innerSerializer) = innerExpression.GetCollectionInfoFromQueryable(containerExpression: expression); |
| 71 | + var localField = outerKeySelectorLambda.TranslateToDottedFieldName(context, wrappedOuterSerializer); |
| 72 | + var foreignField = innerKeySelectorLambda.TranslateToDottedFieldName(context, innerSerializer); |
| 73 | + |
| 74 | + var lookupStage = AstStage.Lookup( |
| 75 | + from: innerCollectionName, |
| 76 | + localField, |
| 77 | + foreignField, |
| 78 | + @as: "_inner"); |
| 79 | + |
| 80 | + var unwindStage = AstStage.Unwind("_inner", preserveNullAndEmptyArrays: true); |
| 81 | + |
| 82 | + var outerParameter = resultSelectorLambda.Parameters[0]; |
| 83 | + var outerField = AstExpression.GetField(AstExpression.RootVar, "_outer"); |
| 84 | + var outerSymbol = context.CreateSymbol(outerParameter, outerField, outerSerializer); |
| 85 | + var innerParameter = resultSelectorLambda.Parameters[1]; |
| 86 | + var innerField = AstExpression.GetField(AstExpression.RootVar, "_inner"); |
| 87 | + var innerSymbol = context.CreateSymbol(innerParameter, innerField, innerSerializer); |
| 88 | + var resultSelectorContext = context.WithSymbols(outerSymbol, innerSymbol); |
| 89 | + var resultSelectorTranslation = ExpressionToAggregationExpressionTranslator.Translate(resultSelectorContext, resultSelectorLambda.Body); |
| 90 | + var (projectStage, projectSerializer) = ProjectionHelper.CreateProjectStage(resultSelectorTranslation); |
| 91 | + |
| 92 | + pipeline = pipeline.AddStages( |
| 93 | + wrapOuterStage, |
| 94 | + lookupStage, |
| 95 | + unwindStage, |
| 96 | + projectStage, |
| 97 | + projectSerializer); |
| 98 | + |
| 99 | + return pipeline; |
| 100 | + } |
| 101 | + |
| 102 | + throw new ExpressionNotSupportedException(expression); |
| 103 | + } |
| 104 | + |
| 105 | + private static readonly HashSet<string> __reservedFieldNames = new HashSet<string> { "_outer", "_inner" }; |
| 106 | + |
| 107 | + private static void ThrowIfReservedFieldNames(MethodCallExpression expression, IBsonSerializer serializer) |
| 108 | + { |
| 109 | + if (serializer is not IBsonDocumentSerializer documentSerializer) |
| 110 | + return; |
| 111 | + |
| 112 | + var conflicting = new List<string>(); |
| 113 | + foreach (var member in serializer.ValueType.GetMembers()) |
| 114 | + { |
| 115 | + if (documentSerializer.TryGetMemberSerializationInfo(member.Name, out var info) && |
| 116 | + __reservedFieldNames.Contains(info.ElementName)) |
| 117 | + { |
| 118 | + conflicting.Add(info.ElementName); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if (conflicting.Count > 0) |
| 123 | + { |
| 124 | + throw new ExpressionNotSupportedException(expression, |
| 125 | + because: $"the outer document type uses reserved field name(s) {string.Join(", ", conflicting.Select(n => $"'{n}'"))} which are used internally by LeftJoin"); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments