Skip to content

dbWriteTableArrow() creates double precision for int64 columns and loses precision #640

Not planned

Description

@krlmlr

Writing an Arrow int64 column with dbWriteTableArrow() creates a double precision column and stores the values as doubles, so values above 2^53 are silently rounded. dbWriteTable() with an integer64 column creates bigint and keeps the value.

Location: DBI default methods. dbCreateTableArrow() derives the column types from nanoarrow::infer_nanoarrow_ptype(), which maps int64 to double() (nanoarrow default), and dbAppendTableArrow() converts each batch with as.data.frame(), again to double. The same holds for uint64 and decimal128, see the type mapping issue for the other types.

Reprex against the CRAN release (the output on main at 41cd4be is the same):

Sys.setenv(PGHOST = "localhost", PGPORT = "5432", PGUSER = "runner", PGPASSWORD = "test123", PGDATABASE = "testdb")
library(DBI)
con <- dbConnect(RPostgres::Postgres())
tbl <- arrow::arrow_table(x = arrow::Array$create(bit64::as.integer64("9007199254740993")))
dbWriteTableArrow(con, "t_int64", tbl, overwrite = TRUE)
dbGetQuery(con, "SELECT pg_typeof(x)::text AS type, x::text AS value FROM t_int64")
#>               type                 value
#> 1 double precision 9.007199254740992e+15
# Expected: bigint 9007199254740993

# The data frame path gets both right
dbWriteTable(con, "t_int64", data.frame(x = bit64::as.integer64("9007199254740993")), overwrite = TRUE)
dbGetQuery(con, "SELECT pg_typeof(x)::text AS type, x::text AS value FROM t_int64")
#>     type            value
#> 1 bigint 9007199254740993
dbRemoveTable(con, "t_int64")

dbGetQuery(con, "SHOW server_version")
#>                          server_version
#> 1 16.15 (Ubuntu 16.15-0ubuntu0.24.04.1)
dbDisconnect(con)
as.data.frame(sessioninfo::package_info(c("RPostgres", "DBI", "nanoarrow", "arrow"), dependencies = FALSE))[c("package", "ondiskversion", "source")]
#>             package ondiskversion         source
#> arrow         arrow      26.0.1.1           RSPM
#> DBI             DBI         1.3.0           RSPM
#> nanoarrow nanoarrow         0.9.0           RSPM
#> RPostgres RPostgres        1.4.10 RSPM (R 4.5.0)

Created on 2026-11-29 with reprex v2.1.1

  • fix
  • test (DBItest's 64-bit Arrow round-trip specs are skipped unconditionally, so they do not catch this)
  • report upstream (DBI default methods)

Severity: high (proposed, Kirill may change). Silent data loss.

Native Arrow support is coordinated in #631 and is expected to fix this by construction, since the native create and append map int64 to bigint without a detour through R doubles.

Activity

  1. added
    status: open
    on Nov 29, 2026
  2. krlmlr commented on Nov 29, 2026

    @krlmlr
    MemberAuthor

    Closed to keep the issue tracker for issues reported by others; the work here is still open and tracked with the status: open label.


    Generated by Claude Code

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

    bugstatus: open

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions