pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #13805: plpgsql execute using expression evaluate wrong
6+ messages / 5 participants
[nested] [flat]

* BUG #13805: plpgsql execute using expression evaluate wrong
@ 2015-12-08 03:54 amutu@amutu.com
  2015-12-08 06:08 ` Re: BUG #13805: plpgsql execute using expression evaluate wrong David Gould <daveg@sonic.net>
  0 siblings, 1 reply; 6+ messages in thread

From: amutu@amutu.com @ 2015-12-08 03:54 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      13805
Logged by:          Jov
Email address:      amutu@amutu.com
PostgreSQL version: 9.4.5
Operating system:   CentOS 6
Description:        

test case:

CREATE OR REPLACE FUNCTION public.test()
RETURNS void
LANGUAGE plpgsql
AS $function$
declare
n int = 4;
rt timestamp[];
rt2 interval;
begin
execute $$select array_agg(g.i) from generate_series(now() - ('$1
month')::interval,now(),'1 month') g(i)$$ into r
t using n;
raise notice 'rt : %',rt;

execute $$select array_agg(g.i) from generate_series(now() - ($1::varchar ||
' month')::interval,now(),'1 month')
g(i)$$ into rt using n;
raise notice 'rt : %',rt;

execute $$select ('$1 month')::interval $$ into rt2 using n;
raise notice 'rt2 : %',rt2;

execute $$select ($1::varchar || ' month')::interval $$ into rt2 using n;
raise notice 'rt2 : %',rt2;
end;

$function$
~
~
No changes
db_001=# select * from test();
NOTICE: rt : {"2015-11-08 11:46:28.500811","2015-12-08
11:46:28.500811"}-----wrong answer,should 4 elements
NOTICE: rt : {"2015-08-08 11:46:28.500811","2015-09-08
11:46:28.500811","2015-10-08 11:46:28.500811","2015-11-08
11:46:28.500811","2015-12-08 11:46:28.500811"}
NOTICE: rt2 : 1 mon-----wrong,should 4 mons
NOTICE: rt2 : 4 mons
test
------

(1 row)



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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #13805: plpgsql execute using expression evaluate wrong
  2015-12-08 03:54 BUG #13805: plpgsql execute using expression evaluate wrong amutu@amutu.com
@ 2015-12-08 06:08 ` David Gould <daveg@sonic.net>
  2015-12-08 06:15   ` Re: BUG #13805: plpgsql execute using expression evaluate wrong Jov <amutu@amutu.com>
  2015-12-08 06:36   ` Re: BUG #13805: plpgsql execute using expression evaluate wrong Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 6+ messages in thread

From: David Gould @ 2015-12-08 06:08 UTC (permalink / raw)
  To: amutu@amutu.com; +Cc: pgsql-bugs

On Tue, 08 Dec 2015 03:54:44 +0000
amutu@amutu.com wrote:


> execute $$select ('$1 month')::interval $$ into rt2 using n;
> raise notice 'rt2 : %',rt2;
> 
> execute $$select ($1::varchar || ' month')::interval $$ into rt2 using n;
> raise notice 'rt2 : %',rt2;
> end;

> NOTICE: rt2 : 1 mon-----wrong,should 4 mons
> NOTICE: rt2 : 4 mons

You may have found a bug, but if so, it is not where you think it is. The
expression: '$1 month' is a text literal. PL/PGsql does not interpolate $n
like a shell, it only does the substitution where a variable could
ordinarily exist.

However, it does seem a little odd that the literal syntax for
intervals accepts the '$':

postgres=# select '$1 month'::interval, '$4 month'::interval;
 interval | interval 
----------+----------
 1 mon    | 4 mons
(1 row)

I would have expected it to raise an error. The documentation does not shed
any light on this. Anyone?

-dg 

-- 
David Gould              510 282 0869         daveg@sonic.net
If simplicity worked, the world would be overrun with insects.


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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #13805: plpgsql execute using expression evaluate wrong
  2015-12-08 03:54 BUG #13805: plpgsql execute using expression evaluate wrong amutu@amutu.com
  2015-12-08 06:08 ` Re: BUG #13805: plpgsql execute using expression evaluate wrong David Gould <daveg@sonic.net>
@ 2015-12-08 06:15   ` Jov <amutu@amutu.com>
  1 sibling, 0 replies; 6+ messages in thread

From: Jov @ 2015-12-08 06:15 UTC (permalink / raw)
  To: David Gould <daveg@sonic.net>; +Cc: pgsql-bugs

Yes,I think this trigger an error is reasonable.It take me 2 hours to find
the problem,not friendly.

Jov
blog: http:amutu.com/blog <http://amutu.com/blog;

2015-12-08 14:08 GMT+08:00 David Gould <daveg@sonic.net>:

> On Tue, 08 Dec 2015 03:54:44 +0000
> amutu@amutu.com wrote:
>
>
> > execute $$select ('$1 month')::interval $$ into rt2 using n;
> > raise notice 'rt2 : %',rt2;
> >
> > execute $$select ($1::varchar || ' month')::interval $$ into rt2 using n;
> > raise notice 'rt2 : %',rt2;
> > end;
>
> > NOTICE: rt2 : 1 mon-----wrong,should 4 mons
> > NOTICE: rt2 : 4 mons
>
> You may have found a bug, but if so, it is not where you think it is. The
> expression: '$1 month' is a text literal. PL/PGsql does not interpolate $n
> like a shell, it only does the substitution where a variable could
> ordinarily exist.
>
> However, it does seem a little odd that the literal syntax for
> intervals accepts the '$':
>
> postgres=# select '$1 month'::interval, '$4 month'::interval;
>  interval | interval
> ----------+----------
>  1 mon    | 4 mons
> (1 row)
>
> I would have expected it to raise an error. The documentation does not shed
> any light on this. Anyone?
>
> -dg
>
> --
> David Gould              510 282 0869         daveg@sonic.net
> If simplicity worked, the world would be overrun with insects.
>

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #13805: plpgsql execute using expression evaluate wrong
  2015-12-08 03:54 BUG #13805: plpgsql execute using expression evaluate wrong amutu@amutu.com
  2015-12-08 06:08 ` Re: BUG #13805: plpgsql execute using expression evaluate wrong David Gould <daveg@sonic.net>
@ 2015-12-08 06:36   ` Tom Lane <tgl@sss.pgh.pa.us>
  2015-12-08 07:24     ` Re: BUG #13805: plpgsql execute using expression evaluate wrong David Gould <daveg@sonic.net>
  2015-12-08 07:57     ` Re: BUG #13805: plpgsql execute using expression evaluate wrong Terje Elde <terje@elde.net>
  1 sibling, 2 replies; 6+ messages in thread

From: Tom Lane @ 2015-12-08 06:36 UTC (permalink / raw)
  To: David Gould <daveg@sonic.net>; +Cc: amutu@amutu.com; pgsql-bugs

David Gould <daveg@sonic.net> writes:
> However, it does seem a little odd that the literal syntax for
> intervals accepts the '$':
> ...
> I would have expected it to raise an error. The documentation does not shed
> any light on this. Anyone?

The datetime input parser tends to consider most non-alphanumeric
characters as being insignificant except as field separators.  We could
tighten that up, but I think we should tread pretty carefully for fear of
breaking cases that used to work.  A trivial example:

regression=# select '1 mon 1 day'::interval;
  interval   
-------------
 1 mon 1 day
(1 row)

regression=# select '1 mon, 1 day'::interval;
  interval   
-------------
 1 mon 1 day
(1 row)

If we started to reject the second case, we'd likely get complaints.
But the parser doesn't see that as any different from

regression=# select '1 mon$ 1 day'::interval;
  interval   
-------------
 1 mon 1 day
(1 row)

Trying to decide which characters are legitimate noise and which
aren't seems like a tarbaby best not to get stuck to :-(

			regards, tom lane


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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #13805: plpgsql execute using expression evaluate wrong
  2015-12-08 03:54 BUG #13805: plpgsql execute using expression evaluate wrong amutu@amutu.com
  2015-12-08 06:08 ` Re: BUG #13805: plpgsql execute using expression evaluate wrong David Gould <daveg@sonic.net>
  2015-12-08 06:36   ` Re: BUG #13805: plpgsql execute using expression evaluate wrong Tom Lane <tgl@sss.pgh.pa.us>
@ 2015-12-08 07:24     ` David Gould <daveg@sonic.net>
  1 sibling, 0 replies; 6+ messages in thread

From: David Gould @ 2015-12-08 07:24 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: amutu@amutu.com; pgsql-bugs

On Tue, 08 Dec 2015 01:36:39 -0500
Tom Lane <tgl@sss.pgh.pa.us> wrote:

> The datetime input parser tends to consider most non-alphanumeric
> characters as being insignificant except as field separators.  We could
> tighten that up, but I think we should tread pretty carefully for fear of
> breaking cases that used to work.  A trivial example:
...
> regression=# select '1 mon$ 1 day'::interval;
>   interval   
> -------------
>  1 mon 1 day
> (1 row)
> 
> Trying to decide which characters are legitimate noise and which
> aren't seems like a tarbaby best not to get stuck to :-(

# select '#!1!#month,?=~&(12$[day*() '::interval;
   interval    
---------------
 1 mon 12 days
(1 row)

I feel so ... dirty....

-dg

-- 
David Gould              510 282 0869         daveg@sonic.net
If simplicity worked, the world would be overrun with insects.


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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: BUG #13805: plpgsql execute using expression evaluate wrong
  2015-12-08 03:54 BUG #13805: plpgsql execute using expression evaluate wrong amutu@amutu.com
  2015-12-08 06:08 ` Re: BUG #13805: plpgsql execute using expression evaluate wrong David Gould <daveg@sonic.net>
  2015-12-08 06:36   ` Re: BUG #13805: plpgsql execute using expression evaluate wrong Tom Lane <tgl@sss.pgh.pa.us>
@ 2015-12-08 07:57     ` Terje Elde <terje@elde.net>
  1 sibling, 0 replies; 6+ messages in thread

From: Terje Elde @ 2015-12-08 07:57 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: David Gould <daveg@sonic.net>; amutu@amutu.com; pgsql-bugs


> On 08 Dec 2015, at 07:36, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> 
> Trying to decide which characters are legitimate noise and which
> aren't seems like a tarbaby best not to get stuck to :-(

I'd like to argue that if you need PostgreSQL-native time from a string, and want more control and verification, that's probably something that's best left to the application anyway.

I fail to see how PostgreSQL can reasonably be made to be "perfect" for all possible cases of dirty input being casted to interval.

The application is in a much better position to deal with this, presumably knowing more about the input, and being a cleaner place to put any parsing and handling. 



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2015-12-08 07:57 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-12-08 03:54 BUG #13805: plpgsql execute using expression evaluate wrong amutu@amutu.com
2015-12-08 06:08 ` David Gould <daveg@sonic.net>
2015-12-08 06:15   ` Jov <amutu@amutu.com>
2015-12-08 06:36   ` Tom Lane <tgl@sss.pgh.pa.us>
2015-12-08 07:24     ` David Gould <daveg@sonic.net>
2015-12-08 07:57     ` Terje Elde <terje@elde.net>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox