-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSHARP-4656 Simplify A : "$A" to A : 1 only on find (#1087)
- Loading branch information
1 parent
5a973c2
commit 1ef7dd3
Showing
22 changed files
with
169 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Optimizers/AstFindProjectionSimplifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* 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 MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Stages; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Optimizers | ||
{ | ||
internal class AstFindProjectionSimplifier : AstSimplifier | ||
{ | ||
public override AstNode VisitProjectStageSetFieldSpecification(AstProjectStageSetFieldSpecification node) | ||
{ | ||
node = (AstProjectStageSetFieldSpecification)base.VisitProjectStageSetFieldSpecification(node); | ||
|
||
// { path : '$path' } => { path : 1 } | ||
if (node.Value is AstFieldPathExpression fieldPathExpression && | ||
fieldPathExpression.Path == $"${node.Path}") | ||
{ | ||
return AstProject.Include(node.Path); | ||
} | ||
|
||
return node; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
tests/MongoDB.Driver.Tests/FindExpressionProjectionDefinitionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* 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 FluentAssertions; | ||
using MongoDB.Bson.Serialization; | ||
using Xunit; | ||
|
||
namespace MongoDB.Driver.Tests | ||
{ | ||
public class FindExpressionProjectionDefinitionTests | ||
{ | ||
[Fact] | ||
public void Projection_to_class_should_work() | ||
=> AssertProjection( | ||
x => new Projection { A = x.A, X = x.B }, | ||
"{ A : 1, X : '$B', _id : 0 }"); | ||
|
||
[Fact] | ||
public void Projection_to_anonymous_type_should_work() | ||
=> AssertProjection( | ||
x => new { x.A, X = x.B }, | ||
"{ A : 1, X : '$B', _id : 0 }"); | ||
|
||
private void AssertProjection<TProjection>( | ||
Expression<Func<Document, TProjection>> expression, | ||
string expectedProjection) | ||
{ | ||
var projection = new FindExpressionProjectionDefinition<Document, TProjection>(expression); | ||
|
||
var renderedProjection = projection.Render( | ||
BsonSerializer.LookupSerializer<Document>(), | ||
BsonSerializer.SerializerRegistry); | ||
|
||
renderedProjection.Document.Should().BeEquivalentTo(expectedProjection); | ||
} | ||
|
||
private class Document | ||
{ | ||
public string A { get; set; } | ||
|
||
public int B { get; set; } | ||
} | ||
|
||
private class Projection | ||
{ | ||
public string A { get; set; } | ||
|
||
public int X { get; set; } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.