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

Implement pushdown for aggregate for PostgreSQL #3607

Merged
merged 10 commits into from
Oct 23, 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 Oct 18, 2023
commit 44e5645eba1065820d514a0a22ea5bd8f1b5d348
20 changes: 16 additions & 4 deletions internal/handlers/sqlite/msg_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,20 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
var iter iterator.Interface[struct{}, *types.Document]

if len(collStatsDocuments) == len(stagesDocuments) {
// TODO https://github.com/FerretDB/FerretDB/issues/3235
// TODO https://github.com/FerretDB/FerretDB/issues/3181
iter, err = processStagesDocuments(ctx, closer, &stagesDocumentsParams{c, stagesDocuments})
filter, _ := aggregations.GetPushdownQuery(aggregationStages)

// only documents stages or no stages - fetch documents from the DB and apply stages to them
qp := new(backends.QueryParams)

if !h.DisableFilterPushdown {
qp.Filter = filter
}

//if h.EnableSortPushdown {
// qp.Sort = sort
//}

iter, err = processStagesDocuments(ctx, closer, &stagesDocumentsParams{c, qp, stagesDocuments})
} else {
// TODO https://github.com/FerretDB/FerretDB/issues/2423
statistics := stages.GetStatistics(collStatsDocuments)
Expand Down Expand Up @@ -323,12 +334,13 @@ func (h *Handler) MsgAggregate(ctx context.Context, msg *wire.OpMsg) (*wire.OpMs
// stagesDocumentsParams contains the parameters for processStagesDocuments.
type stagesDocumentsParams struct {
c backends.Collection
qp *backends.QueryParams
stages []aggregations.Stage
}

// processStagesDocuments retrieves the documents from the database and then processes them through the stages.
func processStagesDocuments(ctx context.Context, closer *iterator.MultiCloser, p *stagesDocumentsParams) (types.DocumentsIterator, error) { //nolint:lll // for readability
queryRes, err := p.c.Query(ctx, nil)
queryRes, err := p.c.Query(ctx, p.qp)
if err != nil {
closer.Close()
return nil, lazyerrors.Error(err)
Expand Down
10 changes: 8 additions & 2 deletions internal/handlers/sqlite/msg_explain.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"github.com/FerretDB/FerretDB/build/version"
"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/handlers/common"
"github.com/FerretDB/FerretDB/internal/handlers/common/aggregations"
"github.com/FerretDB/FerretDB/internal/handlers/commonerrors"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
Expand Down Expand Up @@ -79,8 +80,13 @@
}

var qp backends.ExplainParams
if !h.DisableFilterPushdown {
qp.Filter = params.Filter

if params.Aggregate {
qp.Filter, _ = aggregations.GetPushdownQuery(params.StagesDocs)
}

if h.DisableFilterPushdown {
qp.Filter = nil

Check warning on line 89 in internal/handlers/sqlite/msg_explain.go

View check run for this annotation

Codecov / codecov/patch

internal/handlers/sqlite/msg_explain.go#L89

Added line #L89 was not covered by tests
}

// Skip sorting if there are more than one sort parameters
Expand Down
Loading