1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
require File.join(File.dirname(__FILE__), '..', '..', '..', 'spec_helper')
module Arel
describe Join do
before do
@relation1 = Table(:users)
@relation2 = Table(:photos)
@predicate = @relation1[:id].eq(@relation2[:user_id])
end
describe 'when joining aggregated relations' do
before do
@aggregation = @relation2 \
.group(@relation2[:user_id]) \
.project(@relation2[:user_id], @relation2[:id].count.as(:cnt)) \
end
describe '#to_sql' do
# CLEANUP
it '' do
sql = @relation1.join(@relation2.take(3)).on(@predicate).to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `users`.`id`, `users`.`name`, `photos_external`.`id`, `photos_external`.`user_id`, `photos_external`.`camera_id`
FROM `users`
INNER JOIN (SELECT `photos`.`id`, `photos`.`user_id`, `photos`.`camera_id` FROM `photos` LIMIT 3) AS `photos_external`
ON `users`.`id` = `photos_external`.`user_id`
})
end
adapter_is_not :mysql do
sql.should be_like(%Q{
SELECT "users"."id", "users"."name", "photos_external"."id", "photos_external"."user_id", "photos_external"."camera_id"
FROM "users"
INNER JOIN (SELECT "photos"."id", "photos"."user_id", "photos"."camera_id" FROM "photos" LIMIT 3) AS "photos_external"
ON "users"."id" = "photos_external"."user_id"
})
end
end
describe 'with the aggregation on the right' do
it 'manufactures sql joining the left table to a derived table' do
sql = @relation1.join(@aggregation).on(@predicate).to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `users`.`id`, `users`.`name`, `photos_external`.`user_id`, `photos_external`.`cnt`
FROM `users`
INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external`
ON `users`.`id` = `photos_external`.`user_id`
})
end
adapter_is_not :mysql do
sql.should be_like(%Q{
SELECT "users"."id", "users"."name", "photos_external"."user_id", "photos_external"."cnt"
FROM "users"
INNER JOIN (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external"
ON "users"."id" = "photos_external"."user_id"
})
end
end
end
describe 'with the aggregation on the left' do
it 'manufactures sql joining the right table to a derived table' do
sql = @aggregation.join(@relation1).on(@predicate).to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `photos_external`.`user_id`, `photos_external`.`cnt`, `users`.`id`, `users`.`name`
FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external`
INNER JOIN `users`
ON `users`.`id` = `photos_external`.`user_id`
})
end
adapter_is_not :mysql do
sql.should be_like(%Q{
SELECT "photos_external"."user_id", "photos_external"."cnt", "users"."id", "users"."name"
FROM (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external"
INNER JOIN "users"
ON "users"."id" = "photos_external"."user_id"
})
end
end
end
describe 'with the aggregation on both sides' do
it 'it properly aliases the aggregations' do
aggregation2 = @aggregation.alias
sql = @aggregation.join(aggregation2).on(aggregation2[:user_id].eq(@aggregation[:user_id])).to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `photos_external`.`user_id`, `photos_external`.`cnt`, `photos_external_2`.`user_id`, `photos_external_2`.`cnt`
FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external`
INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` GROUP BY `photos`.`user_id`) AS `photos_external_2`
ON `photos_external_2`.`user_id` = `photos_external`.`user_id`
})
end
adapter_is_not :mysql do
sql.should be_like(%Q{
SELECT "photos_external"."user_id", "photos_external"."cnt", "photos_external_2"."user_id", "photos_external_2"."cnt"
FROM (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external"
INNER JOIN (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" GROUP BY "photos"."user_id") AS "photos_external_2"
ON "photos_external_2"."user_id" = "photos_external"."user_id"
})
end
end
end
describe 'when the aggration has a where' do
describe 'with the aggregation on the left' do
it "manufactures sql keeping wheres on the aggregation within the derived table" do
sql = @relation1.join(@aggregation.where(@aggregation[:user_id].eq(1))).on(@predicate).to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `users`.`id`, `users`.`name`, `photos_external`.`user_id`, `photos_external`.`cnt`
FROM `users`
INNER JOIN (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` WHERE `photos`.`user_id` = 1 GROUP BY `photos`.`user_id`) AS `photos_external`
ON `users`.`id` = `photos_external`.`user_id`
})
end
adapter_is_not :mysql do
sql.should be_like(%Q{
SELECT "users"."id", "users"."name", "photos_external"."user_id", "photos_external"."cnt"
FROM "users"
INNER JOIN (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" WHERE "photos"."user_id" = 1 GROUP BY "photos"."user_id") AS "photos_external"
ON "users"."id" = "photos_external"."user_id"
})
end
end
end
describe 'with the aggregation on the right' do
it "manufactures sql keeping wheres on the aggregation within the derived table" do
sql = @aggregation.where(@aggregation[:user_id].eq(1)).join(@relation1).on(@predicate).to_sql
adapter_is :mysql do
sql.should be_like(%Q{
SELECT `photos_external`.`user_id`, `photos_external`.`cnt`, `users`.`id`, `users`.`name`
FROM (SELECT `photos`.`user_id`, COUNT(`photos`.`id`) AS `cnt` FROM `photos` WHERE `photos`.`user_id` = 1 GROUP BY `photos`.`user_id`) AS `photos_external`
INNER JOIN `users`
ON `users`.`id` = `photos_external`.`user_id`
})
end
adapter_is_not :mysql do
sql.should be_like(%Q{
SELECT "photos_external"."user_id", "photos_external"."cnt", "users"."id", "users"."name"
FROM (SELECT "photos"."user_id", COUNT("photos"."id") AS "cnt" FROM "photos" WHERE "photos"."user_id" = 1 GROUP BY "photos"."user_id") AS "photos_external"
INNER JOIN "users"
ON "users"."id" = "photos_external"."user_id"
})
end
end
end
end
end
end
end
end
|