Skip to content

Commit

Permalink
Remove limit for fetching secondary docs
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed May 6, 2024
1 parent e4c59a9 commit 8e04962
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 9 deletions.
9 changes: 2 additions & 7 deletions planner/type_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,15 @@ func (n *typeJoinOne) Kind() string {
return "typeJoinOne"
}

func fetchDocsWithFieldValue(plan planNode, fieldName string, val any, limit uint) ([]core.Doc, error) {
func fetchDocsWithFieldValue(plan planNode, fieldName string, val any) ([]core.Doc, error) {
propIndex := plan.DocumentMap().FirstIndexOfName(fieldName)
setSubTypeFilterToScanNode(plan, propIndex, val)

if err := plan.Init(); err != nil {
return nil, NewErrSubTypeInit(err)
}

docs := make([]core.Doc, 0, limit)
var docs []core.Doc
for {
next, err := plan.Next()
if err != nil {
Expand All @@ -313,10 +313,6 @@ func fetchDocsWithFieldValue(plan planNode, fieldName string, val any, limit uin
}

docs = append(docs, plan.Value())

if limit > 0 && len(docs) >= int(limit) {
break
}
}

return docs, nil
Expand Down Expand Up @@ -587,7 +583,6 @@ func (join *invertibleTypeJoin) Next() (bool, error) {
// otherwise the user would not have been able to request it.
join.dir.secondaryField.Value(),
firstDoc.GetID(),
join.secondaryFetchLimit,
)
if err != nil {
return false, err
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/index/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func getUserDocs() predefined.DocsList {
},
{
"model": "Playstation 5",
"year": 2022,
"year": 2021,
"type": "game_console",
"specs": map[string]any{
"CPU": 3.5,
Expand Down
112 changes: 111 additions & 1 deletion tests/integration/index/query_with_relation_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func TestQueryWithIndexOnOneToOnePrimaryRelation_IfFilterOnIndexedFieldOfRelatio
},
testUtils.Request{
Request: makeExplainQuery(req2),
Asserter: testUtils.NewExplainAsserter().WithFieldFetches(15).WithIndexFetches(3),
Asserter: testUtils.NewExplainAsserter().WithFieldFetches(33).WithIndexFetches(3),
},
},
}
Expand Down Expand Up @@ -553,3 +553,113 @@ func TestQueryWithIndexOnOneToOne_IfFilterOnIndexedRelation_ShouldFilter(t *test

testUtils.ExecuteTestCase(t, test)
}

func TestQueryWithIndexOnManyToOne_IfFilterOnIndexedField_ShouldFilterWithExplain(t *testing.T) {
req := `query {
Device(filter: {
year: {_eq: 2021}
}) {
model
owner {
name
}
}
}`
test := testUtils.TestCase{
Description: "With filter on indexed field of secondary relation (N-1) should fetch secondary and primary objects",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type User {
name: String
devices: [Device]
}
type Device {
model: String
year: Int @index
owner: User
}
`,
},
testUtils.CreatePredefinedDocs{
Docs: getUserDocs(),
},
testUtils.Request{
Request: req,
Results: []map[string]any{
{
"model": "Playstation 5",
"owner": map[string]any{
"name": "Islam",
},
},
{
"model": "Playstation 5",
"owner": map[string]any{
"name": "Addo",
},
},
{
"model": "iPhone 10",
"owner": map[string]any{
"name": "Addo",
},
},
},
},
testUtils.Request{
Request: makeExplainQuery(req),
Asserter: testUtils.NewExplainAsserter().WithFieldFetches(9).WithIndexFetches(3),
},
},
}

testUtils.ExecuteTestCase(t, test)
}

func TestQueryWithIndexOnManyToOne_IfFilterOnIndexedRelation_ShouldFilterWithExplain(t *testing.T) {
req := `query {
Device(filter: {
owner: {age: {_eq: 48}}
}) {
model
}
}`
test := testUtils.TestCase{
Description: "Upon querying secondary object with filter on indexed field of primary relation (in 1-N) should fetch all secondary objects of the same primary one",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type User {
name: String
age: Int @index
devices: [Device]
}
type Device {
model: String
owner: User
}
`,
},
testUtils.CreatePredefinedDocs{
Docs: getUserDocs(),
},
testUtils.Request{
Request: req,
Results: []map[string]any{
{"model": "iPad Mini"},
{"model": "iPhone 13"},
{"model": "MacBook Pro"},
},
},
testUtils.Request{
Request: makeExplainQuery(req),
Asserter: testUtils.NewExplainAsserter().WithFieldFetches(44).WithIndexFetches(1),
},
},
}

testUtils.ExecuteTestCase(t, test)
}

0 comments on commit 8e04962

Please sign in to comment.