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 filter pushdown #3482

Merged
merged 26 commits into from
Oct 9, 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
lint
  • Loading branch information
noisersup committed Sep 29, 2023
commit 2dd28582961ff03c4b5a338e812c757ba85e57aa
14 changes: 10 additions & 4 deletions internal/backends/postgresql/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

if p == nil {
return &backends.QueryResult{
Iter: newQueryIterator(ctx, nil, nil),
Iter: newQueryIterator(ctx, &iteratorParams{}),

Check warning on line 61 in internal/backends/postgresql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/collection.go#L61

Added line #L61 was not covered by tests
}, nil
}

Expand All @@ -69,7 +69,7 @@

if meta == nil {
return &backends.QueryResult{
Iter: newQueryIterator(ctx, nil, nil),
Iter: newQueryIterator(ctx, &iteratorParams{}),

Check warning on line 72 in internal/backends/postgresql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/collection.go#L72

Added line #L72 was not covered by tests
}, nil
}

Expand All @@ -86,7 +86,9 @@
}

return &backends.QueryResult{
Iter: newQueryIterator(ctx, rows, nil),
Iter: newQueryIterator(ctx, &iteratorParams{
rows: rows,
}),

Check warning on line 91 in internal/backends/postgresql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/collection.go#L89-L91

Added lines #L89 - L91 were not covered by tests
}, nil
}

Expand Down Expand Up @@ -246,49 +248,53 @@

// Explain implements backends.Collection interface.
func (c *collection) Explain(ctx context.Context, params *backends.ExplainParams) (*backends.ExplainResult, error) {
p, err := c.r.DatabaseGetExisting(ctx, c.dbName)
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 254 in internal/backends/postgresql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/collection.go#L251-L254

Added lines #L251 - L254 were not covered by tests

if p == nil {
return &backends.ExplainResult{}, nil
}

Check warning on line 258 in internal/backends/postgresql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/collection.go#L256-L258

Added lines #L256 - L258 were not covered by tests

meta, err := c.r.CollectionGet(ctx, c.dbName, c.name)
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 263 in internal/backends/postgresql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/collection.go#L260-L263

Added lines #L260 - L263 were not covered by tests

if meta == nil {
return &backends.ExplainResult{
QueryPlanner: must.NotFail(types.NewDocument()),
}, nil
}

Check warning on line 269 in internal/backends/postgresql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/collection.go#L265-L269

Added lines #L265 - L269 were not covered by tests

// TODO https://github.com/FerretDB/FerretDB/issues/3414
q := fmt.Sprintf(
`EXPLAIN (VERBOSE true, FORMAT JSON) SELECT %s FROM %s`,
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
metadata.DefaultColumn,
pgx.Identifier{c.dbName, meta.TableName}.Sanitize(),
)

rows, err := p.Query(ctx, q)
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 281 in internal/backends/postgresql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/collection.go#L272-L281

Added lines #L272 - L281 were not covered by tests

iter := newQueryIterator(ctx, rows, unmarshalExplain)
iter := newQueryIterator(ctx, &iteratorParams{
rows: rows,
unmarshal: unmarshalExplain,
})

defer iter.Close()

_, queryPlan, err := iter.Next()
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 293 in internal/backends/postgresql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/collection.go#L283-L293

Added lines #L283 - L293 were not covered by tests

return &backends.ExplainResult{
QueryPlanner: must.NotFail(types.NewDocument("Plan", queryPlan)),
}, nil

Check warning on line 297 in internal/backends/postgresql/collection.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/collection.go#L295-L297

Added lines #L295 - L297 were not covered by tests
}

// Stats implements backends.Collection interface.
Expand Down
16 changes: 11 additions & 5 deletions internal/backends/postgresql/query_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
m sync.Mutex
}

// iteratorParams contains parameters for building an iterator.
type iteratorParams struct {
rows pgx.Rows
unmarshal func(b []byte) (*types.Document, error) // if set, iterator uses unmarshal to convert row to *types.Document.
}

// newQueryIterator returns a new queryIterator for the given Rows.
//
// Iterator's Close method closes rows.
Expand All @@ -49,16 +55,16 @@
//
// Nil rows are possible and return already done iterator.
// It still should be Close'd.
func newQueryIterator(ctx context.Context, rows pgx.Rows, unmarshal func(b []byte) (*types.Document, error)) types.DocumentsIterator {
if unmarshal == nil {
unmarshal = sjson.Unmarshal
func newQueryIterator(ctx context.Context, params *iteratorParams) types.DocumentsIterator {
if params.unmarshal == nil {
params.unmarshal = sjson.Unmarshal
}

Check warning on line 61 in internal/backends/postgresql/query_iterator.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/query_iterator.go#L58-L61

Added lines #L58 - L61 were not covered by tests

iter := &queryIterator{
ctx: ctx,
rows: rows,
rows: params.rows,
token: resource.NewToken(),
unmarshal: unmarshal,
unmarshal: params.unmarshal,

Check warning on line 67 in internal/backends/postgresql/query_iterator.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/query_iterator.go#L64-L67

Added lines #L64 - L67 were not covered by tests
}
resource.Track(iter, iter.token)

Expand Down Expand Up @@ -102,7 +108,7 @@
return unused, nil, lazyerrors.Error(err)
}

doc, err := iter.unmarshal(b)

Check warning on line 111 in internal/backends/postgresql/query_iterator.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/query_iterator.go#L111

Added line #L111 was not covered by tests
if err != nil {
iter.close()
return unused, nil, lazyerrors.Error(err)
Expand Down
Loading