Unix Technical Forum

strange (numeric) casting behaviour

This is a discussion on strange (numeric) casting behaviour within the pgsql Bugs forums, part of the PostgreSQL category; --> I'm experience some strange behaviour when casting numeric values. Given the following SQL-Statement: SELECT a_int, a_num, CAST (a_num AS ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Bugs

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 10:01 AM
Sven Welte
 
Posts: n/a
Default strange (numeric) casting behaviour

I'm experience some strange behaviour when casting numeric values.

Given the following SQL-Statement:

SELECT
a_int,
a_num,
CAST (a_num AS NUMERIC(9,1)) AS CastTo9_1,
CAST (a_num AS NUMERIC(9,2)) AS CastTo9_2,
CAST (a_num AS NUMERIC(9,3)) AS CastTo9_3
FROM f_numtest();

Generated output is:
a_int | a_num | castto9_1 | castto9_2 | castto9_3
-------+--------------------+-----------+--------------------+-----------
1525 | 8.6317245901639344 | 8.6 | 8.6317245901639344 | 8.632

Expected output was:
a_int | a_num | castto9_1 | castto9_2 | castto9_3
-------+--------------------+-----------+--------------------+-----------
1525 | 8.6317245901639344 | 8.6 | --> 8.63 | 8.632



reproducable testcase:

CREATE TABLE numtest (
n NUMERIC(9,2),
i INTEGER
);
INSERT INTO numTest VALUES (13163.38000, 1525);

CREATE TABLE frslt_test (
a_int INTEGER,
a_num NUMERIC(9,2)
);

CREATE OR REPLACE FUNCTION f_numTest() RETURNS SETOF frslt_test AS '
SELECT CAST (SUM(i) AS INTEGER),
SUM(n)/SUM(i)
FROM Numtest
' LANGUAGE 'sql';

SELECT
a_int,
a_num,
CAST (a_num AS NUMERIC(9,1)) AS CastTo9_1,
CAST (a_num AS NUMERIC(9,2)) AS CastTo9_2,
CAST (a_num AS NUMERIC(9,3)) AS CastTo9_3
FROM f_numtest();

select version();

a_int | a_num | castto9_1 | castto9_2 | castto9_3
-------+--------------------+-----------+--------------------+-----------
1525 | 8.6317245901639344 | 8.6 | 8.6317245901639344 | 8.632

version
------------------------------------------------------------------------------------------------------------
PostgreSQL 8.1.3 on i486-pc-linux-gnu, compiled by GCC gcc-4.0.gcc-opt (GCC) 4.0.3 (Ubuntu 4.0.3-1ubuntu4)


---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 10:01 AM
Tom Lane
 
Posts: n/a
Default Re: strange (numeric) casting behaviour

Sven Welte <Sven.Welte@gmxpro.net> writes:
> I'm experience some strange behaviour when casting numeric values.


This doesn't really have anything to do with casting as such. The
system thinks it can throw away the cast to NUMERIC(9,2) because the
function result is declared as already NUMERIC(9,2) ... but the function
is not actually constraining its result that way. In general a function
returning NUMERIC is considered to return unconstrained NUMERIC (and
likewise for VARCHAR etc).

Possibly we should disallow the length decoration on columns used in a
function result typedef, because it fools people into thinking that such
a decoration will be enforced.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:19 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com