honeysql

liebs 2023-11-30T17:23:02.508999Z

hey so I'm rewriting this monstrous stored procedure and I noticed that through accidental incorrect usage of :join-by , HoneySQL throws this exception:

; Execution error (ExceptionInfo) at honey.sql/format-item-selection (sql.cljc:533).
; illegal syntax in select expression
which didn't mislead me for too long but I'm wondering if this is a SQL quirk or not.

p-himik 2023-11-30T17:35:38.629069Z

So an incorrect usage results in an exception. What exactly would be the SQL quirk?

liebs 2023-11-30T17:36:22.291009Z

What I'm asking is why using :join-by (albeit incorrectly) tells me that the syntax error is in the select expression.

liebs 2023-11-30T17:37:39.484689Z

I fixed my problem, so it's a matter of knowing whether this has something to do with how joins work internally, bc I do not know.

p-himik 2023-11-30T17:38:10.685549Z

What's the sqlmap that triggered the error?

liebs 2023-11-30T17:42:14.020209Z

fast, minimal repro but the problem is clearly with that :and

{:select [:thing]
   :from [[:table :t]]
   :join-by [:left [[:table2 :t2]]
             :join [[:table3 :t3]
                    [:= :t2.thing2 :t.thing]
                    [:and [:= :t2.more-conds :t3.more-conds]]]]}

liebs 2023-11-30T17:42:51.122249Z

It's really not a big deal that I understand this, but curiosity killed the cat.

p-himik 2023-11-30T17:43:17.802989Z

That works for me just fine though. Meaning, sql/format works.

liebs 2023-11-30T17:44:26.132979Z

hm, well let me try something closer to the actual query, sorry, business stuff I can't reveal.

p-himik 2023-11-30T17:45:05.621129Z

If you join with any subqueries, then perhaps a :select in one of those subqueries is malformed.

liebs 2023-11-30T17:47:57.374649Z

when I comment out what seems to be the offending clause in my query, it works fine. This is what that looks like:

{:join-by [:left [[:Things :ch]]
             :join [[:ThingCode :cc]
                    [:= :ch.ThingCode :cc.ThingCode]]
             :join [[:Degree :deg]
                    [:= :cc.Degree :deg.DegreeCode]
                    [:and
                     [:= :c.ThingCase :ch.ThingCase]
                     [:= :ch.Primary 1]]]
             :left [[:Alias :d]
                    [:and
                     [:= :c.Customer :d.Person]
                     [:= :d.Primary 1]
                     [:= :d.Active 1]]]]}
i.e., if I get rid of that second :join, there are no errors.

liebs 2023-11-30T17:49:24.267849Z

it's a really huge CTE that is causing me a major headache as it is, so I am hard-pressed to create a better repro.

p-himik 2023-11-30T17:50:33.877409Z

It's just an example of "garbage in, garbage out", nothing incredibly interesting here. If you really want to know, just print out the stacktrace and follow it.

liebs 2023-11-30T17:51:07.814119Z

fair enough, thanks.