space required before negative

Started by Geoff Winklessabout 10 years ago3 messagesgeneral
Jump to latest
#1Geoff Winkless
pgsqladmin@geoff.dj

Hi

I was surprised to find that whitespace is required between the !=
operator and a negative sign, otherwise postgres believes that I'm
intending !=- as an operator (I get "operator does not exist: integer
!=- integer").

This isn't the case with <>-x.

Is this intentional? I couldn't find reference to it in the
documentation (certainly not in
http://www.postgresql.org/docs/9.5/static/functions-comparison.html).

db=# SELECT 'yes' WHERE 1!=-1;
ERROR: operator does not exist: integer !=- integer
LINE 1: SELECT 'yes' WHERE 1!=-1;
^
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.
Time: 0.608 ms

db=# SELECT 'yes' WHERE 1<>-1;
?column?
----------
yes
(1 row)

I get this with fieldnames too, so it's not just a parsing-literal problem...

This is on 9.5, also on 9.5.1.

Geoff

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Geoff Winkless (#1)
Re: space required before negative

Geoff Winkless <pgsqladmin@geoff.dj> writes:

I was surprised to find that whitespace is required between the !=
operator and a negative sign, otherwise postgres believes that I'm
intending !=- as an operator (I get "operator does not exist: integer
!=- integer").

This isn't the case with <>-x.

Is this intentional? I couldn't find reference to it in the
documentation (certainly not in
http://www.postgresql.org/docs/9.5/static/functions-comparison.html).

That's a syntax issue, so the place to look is
http://www.postgresql.org/docs/9.5/static/sql-syntax-lexical.html#SQL-SYNTAX-OPERATORS

In general you need whitespace between adjacent operator names, but
we hacked the lexer enough so that you can get away without it
in the single case of a prefix "+" or "-", so long as the previous
operator name contains only characters used in SQL operator names.

It would likely have been cleaner to just disallow operator names
ending in "+" or "-", but we had several long-established names that
failed to conform to that, so this was the best compromise we could
come up with between flexibility and SQL standard compliance.

regards, tom lane

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Geoff Winkless
pgsqladmin@geoff.dj
In reply to: Tom Lane (#2)
Re: space required before negative

On 3 March 2016 at 17:30, Tom Lane <tgl@sss.pgh.pa.us> wrote:

That's a syntax issue, so the place to look is
http://www.postgresql.org/docs/9.5/static/sql-syntax-lexical.html#SQL-SYNTAX-OPERATORS

Ah, thanks. Perhaps a note in the operators page might be helpful?

I'll change to use <> in place of !=; quite apart from being ANSI
(which I hadn't realised) it has the advantage over != that I get
nostalgia for 1983 and BASIC :)

Geoff

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general