From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
---|---|
To: | PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org> |
Cc: | Marko Tiikkaja <marko(dot)tiikkaja(at)cs(dot)helsinki(dot)fi>, Marko Tiikkaja <marko(at)joh(dot)to> |
Subject: | proposal: minscale, rtrim, btrim functions for numeric |
Date: | 2019-11-09 19:48:11 |
Message-ID: | CAFj8pRDjs-navGASeF0Wk74N36YGFJ+v=Ok9_knRa7vDc-qugg@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-hackers |
Hi,
four years ago Marko Tiikkaja send a patch for numeric_trim functions. This
functions removed ending zeroes from numeric value. This is useful feature,
but there was not any progress on this patch. I think so this feature can
be interesting, so I would to revitalize this patch.
Original discussion
https://www.postgresql-archive.org/Add-numeric-trim-numeric-td5874444.html
Based on this discussion I would to implement three functions - prototype
implementation is in plpsql and sql - final implementation will be in C.
-- returns minimal scale when the rounding the value to this scale doesn't
-- lost any informations.
CREATE OR REPLACE FUNCTION pg_catalog.minscale(numeric)
RETURNS integer
LANGUAGE plpgsql
AS $function$
begin
for i in 0..256
loop
if round($1, i) = $1 then
return i;
end if;
end loop;
end;
$function$
-- trailing zeroes from end
-- trimming only zero for numeric type has sense
CREATE OR REPLACE FUNCTION pg_catalog.rtrim(numeric)
RETURNS numeric AS $$
SELECT round($1, pg_catalog.minscale($1))
$$ LANGUAGE sql;
-- this is due support trim function
CREATE OR REPLACE FUNCTION pg_catalog.btrim(numeric)
RETURNS numeric AS $$
SELECT pg_catalog.rtrim($1)
$$ LANGUAGE sql;
postgres=# select trim(10.22000);
┌───────┐
│ btrim │
╞═══════╡
│ 10.22 │
└───────┘
(1 row)
postgres=# select rtrim(10.34900);
┌────────┐
│ rtrim │
╞════════╡
│ 10.349 │
└────────┘
(1 row)
What do you think about it?
Regards
Pavel
From | Date | Subject | |
---|---|---|---|
Next Message | Tom Lane | 2019-11-09 20:24:12 | Re: Removing pg_pltemplate and creating "trustable" extensions |
Previous Message | Tom Lane | 2019-11-09 17:06:33 | int64-timestamp-dependent test vs. --disable-integer-timestamps |