Skip to content

Commit

Permalink
Support dot notation in projection (#2536)
Browse files Browse the repository at this point in the history
Closes #2430.
  • Loading branch information
chilagrow authored May 12, 2023
1 parent f98dc95 commit 2516bb1
Show file tree
Hide file tree
Showing 6 changed files with 396 additions and 28 deletions.
29 changes: 22 additions & 7 deletions integration/aggregate_documents_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,9 @@ func TestAggregateCompatGroupSum(t *testing.T) {
Remove("ArrayAndDocuments").
// TODO: handle $sum of doubles near max precision.
// https://github.com/FerretDB/FerretDB/issues/2300
Remove("Doubles")
Remove("Doubles").
// TODO: https://github.com/FerretDB/FerretDB/issues/2616
Remove("ArrayDocuments")

testCases := map[string]aggregateStagesCompatTestCase{
"GroupNullID": {
Expand Down Expand Up @@ -1189,12 +1191,6 @@ func TestAggregateCompatSort(t *testing.T) {
}}}},
},

"DotNotation": {
pipeline: bson.A{bson.D{{"$sort", bson.D{
{"v.foo", 1},
{"_id", 1}, // sort by _id when v is the same.
}}}},
},
"DotNotationIndex": {
pipeline: bson.A{bson.D{{"$sort", bson.D{
{"v.0", 1},
Expand Down Expand Up @@ -1235,6 +1231,25 @@ func TestAggregateCompatSort(t *testing.T) {
testAggregateStagesCompat(t, testCases)
}

func TestAggregateCompatSortDotNotation(t *testing.T) {
t.Parallel()

providers := shareddata.AllProviders().
// TODO: https://github.com/FerretDB/FerretDB/issues/2617
Remove("ArrayDocuments")

testCases := map[string]aggregateStagesCompatTestCase{
"DotNotation": {
pipeline: bson.A{bson.D{{"$sort", bson.D{
{"v.foo", 1},
{"_id", 1}, // sort by _id when v is the same.
}}}},
},
}

testAggregateStagesCompatWithProviders(t, providers, testCases)
}

func TestAggregateCompatUnwind(t *testing.T) {
t.Parallel()

Expand Down
20 changes: 16 additions & 4 deletions integration/query_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,10 +254,6 @@ func TestQueryCompatSort(t *testing.T) {
resultType: emptyResult,
},

"DotNotation": {
filter: bson.D{},
sort: bson.D{{"v.foo", 1}, {"_id", 1}},
},
"DotNotationIndex": {
filter: bson.D{},
sort: bson.D{{"v.0", 1}, {"_id", 1}},
Expand Down Expand Up @@ -296,6 +292,22 @@ func TestQueryCompatSort(t *testing.T) {
testQueryCompat(t, testCases)
}

func TestQueryCompatSortDotNotation(t *testing.T) {
t.Parallel()

providers := shareddata.AllProviders().
// TODO: https://github.com/FerretDB/FerretDB/issues/2618
Remove("ArrayDocuments")

testCases := map[string]queryCompatTestCase{
"DotNotation": {
filter: bson.D{},
sort: bson.D{{"v.foo", 1}, {"_id", 1}},
},
}
testQueryCompatWithProviders(t, providers, testCases)
}

func TestQueryCompatSkip(t *testing.T) {
t.Parallel()

Expand Down
72 changes: 66 additions & 6 deletions integration/query_projection_compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func TestQueryProjectionCompat(t *testing.T) {
filter: bson.D{},
projection: bson.D{{"foo", 1.24}, {"bar", true}},
},
"Include2FieldsReverse": {
filter: bson.D{},
projection: bson.D{{"bar", true}, {"foo", 1.24}},
},
"Exclude2Fields": {
filter: bson.D{},
projection: bson.D{{"foo", int32(0)}, {"bar", false}},
Expand Down Expand Up @@ -141,33 +145,89 @@ func TestQueryProjectionCompat(t *testing.T) {
"DotNotationInclude": {
filter: bson.D{},
projection: bson.D{{"v.foo", true}},
skip: "https://github.com/FerretDB/FerretDB/issues/2430",
},
"DotNotationIncludeTwo": {
filter: bson.D{},
projection: bson.D{{"v.foo", true}, {"v.array", true}},
skip: "https://github.com/FerretDB/FerretDB/issues/2430",
},
"DotNotationIncludeTwoReverse": {
filter: bson.D{},
projection: bson.D{{"v.array", true}, {"v.foo", true}},
},
"DotNotationIncludeTwoArray": {
filter: bson.D{},
projection: bson.D{{"v.foo", true}, {"v.bar", true}},
},
"DotNotationExclude": {
filter: bson.D{},
projection: bson.D{{"v.foo", false}},
skip: "https://github.com/FerretDB/FerretDB/issues/2430",
},
"DotNotationExcludeTwo": {
filter: bson.D{},
projection: bson.D{{"v.foo", false}, {"v.array", false}},
skip: "https://github.com/FerretDB/FerretDB/issues/2430",
},
"DotNotationExcludeSecondLevel": {
filter: bson.D{},
projection: bson.D{{"v.array.42", false}},
skip: "https://github.com/FerretDB/FerretDB/issues/2430",
},
"DotNotationIncludeSecondLevel": {
filter: bson.D{},
projection: bson.D{{"v.array.42", true}},
},
"DotNotationIncludeExclude": {
filter: bson.D{},
projection: bson.D{{"v.foo", true}, {"v.array", false}},
resultType: emptyResult,
skip: "https://github.com/FerretDB/FerretDB/issues/2430",
},
"DotNotation5LevelInclude": {
filter: bson.D{},
projection: bson.D{{"v.a.b.c.d", true}},
},
"DotNotation5LevelExclude": {
filter: bson.D{},
projection: bson.D{{"v.a.b.c.d", false}},
},
"DotNotation4LevelInclude": {
filter: bson.D{},
projection: bson.D{{"v.a.b.c", true}},
},
"DotNotation4LevelExclude": {
filter: bson.D{},
projection: bson.D{{"v.a.b.c", false}},
},
"DotNotationArrayInclude": {
filter: bson.D{},
projection: bson.D{{"v.array.0", true}},
},
"DotNotationArrayExclude": {
filter: bson.D{},
projection: bson.D{{"v.array.0", false}},
},
"DotNotationArrayPathInclude": {
filter: bson.D{},
projection: bson.D{{"v.0.foo", true}},
},
"DotNotationArrayPathExclude": {
filter: bson.D{},
projection: bson.D{{"v.0.foo", false}},
},
"DotNotationManyInclude": {
filter: bson.D{},
projection: bson.D{
{"v.42", true},
{"v.non-existent", true},
{"v.foo", true},
{"v.array", true},
},
},
"DotNotationManyExclude": {
filter: bson.D{},
projection: bson.D{
{"v.42", false},
{"v.non-existent", false},
{"v.foo", false},
{"v.array", false},
},
},
}

Expand Down
46 changes: 46 additions & 0 deletions integration/shareddata/composites.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,38 @@ var DocumentsDocuments = &Values[primitive.ObjectID]{
},
}

// DocumentsDeeplyNested contains documents nested in multiple levels for tests.
var DocumentsDeeplyNested = &Values[string]{
name: "DocumentsDeeplyNested",
backends: []string{"ferretdb-pg", "mongodb"},
data: map[string]any{
"two": bson.D{{"a", bson.D{{"b", 12}}}},
"three": bson.D{{"a", bson.D{{"b", bson.D{{"c", 12}}}}}},
"four": bson.D{
{"a", bson.D{
{"b", bson.D{
{"c", bson.D{
{"d", 123},
}},
{"e", 13},
}},
{"f", 14},
}},
{"g", 15},
},
"array": bson.D{
{"a", bson.D{
{"b", bson.D{
{"c", bson.A{1, 2}},
{"e", 13},
}},
{"f", 14},
}},
{"g", 15},
},
},
}

// ArrayStrings contains an array with string values for tests.
// Tigris JSON schema validator contains extra properties to make it suitable for more tests.
var ArrayStrings = &Values[string]{
Expand Down Expand Up @@ -380,5 +412,19 @@ var ArrayDocuments = &Values[string]{
bson.A{bson.D{{"bar", "hello"}}},
}},
},
"array-three-documents": bson.A{
bson.D{{
"bar",
bson.A{bson.D{{"a", "b"}}},
}},
bson.D{{
"foo",
bson.A{bson.D{{"bar", "hello"}}},
}},
bson.D{{
"foo",
bson.A{bson.D{{"bar", "hello"}}},
}},
},
},
}
1 change: 1 addition & 0 deletions integration/shareddata/shareddata.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func AllProviders() Providers {
DocumentsDoubles,
DocumentsStrings,
DocumentsDocuments,
DocumentsDeeplyNested,

ArrayStrings,
ArrayDoubles,
Expand Down
Loading

0 comments on commit 2516bb1

Please sign in to comment.