Skip to content

Commit

Permalink
Implement $sort aggregation pipeline stage (#2093)
Browse files Browse the repository at this point in the history
  • Loading branch information
chilagrow authored Mar 1, 2023
1 parent 1fa849f commit dfd46b3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 20 deletions.
9 changes: 7 additions & 2 deletions internal/handlers/common/aggregations/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/types"
)

Expand All @@ -30,7 +31,11 @@ type match struct {
func newMatch(stage *types.Document) (Stage, error) {
filter, err := common.GetRequiredParam[*types.Document](stage, "$match")
if err != nil {
return nil, err
return nil, commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrMatchBadExpression,
"the match filter must be an expression in an object",
"aggregate",
)
}

return &match{
Expand Down Expand Up @@ -58,5 +63,5 @@ func (m *match) Process(ctx context.Context, in []*types.Document) ([]*types.Doc

// check interfaces
var (
_ Stage = (*count)(nil)
_ Stage = (*match)(nil)
)
60 changes: 60 additions & 0 deletions internal/handlers/common/aggregations/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2021 FerretDB Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package aggregations

import (
"context"

"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/types"
)

// sort represents $sort stage.
type sort struct {
fields *types.Document
}

// newSort creates a new $sort stage.
func newSort(stage *types.Document) (Stage, error) {
fields, err := common.GetRequiredParam[*types.Document](stage, "$sort")
if err != nil {
return nil, commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrSortBadExpression,
"the $sort key specification must be an object",
"aggregate",
)
}

// TODO: https://github.com/FerretDB/FerretDB/issues/2090

return &sort{
fields: fields,
}, nil
}

// Process implements Stage interface.
func (m *sort) Process(ctx context.Context, in []*types.Document) ([]*types.Document, error) {
if err := common.SortDocuments(in, m.fields); err != nil {
return nil, err
}

return in, nil
}

// check interfaces
var (
_ Stage = (*sort)(nil)
)
3 changes: 2 additions & 1 deletion internal/handlers/common/aggregations/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ import (
type newStageFunc func(stage *types.Document) (Stage, error)

// Stage is a common interface for all aggregation stages.
//
// TODO use iterators instead of slices of documents
// https://github.com/FerretDB/FerretDB/issues/1889.
type Stage interface {
// Process applies an aggregate stage on `in` document, it could modify `in` in-place.
Process(ctx context.Context, in []*types.Document) ([]*types.Document, error)
}

Expand All @@ -38,6 +38,7 @@ var stages = map[string]newStageFunc{
// sorted alphabetically
"$count": newCount,
"$match": newMatch,
"$sort": newSort,
}

// NewStage creates a new aggregation stage.
Expand Down
6 changes: 6 additions & 0 deletions internal/handlers/commonerrors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ const (
// ErrDuplicateKey indicates duplicate key violation.
ErrDuplicateKey = ErrorCode(11000) // Location11000

// ErrMatchBadExpression indicates match filter is not object.
ErrMatchBadExpression = ErrorCode(15959)

// ErrSortBadExpression indicates sort expression is not object.
ErrSortBadExpression = ErrorCode(15973)

// ErrSortBadValue indicates bad value in sort input.
ErrSortBadValue = ErrorCode(15974) // Location15974

Expand Down
38 changes: 21 additions & 17 deletions internal/handlers/commonerrors/errorcode_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dfd46b3

Please sign in to comment.