diff --git a/CHANGELOG.md b/CHANGELOG.md index bb960db..be822e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changes +* 2.4.next in progress + * Address [#504](https://github.com/seancorfield/honeysql/issues/504) by adding special syntax for ignore/respect nulls. More work will be needed to support distinct/order by/limit in BigQuery array aggregation. + * 2.4.1066 -- 2023-08-27 * Add `:select` with function call and alias example to README (PR [#502](https://github.com/seancorfield/honeysql/pull/502) [@markbastian](https://github.com/markbastian)). * Address [#501](https://github.com/seancorfield/honeysql/issues/501) by making `INSERT INTO` (and `REPLACE INTO`) use the `:columns` or `:values` clauses to produce column names (which are then omitted from those other clauses). diff --git a/doc/special-syntax.md b/doc/special-syntax.md index 6f6e3de..009e2ba 100644 --- a/doc/special-syntax.md +++ b/doc/special-syntax.md @@ -215,6 +215,18 @@ FROM aa 100] ``` +## ignore/respect nulls + +Both of these accept a single argument -- an expression -- and +renders that expression followed by `IGNORE NULLS` or `RESPECT NULLS`: + +```clojure +(sql/format-expr [:array_agg [:ignore-nulls :a]]) +;;=> ["ARRAY_AGG(a IGNORE NULLS)"] +(sql/format-expr [:array_agg [:respect-nulls :a]]) +;;=> ["ARRAY_AGG(a RESPECT NULLS)"] +``` + ## inline Accepts a single argument and tries to render it as a diff --git a/src/honey/sql.cljc b/src/honey/sql.cljc index 07c2e8c..4926b7a 100644 --- a/src/honey/sql.cljc +++ b/src/honey/sql.cljc @@ -1605,6 +1605,10 @@ (into paramsx) (into params)))) +(defn ignore-respect-nulls [k [x]] + (let [[sql & params] (format-expr x)] + (into [(str sql " " (sql-kw k))] params))) + (def ^:private special-syntax (atom {;; these "functions" are mostly used in column @@ -1686,6 +1690,7 @@ (into params-p) (into params-e)))) :filter expr-clause-pairs + :ignore-nulls ignore-respect-nulls :inline (fn [_ [x]] (binding [*inline* true] @@ -1766,6 +1771,7 @@ (raw-render (first xs)) ;; ...but allow for multiple arguments now: (raw-render xs))) + :respect-nulls ignore-respect-nulls :within-group expr-clause-pairs})) (defn- format-equality-expr [op' op expr nested]