Skip to content
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

Support recursive operator calls for $sum aggregation accumulator #3116

Merged
merged 18 commits into from
Jul 31, 2023
Prev Previous commit
Next Next commit
wip
  • Loading branch information
noisersup committed Jul 26, 2023
commit 53c96f9d996c9af5de67b52872f3693087b154ba
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"errors"

"github.com/FerretDB/FerretDB/internal/handlers/common/aggregations"
"github.com/FerretDB/FerretDB/internal/handlers/common/aggregations/operators"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/iterator"
Expand All @@ -44,7 +45,21 @@ func newSum(accumulation *types.Document) (Accumulator, error) {
"$sum (accumulator)",
)
case *types.Document:
accumulator.number = int32(0)
if !operators.IsOperator(expr) {
//TODO
accumulator.number = int32(0)
}

_, err := operators.NewOperator(expr)

if err != nil {
var opErr operators.OperatorError
if !errors.As(err, &opErr) {
return nil, lazyerrors.Error(err)
}

return nil, opErr
}

case float64:
accumulator.number = expr
Expand Down
9 changes: 8 additions & 1 deletion internal/handlers/common/aggregations/operators/operators.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func IsOperator(doc *types.Document) bool {

// NewOperator returns operator from provided document.
// The document should look like: `{<$operator>: <operator-value>}`.
// It also validates the document.
//
// Before calling NewOperator on document it's recommended to validate
// document before by using IsOperator on it.
Expand All @@ -95,7 +96,13 @@ func NewOperator(doc *types.Document) (Operator, error) {

switch {
case supported:
return newOperator(doc)
op, err := newOperator(doc)
if err == nil {
_, err = op.Process(nil)
}

return op, err

case unsupported:
return nil, newOperatorError(
ErrNotImplemented,
Expand Down