pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #13885: float->string conversion loses precision server-side on JDBC connection
3+ messages / 3 participants
[nested] [flat]

* BUG #13885: float->string conversion loses precision server-side on JDBC connection
@ 2016-01-24 20:29  xtracoder@gmail.com
  0 siblings, 1 reply; 3+ messages in thread

From: xtracoder@gmail.com @ 2016-01-24 20:29 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      13885
Logged by:          XtraCoder
Email address:      xtracoder@gmail.com
PostgreSQL version: 9.5.0
Operating system:   Window 7
Description:        

I have a PostgreSQL function, which accepts JSON, performs some processing,
and returns JSON. Something strange is happening when calling stored
procedure from Java application - float->string conversion is incorrect.

Processing is mostly filtering of data and returning restructured and
reorganized result. For simplicity let's assume input is map `[name->float]`
and output is `[float]`.

The problem is that during intermediate data storage of values into native
PostgreSQL data type float values loose precision/accuracy/formatting.

Here is the code to reproduce

    CREATE OR REPLACE FUNCTION do_dummy()
        RETURNS void LANGUAGE plpgsql AS $$
    DECLARE
        v_float float[];
        v_json jsonb;
        v_str varchar;
    BEGIN
        v_float[0] = 4.1;
        raise notice 'v_float = %', v_float[0];
        raise notice 'jsonb float -> %', ('{"v": 4.1}'::jsonb)->'v';
        raise notice 'jsonb float ->> %', ('{"v": 4.1}'::jsonb)->>'v';

        v_float[0] = ('{"v": 4.1}'::jsonb)->>'v';
        raise notice 'jsonb float ->>::float %', v_float[0];

        v_json = array_to_json(v_float);
        raise notice 'jsonb: %', v_json;

        v_str = concat('jsonb as string: ', v_json::varchar);
        raise notice '%', v_str;
    END $$

When executing

    select do_dummy();

... via pgAdmin, output is following and is as expected:

    NOTICE:  v_float = 4.1
    NOTICE:  jsonb float -> 4.1
    NOTICE:  jsonb float ->> 4.1
    NOTICE:  jsonb float ->>::float 4.1
    NOTICE:  jsonb: [4.1]
    NOTICE:  jsonb as string: [4.1]

When executing same from Java application, result is following

    NOTICE: v_float = 4.0999999999999996
    NOTICE: jsonb float -> 4.1
    NOTICE: jsonb float ->> 4.1
    NOTICE: jsonb float ->>::float 4.0999999999999996
    NOTICE: jsonb: [4.0999999999999996]
    NOTICE: jsonb as string: [4.0999999999999996]

Since no data is transferred to/from server in function call (except
'notice' messages generated server-side) - problem occurs completely
server-side, but something in JDBC driver is triggering the problem.

-------------------------------------------------------
What can be wrong with JDBC connection in this regard?
-------------------------------------------------------

I'm using jdbc driver v9.4.1207 (the latest one at the moment).
Tested from my own app, http://www.squirrelsql.org/ and
http://bits.netbeans.org/download/trunk/nightly/latest (latest dev
version).

 



-- 
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] 3+ messages in thread

* Re: BUG #13885: float->string conversion loses precision server-side on JDBC connection
@ 2016-01-25 16:06  Tom Lane <tgl@sss.pgh.pa.us>
  parent: xtracoder@gmail.com
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2016-01-25 16:06 UTC (permalink / raw)
  To: xtracoder@gmail.com; +Cc: pgsql-bugs

xtracoder@gmail.com writes:
> I have a PostgreSQL function, which accepts JSON, performs some processing,
> and returns JSON. Something strange is happening when calling stored
> procedure from Java application - float->string conversion is incorrect.

I don't think it's "incorrect".  I believe the displayed difference here
is because the JDBC driver sets extra_float_digits to 3 or so.
Compare this in psql:

regression=# do $$ declare x float := 4.1; begin raise notice 'x = %', x; end$$;
NOTICE:  x = 4.1
DO
regression=# set extra_float_digits = 3;
SET
regression=# do $$ declare x float := 4.1; begin raise notice 'x = %', x; end$$;
NOTICE:  x = 4.09999999999999964
DO

The uglier-looking number is a more precise representation of the actual
float4 value.

			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] 3+ messages in thread

* Re: BUG #13885: float->string conversion loses precision server-side on JDBC connection
@ 2016-01-25 17:47  Xtra Coder <xtracoder@gmail.com>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 3+ messages in thread

From: Xtra Coder @ 2016-01-25 17:47 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

Yes, this is not bug in terms of 'float->string' convertion - people in
other forum also pointed out to JDBC, "extra_float_digits" and consequences.


On Mon, Jan 25, 2016 at 7:06 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

> xtracoder@gmail.com writes:
> > I have a PostgreSQL function, which accepts JSON, performs some
> processing,
> > and returns JSON. Something strange is happening when calling stored
> > procedure from Java application - float->string conversion is incorrect.
>
> I don't think it's "incorrect".  I believe the displayed difference here
> is because the JDBC driver sets extra_float_digits to 3 or so.
> Compare this in psql:
>
> regression=# do $$ declare x float := 4.1; begin raise notice 'x = %', x;
> end$$;
> NOTICE:  x = 4.1
> DO
> regression=# set extra_float_digits = 3;
> SET
> regression=# do $$ declare x float := 4.1; begin raise notice 'x = %', x;
> end$$;
> NOTICE:  x = 4.09999999999999964
> DO
>
> The uglier-looking number is a more precise representation of the actual
> float4 value.
>
>                         regards, tom lane
>

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


end of thread, other threads:[~2016-01-25 17:47 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-01-24 20:29 BUG #13885: float->string conversion loses precision server-side on JDBC connection xtracoder@gmail.com
2016-01-25 16:06 ` Tom Lane <tgl@sss.pgh.pa.us>
2016-01-25 17:47   ` Xtra Coder <xtracoder@gmail.com>

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