-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-3222: Add LINQ support for median and percentile accumulators/window functions #1743
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
Open
adelinowona
wants to merge
15
commits into
mongodb:main
Choose a base branch
from
adelinowona:csharp3222
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,382
−72
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
38f560a
Add enumerable extensions for median and percentile
adelinowona fd1c493
add setWindowFields extensions for median and percentile
adelinowona 2ade9cd
Add AstExpression support for median and percentile accumulators/wind…
adelinowona 2e8f590
Implement translators for median and percentile methods
adelinowona c0d00b3
Update grouping pipeline optimizer
adelinowona a0d526d
add tests
adelinowona 4bc77a6
fix some tests failures
adelinowona 534f650
rearrange the methods in MongoEnumerable.cs
adelinowona 3951103
add median feature and undo accidental change
adelinowona f9b312a
use reflection pattern to recognize methods in translators
adelinowona 1494e7e
fix complex accumulator visitor bug
adelinowona fb5c92d
use separate classes for median and percentile accumulator ASTs
adelinowona cb03c99
use StandardSerializers
adelinowona a4a2288
update return types and tests
adelinowona 32c473f
address pr comments
adelinowona File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
271 changes: 270 additions & 1 deletion
271
src/MongoDB.Driver/Linq/ISetWindowFieldsPartitionExtensions.cs
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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
63 changes: 63 additions & 0 deletions
63
...MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstMedianAccumulatorExpression.cs
This file contains hidden or 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,63 @@ | ||
/* 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.Bson; | ||
using MongoDB.Driver.Core.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions | ||
{ | ||
internal sealed class AstMedianAccumulatorExpression : AstAccumulatorExpression | ||
{ | ||
private readonly AstExpression _input; | ||
|
||
public AstMedianAccumulatorExpression(AstExpression input) | ||
{ | ||
_input = Ensure.IsNotNull(input, nameof(input)); | ||
} | ||
|
||
public AstExpression Input => _input; | ||
|
||
public override AstNodeType NodeType => AstNodeType.MedianAccumulatorExpression; | ||
|
||
public override AstNode Accept(AstNodeVisitor visitor) | ||
{ | ||
return visitor.VisitMedianAccumulatorExpression(this); | ||
} | ||
|
||
public override BsonValue Render() | ||
{ | ||
return new BsonDocument | ||
{ | ||
{ | ||
"$median", new BsonDocument | ||
{ | ||
{ "input", _input.Render() }, | ||
{ "method", "approximate" } // server requires this parameter but currently only allows this value | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public AstMedianAccumulatorExpression Update(AstExpression input) | ||
{ | ||
if (input == _input) | ||
{ | ||
return this; | ||
} | ||
return new AstMedianAccumulatorExpression(input); | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstMedianExpression.cs
This file contains hidden or 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,63 @@ | ||
/* 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.Bson; | ||
using MongoDB.Driver.Core.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions | ||
{ | ||
internal sealed class AstMedianExpression : AstExpression | ||
{ | ||
private readonly AstExpression _input; | ||
|
||
public AstMedianExpression(AstExpression input) | ||
{ | ||
_input = Ensure.IsNotNull(input, nameof(input)); | ||
} | ||
|
||
public AstExpression Input => _input; | ||
|
||
public override AstNodeType NodeType => AstNodeType.MedianExpression; | ||
|
||
public override AstNode Accept(AstNodeVisitor visitor) | ||
{ | ||
return visitor.VisitMedianExpression(this); | ||
} | ||
|
||
public override BsonValue Render() | ||
{ | ||
return new BsonDocument | ||
{ | ||
{ | ||
"$median", new BsonDocument | ||
{ | ||
{ "input", _input.Render() }, | ||
{ "method", "approximate" } // server requires this parameter but currently only allows this value | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public AstMedianExpression Update(AstExpression input) | ||
{ | ||
if (input == _input) | ||
{ | ||
return this; | ||
} | ||
return new AstMedianExpression(input); | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstMedianWindowExpression.cs
This file contains hidden or 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,69 @@ | ||
/* 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.Bson; | ||
using MongoDB.Driver.Core.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions | ||
{ | ||
internal sealed class AstMedianWindowExpression : AstWindowExpression | ||
{ | ||
private readonly AstExpression _input; | ||
private readonly AstWindow _window; | ||
|
||
public AstMedianWindowExpression(AstExpression input, AstWindow window) | ||
{ | ||
_input = Ensure.IsNotNull(input, nameof(input)); | ||
_window = window; | ||
} | ||
|
||
public AstExpression Input => _input; | ||
|
||
public AstWindow Window => _window; | ||
|
||
public override AstNodeType NodeType => AstNodeType.MedianWindowExpression; | ||
|
||
public override AstNode Accept(AstNodeVisitor visitor) | ||
{ | ||
return visitor.VisitMedianWindowExpression(this); | ||
} | ||
|
||
public override BsonValue Render() | ||
{ | ||
return new BsonDocument | ||
{ | ||
{ | ||
"$median", new BsonDocument | ||
{ | ||
{ "input", _input.Render() }, | ||
{ "method", "approximate" } // server requires this parameter but currently only allows this value | ||
} | ||
}, | ||
{ "window", _window?.Render(), _window != null } | ||
}; | ||
} | ||
|
||
public AstMedianWindowExpression Update(AstExpression input, AstWindow window) | ||
{ | ||
if (input == _input && window == _window) | ||
{ | ||
return this; | ||
} | ||
|
||
return new AstMedianWindowExpression(input, window); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...oDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstPercentileAccumulatorExpression.cs
This file contains hidden or 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,68 @@ | ||
/* 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.Bson; | ||
using MongoDB.Driver.Core.Misc; | ||
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors; | ||
|
||
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions | ||
{ | ||
internal sealed class AstPercentileAccumulatorExpression : AstAccumulatorExpression | ||
{ | ||
private readonly AstExpression _input; | ||
private readonly AstExpression _percentiles; | ||
|
||
public AstPercentileAccumulatorExpression(AstExpression input, AstExpression percentiles) | ||
{ | ||
_input = Ensure.IsNotNull(input, nameof(input)); | ||
_percentiles = Ensure.IsNotNull(percentiles, nameof(percentiles)); | ||
} | ||
|
||
public AstExpression Input => _input; | ||
|
||
public AstExpression Percentiles => _percentiles; | ||
|
||
public override AstNodeType NodeType => AstNodeType.PercentileAccumulatorExpression; | ||
|
||
public override AstNode Accept(AstNodeVisitor visitor) | ||
{ | ||
return visitor.VisitPercentileAccumulatorExpression(this); | ||
} | ||
|
||
public override BsonValue Render() | ||
{ | ||
return new BsonDocument | ||
{ | ||
{ | ||
"$percentile", new BsonDocument | ||
{ | ||
{ "input", _input.Render() }, | ||
{ "p", _percentiles.Render() }, | ||
{ "method", "approximate" } // server requires this parameter but currently only allows this value | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public AstPercentileAccumulatorExpression Update(AstExpression input, AstExpression percentiles) | ||
{ | ||
if (input == _input && percentiles == _percentiles) | ||
{ | ||
return this; | ||
} | ||
return new AstPercentileAccumulatorExpression(input, percentiles); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.