Skip to content

Commit fb5c92d

Browse files
committed
use separate classes for median and percentile accumulator ASTs
1 parent 1494e7e commit fb5c92d

File tree

8 files changed

+177
-196
lines changed

8 files changed

+177
-196
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ internal enum AstNodeType
3131
BucketStage,
3232
CollStatsStage,
3333
ComparisonFilterOperation,
34-
ComplexAccumulatorExpression,
3534
ComputedArrayExpression,
3635
ComputedDocumentExpression,
3736
ComputedField,
@@ -95,6 +94,7 @@ internal enum AstNodeType
9594
MatchesNothingFilter,
9695
MatchStage,
9796
MedianExpression,
97+
MedianAccumulatorExpression,
9898
MedianWindowExpression,
9999
MergeStage,
100100
ModFilterOperation,
@@ -108,6 +108,7 @@ internal enum AstNodeType
108108
OrFilter,
109109
OutStage,
110110
PercentileExpression,
111+
PercentileAccumulatorExpression,
111112
PercentileWindowExpression,
112113
PickAccumulatorExpression,
113114
PickExpression,

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

Lines changed: 0 additions & 66 deletions
This file was deleted.

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

Lines changed: 0 additions & 38 deletions
This file was deleted.

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,6 @@ public static AstExpression Comparison(AstBinaryOperator comparisonOperator, Ast
209209
};
210210
}
211211

212-
public static AstComplexAccumulatorExpression ComplexAccumulator(AstComplexAccumulatorOperator @operator, Dictionary<string, AstExpression> args)
213-
{
214-
return new AstComplexAccumulatorExpression(@operator, args);
215-
}
216-
217212
public static AstExpression ComputedArray(IEnumerable<AstExpression> items)
218213
{
219214
return new AstComputedArrayExpression(items);
@@ -607,6 +602,11 @@ public static AstExpression Median(AstExpression input)
607602
return new AstMedianExpression(input);
608603
}
609604

605+
public static AstMedianAccumulatorExpression MedianAccumulator(AstExpression input)
606+
{
607+
return new AstMedianAccumulatorExpression(input);
608+
}
609+
610610
public static AstMedianWindowExpression MedianWindowExpression(AstExpression input, AstWindow window)
611611
{
612612
return new AstMedianWindowExpression(input, window);
@@ -673,6 +673,11 @@ public static AstPercentileExpression Percentile(AstExpression input, AstExpress
673673
return new AstPercentileExpression(input, percentiles);
674674
}
675675

676+
public static AstPercentileAccumulatorExpression PercentileAccumulator(AstExpression input, AstExpression percentiles)
677+
{
678+
return new AstPercentileAccumulatorExpression(input, percentiles);
679+
}
680+
676681
public static AstPercentileWindowExpression PercentileWindowExpression(AstExpression input, AstExpression percentiles, AstWindow window)
677682
{
678683
return new AstPercentileWindowExpression(input, percentiles, window);
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
}
64+
}
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)