yeshql icon indicating copy to clipboard operation
yeshql copied to clipboard

Yeshql cannot handle :: cast in postgresql

Open noxecane opened this issue 6 years ago • 2 comments

Expected Behaviour

[yesh|
  -- name: getMytable :: ()
  select *, now() - '1 hour ago'::interval from mytable;
|]

should generate

select *, now() - '1 hour ago'::interval from mytable;

Actual Behaviour

select *, now() - '1 hour ago'

My guess is yeshql is detecting :: in queries. I thought it would only use those in comments(--) For now I am using cast directly. I would have liked to send a patch but I know close to nothing of neither template-haskell not QuasiQuoting

noxecane avatar Oct 08 '18 18:10 noxecane

You are right, this doesn't work - :: inside queries is yeshql magic, interpreted as a type annotation, as an alternative to annotating parameters in comments. That is, instead of this:

-- name: getMytable :: ()
-- :id :: Int
SELECT * FROM mytable WHERE id = :id

...you could also write:

-- name: getMytable :: ()
SELECT * FROM mytable WHERE id = :id :: Int

Both forms are equivalent, however, the first form allows you to order parameters independently from the order in which they are found inside the query.

I'm not entirely sure how to fix this clash with PostgreSQL syntax; possibilities would include:

  • Removing this feature, allowing type annotations only in comments. This is the most thorough solution, but sacrifices a potentially useful feature.
  • Add a switch somewhere to opt into or out of this feature. This would complicate the API a little, but could be worth it.
  • Add an option somewhere to change the syntax for type annotations. This would complicate the API a little more though.
  • Make the parser more picky - we only really want to accept such type annotations after named parameters, e.g. :id :: Int, but not after anything else, so 'foo' :: text should not parse as a type annotation, but be left alone. This of course still leaves us with queries where you want to have a parameter as one type, but then have PostgreSQL cast it to another, which can be worked around, but it'd still be a somewhat surprising wart.

tdammers avatar Oct 08 '18 20:10 tdammers

I think the second option balances things. I would have preferred the first one, but since this is a postgresql only syntax it'll be better to use an opt-out switch. The other two just bring complexity that'll be too much for a simple library

noxecane avatar Oct 08 '18 20:10 noxecane