-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CSHARP-4744: Improve optimization of Count with predicate in Group.
- Loading branch information
Showing
5 changed files
with
113 additions
and
24 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
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
85 changes: 85 additions & 0 deletions
85
tests/MongoDB.Driver.Tests/Linq/Linq3Implementation/Jira/CSharp4744Tests.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,85 @@ | ||
/* 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.Bson; | ||
using MongoDB.Bson.Serialization.Attributes; | ||
using MongoDB.Driver.Linq; | ||
using MongoDB.TestHelpers.XunitExtensions; | ||
using Xunit; | ||
|
||
namespace MongoDB.Driver.Tests.Linq.Linq3Implementation.Jira | ||
{ | ||
public class CSharp4744Tests : Linq3IntegrationTest | ||
{ | ||
[Theory] | ||
[ParameterAttributeData] | ||
public void ReplaceOne( | ||
[Values(LinqProvider.V2, LinqProvider.V3)] LinqProvider linqProvider) | ||
{ | ||
var collection = GetCollection(linqProvider); | ||
|
||
var queryable = collection.AsQueryable() | ||
.GroupBy(x => x.FooName, (x, y) => new Summary() | ||
{ | ||
FooName = x, | ||
Count = y.Count(x => x.State == State.Running) | ||
}); | ||
|
||
var stages = Translate(collection, queryable); | ||
if (linqProvider == LinqProvider.V2) | ||
{ | ||
AssertStages( | ||
stages, | ||
"{ $group: { _id : '$FooName', Count : { $sum : { $cond : [{ $eq : ['$State', 1] }, 1, 0] } } } }"); // note: 1 instead of "Running" is an error | ||
} | ||
else | ||
{ | ||
AssertStages( | ||
stages, | ||
"{ $group: { _id : '$FooName', __agg0 : { $sum : { $cond : { if : { $eq : ['$State', 'Running'] }, then : 1, else : 0 } } } } }", | ||
"{ $project : { FooName : '$_id', Count : '$__agg0', _id : 0 } }"); | ||
} | ||
} | ||
|
||
private IMongoCollection<Foo> GetCollection(LinqProvider linqProvider) | ||
{ | ||
var collection = GetCollection<Foo>("test", linqProvider); | ||
CreateCollection(collection); | ||
return collection; | ||
} | ||
|
||
public enum State | ||
{ | ||
Started, | ||
Running, | ||
Complete | ||
} | ||
|
||
public class Foo | ||
{ | ||
public string FooName; | ||
[BsonRepresentation(BsonType.String)] | ||
public State State; | ||
} | ||
|
||
public class Summary | ||
{ | ||
public string FooName; | ||
public int Count; | ||
} | ||
} | ||
} |
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