Skip to content

Commit 3eb0282

Browse files
rstamBorisDog
authored andcommitted
CSHARP-4116: Optimize { $expr : true } to { }.
1 parent 4fd5fec commit 3eb0282

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Optimizers/AstSimplifier.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
* limitations under the License.
1414
*/
1515

16+
using MongoDB.Bson;
1617
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions;
18+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Filters;
19+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Stages;
1720
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors;
1821

1922
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Optimizers
@@ -105,6 +108,21 @@ static AstExpression UltimateGetFieldInput(AstGetFieldExpression getField)
105108
}
106109
}
107110

111+
public override AstNode VisitMatchStage(AstMatchStage node)
112+
{
113+
node = (AstMatchStage)base.VisitMatchStage(node);
114+
115+
if (node.Filter is AstExprFilter exprFilter &&
116+
exprFilter.Expression is AstConstantExpression constantExpression &&
117+
constantExpression.Value is BsonBoolean booleanValue)
118+
{
119+
var simpleFilter = booleanValue.Value ? AstFilter.MatchesEverything() : AstFilter.MatchesNothing();
120+
return AstStage.Match(simpleFilter);
121+
}
122+
123+
return node;
124+
}
125+
108126
public override AstNode VisitUnaryExpression(AstUnaryExpression node)
109127
{
110128
// { $first : <arg> } => { $arrayElemAt : [<arg>, 0] } (or -1 for $last)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.Linq;
17+
using MongoDB.Bson;
18+
using Xunit;
19+
20+
namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests.Jira
21+
{
22+
public class CSharp4116Tests : Linq3IntegrationTest
23+
{
24+
[Theory]
25+
[InlineData(false, "{ $match : { _id : { $type : -1 } } }")]
26+
[InlineData(true, "{ $match : { } }")]
27+
public void Optimize_match_with_expr(bool value, string expectedStage)
28+
{
29+
var collection = GetCollection<BsonDocument>();
30+
var queryable = collection.AsQueryable()
31+
.Where(x => value);
32+
33+
var stages = Translate(collection, queryable);
34+
35+
AssertStages(stages, expectedStage);
36+
}
37+
}
38+
}

tests/MongoDB.Driver.Tests/Linq/Linq3ImplementationTests/Linq3IntegrationTest.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ namespace MongoDB.Driver.Tests.Linq.Linq3ImplementationTests
2525
{
2626
public abstract class Linq3IntegrationTest
2727
{
28+
protected void AssertStages(IEnumerable<BsonDocument> stages, params string[] expectedStages)
29+
{
30+
AssertStages(stages, (IEnumerable<string>)expectedStages);
31+
}
32+
2833
protected void AssertStages(IEnumerable<BsonDocument> stages, IEnumerable<string> expectedStages)
2934
{
3035
stages.Should().Equal(expectedStages.Select(json => BsonDocument.Parse(json)));

0 commit comments

Comments
 (0)