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
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 25, 2023
commit 12d2d3d2a6ed2ff141c7f03caeaa2cd52eff84da
17 changes: 9 additions & 8 deletions internal/handlers/pg/pgdb/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,9 @@ func buildIterator(ctx context.Context, tx pgx.Tx, p *iteratorParams) (types.Doc

query += ` FROM ` + pgx.Identifier{p.schema, p.table}.Sanitize()

where, args, err := prepareWhereClause(p.filter)
var placeholder Placeholder

where, args, err := prepareWhereClause(&placeholder, p.filter)
if err != nil {
return nil, lazyerrors.Error(err)
}
Expand All @@ -176,7 +178,7 @@ func buildIterator(ctx context.Context, tx pgx.Tx, p *iteratorParams) (types.Doc
}

if p.nativeSort {
sort, arg, err := prepareSortClause(p.filter, asc)
sort, arg, err := prepareSortClause(&placeholder, p.filter, asc)
if err != nil {
return nil, lazyerrors.Error(err)
}
Expand All @@ -202,7 +204,7 @@ const (
asc order = 1
)

func prepareSortClause(sqlFilters *types.Document, o order) (string, any, error) {
func prepareSortClause(p *Placeholder, sqlFilters *types.Document, o order) (string, any, error) {
iter := sqlFilters.Iterator()
defer iter.Close()

Expand Down Expand Up @@ -242,14 +244,13 @@ func prepareSortClause(sqlFilters *types.Document, o order) (string, any, error)
panic(fmt.Sprint("forbidden order:", o))
}

return " ORDER BY $1" + sqlOrder, key, nil
return fmt.Sprintf(" ORDER BY %s %s", p.Next(), sqlOrder), key, nil
}

// prepareWhereClause adds WHERE clause with given filters to the query and returns the query and arguments.
func prepareWhereClause(sqlFilters *types.Document) (string, []any, error) {
func prepareWhereClause(p *Placeholder, sqlFilters *types.Document) (string, []any, error) {
var filters []string
var args []any
var p Placeholder

iter := sqlFilters.Iterator()
defer iter.Close()
Expand Down Expand Up @@ -307,7 +308,7 @@ func prepareWhereClause(sqlFilters *types.Document) (string, []any, error) {

switch k {
case "$eq":
if f, a := filterEqual(&p, rootKey, v); f != "" {
if f, a := filterEqual(p, rootKey, v); f != "" {
filters = append(filters, f)
args = append(args, a...)
}
Expand Down Expand Up @@ -349,7 +350,7 @@ func prepareWhereClause(sqlFilters *types.Document) (string, []any, error) {
// type not supported for pushdown

case float64, string, types.ObjectID, bool, time.Time, int32, int64:
if f, a := filterEqual(&p, rootKey, v); f != "" {
if f, a := filterEqual(p, rootKey, v); f != "" {
filters = append(filters, f)
args = append(args, a...)
}
Expand Down