Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BIGINT read/write support #94

Closed
natecobb opened this issue Sep 2, 2016 · 6 comments
Closed

BIGINT read/write support #94

natecobb opened this issue Sep 2, 2016 · 6 comments

Comments

@natecobb
Copy link

natecobb commented Sep 2, 2016

library(DBI)
library(RPostgres)

show_table = function(conn, table = "test_bigints") {
  dbGetQuery(conn, 
             sprintf("select column_name, data_type as field_type, character_maximum_length
                from INFORMATION_SCHEMA.COLUMNS where table_name = %s;", dbQuoteString(conn, table)))
}

conn <- dbConnect(RPostgres::Postgres())
x <- data.frame(db_variables = c(127, 32767, 8388607, 2147483647, 4294967295))

# Basic case
# RPostgres::dbWriteTable selects REAL (4 bytes) when creating a table. 
# This corrupts silently on insert.
# Same effect if we disable COPY
dbWriteTable(conn, "test_bigints", x, temporary = TRUE, overwrite = TRUE, copy = TRUE)
y = dbGetQuery(conn, "SELECT db_variables FROM test_bigints")
show_table(conn)
cbind(x$db_variables, typeof(x$db_variables), y, (y == x$db_variables))

# What if we create our own table with a correct type?
# Seems to work, but looks like we have a problem getting them back
dbExecute(conn, "DROP TABLE IF EXISTS test_bigints ")
dbExecute(conn, "CREATE TEMPORARY TABLE test_bigints (db_variables INT8)")
dbWriteTable(conn, "test_bigints", x, temporary = TRUE, append = TRUE, copy = TRUE)
y = dbGetQuery(conn, "SELECT db_variables FROM test_bigints")
show_table(conn)
cbind(x$db_variables, typeof(x$db_variables), y, (y == x$db_variables))

# Looks like BIGINTs are getting cast wrong, works fine if we 
# pull them down as numeric
y = dbGetQuery(conn, "SELECT db_variables::numeric FROM test_bigints")
show_table(conn)
cbind(x$db_variables, typeof(x$db_variables), y, (y == x$db_variables))

# What if we go bigger, to a true BIGINT? We throw an error for invalid syntax!
local_variables_2 = c(x$db_variables, 1051583408235890)
x2 <- data.frame(db_variables = local_variables_2)
dbWriteTable(conn, "test_bigints", x2, temporary = TRUE, append = TRUE, copy = TRUE)

#Error in eval(substitute(expr), envir, enclos) : 
#  ERROR:  invalid input syntax for integer: "1.05158340823589e+15"
#CONTEXT:  COPY test_bigints, line 6, column db_variables: "1.05158340823589e+15"

# But not if we use SQL statements rather than a COPY stream
dbExecute(conn, "TRUNCATE TABLE test_bigints")
dbWriteTable(conn, "test_bigints", x2, temporary = TRUE, append = TRUE, copy = FALSE)
y = dbGetQuery(conn, "SELECT db_variables::numeric FROM test_bigints")
show_table(conn)
cbind(local_variables_2, y, y == local_variables_2)

# Oddly enough we can write with COPY into a DOUBLE PRECISION data type
dbExecute(conn, "DROP TABLE IF EXISTS test_bigints ")
dbExecute(conn, "CREATE TEMPORARY TABLE test_bigints (db_variables DOUBLE PRECISION)")
dbWriteTable(conn, "test_bigints", x2, temporary = TRUE, append = TRUE, copy = TRUE)
y = dbGetQuery(conn, "SELECT db_variables::numeric FROM test_bigints")
show_table(conn)
cbind(local_variables_2, y, y == local_variables_2)

dbDisconnect(conn)


@parisni
Copy link

parisni commented Jan 9, 2017

Hi, is there any plan for those 8bytes types problems ?

I could find some time to work on a PR, but maybe this takes 5 min to @hadley

@parisni
Copy link

parisni commented Jan 9, 2017

See #112

@krlmlr
Copy link
Member

krlmlr commented Nov 6, 2017

Thanks. 64-bit integers will be available via the bit64::integer64 data type, and optionally as numerics/integers and strings.

@krlmlr
Copy link
Member

krlmlr commented Nov 27, 2017

Should work now.

@krlmlr krlmlr closed this as completed Nov 27, 2017
@rsaporta
Copy link

Can confirm this works. Thanks for the update.

@github-actions
Copy link

github-actions bot commented Dec 7, 2020

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.

@github-actions github-actions bot locked and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants