dialect/sql: reduce the number of joins on neighbors without predicates (#154)

Summary:
Pull Request resolved: https://github.com/facebookincubator/ent/pull/154

Loading the "form" table is not required where there's no predicate on the join.

Reviewed By: alexsn

Differential Revision: D18421062

fbshipit-source-id: cb2a045a5a8a76ea4f07e5cf4305a6da338a616e
This commit is contained in:
Ariel Mashraki
2019-11-10 21:50:18 -08:00
committed by Facebook Github Bot
parent 514c0ff6d3
commit 73e294a21e
2 changed files with 36 additions and 4 deletions

View File

@@ -78,13 +78,10 @@ func Neighbors(dialect string, s *Step) (q *Selector) {
pk1, pk2 = pk2, pk1
}
to := builder.Table(s.To.Table)
from := builder.Table(s.From.Table)
join := builder.Table(s.Edge.Table)
match := builder.Select(join.C(pk1)).
From(join).
Join(from).
On(join.C(pk2), from.C(s.From.Column)).
Where(EQ(from.C(s.From.Column), s.From.V))
Where(EQ(join.C(pk2), s.From.V))
q = builder.Select().
From(to).
Join(match).

View File

@@ -159,6 +159,41 @@ func TestNeighbors(t *testing.T) {
wantQuery: "SELECT * FROM `users` JOIN (SELECT `parent_id` FROM `users` WHERE `id` = ?) AS `t1` ON `users`.`id` = `t1`.`parent_id`",
wantArgs: []interface{}{2},
},
{
name: "M2M/2type",
input: func() *Step {
step := &Step{}
step.From.V = 2
step.From.Table = "groups"
step.From.Column = "id"
step.To.Table = "users"
step.To.Column = "id"
step.Edge.Rel = M2M
step.Edge.Table = "user_groups"
step.Edge.Columns = []string{"group_id", "user_id"}
return step
}(),
wantQuery: "SELECT * FROM `users` JOIN (SELECT `user_groups`.`user_id` FROM `user_groups` WHERE `user_groups`.`group_id` = ?) AS `t1` ON `users`.`id` = `t1`.`user_id`",
wantArgs: []interface{}{2},
},
{
name: "M2M/2type/inverse",
input: func() *Step {
step := &Step{}
step.From.V = 2
step.From.Table = "users"
step.From.Column = "id"
step.To.Table = "groups"
step.To.Column = "id"
step.Edge.Rel = M2M
step.Edge.Inverse = true
step.Edge.Table = "user_groups"
step.Edge.Columns = []string{"group_id", "user_id"}
return step
}(),
wantQuery: "SELECT * FROM `groups` JOIN (SELECT `user_groups`.`group_id` FROM `user_groups` WHERE `user_groups`.`user_id` = ?) AS `t1` ON `groups`.`id` = `t1`.`group_id`",
wantArgs: []interface{}{2},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {