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

Projection: <field>: <1 or true> and <field>: <0 or false> #377

Merged
merged 11 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/bin/
/vendor/
cover.txt
cover.html
old.txt
new.txt

Expand Down
91 changes: 89 additions & 2 deletions internal/handlers/common/projection.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,97 @@

package common

import "github.com/FerretDB/FerretDB/internal/types"
import (
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

// isProjectionInclusion: projection can be only inclusion or exlusion. Validate and return true if inclusion.
// Exception for the _id field
func isProjectionInclusion(projection *types.Document) (inclusion bool, err error) {
errMsg := "projection must contain only inclusions or exclusions"
var exclusion bool
for k, v := range projection.Map() {
if k == "_id" { // _id is a special case and can be both
continue
}
switch v := v.(type) {
case bool:
if v {
if exclusion {
err = lazyerrors.New(errMsg)
return
}
inclusion = true
} else {
if inclusion {
err = lazyerrors.New(errMsg)
return
}
exclusion = true
}
case int32, int64, float64:
if compareScalars(v, int32(0)) == equal {
if inclusion {
err = lazyerrors.New(errMsg)
return
}
exclusion = true
} else {
if exclusion {
err = lazyerrors.New(errMsg)
return
}
inclusion = true
}
default:
err = lazyerrors.Errorf("unsupported operation %s %v (%T)", k, v, v)
return
}
}
return
}

// ProjectDocuments modifies given documents in places according to the given projection.
func ProjectDocuments(docs []*types.Document, projection *types.Document) error {
// TODO
if projection.Len() == 0 {
return nil
}

inclusion, err := isProjectionInclusion(projection)
if err != nil {
return err
}

projectionMap := projection.Map()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it can be done w/o creation of new variable..

for i := 0; i < len(docs); i++ {
for k := range docs[i].Map() {

v, ok := projectionMap[k]
if !ok {
if k == "_id" { // if _id is not in projection map, do not do anything with it
continue
}
if inclusion {
docs[i].Remove(k)
}
continue
}

switch v := v.(type) { // found in the projection
case bool:
if !v {
docs[i].Remove(k)
}
case int32, int64, float64:
if compareScalars(v, int32(0)) == equal {
AlekSi marked this conversation as resolved.
Show resolved Hide resolved
docs[i].Remove(k)
}
default:
return lazyerrors.Errorf("unsupported operation %s %v (%T)", k, v, v)
}
}
}

return nil
}
71 changes: 68 additions & 3 deletions internal/handlers/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -810,12 +810,12 @@ func TestReadOnlyHandlers(t *testing.T) {
},
},

"FindProjectionActorsFirstAndLastName": {
"FindProjectionInclusions": {
req: types.MustNewDocument(
"find", "actor",
"projection", types.MustNewDocument(
"first_name", int32(1),
"last_name", int32(1),
"last_update", true,
),
"filter", types.MustNewDocument(
"actor_id", int32(28),
Expand All @@ -826,8 +826,73 @@ func TestReadOnlyHandlers(t *testing.T) {
"cursor", types.MustNewDocument(
"firstBatch", types.MustNewArray(
types.MustNewDocument(
"first_name", "WOODY",
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x1c},
"last_name", "HOFFMAN",
"last_update", time.Date(2020, 2, 15, 9, 34, 33, 0, time.UTC).Local(),
),
),
"id", int64(0),
"ns", "", // set by compareFunc
),
"ok", float64(1),
),
compareFunc: func(t testing.TB, _, expected, actual *types.Document) {
actualV := testutil.GetByPath(t, actual, "cursor", "ns")
testutil.SetByPath(t, expected, actualV, "cursor", "ns")
assert.Equal(t, expected, actual)
},
},

"FindProjectionExclusions": {
req: types.MustNewDocument(
"find", "actor",
"projection", types.MustNewDocument(
"first_name", int32(0),
"actor_id", false,
),
"filter", types.MustNewDocument(
"actor_id", int32(28),
),
),
reqSetDB: true,
resp: types.MustNewDocument(
"cursor", types.MustNewDocument(
"firstBatch", types.MustNewArray(
types.MustNewDocument(
"_id", types.ObjectID{0x61, 0x2e, 0xc2, 0x80, 0x00, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x1c},
"last_name", "HOFFMAN",
"last_update", time.Date(2020, 2, 15, 9, 34, 33, 0, time.UTC).Local(),
),
),
"id", int64(0),
"ns", "", // set by compareFunc
),
"ok", float64(1),
),
compareFunc: func(t testing.TB, _, expected, actual *types.Document) {
actualV := testutil.GetByPath(t, actual, "cursor", "ns")
testutil.SetByPath(t, expected, actualV, "cursor", "ns")
assert.Equal(t, expected, actual)
},
},

"FindProjectionIDInclusion": {
req: types.MustNewDocument(
"find", "actor",
"projection", types.MustNewDocument(
"_id", false,
"actor_id", int32(1),
),
"filter", types.MustNewDocument(
"actor_id", int32(28),
),
),
reqSetDB: true,
resp: types.MustNewDocument(
"cursor", types.MustNewDocument(
"firstBatch", types.MustNewArray(
types.MustNewDocument(
"actor_id", int32(28),
),
),
"id", int64(0),
Expand Down