Fork me on GitHub
#honeysql
<
2023-11-30
>
Ben Lieberman17:11:02

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-himik17:11:38

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

Ben Lieberman17:11:22

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

Ben Lieberman17:11:39

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-himik17:11:10

What's the sqlmap that triggered the error?

Ben Lieberman17:11:14

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]]]]}

Ben Lieberman17:11:51

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

p-himik17:11:17

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

Ben Lieberman17:11:26

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

p-himik17:11:05

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

Ben Lieberman17:11:57

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.

Ben Lieberman17:11:24

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-himik17:11:33

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.

Ben Lieberman17:11:07

fair enough, thanks.