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 28, 2023
commit 281bed0227d6d786bdfd0091c01cb9fe1a4cf151
14 changes: 0 additions & 14 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,6 @@ tasks:
--postgresql-url=postgres://username@127.0.0.1:5432/ferretdb
--test-records-dir=tmp/records

run-sort:
desc: "Run FerretDB with `pg` handler"
deps: [build-host]
cmds:
- >
bin/ferretdb{{exeExt}} -test.coverprofile=cover.txt --
--listen-addr=:27017
--proxy-addr=127.0.0.1:47017
--mode=diff-normal
--handler=pg
--postgresql-url=postgres://username@127.0.0.1:5432/ferretdb
--test-records-dir=tmp/records
--test-enable-sorting-pushdown

run-tigris:
desc: "Run FerretDB with `tigris` handler"
deps: [build-host]
Expand Down
103 changes: 52 additions & 51 deletions internal/handlers/pg/pgdb/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func buildIterator(ctx context.Context, tx pgx.Tx, p *iteratorParams) (types.Doc
}

if p.sort != nil {
sort, sortArgs, err := prepareSortClause(&placeholder, p.sort)
sort, sortArgs, err := prepareOrderByClause(&placeholder, p.sort)
if err != nil {
return nil, nil, lazyerrors.Error(err)
}
Expand All @@ -208,56 +208,6 @@ func buildIterator(ctx context.Context, tx pgx.Tx, p *iteratorParams) (types.Doc
return newIterator(ctx, rows, p), &results, nil
}

func prepareSortClause(p *Placeholder, sort *types.Document) (string, []any, error) {
iter := sort.Iterator()
defer iter.Close()

var key string
var order types.SortType

for {
k, v, err := iter.Next()
if err != nil {
if errors.Is(err, iterator.ErrIteratorDone) {
break
}

return "", nil, lazyerrors.Error(err)
}

// Skip sorting if there are more than one sort parameters
if order != 0 {
return "", nil, nil
}

order, err = common.GetSortType(k, v)
if err != nil {
return "", nil, err
}

key = k
}

// Skip sorting dot notation
if strings.ContainsRune(key, '.') {
return "", nil, nil
}

var sqlOrder string
switch order {
case types.Descending:
sqlOrder = "DESC"
case types.Ascending:
sqlOrder = "ASC"
case 0:
return "", nil, nil
default:
panic(fmt.Sprint("forbidden order:", order))
}

return fmt.Sprintf(" ORDER BY %s %s", p.Next(), sqlOrder), []any{key}, nil
}

// prepareWhereClause adds WHERE clause with given filters to the query and returns the query and arguments.
func prepareWhereClause(p *Placeholder, sqlFilters *types.Document) (string, []any, error) {
var filters []string
Expand Down Expand Up @@ -379,6 +329,57 @@ func prepareWhereClause(p *Placeholder, sqlFilters *types.Document) (string, []a
return filter, args, nil
}

// prepareOrderByClause adds ORDER BY clause with given sort document and returns the query and arguments.
func prepareOrderByClause(p *Placeholder, sort *types.Document) (string, []any, error) {
iter := sort.Iterator()
defer iter.Close()

var key string
var order types.SortType

for {
k, v, err := iter.Next()
if err != nil {
if errors.Is(err, iterator.ErrIteratorDone) {
break
}

return "", nil, lazyerrors.Error(err)
}

// Skip sorting if there are more than one sort parameters
if order != 0 {
return "", nil, nil
}

order, err = common.GetSortType(k, v)
if err != nil {
return "", nil, err
}

key = k
}

// Skip sorting dot notation
if strings.ContainsRune(key, '.') {
return "", nil, nil
}

var sqlOrder string
switch order {
case types.Descending:
sqlOrder = "DESC"
case types.Ascending:
sqlOrder = "ASC"
case 0:
return "", nil, nil
default:
panic(fmt.Sprint("forbidden order:", order))
}

return fmt.Sprintf(" ORDER BY %s %s", p.Next(), sqlOrder), []any{key}, nil
}

// filterEqual returns the proper SQL filter with arguments that filters documents
// where the value under k is equal to v.
func filterEqual(p *Placeholder, k string, v any) (filter string, args []any) {
Expand Down