honeysql

markaddleman 2023-03-31T18:11:49.831739Z

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

markaddleman 2023-03-31T18:12:09.496619Z

(-> {: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

markaddleman 2023-03-31T18:12:38.663779Z

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

markaddleman 2023-03-31T18:13:22.144149Z

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

markaddleman 2023-03-31T18:13:38.974309Z

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

p-himik 2023-03-31T18:16:53.268559Z

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-himik 2023-03-31T18:16:57.678149Z

Same as with :select.

markaddleman 2023-03-31T18:17:24.089539Z

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

p-himik 2023-03-31T18:18:01.453359Z

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

markaddleman 2023-03-31T18:18:47.600039Z

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

markaddleman 2023-03-31T18:19:35.453939Z

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

seancorfield 2023-03-31T18:23:32.834329Z

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 🙂

🤣 1
markaddleman 2023-03-31T18:24:47.101759Z

Understood

markaddleman 2023-03-31T18:25:25.791899Z

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

seancorfield 2023-03-31T18:35:09.894869Z

Exactly like select, yes.

seancorfield 2023-03-31T18:36:04.096619Z

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

markaddleman 2023-03-31T18:36:31.615079Z

I should have RTFM. Thanks!

seancorfield 2023-03-31T18:38:02.535019Z

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

👍 1