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

ToField for negative numbers should be parenthesized #143

Open
ChickenProp opened this issue Jul 25, 2024 · 0 comments · May be fixed by #145
Open

ToField for negative numbers should be parenthesized #143

ChickenProp opened this issue Jul 25, 2024 · 0 comments · May be fixed by #145
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@ChickenProp
Copy link

Postgresql parses - <num> :: <type> as - (<num> :: <type>). That can cause problems when trying to reference a type's minimum value:

> select -2147483648 :: int4;
ERROR:  integer out of range
> select (-2147483648) :: int4;
    int4     
-------------
 -2147483648

In the context of postgresql-simple, we can get problems if we try to do e.g.

execute conn "INSERT INTO foo (col) VALUES (?::int4)" (Only someInt)
query conn "SELECT * FROM ?" (Values ["int4"] [Only someInt])

I think postgres has symmetric ranges for non-integer numbers, but since those can get cast to ints, it can still make a difference whether they're parenthesized:

> select -2147483648.2 :: int4;
ERROR:  integer out of range
> select (-2147483648.2) :: int4;
    int4     
-------------
 -2147483648
(1 row)
@phadej phadej added help wanted Extra attention is needed good first issue Good for newcomers labels Jul 25, 2024
ChickenProp added a commit to proda-ai/postgresql-simple that referenced this issue Aug 5, 2024
This is necessary because in PostgreSQL, `-` is a unary operator that
has lower precedence than the `::` operator, and that can cause problems
at the edge of allowed ranges.

For example, `-32768::int2` is parsed as `-(32768::int2)`, which throws
an "out of range" error, even though `(-32768)::int2` is accepted.

Closes haskellari#143.
@ChickenProp ChickenProp linked a pull request Aug 5, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants