Skip to content

Commit a0ff47c

Browse files
committed
query: add #:distinct-on to select
1 parent 5e4bcb2 commit a0ff47c

6 files changed

Lines changed: 26 additions & 8 deletions

File tree

‎deta-doc/deta.scrbl‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,8 +913,10 @@ queries.
913913
((select _ maybe-distinct q-expr ...+)
914914
(select query maybe-distinct q-expr ...+))
915915
#:grammar
916-
[(maybe-distinct (code:line)
917-
#:distinct)]
916+
[(maybe-distinct
917+
(code:line)
918+
(code:line #:distinct)
919+
(code:line #:distinct-on (q-expr ...+)))]
918920
#:contracts
919921
([query query?])]{
920922

‎deta-lib/info.rkt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#lang info
22

33
(define license 'BSD-3-Clause)
4-
(define version "0.18")
4+
(define version "0.19")
55
(define collection "deta")
66

77
(define deps '("base"

‎deta-lib/private/dialect/standard.rkt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@
240240
[(select distinct? columns from where group-by union order-by offset limit)
241241
(write-string "SELECT ")
242242
(when distinct?
243-
(write-string "DISTINCT "))
243+
(write-string "DISTINCT ")
244+
(when (pair? distinct?)
245+
(write-string "ON (")
246+
(write/sep distinct? write-expr)
247+
(write-string ") ")))
244248
(cond
245249
[(null? columns) (write-string "*")]
246250
[else (write-stmt columns)])

‎deta-lib/private/query.rkt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
[returning (-> query? ast:expr? ast:expr? ... query?)]
101101
[select
102102
(->* [query? ast:expr?]
103-
[#:distinct? boolean?]
103+
[#:distinct? (or/c boolean? list?)]
104104
#:rest (listof ast:expr?)
105105
query?)]
106106
[select-for-schema

‎deta-lib/query.rkt‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@
370370
(define (syntax->column-reference stx)
371371
(match-define (list _ a b)
372372
(regexp-match column-reference-re (symbol->string (syntax->datum stx))))
373-
374373
(cons (datum->syntax stx a)
375374
(datum->syntax stx (id->column-name b))))
376375

@@ -571,9 +570,14 @@
571570

572571
(syntax-parse stx
573572
[(_ q:from-expr
574-
(~alt (~optional (~and #:distinct distinct))) ...
573+
(~alt (~optional (~and #:distinct distinct))
574+
(~optional (~seq #:distinct-on (distinct-expr:q-expr ...+)))) ...
575575
e:q-expr ...+)
576-
#:with distinct? (if (attribute distinct) #'#t #'#f)
576+
#:with distinct?
577+
(cond
578+
[(attribute distinct-expr) #'(list distinct-expr.e ...)]
579+
[(attribute distinct) #'#t]
580+
[else #'#f])
577581
#'(dyn:select
578582
#:distinct? distinct?
579583
q.e e.e ...)]))

‎deta-test/deta/sql-postgresql.rkt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@
247247
(select #:distinct t.name t.count))
248248
"SELECT DISTINCT t.name, t.\"count\" FROM tags AS t"))
249249

250+
(test-case "suppports DISTINCT ON in from-queries"
251+
(check-emitted
252+
(~> (from "actions" #:as a)
253+
(select #:distinct-on (a.agent-id) a.agent-id a.action)
254+
(order-by ([a.agent-id]
255+
[a.created-at #:desc])))
256+
"SELECT DISTINCT ON (a.agent_id) a.agent_id, a.\"action\" FROM actions AS a ORDER BY a.agent_id, a.created_at DESC"))
257+
250258
(test-case "supports COND as an alias for CASE"
251259
(check-emitted
252260
(select

0 commit comments

Comments
 (0)