Skip to content
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

CSHARP-5473: Provide API to turn LINQ expression into MQL. #1601

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/MongoDB.Driver/Linq/IMongoQueryProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ public interface IMongoQueryProvider : IQueryProvider
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The value that results from executing the specified query.</returns>
Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Translates an IQueryable to MQL.
/// </summary>
/// <param name="queryable">The IQueryable.</param>
/// <param name="outputSerializer">The output serializer.</param>
/// <typeparam name="TResult">The type of the result.</typeparam>
/// <returns>An array of MQL pipeline stages represented as BsonDocuments.</returns>
BsonDocument[] Translate<TResult>(IQueryable<TResult> queryable, out IBsonSerializer<TResult> outputSerializer);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>(IQueryable<TResult> queryable, out IBsonSerializer<TResult> outputSerializer);
}

internal sealed class MongoQueryProvider<TDocument> : MongoQueryProvider
Expand Down Expand Up @@ -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>(IQueryable<TResult> queryable, out IBsonSerializer<TResult> outputSerializer)
{
var translationOptions = GetTranslationOptions();
var executableQuery = ExpressionToExecutableQueryTranslator.Translate<TDocument, TResult>(provider: this, queryable.Expression, translationOptions);
var stages = executableQuery.Pipeline.Ast.Stages;
outputSerializer = (IBsonSerializer<TResult>)executableQuery.Pipeline.OutputSerializer;
return stages.Select(s => s.Render().AsBsonDocument).ToArray();
}
}
}
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_should_work()
{
var collection = GetCollection();

var queryable = collection.AsQueryable()
.Select(x => x.X + 1);

var provider = (IMongoQueryProvider)queryable.Provider;
var stages = provider.Translate(queryable, 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; }
}
}
}