Skip to content

Commit 1a56b9e

Browse files
committed
CSHARP-4186: Refactor AstSetWindowFieldsWindowExpression.
1 parent 4173887 commit 1a56b9e

File tree

45 files changed

+1530
-683
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1530
-683
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ internal enum AstNodeType
2424
AndExpression,
2525
AndFilter,
2626
BinaryExpression,
27+
BinaryWindowExpression,
2728
BitsAllClearFilterOperation,
2829
BitsAllSetFilterOperation,
2930
BitsAnyClearFilterOperation,
@@ -52,8 +53,10 @@ internal enum AstNodeType
5253
DateToStringExpression,
5354
DateTruncExpression,
5455
DensifyStage,
56+
DerivativeOrIntegralWindowExpression,
5557
ElemMatchFilterOperation,
5658
ExistsFilterOperation,
59+
ExponentialMovingAverageWindowExpression,
5760
ExprFilter,
5861
FacetStage,
5962
FacetStageFacet,
@@ -98,6 +101,7 @@ internal enum AstNodeType
98101
NinFilterOperation,
99102
NorFilter,
100103
NotFilterOperation,
104+
NullaryWindowExpression,
101105
OrExpression,
102106
OrFilter,
103107
OutStage,
@@ -120,7 +124,8 @@ internal enum AstNodeType
120124
RTrimExpression,
121125
SampleStage,
122126
SetStage,
123-
SetWindowFieldsWindowExpression,
127+
SetWindowFieldsStage,
128+
ShiftWindowExpression,
124129
SizeFilterOperation,
125130
SkipStage,
126131
SliceExpression,
@@ -133,12 +138,14 @@ internal enum AstNodeType
133138
TrimExpression,
134139
TypeFilterOperation,
135140
UnaryExpression,
141+
UnaryWindowExpression,
136142
UnionWithStage,
137143
UnsetStage,
138144
UnwindStage,
139145
VarBinding,
140146
VarExpression,
141147
WhereFilter,
148+
WindowField,
142149
ZipExpression
143150
}
144151
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
19+
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast
20+
{
21+
internal static class AstSort
22+
{
23+
public static AstSortField Field(string path, AstSortOrder order)
24+
{
25+
return new AstSortField(path, order);
26+
}
27+
}
28+
29+
internal sealed class AstSortField
30+
{
31+
private readonly string _path;
32+
private readonly AstSortOrder _order;
33+
34+
public AstSortField(string path, AstSortOrder order)
35+
{
36+
_path = Ensure.IsNotNull(path, nameof(path));
37+
_order = Ensure.IsNotNull(order, nameof(order));
38+
}
39+
40+
public AstSortOrder Order => _order;
41+
public string Path => _path;
42+
43+
public BsonElement RenderAsElement()
44+
{
45+
return new BsonElement(_path, _order.Render());
46+
}
47+
48+
public override string ToString() => RenderAsElement().ToJson();
49+
}
50+
51+
internal abstract class AstSortOrder
52+
{
53+
private readonly static AstSortOrder __ascending = new AstAscendingSortOrder();
54+
private readonly static AstSortOrder __descending = new AstDescendingSortOrder();
55+
private readonly static AstSortOrder __metaTextScore = new AstMetaTextScoreSortOrder();
56+
57+
public static AstSortOrder Ascending => __ascending;
58+
public static AstSortOrder Descending => __descending;
59+
public static AstSortOrder MetaTextScore => __metaTextScore;
60+
61+
public abstract BsonValue Render();
62+
63+
public override string ToString() => Render().ToJson();
64+
}
65+
66+
internal sealed class AstAscendingSortOrder : AstSortOrder
67+
{
68+
public override BsonValue Render() => 1;
69+
}
70+
71+
internal sealed class AstDescendingSortOrder : AstSortOrder
72+
{
73+
public override BsonValue Render() => -1;
74+
}
75+
76+
internal sealed class AstMetaTextScoreSortOrder : AstSortOrder
77+
{
78+
public override BsonValue Render() => new BsonDocument("$meta", "textScore");
79+
}
80+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 System.Collections;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using MongoDB.Bson;
20+
using MongoDB.Driver.Core.Misc;
21+
using MongoDB.Driver.Linq.Linq3Implementation.Misc;
22+
23+
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast
24+
{
25+
internal sealed class AstSortFields : IEnumerable<AstSortField>
26+
{
27+
private readonly IReadOnlyList<AstSortField> _fields;
28+
29+
public AstSortFields(IEnumerable<AstSortField> fields)
30+
{
31+
_fields = Ensure.IsNotNull(fields, nameof(fields)).AsReadOnlyList();
32+
}
33+
34+
public IReadOnlyList<AstSortField> Fields => _fields;
35+
36+
public IEnumerator<AstSortField> GetEnumerator() => _fields.GetEnumerator();
37+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
38+
39+
public AstSortFields AddSortField(AstSortField field)
40+
{
41+
Ensure.IsNotNull(field, nameof(field));
42+
return new AstSortFields(_fields.Concat(new[] { field }));
43+
}
44+
45+
public BsonDocument Render()
46+
{
47+
return new BsonDocument(_fields.Select(f => f.RenderAsElement()));
48+
}
49+
50+
public override string ToString() => Render().ToJson();
51+
}
52+
}
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.Expressions;
19+
using MongoDB.Driver.Linq.Linq3Implementation.Ast.Visitors;
20+
21+
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast
22+
{
23+
internal sealed class AstWindowField : AstNode
24+
{
25+
private readonly string _path;
26+
private readonly AstWindowExpression _value;
27+
28+
public AstWindowField(string path, AstWindowExpression value)
29+
{
30+
_path = Ensure.IsNotNull(path, nameof(path));
31+
_value = Ensure.IsNotNull(value, nameof(value));
32+
}
33+
34+
public override AstNodeType NodeType => AstNodeType.WindowField;
35+
public string Path => _path;
36+
public AstWindowExpression Value => _value;
37+
38+
public override AstNode Accept(AstNodeVisitor visitor)
39+
{
40+
return visitor.VisitWindowField(this);
41+
}
42+
43+
public override BsonValue Render()
44+
{
45+
return new BsonDocument(RenderAsElement());
46+
}
47+
48+
public BsonElement RenderAsElement()
49+
{
50+
return new BsonElement(_path, _value.Render());
51+
}
52+
53+
public override string ToString()
54+
{
55+
return $"\"{_path}\" : {_value.Render().ToJson()}";
56+
}
57+
58+
public AstWindowField Update(string path, AstWindowExpression value)
59+
{
60+
if (path == _path && value == _value)
61+
{
62+
return this;
63+
}
64+
65+
return new AstWindowField(path, value);
66+
}
67+
}
68+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 AstBinaryWindowExpression : AstWindowExpression
23+
{
24+
private readonly AstExpression _arg1;
25+
private readonly AstExpression _arg2;
26+
private readonly AstBinaryWindowOperator _operator;
27+
private readonly AstWindow _window;
28+
29+
public AstBinaryWindowExpression(AstBinaryWindowOperator @operator, AstExpression arg1, AstExpression arg2, AstWindow window)
30+
{
31+
_operator = @operator;
32+
_arg1 = Ensure.IsNotNull(arg1, nameof(arg1));
33+
_arg2 = Ensure.IsNotNull(arg2, nameof(arg2));
34+
_window = window;
35+
}
36+
37+
public AstExpression Arg1 => _arg1;
38+
public AstExpression Arg2 => _arg2;
39+
public override AstNodeType NodeType => AstNodeType.BinaryWindowExpression;
40+
public AstBinaryWindowOperator Operator => _operator;
41+
public AstWindow Window => _window;
42+
43+
public override AstNode Accept(AstNodeVisitor visitor)
44+
{
45+
return visitor.VisitBinaryWindowExpression(this);
46+
}
47+
48+
public override BsonValue Render()
49+
{
50+
return new BsonDocument
51+
{
52+
{ _operator.Render(), new BsonArray { _arg1.Render(), _arg2.Render() } },
53+
{ "window", _window?.Render(), _window != null }
54+
};
55+
}
56+
57+
public AstBinaryWindowExpression Update(AstBinaryWindowOperator @operator, AstExpression arg1, AstExpression arg2, AstWindow window)
58+
{
59+
if (@operator == _operator && arg1 == _arg1 && arg2 == _arg2 && window == _window)
60+
{
61+
return this;
62+
}
63+
64+
return new AstBinaryWindowExpression(@operator, arg1, arg2, window);
65+
}
66+
}
67+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 System;
17+
18+
namespace MongoDB.Driver.Linq.Linq3Implementation.Ast.Expressions
19+
{
20+
internal enum AstBinaryWindowOperator
21+
{
22+
CovariancePopulation,
23+
CovarianceSample
24+
}
25+
26+
internal static class AstBinaryWindowOperatorExtensions
27+
{
28+
public static string Render(this AstBinaryWindowOperator @operator)
29+
{
30+
return @operator switch
31+
{
32+
AstBinaryWindowOperator.CovariancePopulation => "$covariancePop",
33+
AstBinaryWindowOperator.CovarianceSample => "$covarianceSamp",
34+
_ => throw new InvalidOperationException($"Unexpected binary window operator: {@operator}.")
35+
};
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)