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 7 commits
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
10 changes: 1 addition & 9 deletions integration/aggregate_documents_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,6 @@ func TestAggregateCompatOptions(t *testing.T) {
}

func TestAggregateCompatStages(t *testing.T) {
setup.SkipForPostgreSQL(t, "https://github.com/FerretDB/FerretDB/issues/3520")

t.Parallel()

testCases := map[string]aggregateStagesCompatTestCase{
Expand Down Expand Up @@ -904,8 +902,6 @@ func TestAggregateCompatGroupCount(t *testing.T) {
}

func TestAggregateCompatLimit(t *testing.T) {
setup.SkipForPostgreSQL(t, "https://github.com/FerretDB/FerretDB/issues/3520")

t.Parallel()

testCases := map[string]aggregateStagesCompatTestCase{
Expand Down Expand Up @@ -1247,8 +1243,6 @@ func TestAggregateCompatGroupSum(t *testing.T) {
}

func TestAggregateCompatMatch(t *testing.T) {
setup.SkipForPostgreSQL(t, "https://github.com/FerretDB/FerretDB/issues/3520")

t.Parallel()

// TODO https://github.com/FerretDB/FerretDB/issues/2291
Expand All @@ -1257,7 +1251,7 @@ func TestAggregateCompatMatch(t *testing.T) {
testCases := map[string]aggregateStagesCompatTestCase{
"ID": {
pipeline: bson.A{bson.D{{"$match", bson.D{{"_id", "string"}}}}},
resultPushdown: pgPushdown,
resultPushdown: allPushdown,
},
"Int": {
pipeline: bson.A{
Expand Down Expand Up @@ -1510,8 +1504,6 @@ func TestAggregateCompatUnwind(t *testing.T) {
}

func TestAggregateCompatSkip(t *testing.T) {
setup.SkipForPostgreSQL(t, "https://github.com/FerretDB/FerretDB/issues/3520")

t.Parallel()

testCases := map[string]aggregateStagesCompatTestCase{
Expand Down
34 changes: 30 additions & 4 deletions internal/handlers/sqlite/msg_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,34 @@
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, sort := 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
}

// Skip sorting if there are more than one sort parameters
if h.EnableSortPushdown && sort.Len() == 1 {
var order types.SortType

k := sort.Keys()[0]
v := sort.Values()[0]

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

Check warning on line 281 in internal/handlers/sqlite/msg_aggregate.go

View check run for this annotation

Codecov / codecov/patch

internal/handlers/sqlite/msg_aggregate.go#L273-L281

Added lines #L273 - L281 were not covered by tests
chilagrow marked this conversation as resolved.
Show resolved Hide resolved

qp.Sort = &backends.SortField{
Key: k,
Descending: order == types.Descending,
}

Check warning on line 286 in internal/handlers/sqlite/msg_aggregate.go

View check run for this annotation

Codecov / codecov/patch

internal/handlers/sqlite/msg_aggregate.go#L283-L286

Added lines #L283 - L286 were not covered by tests
}

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 +348,13 @@
// 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
22 changes: 18 additions & 4 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 @@ -78,13 +79,18 @@
return nil, lazyerrors.Error(err)
}

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

sort := params.Sort

if params.Aggregate {
qp.Filter, sort = aggregations.GetPushdownQuery(params.StagesDocs)
chilagrow marked this conversation as resolved.
Show resolved Hide resolved
}

// Skip sorting if there are more than one sort parameters
if h.EnableSortPushdown && params.Sort.Len() == 1 {
if h.EnableSortPushdown && sort.Len() == 1 {
var order types.SortType

k := params.Sort.Keys()[0]
Expand All @@ -110,6 +116,14 @@
qp.Limit = params.Limit
}

if h.DisableFilterPushdown {
qp.Filter = nil
}

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

View check run for this annotation

Codecov / codecov/patch

internal/handlers/sqlite/msg_explain.go#L120-L121

Added lines #L120 - L121 were not covered by tests

if !h.EnableSortPushdown {
qp.Sort = nil
}

res, err := coll.Explain(ctx, &qp)
if err != nil {
return nil, lazyerrors.Error(err)
Expand Down
Loading