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

Pushdown simplest sorting for aggregate command #2530

Merged
merged 26 commits into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
wip
  • Loading branch information
noisersup committed Apr 27, 2023
commit 78605d09e879fcb8f288af07ac83cf9f9c8948fb
6 changes: 3 additions & 3 deletions internal/handlers/common/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func SortDocuments(docs []*types.Document, sortDoc *types.Document) error {
for i, sortKey := range sortDoc.Keys() {
sortField := must.NotFail(sortDoc.Get(sortKey))

sortType, err := getSortType(sortKey, sortField)
sortType, err := GetSortType(sortKey, sortField)
if err != nil {
return err
}
Expand Down Expand Up @@ -125,8 +125,8 @@ func (ds *docsSorter) Less(i, j int) bool {
return ds.sorts[k](p, q)
}

// getSortType determines SortType from input sort value.
func getSortType(key string, value any) (types.SortType, error) {
// GetSortType determines SortType from input sort value.
func GetSortType(key string, value any) (types.SortType, error) {
sortValue, err := GetWholeNumberParam(value)
if err != nil {
switch {
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/pg/msg_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func processStagesDocuments(ctx context.Context, p *stagesDocumentsParams) ([]*t
var docs []*types.Document

if err := p.dbPool.InTransaction(ctx, func(tx pgx.Tx) error {
iter, getErr := pgdb.QueryDocuments(ctx, tx, p.qp)
iter, _, getErr := pgdb.QueryDocuments(ctx, tx, p.qp)
if getErr != nil {
return getErr
}
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/pg/msg_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func execDelete(ctx context.Context, dp *execDeleteParams) (int32, error) {
}

err := dp.dbPool.InTransaction(ctx, func(tx pgx.Tx) error {
iter, err := pgdb.QueryDocuments(ctx, tx, dp.qp)
iter, _, err := pgdb.QueryDocuments(ctx, tx, dp.qp)
if err != nil {
return err
}
Expand Down
10 changes: 4 additions & 6 deletions internal/handlers/pg/msg_explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ func (h *Handler) MsgExplain(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg,

qp.Explain = true

qp.NativeSort = h.NativeSort

explain, err := common.GetRequiredParam[*types.Document](document, "explain")
if err != nil {
return nil, lazyerrors.Error(err)
Expand Down Expand Up @@ -106,18 +104,18 @@ func (h *Handler) MsgExplain(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg,
}

var queryPlanner *types.Document
var results *pgdb.QueryResults

err = dbPool.InTransaction(ctx, func(tx pgx.Tx) error {
var err error
queryPlanner, err = pgdb.Explain(ctx, tx, &qp)
queryPlanner, results, err = pgdb.Explain(ctx, tx, &qp)
return err
})
if err != nil {
return nil, err
}

// if the plan returned filter info or index info, it means that pushdown had been done
pushdown := queryPlanner.HasByPath(types.NewStaticPath("Plan", "Filter")) ||
queryPlanner.HasByPath(types.NewStaticPath("Plan", "Index Cond"))
pushdown := results.FilterPushdown

hostname, err := os.Hostname()
if err != nil {
Expand Down
34 changes: 20 additions & 14 deletions internal/handlers/pg/msg_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er
DB: params.DB,
Collection: params.Collection,
Comment: params.Comment,
NativeSort: h.NativeSort,
}

// get comment from query, e.g. db.collection.find({$comment: "test"})
Expand All @@ -75,15 +74,20 @@ func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er
qp.Filter = params.Filter
}

if h.NativeSort {
qp.Sort = params.Sort
}

var resDocs []*types.Document
err = dbPool.InTransaction(ctx, func(tx pgx.Tx) error {
if params.BatchSize == 0 {
return nil
}

var iter types.DocumentsIterator
var queryRes *pgdb.QueryResults

iter, err = pgdb.QueryDocuments(ctx, tx, qp)
iter, queryRes, err = pgdb.QueryDocuments(ctx, tx, qp)
if err != nil {
return lazyerrors.Error(err)
}
Expand All @@ -93,18 +97,20 @@ func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er

iter = common.FilterIterator(iter, closer, params.Filter)

iter, err = common.SortIterator(iter, closer, params.Sort)
if err != nil {
var pathErr *types.DocumentPathError
if errors.As(err, &pathErr) && pathErr.Code() == types.ErrDocumentPathEmptyKey {
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrPathContainsEmptyElement,
"Empty field names in path are not allowed",
document.Command(),
)
if !queryRes.SortPushdown {
iter, err = common.SortIterator(iter, closer, params.Sort)
if err != nil {
var pathErr *types.DocumentPathError
if errors.As(err, &pathErr) && pathErr.Code() == types.ErrDocumentPathEmptyKey {
return commonerrors.NewCommandErrorMsgWithArgument(
commonerrors.ErrPathContainsEmptyElement,
"Empty field names in path are not allowed",
document.Command(),
)
}

return lazyerrors.Error(err)
}

return lazyerrors.Error(err)
}

iter = common.SkipIterator(iter, closer, params.Skip)
Expand Down Expand Up @@ -182,7 +188,7 @@ func fetchAndFilterDocs(ctx context.Context, fp *fetchParams) ([]*types.Document
fp.qp.Filter = nil
}

iter, err := pgdb.QueryDocuments(ctx, fp.tx, fp.qp)
iter, _, err := pgdb.QueryDocuments(ctx, fp.tx, fp.qp)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/pg/pgdb/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Collections(ctx context.Context, tx pgx.Tx, db string) ([]string, error) {
return []string{}, nil
}

iter, err := buildIterator(ctx, tx, &iteratorParams{
iter, _, err := buildIterator(ctx, tx, &iteratorParams{
schema: db,
table: dbMetadataTableName,
})
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/pg/pgdb/database_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (ms *metadataStorage) get(ctx context.Context, forUpdate bool) (*metadata,
forUpdate: forUpdate,
}

iter, err := buildIterator(ctx, ms.tx, iterParams)
iter, _, err := buildIterator(ctx, ms.tx, iterParams)
if err != nil {
return nil, lazyerrors.Error(err)
}
Expand Down
Loading