Skip to content

CREATE TABLE pg.t AS … creates HUGEINT columns as double precision, so sum(bigint) results above 2^53 are rounded (same problem as #246 for UBIGINT) #623

Open

Description

@hanke580

What happens?

PostgresUtils::ToPostgresType() (src/postgres_utils.cpp:476) maps HUGEINT to DOUBLE. Every PostgreSQL table
the extension creates with a HUGEINT column therefore gets a double precision column, and every value above 2^53
(about 9·10^15) is stored rounded. This covers CREATE TABLE p.t AS …, CREATE TABLE p.t (c HUGEINT) and
COPY FROM DATABASE … TO p. There is no error or warning.

In DuckDB, sum() of a BIGINT column returns HUGEINT. So pushing an aggregate to PostgreSQL loses precision even
though the user never wrote HUGEINT. Row totals of cents, nanosecond sums and ID sums are typical cases.

The same problem for UBIGINT was reported in #246 and fixed by #256, which maps UBIGINT to DECIMAL(20,0). HUGEINT
kept the double mapping. numeric(39,0) (or plain numeric) would hold every HUGEINT value exactly. The sqlite
extension maps HUGEINT to VARCHAR and is exact.

Even inserting into an existing numeric(39,0) column rounds, because the extension reads numeric wider than 38 digits
as DOUBLE. That is the known read mapping (#51, pg_numeric_as_varchar), and I am not reporting it here.

To Reproduce

ATTACH 'host=127.0.0.1 port=5432 user=postgres dbname=postgres' AS p (TYPE postgres);
CREATE TABLE sales AS SELECT * FROM (VALUES (1, 9007199254740993::BIGINT), (2, 1::BIGINT), (2, 2::BIGINT)) v(store, cents);
CREATE TABLE p.totals AS SELECT store, sum(cents) AS total FROM sales GROUP BY store;
SELECT store, sum(cents) FROM sales GROUP BY store ORDER BY store;   -- 1 | 9007199254740993
CREATE TABLE ledger(id INT, amount HUGEINT);
INSERT INTO ledger VALUES (1, 12345678901234567890123), (2, -170141183460469231731687303715884105728);
CREATE TABLE p.ledger AS FROM ledger;

In PostgreSQL:

postgres=# SELECT store, pg_typeof(total), total FROM totals ORDER BY store;
 store |    pg_typeof     |         total
-------+------------------+-----------------------
     1 | double precision | 9.007199254740992e+15        <-- DuckDB computed 9007199254740993
     2 | double precision |                     3

postgres=# SELECT id, pg_typeof(amount), amount FROM ledger ORDER BY id;
 id |    pg_typeof     |         amount
----+------------------+-------------------------
  1 | double precision |  1.2345678901234568e+22      <-- was 12345678901234567890123
  2 | double precision | -1.7014118346046923e+38      <-- was -170141183460469231731687303715884105728

Expected: the column is created as numeric(39,0) (or numeric) and holds the exact values, as UBIGINT does since #256.

OS:

Ubuntu 22.04.2 LTS, x86_64

PostgreSQL Version:

17.11

DuckDB Version:

v2.1.0-dev89158 (main 1b4c3e54ba) + duckdb-postgres main c0f5a3c, built from source; also 1.5.6 with the released postgres_scanner

DuckDB Client:

CLI

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions