File tree Expand file tree Collapse file tree 2 files changed +37
-6
lines changed Expand file tree Collapse file tree 2 files changed +37
-6
lines changed Original file line number Diff line number Diff line change 259
259
{:separator " "
260
260
:emitters {:map emit-where-map}})))
261
261
262
+ (declare emit-select )
263
+
262
264
(defn emit-join-clause [clause content]
263
265
(when (not-empty content)
264
266
(->> content
265
267
(map
266
- (fn [{:keys [type from on] }]
268
+ (fn [{:keys [type from on select as] :as join }]
267
269
(str/join " "
268
270
[(cond
269
- (#{:left :right :full } type)
271
+ (#{:left :right :full :inner } type)
270
272
(str (name type) " join" )
271
273
:else " join" )
272
- (emit-idents (ensure-seq from))
274
+ (if select
275
+ (let [s (emit-select (select-keys join [:select :distinct? :from :group-by :order-by :limit ]))]
276
+ (str " (" s " )"
277
+ (when as
278
+ (str " as " (name as)))))
279
+ (emit-idents (ensure-seq from)))
273
280
(emit-where-clause " on" on)])))
274
281
(str/join " " ))))
275
282
Original file line number Diff line number Diff line change 161
161
" AS purchases_per_offer_loads" ])
162
162
(sql/sql
163
163
'{:with {:get-offer (sql
164
- {:select {:count (count 1 )}
165
- :from :myapp.cues
166
- :where (= :cue " myapp.get-offer" )})
164
+ {:select {:count (count 1 )}
165
+ :from :myapp.cues
166
+ :where (= :cue " myapp.get-offer" )})
167
167
:purchase (sql
168
168
{:select {:count (count 1 )}
169
169
:from :myapp.cues
390
390
:from :a_events
391
391
:where {:id [" foo" " bar" ]
392
392
:name " baz" }}))))
393
+
394
+
395
+ (deftest select-join-test
396
+ (is (= (canon
397
+ [" select b1.id1 as id1 from a_events as b1"
398
+ " inner join (select distinct id1 as id1,id2 as id2,max(id3) as id3"
399
+ " from a_events group by id1,id2) as b2"
400
+ " on (b1.id1 = b2.id1)"
401
+ " and (b1.id2 = b2.id2)"
402
+ " and (b1.id3 = b2.id3)" ])
403
+ (sql/sql
404
+ '{:select {:id1 :b1/id1 }
405
+ :from {:b1 :a_events }
406
+ :join [{:type :inner
407
+ :select {:id1 :id1
408
+ :id2 :id2
409
+ :id3 (max :id3 )}
410
+ :distinct? true
411
+ :from :a_events
412
+ :group-by [:id1 :id2 ]
413
+ :on {:b1/id1 :b2/id1
414
+ :b1/id2 :b2/id2
415
+ :b1/id3 :b2/id3 }
416
+ :as :b2 }]}))))
You can’t perform that action at this time.
0 commit comments