Repository navigation
Description
dbWriteTableArrow() picks the PostgreSQL column type from the R type that nanoarrow would convert each Arrow type to, not from the Arrow type itself. For several Arrow types this gives a wider, lossy or wrong column type. A duration over 24 hours or a negative one even fails in COPY, because durations become time columns, and decimal128 values lose digits because they travel as doubles.
Location: DBI default method dbCreateTableArrow() maps through nanoarrow::infer_nanoarrow_ptype(), then RPostgres get_data_type() maps the R ptype; dbAppendTableArrow() converts the data with as.data.frame(). int64 has its own issue, #640.
Observed mapping (Arrow → PostgreSQL), each still open:
- int8, int16 →
integer(expectedsmallint) - uint32 →
double precision(expectedbigint) - uint64 →
double precision, lossy above 2^53 (expectednumeric) - float32 →
double precision(expectedreal) - decimal128(20, 3) →
double precision, lossy (expectednumeric) - date64 →
timestamp with time zone(expectedtimestampordate) - timestamp[ns] →
timestamp with time zone, with a precision warning (expectedtimestamp) - duration →
time without time zone, fails for values over 24 hours or negative (expectedinterval) - null →
boolean(expected an error, ortext) - test
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(
int8 = arrow::Array$create(1L, type = arrow::int8()),
int16 = arrow::Array$create(1L, type = arrow::int16()),
uint32 = arrow::Array$create(1L, type = arrow::uint32()),
uint64 = arrow::Array$create(1L, type = arrow::uint64()),
float32 = arrow::Array$create(1.5, type = arrow::float32()),
decimal = arrow::Array$create(1.5, type = arrow::decimal128(20, 3)),
date64 = arrow::Array$create(as.Date("2020-01-01"), type = arrow::date64()),
ts_ns = arrow::Array$create(as.POSIXct("2020-01-01", tz = "UTC"), type = arrow::timestamp("ns")),
dur = arrow::Array$create(as.difftime(5, units = "secs"), type = arrow::duration("s")),
null = arrow::Array$create(NA, type = arrow::null())
)
dbWriteTableArrow(con, "t_types", tbl, overwrite = TRUE)
#> Warning in as.data.frame.nanoarrow_array(tmp): 1 value(s) may have incurred
#> loss of precision in conversion to double()
dbGetQuery(con, "SELECT column_name, data_type FROM information_schema.columns WHERE table_name = 't_types' ORDER BY ordinal_position")
#> column_name data_type
#> 1 int8 integer
#> 2 int16 integer
#> 3 uint32 double precision
#> 4 uint64 double precision
#> 5 float32 double precision
#> 6 decimal double precision
#> 7 date64 timestamp with time zone
#> 8 ts_ns timestamp with time zone
#> 9 dur time without time zone
#> 10 null boolean
# Expected: smallint, smallint, bigint, numeric, real, numeric, timestamp without time zone (or date),
# timestamp without time zone, interval, and an error or a text column for null
dbRemoveTable(con, "t_types")
# A duration over 24 hours or a negative one fails
tbl <- arrow::arrow_table(x = arrow::Array$create(as.difftime(c(100000, -5), units = "secs"), type = arrow::duration("s")))
dbWriteTableArrow(con, "t_dur", tbl, overwrite = TRUE)
#> Error:
#> ! COPY returned error : ERROR: date/time field value out of range: " 27:46:40"
#> CONTEXT: COPY t_dur, line 1, column x: " 27:46:40"
# Expected: an interval column with 27:46:40 and -00:00:05
dbRemoveTable(con, "t_dur", fail_if_missing = FALSE)
# decimal128 loses digits
tbl <- arrow::arrow_table(x = arrow::Array$create("12345678901234567.891", type = arrow::string())$cast(arrow::decimal128(20, 3)))
dbWriteTableArrow(con, "t_dec", tbl, overwrite = TRUE)
dbGetQuery(con, "SELECT x::text FROM t_dec")
#> x
#> 1 1.2345678901234568e+16
# Expected: 12345678901234567.891
dbRemoveTable(con, "t_dec")
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
Severity: medium (proposed, Kirill may change). Medium for duration (errors) and decimal (lossy), low for the other mappings.
Native Arrow support is coordinated in #631 and is expected to fix this by construction if the native create maps Arrow types to PostgreSQL types directly (duration → interval, decimal → numeric, and so on).
Activity
krlmlr commentedon Nov 29, 2026
Closed to keep the issue tracker for issues reported by others; the work here is still open and tracked with the
status: openlabel.Generated by Claude Code