PostgreSQL result: numeric column type
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
Anewt |
Incomplete
|
Low
|
Sander van Schouwenburg (Sandworm) |
Bug Description
The 'numeric' column type in a PostgreSQL result is now converted to float. However, 'numeric' doesn't necessarily mean it is a floating point value. Doing a SUM() over some BIGINT column also reports a 'numeric' type, and by converting it to a float I've lost precision information.
I'm not exactly sure what is the best way to handle this. One option would be to leave it a string, but this isn't very typesafe. Another option might be to be able to override the types somehow.
There already is a (ugly) way to do this, but I'd say this is a workaround:
$pq = $db->prepare(
$res = $pq->execute();
$res->data_
$row = $res->fetch();
$res->free();
At the moment I don't need the precision, so this is not a high priority for me. But still, this is a flaw.
> The 'numeric' column type in a PostgreSQL result is now converted to
> float. However, 'numeric' doesn't necessarily mean it is a floating
> point value. Doing a SUM() over some BIGINT column also reports a
> 'numeric' type, and by converting it to a float I've lost precision
> information.
I don't see how one could possibly lose precision in this case. A mathemathical property of integer numbers is that addition, subtraction and multiplication (but not division!) of two integers will always result in an integer. I suspect PostgreSQL somehow detects/assumes that summing over BIGINT may cause numerical overflow, and converts the numbers to floats (which can keep higher numbers).
It could well be that I'm overlooking something here, though. If so, please tell me how (and why) you lost numerical precision in the case you described.