Inconsistent format() behavior for argument-count inconsistency

Lists: pgsql-hackers
From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-hackers(at)postgreSQL(dot)org
Subject: Inconsistent format() behavior for argument-count inconsistency
Date: 2013-01-18 18:03:13
Message-ID: 10550.1358532193@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

regression=# select format('%s %s', 'foo', 'bar');
format
---------
foo bar
(1 row)

regression=# select format('%s %s', 'foo', 'bar', 'baz');
format
---------
foo bar
(1 row)

regression=# select format('%s %s', 'foo');
ERROR: too few arguments for format

Why do we throw an error for too few arguments, but not too many?

(This came up when I started wondering whether the proposed VARIADIC
feature would really be very useful for format(), since it needs a
format string that matches up with its arguments.)

regards, tom lane


From: Stephen Frost <sfrost(at)snowman(dot)net>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgreSQL(dot)org
Subject: Re: Inconsistent format() behavior for argument-count inconsistency
Date: 2013-01-18 18:10:56
Message-ID: 20130118181056.GD16126@tamriel.snowman.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

* Tom Lane (tgl(at)sss(dot)pgh(dot)pa(dot)us) wrote:
> Why do we throw an error for too few arguments, but not too many?

Not sure offhand, though I could see how it might be useful. A use-case
might be that you have a variable template string which is user defined,
where they can choose from the arguments that are passed which ones they
want displayed and how. Otherwise, you'd have to have a bunch of
different call sites to format() depending on which options are
requested.

I'd go look at what C sprintf() does, as I feel like that's what we're
trying to emulate and see what it does.

There is also a patch that I reviewed/commented on about changing format
around a bit. I'm guessing it needs more review/work, just havn't
gotten back around to it yet.

Thanks,

Stephen