Added OVER (PARTITION BY) clause

Supports windowed aggregate functions as fields
{:aggregate :%fn.field :over {:partition-by [:field]}}
This commit is contained in:
Niels van Klaveren 2014-08-19 11:11:09 +02:00 committed by Mike Blume
parent 3b74d14e32
commit 9cf71871f3
2 changed files with 38 additions and 2 deletions

View file

@ -183,6 +183,9 @@
:right-join 140
:full-join 150
:where 160
:aggregate 162
:over 165
:partition 167
:group-by 170
:having 180
:order-by 190
@ -455,7 +458,17 @@
(defmethod format-clause :delete-from [[_ table] _]
(str "DELETE FROM " (to-sql table)))
(defmethod format-clause :over [[_ part-clause] _]
(str "OVER " (to-sql part-clause)))
(defmethod format-clause :aggregate [[_ aggregate-fn] _]
(str (to-sql aggregate-fn)))
(defmethod format-clause :partition-by [[_ fields] _]
(str "PARTITION BY "
(comma-join (map to-sql fields))))
(defn cte->sql
[[cte-name query]]
(str (to-sql cte-name) " AS " (to-sql query)))

View file

@ -219,7 +219,30 @@
(defn delete-from
([table] (delete-from nil table))
([m table] (build-clause :delete-from m table)))
(defmethod build-clause :over [_ m table]
(assoc m :over table))
(defn over
([part-clause] (over nil part-clause))
([m part-clause] (build-clause :over m part-clause)))
(defhelper merge-over [m fields]
(update-in m [:over] (partial apply merge) (collify fields)))
(defhelper aggregate [m aggregate-fn]
(assoc m :aggregate aggregate-fn))
(defn aggregate
([table] (aggregate nil table))
([m table] (build-clause :aggregate m table)))
(defhelper spartition-by [m fields]
(assoc m :partition-by (collify fields)))
(defhelper merge-partition-by [m fields]
(update-in m [:partition-by] concat (collify fields)))
(defmethod build-clause :with [_ m ctes]
(assoc m :with ctes))