Doc #81397 [Com]: PDO_sqlite: DECIMAL values from results are cast to float

From: Date: Thu, 02 Sep 2021 10:02:06 +0000
Subject: Doc #81397 [Com]: PDO_sqlite: DECIMAL values from results are cast to float
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-19151@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81397&edit=1 ID: 81397 Comment by: me at derrabus dot de Reported by: me at derrabus dot de Summary: PDO_sqlite: DECIMAL values from results are cast to float Status: Open Type: Documentation Problem Package: PDO SQLite Operating System: macOS 11.5 PHP Version: 8.1.0beta3 Block user comment: N Private report: N New Comment: Agreed. The new behavior makes sense to me, given that SQLite emulates decimals using floats. Thank you for the insights. Previous Comments: ------------------------------------------------------------------------ [2021-09-01 11:06:49] cmb@php.net Not sure how to proceed here. Switching to doc problem might be appropriate. ------------------------------------------------------------------------ [2021-08-29 14:04:06] cmb@php.net Yes[1]. But even in older PHP versions, the returned string was not necessarily exact, because SQLite3 internally stores the value as IEEE 754 binary64 (aka. double). [1] <https://github.com/php/php-src/blob/php-8.1.0beta3/ext/pdo_sqlite/sqlite_statement.c#L267-L299>. ------------------------------------------------------------------------ [2021-08-29 13:13:43] me at derrabus dot de So in other words, PDO could not tell apart DECIMAL from REAL in result sets because SQLite does not provide that information? ------------------------------------------------------------------------ [2021-08-29 12:05:09] cmb@php.net SQLite3 is special, since it has no strong typing. Instead it uses concepts called "type affinity" and "storage class". If you declare a column of type DECIMAL, it is actually treated as having NUMERIC type affinity, and numbers are stored either as INTEGER or REAL. So there is no way of having precise decimals anyway. You'd need to do the scaling yourself, and declare the column as INT (or anything that forces INTEGER type affinity). See also <https://www.sqlite.org/datatype3.html>. ------------------------------------------------------------------------ [2021-08-29 11:19:05] me at derrabus dot de # A docker-compose.yml that sets up the environment I used: version: '3.6' services: mysql: image: 'mysql' ports: - '3306:3306' environment: - MYSQL_DATABASE=test - MYSQL_USER=testuser - MYSQL_PASSWORD=testpassword - MYSQL_RANDOM_ROOT_PASSWORD=yes postgres: image: 'postgres' ports: - '5432:5432' environment: - POSTGRES_DB=test - POSTGRES_USER=testuser - POSTGRES_PASSWORD=testpassword ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=81397 -- Edit this bug report at https://bugs.php.net/bug.php?id=81397&edit=1

« previous php.doc.bugs (#19151) next »