RE: PostgresSQL 10 | Driver 42.2.5 | Float Conversion Issue

From: "Thangavel, Parameswaran" <Parameswaran(dot)Thangavel(at)rsa(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Vladimir Sitnikov <sitnikov(dot)vladimir(at)gmail(dot)com>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, Andreas Joseph Krogh <andreas(at)visena(dot)com>, "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org>
Subject: RE: PostgresSQL 10 | Driver 42.2.5 | Float Conversion Issue
Date: 2020-10-21 00:21:10
Message-ID: CH2PR19MB37984419355C9F2164F4B74C831C0@CH2PR19MB3798.namprd19.prod.outlook.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Many thanks to all on sharing the thoughts.

However, As an application developer, I like to see all the options that I can evaluate and pick the minimal change that can get things working.

So far, I see following are the options that are suggested.

1. Workaround is to add binaryTransferDisable=700,701 driver connection property (the ids are for FLOAT4 and FLOAT4_ARRAY)
2. Use Java's BigDecimal on the Application side while setting the value through prepared statement.

Any other suggestions which I missed that might work in my scenario?

Thanks
Param

-----Original Message-----
From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Sent: Wednesday, October 21, 2020 4:12 AM
To: Vladimir Sitnikov
Cc: David G. Johnston; Andreas Joseph Krogh; Thangavel, Parameswaran; pgsql-jdbc(at)postgresql(dot)org
Subject: Re: PostgresSQL 10 | Driver 42.2.5 | Float Conversion Issue

[EXTERNAL EMAIL]

Vladimir Sitnikov <sitnikov(dot)vladimir(at)gmail(dot)com> writes:
> Tom>Note the waffling immediately after that, though.
> I assume PostgreSQL switched to Ryu around release 12 or so.

> Should float4_numeric switch to Ryu as well?
> I guess it would be both faster and more precise.

Ryu or not, it'd have the issue of sometimes presenting digits the user wasn't expecting, if the user is under the illusion that float is exact.
I'd rather document "this is always rounded to N digits" than have to explain the cases where Ryu doesn't produce an "exact" result.
You don't have to look far for examples, eg

# select 1234567890::float4;
float4
---------------
1.2345679e+09
(1 row)

# select '0.98765456'::float4;
float4
------------
0.98765457
(1 row)

You have to keep in mind that what Ryu does (or claims to do anyway) is produce the shortest decimal sequence that float4in will map back to the same float4 bit pattern. While this does often produce "unsurprising" results, that is not guaranteed once you're past six digits.

Now, I suppose there is something to be said for ensuring that float::numeric and float::text::numeric produce the same answers, which we'd get if we made float4_numeric and float8_numeric use Ryu instead of the current method. But that's not going to change the fact that the OP is doing it wrong by expecting exact results from what he's doing.

[ digs in archives... ] The original thread about the Ryu patch [1] considered this, and ultimately decided not to on the grounds that float4_numeric is marked immutable and therefore we should not change its behavior. I'm not sure if that argument is really irrefutable, but it's something to think about.

In any case, the JDBC list is not where to be complaining if you want a server behavior change.

regards, tom lane

[1] https://www.postgresql.org/message-id/flat/87r2el1bx6.fsf%40news-spur.riddles.org.uk

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Dave Cramer 2020-10-21 11:10:47 [pgjdbc/pgjdbc] 4c0e70: Update issue templates
Previous Message Tom Lane 2020-10-20 22:41:54 Re: PostgresSQL 10 | Driver 42.2.5 | Float Conversion Issue