Fork me on GitHub
#honeysql
<
2023-03-31
>
markaddleman18:03:49

I think I found a bug in honey’s formatting of partition-by

markaddleman18:03:09

(-> {:select [[[:over [[:lead :event]
                         {:partition-by [[:upper :a]]}]]
                 :random]]
       :from   [[:foo :foo]]}
      (honey.sql/format)
      (first))
produces SELECT LEAD(event) OVER (PARTITION BY upper a) AS random FROM foo AS foo

markaddleman18:03:38

I expected the partition by to be PARTITION BY UPPER(a)

markaddleman18:03:22

this honey produces the correct result

(-> {:select [[[:over [[:lead :event]
                         {:partition-by [[[:upper :a]]]}]]
                 :random]]
       :from   [[:foo :foo]]}
      (honey.sql/format)
      (first))
SELECT LEAD(event) OVER (PARTITION BY UPPER(a)) AS random FROM foo AS foo

markaddleman18:03:38

If this is not user error, I’ll open an issue

p-himik18:03:53

Pretty sure it's user error as :partition-by can accept many columns. Zero nesting, a single keyword - a single column One level of nesting - >= 1 columns Two levels - same but with aliases Three levels - same but with function calls

p-himik18:03:57

Same as with :select.

markaddleman18:03:24

SQL partition-by allows aliases? I’m not sure what that would mean

p-himik18:03:01

It probably doesn't, but I believe that vector nesting is handled in a generic way.

markaddleman18:03:47

that would make sense. If that’s the case, I contend it’s a bug but, of course, Sean is arbiter 🙂

markaddleman18:03:35

btw

(-> {:select [[[:over [[:lead :event]
                         {:partition-by [[[:upper :a] :A]]}]]
                 :random]]
       :from   [[:foo :foo]]}
      (honey.sql/format)
      (first))
produces SELECT LEAD(event) OVER (PARTITION BY UPPER(a) A) AS random FROM foo AS foo

seancorfield18:03:32

Not a bug. Portions of HoneySQL are generic, just to keep maintenance manageable (because SQL is so f***ing complex) -- and there are many, many ways to persuade HoneySQL to generate invalid SQL 🙂

2
markaddleman18:03:25

So, just double wrap functions in the partition by clause?

seancorfield18:03:09

Exactly like select, yes.

seancorfield18:03:04

This is documented, BTW: > :partition-by accepts the same arguments as :select above (even though the allowable SQL generated is much more restrictive). From https://cljdoc.org/d/com.github.seancorfield/honeysql/2.4.1011/doc/getting-started/sql-clause-reference#window-partition-by-and-over

markaddleman18:03:31

I should have RTFM. Thanks!

seancorfield18:03:02

All of the following are formatted like SELECT:

:select          #'format-selects
         :select-distinct #'format-selects
         :select-distinct-on #'format-selects-on
         :delete          (check-where #'format-selects)
         :from            #'format-selects
         :using           #'format-selects
         :cross-join      #'format-selects
         :partition-by    #'format-selects
         :returning       #'format-selects

👍 2