honeysql

Discussion of https://github.com/seancorfield/honeysql :slightly_smiling_face:
2021-05-10T17:46:08.259600Z

I'm new to honey and so far loving it!. V2 has been really solid! I'm composing a sub query and noticed the params are not filled in correctly :thinking_face: .

(sql/format
  {:left-join [[{:select [:c]
                 :from   :a
                 :where  [:= :id 123]}]
               :x]
   :where     [:= :a/id 123]})
=> ["LEFT JOIN (SELECT c FROM a WHERE id = ?) ON x WHERE a.id = ?" 123]
I can work around it by using :where [:= :id [:inline 123]] but it I might be doing something wrong

seancorfield 2021-05-10T18:33:27.260200Z

@jeroen.dejong Hmm, that may be a form of JOIN that just isn’t supported yet.

😮 1
seancorfield 2021-05-10T18:33:34.260400Z

What DB are you using?

2021-05-10T18:34:34.260600Z

postgres

seancorfield 2021-05-10T18:36:27.260900Z

Of course 🙂 OK, I’ll open an issue to review it and see if I can find PG’s docs about that syntax — I know your posted example is simplified but it doesn’t make sense to me as written, so I’ll need to see what the full syntax can be from the docs…

seancorfield 2021-05-10T18:40:28.261100Z

https://github.com/seancorfield/honeysql/issues/326

🚀 1
2021-05-10T18:40:55.261500Z

haha sure thing. I converted this from a snippet I found in another projects codebase 😅; so the exact wizardry is lost on me too to be honest. Thanks !

NoahTheDuke 2021-05-10T18:55:41.261900Z

full syntax for postgres select is here: https://www.postgresql.org/docs/current/sql-select.html

NoahTheDuke 2021-05-10T18:56:15.262600Z

from clause, select: > A sub-SELECT can appear in the FROM clause. This acts as though its output were created as a temporary table for the duration of this single SELECT command. Note that the sub-SELECT must be surrounded by parentheses, and an alias must be provided for it.

seancorfield 2021-05-10T18:57:04.263Z

I can see where the parameter(s) are getting dropped. There’s an assumption in the formatter for JOIN that parameters will only “appear” for a JOIN .. USING .. statement, not a JOIN .. ON .. statement. That’s an easy bug to fix… OK, v2 has an update that “fixes” your example case, if you want to test via :git/url?

🚀 1
NoahTheDuke 2021-05-10T18:57:09.263300Z

from clause, join type: > A JOIN clause combines two FROM items, which for convenience we will refer to as “tables”, though in reality they can be any type of FROM item. Use parentheses if necessary to determine the order of nesting. In the absence of parentheses, JOINs nest left-to-right. In any case JOIN binds more tightly than the commas separating FROM-list items.

seancorfield 2021-05-10T18:58:00.263500Z

I’m going to leave that open until I have tests in place and a better understanding of the implication of making that change…

NoahTheDuke 2021-05-10T18:59:15.264300Z

the requirement to alias the sub-select might be something worth noting, @seancorfield, from the current output it looks like that's not being generated

seancorfield 2021-05-10T19:00:27.264700Z

The code snippet above does not have the alias in the correct place.

NoahTheDuke 2021-05-10T19:00:55.265200Z

cool

2021-05-10T19:01:30.265800Z

That's epic! I'll try it out soon and let you know

seancorfield 2021-05-10T19:01:43.266300Z

But that highlights a second bug in the join formatting, which is that ON is optional, which the current formatter does not support 😐

NoahTheDuke 2021-05-10T19:03:35.266900Z

oops, but also glad i could help

seancorfield 2021-05-10T19:05:38.267300Z

OK, fix for that pushed to v2 as well. Now:

user=> (sql/format
  #_=>   {:left-join [[{:select [:c]
  #_=>                  :from   :a
  #_=>                  :where  [:= :id 123]}
  #_=>                :x]]
  #_=>    :where     [:= :a/id 124]})
["LEFT JOIN (SELECT c FROM a WHERE id = ?) AS x WHERE a.id = ?" 123 124]

✨ 1
seancorfield 2021-05-10T19:05:53.267600Z

Note the nesting on the alias @jeroen.dejong

😬 1
seancorfield 2021-05-10T19:06:14.268Z

(that bug was due to using partition instead of partition-by)

2021-05-10T19:09:25.268900Z

Awesome! Big thanks for quick response 🙂

seancorfield 2021-05-10T19:49:35.269100Z

Thanks for finding bugs! 🙂