-
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
base: main
Are you sure you want to change the base?
Changes from all commits
1837037
b5e24ab
0235684
ef6c98b
3697ac4
84d9e39
91efb8b
dc6d059
cafdd06
2604f8a
61824fd
691b8aa
0cc714d
f878f7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -597,6 +597,21 @@ public static AstExpression Max(AstExpression arg1, AstExpression arg2) | |
return new AstNaryExpression(AstNaryOperator.Max, [arg1, arg2]); | ||
} | ||
|
||
public static AstExpression Median(AstExpression input) | ||
{ | ||
return new AstMedianExpression(input); | ||
} | ||
|
||
public static AstMedianAccumulatorExpression MedianAccumulator(AstExpression input) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider a return type of In most (all?) methods in this class we return the most general class possible. This allows the factory method to sometimes return different types depending on the parameters. |
||
{ | ||
return new AstMedianAccumulatorExpression(input); | ||
} | ||
|
||
public static AstMedianWindowExpression MedianWindowExpression(AstExpression input, AstWindow window) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider a return type of In most (all?) methods in this class we return the most general class possible. This allows the factory method to sometimes return different types depending on the parameters. |
||
{ | ||
return new AstMedianWindowExpression(input, window); | ||
} | ||
|
||
public static AstExpression Min(AstExpression array) | ||
{ | ||
return new AstUnaryExpression(AstUnaryOperator.Min, array); | ||
|
@@ -653,6 +668,21 @@ public static AstExpression Or(params AstExpression[] args) | |
return new AstNaryExpression(AstNaryOperator.Or, flattenedArgs); | ||
} | ||
|
||
public static AstPercentileExpression Percentile(AstExpression input, AstExpression percentiles) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider a return type of In most (all?) methods in this class we return the most general class possible. This allows the factory method to sometimes return different types depending on the parameters. |
||
{ | ||
return new AstPercentileExpression(input, percentiles); | ||
} | ||
|
||
public static AstPercentileAccumulatorExpression PercentileAccumulator(AstExpression input, AstExpression percentiles) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider a return type of In most (all?) methods in this class we return the most general class possible. This allows the factory method to sometimes return different types depending on the parameters. |
||
{ | ||
return new AstPercentileAccumulatorExpression(input, percentiles); | ||
} | ||
|
||
public static AstPercentileWindowExpression PercentileWindowExpression(AstExpression input, AstExpression percentiles, AstWindow window) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider a return type of In most (all?) methods in this class we return the most general class possible. This allows the factory method to sometimes return different types depending on the parameters. |
||
{ | ||
return new AstPercentileWindowExpression(input, percentiles, window); | ||
} | ||
|
||
public static AstExpression PickExpression(AstPickOperator @operator, AstExpression source, AstSortFields sortBy, AstVarExpression @as, AstExpression selector, AstExpression n) | ||
{ | ||
return new AstPickExpression(@operator, source, sortBy, @as, selector, n); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* 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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra blank line. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* 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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra blank line. |
||
} | ||
} |
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); | ||
} | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider a return type of
AstExpression
.In most (all?) methods in this class we return the most general class possible. This allows the factory method to sometimes return different types depending on the parameters.