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 _id aggregation operators for $group stage #3096

Merged
merged 19 commits into from
Jul 26, 2023
Prev Previous commit
Next Next commit
implement helper
  • Loading branch information
noisersup committed Jul 24, 2023
commit 8a49af4a9672e84a16b701c22753fec281b4a816
95 changes: 93 additions & 2 deletions internal/handlers/common/aggregations/stages/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,15 @@ func (g *group) groupDocuments(ctx context.Context, in []*types.Document) ([]gro
case *types.Document:
op, err := operators.NewOperator(groupKey)
if err != nil {
return nil, err
return nil, processOperatorError(err)
}

var group groupMap

for _, doc := range in {
val, err := op.Process(doc)
if err != nil {
return nil, err
return nil, processOperatorError(err)
}

group.addOrAppend(val, doc)
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -297,6 +297,97 @@ func (m *groupMap) addOrAppend(groupKey any, docs ...*types.Document) {
})
}

// processOperatorError takes internal error related to operator evaluation and
// returns proper CommandError that can be returned by $project aggregation stage.
//
// Command error codes:
// - ErrEmptySubProject when operator value is empty.
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
// - ErrFieldPathInvalidName when FieldPath is invalid.
// - ErrNotImplemented when the operator or expression is not implemented yet.
// - ErrOperatorWrongLenOfArgs when the operator has an invalid number of arguments.
// - ErrInvalidPipelineOperator when the operator does not exist.
// - ErrFailedToParse when operator has invalid variable expression.
// - ErrGroupInvalidFieldPath when operator has empty path expression.
func processOperatorError(err error) error {
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
return nil
}
chilagrow marked this conversation as resolved.
Show resolved Hide resolved

var opErr operators.OperatorError
var exErr *aggregations.ExpressionError

switch {
case errors.As(err, &opErr):
switch opErr.Code() {
case operators.ErrTooManyFields:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrFieldPathInvalidName,
"Invalid $group :: caused by :: FieldPath field names may not start with '$'."+
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
" Consider using $getField or $setField.",
"$group (stage)",
)
case operators.ErrNotImplemented:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrNotImplemented,
"Invalid $group :: caused by :: "+opErr.Error(),
"$group (stage)",
)
case operators.ErrArgsInvalidLen:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrOperatorWrongLenOfArgs,
"Invalid $group :: caused by :: "+opErr.Error(),
"$group (stage)",
)
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
case operators.ErrInvalidExpression:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrAggregateInvalidExpression,
"Invalid $group :: caused by :: "+opErr.Error(),
"$group (stage)",
)
case operators.ErrInvalidNestedExpression:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrInvalidPipelineOperator,
"Invalid $group :: caused by :: "+opErr.Error(),
"$group (stage)",
)
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
}

case errors.As(err, &exErr):
switch exErr.Code() {
case aggregations.ErrNotExpression:
// handled by upstream and this should not be reachable for existing expression implementation
fallthrough
case aggregations.ErrInvalidExpression:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrFailedToParse,
"Invalid $group :: caused by :: '$' starts with an invalid character for a user variable name",
"$group (stage)",
)
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
case aggregations.ErrEmptyFieldPath:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrGroupInvalidFieldPath,
"Invalid $group :: caused by :: '$' by itself is not a valid FieldPath",
"$group (stage)",
)
case aggregations.ErrUndefinedVariable:
// TODO https://github.com/FerretDB/FerretDB/issues/2275
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrNotImplemented,
"Aggregation expression variables are not implemented yet",
"$group (stage)",
)
case aggregations.ErrEmptyVariable:
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrFailedToParse,
"Invalid $group :: caused by :: empty variable names are not allowed",
"$group (stage)",
)
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
}
}

return lazyerrors.Error(err)
}

// check interfaces
var (
_ aggregations.Stage = (*group)(nil)
Expand Down