Skip to content
This repository was archived by the owner on Feb 28, 2025. It is now read-only.

PHPLIB-1362 Add tests on Window Operators #51

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions generator/config/accumulator/covariancePop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,25 @@ arguments:
name: expression2
type:
- resolvesToNumber
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/covariancePop/#example'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
orderDate: 1
output:
covariancePopForState:
$covariancePop:
-
# $year: '$orderDate'
$year:
date: '$orderDate'
- '$quantity'
window:
documents:
- 'unbounded'
- 'current'
22 changes: 22 additions & 0 deletions generator/config/accumulator/covarianceSamp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,25 @@ arguments:
name: expression2
type:
- resolvesToNumber
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/covarianceSamp/#example'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
orderDate: 1
output:
covarianceSampForState:
$covarianceSamp:
-
# $year: '$orderDate'
$year:
date: '$orderDate'
- '$quantity'
window:
documents:
- 'unbounded'
- 'current'
25 changes: 25 additions & 0 deletions generator/config/accumulator/denseRank.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,28 @@ encode: object
description: |
Returns the document position (known as the rank) relative to other documents in the $setWindowFields stage partition. There are no gaps in the ranks. Ties receive the same rank.
New in MongoDB 5.0.
tests:
-
name: 'Dense Rank Partitions by an Integer Field'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/denseRank/#dense-rank-partitions-by-an-integer-field'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
quantity: -1
output:
denseRankQuantityForState:
$denseRank: {}
-
name: 'Dense Rank Partitions by a Date Field'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/denseRank/#dense-rank-partitions-by-a-date-field'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
orderDate: 1
output:
denseRankOrderDateForState:
$denseRank: {}
24 changes: 24 additions & 0 deletions generator/config/accumulator/derivative.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,27 @@ arguments:
description: |
A string that specifies the time unit. Use one of these strings: "week", "day","hour", "minute", "second", "millisecond".
If the sortBy field is not a date, you must omit a unit. If you specify a unit, you must specify a date in the sortBy field.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/derivative/#example'
pipeline:
-
$setWindowFields:
partitionBy: '$truckID'
sortBy:
timeStamp: 1
output:
truckAverageSpeed:
$derivative:
input: '$miles'
unit: 'hour'
window:
range:
- -30
- 0
unit: 'second'
-
$match:
truckAverageSpeed:
$gt: 50
13 changes: 13 additions & 0 deletions generator/config/accumulator/documentNumber.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,16 @@ encode: object
description: |
Returns the position of a document (known as the document number) in the $setWindowFields stage partition. Ties result in different adjacent document numbers.
New in MongoDB 5.0.
tests:
-
name: 'Document Number for Each State'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/documentNumber/#document-number-for-each-state'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
quantity: -1
output:
documentNumberForState:
$documentNumber: {}
29 changes: 29 additions & 0 deletions generator/config/accumulator/expMovingAvg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,32 @@ arguments:
description: |
A double that specifies the exponential decay value to use in the exponential moving average calculation. A higher alpha value assigns a lower mathematical significance to previous results from the calculation.
You must specify either N or alpha. You cannot specify both.
tests:
-
name: 'Exponential Moving Average Using N'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/expMovingAvg/#exponential-moving-average-using-n'
pipeline:
-
$setWindowFields:
partitionBy: '$stock'
sortBy:
date: 1
output:
expMovingAvgForStock:
$expMovingAvg:
input: '$price'
N: 2
-
name: 'Exponential Moving Average Using alpha'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/expMovingAvg/#exponential-moving-average-using-alpha'
pipeline:
-
$setWindowFields:
partitionBy: '$stock'
sortBy:
date: 1
output:
expMovingAvgForStock:
$expMovingAvg:
input: '$price'
alpha: 0.75
43 changes: 43 additions & 0 deletions generator/config/accumulator/integral.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# $schema: ../schema.json
name: $integral
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/integral/'
type:
- window
encode: object
description: |
Returns the approximation of the area under a curve.
New in MongoDB 5.0.
arguments:
-
name: input
type:
- resolvesToNumber
- resolvesToDate
-
name: unit
type:
- resolvesToString
optional: true
description: |
A string that specifies the time unit. Use one of these strings: "week", "day","hour", "minute", "second", "millisecond".
If the sortBy field is not a date, you must omit a unit. If you specify a unit, you must specify a date in the sortBy field.
tests:
-
name: 'Example'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/integral/#example'
pipeline:
-
$setWindowFields:
partitionBy: '$powerMeterID'
sortBy:
timeStamp: 1
output:
powerMeterKilowattHours:
$integral:
input: '$kilowatts'
unit: 'hour'
window:
range:
- 'unbounded'
- 'current'
unit: 'hour'
40 changes: 40 additions & 0 deletions generator/config/accumulator/linearFill.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# $schema: ../schema.json
name: $linearFill
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/linearFill/'
type:
- window
encode: single
description: |
Fills null and missing fields in a window using linear interpolation based on surrounding field values.
Available in the $setWindowFields stage.
New in MongoDB 5.3.
arguments:
-
name: expression
type:
- resolvesToNumber
tests:
-
name: 'Fill Missing Values with Linear Interpolation'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/linearFill/#fill-missing-values-with-linear-interpolation'
pipeline:
-
$setWindowFields:
sortBy:
time: 1
output:
price:
$linearFill: '$price'
-
name: 'Use Multiple Fill Methods in a Single Stage'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/linearFill/#use-multiple-fill-methods-in-a-single-stage'
pipeline:
-
$setWindowFields:
sortBy:
time: 1
output:
linearFillPrice:
$linearFill: '$price'
locfPrice:
$locf: '$price'
27 changes: 27 additions & 0 deletions generator/config/accumulator/locf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# $schema: ../schema.json
name: $locf
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/locf/'
type:
- window
encode: single
description: |
Last observation carried forward. Sets values for null and missing fields in a window to the last non-null value for the field.
Available in the $setWindowFields stage.
New in MongoDB 5.2.
arguments:
-
name: expression
type:
- expression
tests:
-
name: 'Fill Missing Values with the Last Observed Value'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/locf/#fill-missing-values-with-the-last-observed-value'
pipeline:
-
$setWindowFields:
sortBy:
time: 1
output:
price:
$locf: '$price'
51 changes: 51 additions & 0 deletions generator/config/accumulator/minN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,54 @@ arguments:
- resolvesToInt
description: |
An expression that resolves to a positive integer. The integer specifies the number of array elements that $maxN returns.
tests:
-
name: 'Find the Minimum Three Scores for a Single Game'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/minN/#find-the-minimum-three-scores-for-a-single-game'
pipeline:
-
$match:
gameId: 'G1'
-
$group:
_id: '$gameId'
minScores:
$minN:
input:
- '$score'
- '$playerId'
n: 3
-
name: 'Finding the Minimum Three Documents Across Multiple Games'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/minN/#finding-the-minimum-three-documents-across-multiple-games'
pipeline:
-
$group:
_id: '$gameId'
minScores:
$minN:
input:
- '$score'
- '$playerId'
n: 3
-
name: 'Computing n Based on the Group Key for $group'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/minN/#computing-n-based-on-the-group-key-for--group'
pipeline:
-
$group:
_id:
gameId: '$gameId'
gamescores:
$minN:
input:
- '$score'
- '$playerId'
n:
$cond:
if:
$eq:
- '$gameId'
- 'G2'
then: 1
else: 3
34 changes: 34 additions & 0 deletions generator/config/accumulator/rank.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# $schema: ../schema.json
name: $rank
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rank/'
type:
- window
encode: object
description: |
Returns the document position (known as the rank) relative to other documents in the $setWindowFields stage partition.
New in MongoDB 5.0.
tests:
-
name: 'Rank Partitions by an Integer Field'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rank/#rank-partitions-by-an-integer-field'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
quantity: -1
output:
rankQuantityForState:
$rank: {}
-
name: 'Rank Partitions by a Date Field'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/rank/#rank-partitions-by-a-date-field'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
orderDate: 1
output:
rankOrderDateForState:
$rank: {}
31 changes: 31 additions & 0 deletions generator/config/accumulator/shift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,34 @@ arguments:
Specifies an optional default expression to evaluate if the document position is outside of the implicit $setWindowFields stage window. The implicit window contains all the documents in the partition.
The default expression must evaluate to a constant value.
If you do not specify a default expression, $shift returns null for documents whose positions are outside of the implicit $setWindowFields stage window.
tests:
-
name: 'Shift Using a Positive Integer'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/shift/#shift-using-a-positive-integer'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
quantity: -1
output:
shiftQuantityForState:
$shift:
output: '$quantity'
by: 1
default: 'Not available'
-
name: 'Shift Using a Negative Integer'
link: 'https://www.mongodb.com/docs/manual/reference/operator/aggregation/shift/#shift-using-a-negative-integer'
pipeline:
-
$setWindowFields:
partitionBy: '$state'
sortBy:
quantity: -1
output:
shiftQuantityForState:
$shift:
output: '$quantity'
by: -1
default: 'Not available'
Loading