A really subtle lexer bug - Mailing list pgsql-hackers

From Andrew Gierth
Subject A really subtle lexer bug
Date
Msg-id 87va851ppl.fsf@news-spur.riddles.org.uk
Whole thread Raw
Responses Re: A really subtle lexer bug
List pgsql-hackers
Currently in PG, the precedence of = and <> is supposed to be equal, and
the precedence of unary - is very high.

So, (true=1 between 1 and 1) parses as ((true)=(1 between 1 and 1)),
and (true=-1 between 1 and 1) parses as ((true)=((-1) between 1 and 1)).
All good so far.

(true<>-1 between 1 and 1) parses as ((true<>(-1)) between 1 and 1). ???

The fault here is in the lexer. The input "<>-" is being lexed as an Op
followed by '-' rather than as NOT_EQUAL followed by '-' because it
looks like a match for a multi-character operator, with the - being
thrown back into the input afterwards. So the precedence of <> gets
inflated to that of Op, which is higher than BETWEEN.

More seriously, this also breaks named arguments:

create function f(a integer) returns integer language sql
 as $$ select a; $$;

select f(a => 1);  -- works
select f(a => -1);  -- works
select f(a =>-1);  -- ERROR:  column "a" does not exist

I guess the fix is to extend the existing special case code that checks
for one character left after removing trailing [+-] and also check for
the two-character ops "<>" ">=" "<=" "=>" "!=". 

-- 
Andrew (irc:RhodiumToad)


pgsql-hackers by date:

Previous
From: Tatsuro Yamada
Date:
Subject: Re: Fix help option of contrib/oid2name
Next
From: David Rowley
Date:
Subject: Calculate total_table_pages after set_base_rel_sizes()