-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5473: Provide API to turn LINQ expression into MQL #1601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b0aca82
d966e77
7eeeb63
dacdf29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -55,6 +55,7 @@ protected MongoQueryProvider( | |||||||||||||||
| public abstract TResult Execute<TResult>(Expression expression); | ||||||||||||||||
| public abstract Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken); | ||||||||||||||||
| public abstract ExpressionTranslationOptions GetTranslationOptions(); | ||||||||||||||||
| public abstract BsonDocument[] Translate<TResult>(Expression expression, out IBsonSerializer<TResult> outputSerializer); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| internal sealed class MongoQueryProvider<TDocument> : MongoQueryProvider | ||||||||||||||||
|
|
@@ -152,5 +153,14 @@ public override ExpressionTranslationOptions GetTranslationOptions() | |||||||||||||||
| var database = _database ?? _collection?.Database; | ||||||||||||||||
| return translationOptions.AddMissingOptionsFrom(database?.Client.Settings.TranslationOptions); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| public override BsonDocument[] Translate<TResult>(Expression expression, out IBsonSerializer<TResult> outputSerializer) | ||||||||||||||||
| { | ||||||||||||||||
| var translationOptions = GetTranslationOptions(); | ||||||||||||||||
| var executableQuery = ExpressionToExecutableQueryTranslator.Translate<TDocument, TResult>(provider: this, expression, translationOptions); | ||||||||||||||||
| var stages = executableQuery.Pipeline.Ast.Stages; | ||||||||||||||||
| outputSerializer = (IBsonSerializer<TResult>)executableQuery.Pipeline.OutputSerializer; | ||||||||||||||||
| return stages.Select(s => s.Render().AsBsonDocument).ToArray(); | ||||||||||||||||
|
||||||||||||||||
| return stages.Select(s => s.Render().AsBsonDocument).ToArray(); | |
| var result = new BsonDocument[stages.Count]; | |
| for (int i = 0; i < stages.Count; i++) | |
| { | |
| result[i] = stages[i].Render().AsBsonDocument; | |
| } | |
| return result; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rstam this doesn't seem worth it to me but I defer the decision to you. Typical pipelines have about 2-10 stages and this also is not in a hot loop. We will increase verbosity for minor performance gains.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* 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.Linq; | ||
| using FluentAssertions; | ||
| using MongoDB.Driver.Linq; | ||
| using Xunit; | ||
|
|
||
| namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira | ||
| { | ||
| public class CSharp5473Tests : Linq3IntegrationTest | ||
| { | ||
| [Fact] | ||
| public void Translate_expression_should_work() | ||
| { | ||
| var collection = GetCollection(); | ||
| var queryable = collection.AsQueryable() | ||
| .Select(x => x.X + 1); | ||
| var expression = queryable.Expression; // queryable was just used as an easy way to create the expression and the provider | ||
| var provider = (IMongoQueryProvider)queryable.Provider; | ||
|
|
||
| var stages = provider.Translate<int>(expression, out var outputSerializer); | ||
| AssertStages(stages, "{ $project : { _v : { $add : ['$X', 1] }, _id : 0 } }"); | ||
|
|
||
| var pipeline = new BsonDocumentStagePipelineDefinition<C, int>(stages, outputSerializer); | ||
| var result = collection.Aggregate(pipeline).Single(); | ||
| result.Should().Be(2); | ||
| } | ||
|
|
||
| private IMongoCollection<C> GetCollection() | ||
| { | ||
| var collection = GetCollection<C>("test"); | ||
| CreateCollection( | ||
| collection, | ||
| new C { Id = 1, X = 1 }); | ||
| return collection; | ||
| } | ||
|
|
||
| private class C | ||
| { | ||
| public int Id { get; set; } | ||
| public int X { get; set; } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adelin commented:
The latest commit implements his suggestion.
@BorisDog and @damieng will this work for you two in the Analyzer and the EF Core Provider?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think receiving expression is more consistent with
ExecuteAsync.Analyzer will not use this functionality at all.