On 23 April 2018 at 08:23, Miles Elam <mileselam@gmail.com> wrote:
> I would like to donate some code to the project, formatting numbers as any
> base from 2 to 64. The FAQ describes contributions to the core code, but
> it's possible contrib is a better target. This is all of course contingent
> on how well received this extension code is of course. Code available at the
> following link.
>
> https://github.com/ttfkam/pg_formatbase
Personally, I think this is a better candidate for being incorporated
directly rather than as a contrib. This sort of utility is much less
useful if you cannot rely on it being present.
I'm not convinced by the wisdom of adding int8 overloads, etc, with a
second argument. I'd rather this be named as a separate function. I
realise that many programming languages do this, but it's IMO less
discoverable this way, and might make our life harder if we later need
to overload these functions in a different way.
We already have to_hex. So to_base seems a reasonable choice. Then
adding a from_hex, from_base seems natural. Bonus points if you add
to/from base64 and oct while you're at it.
We don't seem to have a "from_hex" or "int8_from_hex", which is a
bewildering oversight really, and we don't accept literals:
test=> select int8 0x1234;
ERROR: syntax error at or near "0"
LINE 1: select int8 0x1234;
^
test=> select int8 '0x1234';
ERROR: invalid input syntax for integer: "0x1234"
LINE 1: select int8 '0x1234';
... unless you abuse our nonstandard bitstring SQL extension:
test=> select x'1234';
?column?
------------------
0001001000110100
(1 row)
test=> select pg_typeof( x'1234' );
pg_typeof
-----------
bit
(1 row)
test=> select x'1234'::int8;
int8
------
4660
(1 row)
which won't work for anyone using bind parameters, so it's not that
handy really.
I'm also amused by
test=> select 0x1234;
x1234
-------
0
(1 row)
because of our willingness to ignore the whitespace between a value
and the column label.
While I'm on that topic, I've never found anything that unquotes a
literal or identifier without going through the full parser, some sort
of unquote_literal. Guess I should find time to scratch that itch
myself soon.
--
Craig Ringer http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services