Fork me on GitHub
#honeysql
<
2023-06-15
>
orestis07:06:43

How could I generate this syntax? (Postgres array access subscript):

select eb.post_ids[1] from eb;

orestis07:06:14

I'm interested mainly in tacking the [1] array subscript to any column in a select...

p-himik08:06:40

I'm surprised I can't find anything on it, anywhere. Also surprised that I have never had a need for it myself. I'd probably add a custom function/operator and make a PR for HoneySQL.

seancorfield14:06:54

I'd be happy to add something like [:ix :col index-expr] => col[index-expr] but the name needs some consideration + tests + docs.

orestis17:06:47

is :[] a valid clojure keyword, I wonder?

orestis17:06:15

No, it's not 😄

orestis17:06:56

The col must also be a col-expr

orestis17:06:33

At least in my case, the I ended up doing

[:raw "(array_agg(timestamp))[1] as timestamp"]

orestis17:06:45

I needed to also wrap the col-expr with parentheses

p-himik17:06:47

PG-related parts of HoneySQL use things like (def at> (keyword "@>")).

seancorfield18:06:40

Good point about needing ( .. ) if you have a col-expr. Does PG allow (col)[1] or are parens only allowed for an expression there? (I never use PG so I've no idea how fussy it is)

orestis18:06:17

initial testing shows that parenthesis are allowed everywhere, even for a single col

2
orestis18:06:23

they are required for most everything else