Skip to content

Commit c288aad

Browse files
authored
CSHARP-3222: Add LINQ support for median and percentile accumulators/window functions (#1743)
1 parent 34b020c commit c288aad

23 files changed

+3382
-72
lines changed

src/MongoDB.Driver/Core/Misc/Feature.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ public class Feature
8383
private static readonly Feature __loookupConciseSyntax = new Feature("LoookupConciseSyntax", WireVersion.Server50);
8484
private static readonly Feature __loookupDocuments= new Feature("LoookupDocuments", WireVersion.Server60);
8585
private static readonly Feature __mmapV1StorageEngine = new Feature("MmapV1StorageEngine", WireVersion.Zero, WireVersion.Server42);
86+
private static readonly Feature __medianOperator = new Feature("MedianOperator", WireVersion.Server70);
87+
private static readonly Feature __percentileOperator = new Feature("PercentileOperator", WireVersion.Server70);
8688
private static readonly Feature __pickAccumulatorsNewIn52 = new Feature("PickAccumulatorsNewIn52", WireVersion.Server52);
8789
private static readonly Feature __rankFusionStage = new Feature("RankFusionStage", WireVersion.Server81);
8890
private static readonly Feature __regexMatch = new Feature("RegexMatch", WireVersion.Server42);
@@ -401,6 +403,16 @@ public class Feature
401403
[Obsolete("This feature was removed in server version 4.2. As such, this property will be removed in a later release.")]
402404
public static Feature MmapV1StorageEngine => __mmapV1StorageEngine;
403405

406+
/// <summary>
407+
/// Gets the $median operator added in 7.0
408+
/// </summary>
409+
public static Feature MedianOperator => __medianOperator;
410+
411+
/// <summary>
412+
/// Gets the $percentile operator added in 7.0
413+
/// </summary>
414+
public static Feature PercentileOperator => __percentileOperator;
415+
404416
/// <summary>
405417
/// Gets the pick accumulators new in 5.2 feature.
406418
/// </summary>

src/MongoDB.Driver/Linq/ISetWindowFieldsPartitionExtensions.cs

Lines changed: 270 additions & 1 deletion
Large diffs are not rendered by default.

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/AstNodeType.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ internal enum AstNodeType
9393
MatchesEverythingFilter,
9494
MatchesNothingFilter,
9595
MatchStage,
96+
MedianExpression,
97+
MedianAccumulatorExpression,
98+
MedianWindowExpression,
9699
MergeStage,
97100
ModFilterOperation,
98101
NaryExpression,
@@ -104,6 +107,9 @@ internal enum AstNodeType
104107
NullaryWindowExpression,
105108
OrFilter,
106109
OutStage,
110+
PercentileExpression,
111+
PercentileAccumulatorExpression,
112+
PercentileWindowExpression,
107113
PickAccumulatorExpression,
108114
PickExpression,
109115
Pipeline,

src/MongoDB.Driver/Linq/Linq3Implementation/Ast/Expressions/AstExpression.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,21 @@ public static AstExpression Max(AstExpression arg1, AstExpression arg2)
597597
return new AstNaryExpression(AstNaryOperator.Max, [arg1, arg2]);
598598
}
599599

600+
public static AstExpression Median(AstExpression input)
601+
{
602+
return new AstMedianExpression(input);
603+
}
604+
605+
public static AstAccumulatorExpression MedianAccumulator(AstExpression input)
606+
{
607+
return new AstMedianAccumulatorExpression(input);
608+
}
609+
610+
public static AstWindowExpression MedianWindowExpression(AstExpression input, AstWindow window)
611+
{
612+
return new AstMedianWindowExpression(input, window);
613+
}
614+
600615
public static AstExpression Min(AstExpression array)
601616
{
602617
return new AstUnaryExpression(AstUnaryOperator.Min, array);
@@ -653,6 +668,21 @@ public static AstExpression Or(params AstExpression[] args)
653668
return new AstNaryExpression(AstNaryOperator.Or, flattenedArgs);
654669
}
655670

671+
public static AstExpression Percentile(AstExpression input, AstExpression percentiles)
672+
{
673+
return new AstPercentileExpression(input, percentiles);
674+
}
675+
676+
public static AstAccumulatorExpression PercentileAccumulator(AstExpression input, AstExpression percentiles)
677+
{
678+
return new AstPercentileAccumulatorExpression(input, percentiles);
679+
}
680+
681+
public static AstWindowExpression PercentileWindowExpression(AstExpression input, AstExpression percentiles, AstWindow window)
682+
{
683+
return new AstPercentileWindowExpression(input, percentiles, window);
684+
}
685+
656686
public static AstExpression PickExpression(AstPickOperator @operator, AstExpression source, AstSortFields sortBy, AstVarExpression @as, AstExpression selector, AstExpression n)
657687
{
658688
return new AstPickExpression(@operator, source, sortBy, @as, selector, n);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 MongoDB.Bson;
17+
using MongoDB.Driver.Core.Misc;
18+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors;
19+
20+
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
21+
{
22+
internal sealed class AstMedianAccumulatorExpression : AstAccumulatorExpression
23+
{
24+
private readonly AstExpression _input;
25+
26+
public AstMedianAccumulatorExpression(AstExpression input)
27+
{
28+
_input = Ensure.IsNotNull(input, nameof(input));
29+
}
30+
31+
public AstExpression Input => _input;
32+
33+
public override AstNodeType NodeType => AstNodeType.MedianAccumulatorExpression;
34+
35+
public override AstNode Accept(AstNodeVisitor visitor)
36+
{
37+
return visitor.VisitMedianAccumulatorExpression(this);
38+
}
39+
40+
public override BsonValue Render()
41+
{
42+
return new BsonDocument
43+
{
44+
{
45+
"$median", new BsonDocument
46+
{
47+
{ "input", _input.Render() },
48+
{ "method", "approximate" } // server requires this parameter but currently only allows this value
49+
}
50+
}
51+
};
52+
}
53+
54+
public AstMedianAccumulatorExpression Update(AstExpression input)
55+
{
56+
if (input == _input)
57+
{
58+
return this;
59+
}
60+
return new AstMedianAccumulatorExpression(input);
61+
}
62+
}
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 MongoDB.Bson;
17+
using MongoDB.Driver.Core.Misc;
18+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors;
19+
20+
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
21+
{
22+
internal sealed class AstMedianExpression : AstExpression
23+
{
24+
private readonly AstExpression _input;
25+
26+
public AstMedianExpression(AstExpression input)
27+
{
28+
_input = Ensure.IsNotNull(input, nameof(input));
29+
}
30+
31+
public AstExpression Input => _input;
32+
33+
public override AstNodeType NodeType => AstNodeType.MedianExpression;
34+
35+
public override AstNode Accept(AstNodeVisitor visitor)
36+
{
37+
return visitor.VisitMedianExpression(this);
38+
}
39+
40+
public override BsonValue Render()
41+
{
42+
return new BsonDocument
43+
{
44+
{
45+
"$median", new BsonDocument
46+
{
47+
{ "input", _input.Render() },
48+
{ "method", "approximate" } // server requires this parameter but currently only allows this value
49+
}
50+
}
51+
};
52+
}
53+
54+
public AstMedianExpression Update(AstExpression input)
55+
{
56+
if (input == _input)
57+
{
58+
return this;
59+
}
60+
return new AstMedianExpression(input);
61+
}
62+
}
63+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 MongoDB.Bson;
17+
using MongoDB.Driver.Core.Misc;
18+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors;
19+
20+
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
21+
{
22+
internal sealed class AstMedianWindowExpression : AstWindowExpression
23+
{
24+
private readonly AstExpression _input;
25+
private readonly AstWindow _window;
26+
27+
public AstMedianWindowExpression(AstExpression input, AstWindow window)
28+
{
29+
_input = Ensure.IsNotNull(input, nameof(input));
30+
_window = window;
31+
}
32+
33+
public AstExpression Input => _input;
34+
35+
public AstWindow Window => _window;
36+
37+
public override AstNodeType NodeType => AstNodeType.MedianWindowExpression;
38+
39+
public override AstNode Accept(AstNodeVisitor visitor)
40+
{
41+
return visitor.VisitMedianWindowExpression(this);
42+
}
43+
44+
public override BsonValue Render()
45+
{
46+
return new BsonDocument
47+
{
48+
{
49+
"$median", new BsonDocument
50+
{
51+
{ "input", _input.Render() },
52+
{ "method", "approximate" } // server requires this parameter but currently only allows this value
53+
}
54+
},
55+
{ "window", _window?.Render(), _window != null }
56+
};
57+
}
58+
59+
public AstMedianWindowExpression Update(AstExpression input, AstWindow window)
60+
{
61+
if (input == _input && window == _window)
62+
{
63+
return this;
64+
}
65+
66+
return new AstMedianWindowExpression(input, window);
67+
}
68+
}
69+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 MongoDB.Bson;
17+
using MongoDB.Driver.Core.Misc;
18+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors;
19+
20+
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
21+
{
22+
internal sealed class AstPercentileAccumulatorExpression : AstAccumulatorExpression
23+
{
24+
private readonly AstExpression _input;
25+
private readonly AstExpression _percentiles;
26+
27+
public AstPercentileAccumulatorExpression(AstExpression input, AstExpression percentiles)
28+
{
29+
_input = Ensure.IsNotNull(input, nameof(input));
30+
_percentiles = Ensure.IsNotNull(percentiles, nameof(percentiles));
31+
}
32+
33+
public AstExpression Input => _input;
34+
35+
public AstExpression Percentiles => _percentiles;
36+
37+
public override AstNodeType NodeType => AstNodeType.PercentileAccumulatorExpression;
38+
39+
public override AstNode Accept(AstNodeVisitor visitor)
40+
{
41+
return visitor.VisitPercentileAccumulatorExpression(this);
42+
}
43+
44+
public override BsonValue Render()
45+
{
46+
return new BsonDocument
47+
{
48+
{
49+
"$percentile", new BsonDocument
50+
{
51+
{ "input", _input.Render() },
52+
{ "p", _percentiles.Render() },
53+
{ "method", "approximate" } // server requires this parameter but currently only allows this value
54+
}
55+
}
56+
};
57+
}
58+
59+
public AstPercentileAccumulatorExpression Update(AstExpression input, AstExpression percentiles)
60+
{
61+
if (input == _input && percentiles == _percentiles)
62+
{
63+
return this;
64+
}
65+
return new AstPercentileAccumulatorExpression(input, percentiles);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)