Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document hint for comparing a Maybe value with nullable, treating NULL as comparable value #128

Open
robinp opened this issue Oct 24, 2023 · 0 comments

Comments

@robinp
Copy link

robinp commented Oct 24, 2023

Hello,

This is more to leave an issue footprint for future readers and self, but might be nice if a hint ended up in the haddocks intro section as well.

The problem

I, as a unit-radius user, ended up trying to compare a nullable value with a bound Maybe value in a query using plain equality, like

PG.query conn "SELECT ... FROM ... WHERE nullable_thing = ?" (PG.Only mbSomething)

Which understandably doesn't work for a NULL - Nothing pair, since SQL's NULL is contagious, once it is a parameter of a comparison, the whole result is NULL AFAIU, instead of a match.

The trick that saves the day

https://www.postgresql.org/docs/current/functions-comparison.html documents

datatype IS NOT DISTINCT FROM datatype → boolean
Equal, treating null as a comparable value.
1 IS NOT DISTINCT FROM NULL → f (rather than NULL)
NULL IS NOT DISTINCT FROM NULL → t (rather than NULL)

So changing = to IS NOT DISTINCT FROM in the query helped. So it is advisable to use if one wants to strictly treat a nullable sql type as a Maybe Haskell-type, for comparison.

@robinp robinp changed the title Document hint for comparing a Maybe value with nullable, treating null as comparable value Document hint for comparing a Maybe value with nullable, treating NULL as comparable value Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant