Skip to content

Commit

Permalink
query: fix #:cross joins
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdanp committed Aug 26, 2024
1 parent 1a5a241 commit 85aa914
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 18 deletions.
17 changes: 10 additions & 7 deletions deta-doc/deta.scrbl
Original file line number Diff line number Diff line change
Expand Up @@ -730,10 +730,10 @@ queries.

@defform*[
#:literals (subquery unquote)
((join query maybe-type maybe-lateral table-name #:as alias #:on q-expr)
(join query maybe-type maybe-lateral schema-id #:as alias #:on q-expr)
(join query maybe-type maybe-lateral (subquery query) #:as alias #:on q-expr)
(join query maybe-type maybe-lateral (unquote table-name-expr) #:as alias #:on q-expr))
((join query maybe-type maybe-lateral table-name #:as alias maybe-condition)
(join query maybe-type maybe-lateral schema-id #:as alias maybe-condition)
(join query maybe-type maybe-lateral (subquery query) #:as alias maybe-condition)
(join query maybe-type maybe-lateral (unquote table-name-expr) #:as alias maybe-condition))
#:grammar
([maybe-type (code:line)
(code:line #:inner)
Expand All @@ -742,13 +742,16 @@ queries.
(code:line #:full)
(code:line #:cross)]
[maybe-lateral (code:line)
(code:line #:lateral)])
(code:line #:lateral)]
[maybe-condition (code:line)
(code:line #:on q-expr)])
#:contracts
([table-name non-empty-string?]
[query query?])]{

Adds a @tt{JOIN} to @racket[query]. If a join type is not provided,
then the join defaults to an @tt{INNER} join.
Adds a @tt{JOIN} to @racket[query]. If a join type is not provided,
then the join defaults to an @tt{INNER} join. A join condition is
required for all join types apart from @racket[#:cross].

@interaction[
#:eval reference-eval
Expand Down
2 changes: 1 addition & 1 deletion deta-lib/info.rkt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#lang info

(define license 'BSD-3-Clause)
(define version "0.15")
(define version "0.15.1")
(define collection "deta")

(define deps '("base"
Expand Down
5 changes: 3 additions & 2 deletions deta-lib/private/dialect/standard.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,9 @@
(when lateral?
(write-string "LATERAL "))
(write-expr with)
(write-string " ON ")
(write-expr constraint)]
(unless (eq? type 'cross)
(write-string " ON ")
(write-expr constraint))]

[(where e)
(write-string "WHERE ")
Expand Down
6 changes: 3 additions & 3 deletions deta-lib/private/query.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
#:lateral? boolean?
#:with (or/c ast:subquery? schema? string? symbol?)
#:as symbol?
#:on ast:expr?
#:on (or/c #f ast:expr?)
query?)]
[limit (-> query? (or/c ast:scalar? ast:placeholder?) query?)]
[offset (-> query? (or/c ast:scalar? ast:placeholder?) query?)]
Expand All @@ -99,8 +99,8 @@
[project-virtual-fields (-> query? query?)]
[returning (-> query? ast:expr? ast:expr? ... query?)]
[select
(->* (query? ast:expr?)
(#:distinct? boolean?)
(->* [query? ast:expr?]
[#:distinct? boolean?]
#:rest (listof ast:expr?)
query?)]
[select-for-schema
Expand Down
19 changes: 15 additions & 4 deletions deta-lib/query.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -474,16 +474,27 @@
#'(dyn:group-by q e.e ...)]))

(define-syntax (join stx)
(define-syntax-class join-type
(define-syntax-class qualified-join-type
(pattern #:inner #:with type #''inner)
(pattern #:left #:with type #''left)
(pattern #:right #:with type #''right)
(pattern #:full #:with type #''full)
(pattern #:cross #:with type #''cross))
(pattern #:full #:with type #''full))

(syntax-parse stx
[(_ q:expr
(~optional t:join-type)
#:cross
(~optional (~and #:lateral lateral))
source:q-source
#:as alias:id)
#:with lateral? (if (attribute lateral) #'#t #'#f)
#'(dyn:join q
#:type 'cross
#:lateral? lateral?
#:with source.e
#:as 'alias
#:on #f)]
[(_ q:expr
(~optional t:qualified-join-type)
(~optional (~and #:lateral lateral))
source:q-source
#:as alias:id
Expand Down
22 changes: 21 additions & 1 deletion deta-test/deta/sql-postgresql.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,27 @@
#:as pi
#:on #t)
(select p.* pi.*))
"SELECT p.*, pi.* FROM posts AS p LEFT JOIN LATERAL (SELECT * FROM post_images AS pi WHERE pi.post_id = p.id ORDER BY pi.id DESC LIMIT 1) AS pi ON TRUE")))
"SELECT p.*, pi.* FROM posts AS p LEFT JOIN LATERAL (SELECT * FROM post_images AS pi WHERE pi.post_id = p.id ORDER BY pi.id DESC LIMIT 1) AS pi ON TRUE"))

(test-case "emits cross joins"
(check-emitted
(~> (from "posts" #:as p)
(join #:cross "post_images" #:as pi)
(select p.* pi.*))
"SELECT p.*, pi.* FROM posts AS p CROSS JOIN post_images AS pi"))

(test-case "emits cross lateral joins"
(check-emitted
(~> (from "posts" #:as p)
(join #:cross
#:lateral
(subquery
(~> (from "post_images" #:as pi)
(where (= pi.post-id p.id))
(limit 1)))
#:as pi)
(select p.*))
"SELECT p.* FROM posts AS p CROSS JOIN LATERAL (SELECT * FROM post_images AS pi WHERE pi.post_id = p.id LIMIT 1) AS pi")))

(test-suite
"group-by"
Expand Down

0 comments on commit 85aa914

Please sign in to comment.