Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion R/tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ sql_data_copy <- function(value, conn, row.names = FALSE) {
function(x) {
if (is.null(x)) {
NA_character_
} else {
} else if (is.raw(x)) {
paste0("\\x", paste(format(x), collapse = ""))
} else {
stopc("Lists must contain raw vectors or NULL")
}
},
character(1)
Expand Down
113 changes: 113 additions & 0 deletions tests/testthat/test-dbWriteTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,116 @@ if (postgresHasDefault()) {
})
})
}

test_that("list columns that are not lists of raw are rejected (#648)", {
skip_if_not(postgresHasDefault())
con <- postgresDefault()
on.exit(dbDisconnect(con))

df <- data.frame(id = 1:3)
df$x <- list(1:2, NULL, c("a", "b"))

expect_error(
dbWriteTable(con, "t_list_648", df, temporary = TRUE),
"Lists must contain raw vectors or NULL"
)
expect_false(dbExistsTable(con, "t_list_648"))

expect_error(
dbWriteTable(con, "t_list_648", df, temporary = TRUE, copy = FALSE),
"Lists must contain raw vectors or NULL"
)
expect_false(dbExistsTable(con, "t_list_648"))

dbCreateTable(
con,
"t_list_648",
c(id = "INTEGER", x = "BYTEA"),
temporary = TRUE
)
expect_error(
dbAppendTable(con, "t_list_648", df),
"Lists must contain raw vectors or NULL"
)
expect_equal(nrow(dbReadTable(con, "t_list_648")), 0L)
})

test_that("lists of raw and blobs are still written as bytea (#648)", {
skip_if_not(postgresHasDefault())
con <- postgresDefault()
on.exit(dbDisconnect(con))

df <- data.frame(id = 1:3)
df$x <- list(as.raw(1:2), NULL, as.raw(c(0xab, 0xff)))
df$y <- blob::blob(as.raw(3), NULL, raw(0))

dbWriteTable(con, "t_raw_648", df, temporary = TRUE)
out <- dbGetQuery(
con,
"SELECT pg_typeof(x)::text AS tx, pg_typeof(y)::text AS ty, x, y FROM t_raw_648 ORDER BY id"
)
expect_equal(out$tx, rep("bytea", 3))
expect_equal(out$ty, rep("bytea", 3))
expect_equal(as.list(out$x), df$x)
expect_equal(as.list(out$y), as.list(df$y))
})

test_that("AsIs lists of raw are written as bytea (#648)", {
skip_if_not(postgresHasDefault())
con <- postgresDefault()
on.exit(dbDisconnect(con))

df <- data.frame(id = 1:2)
df$x <- I(list(as.raw(1:2), NULL))

dbWriteTable(con, "t_asis_648", df, temporary = TRUE)
out <- dbGetQuery(
con,
"SELECT pg_typeof(x)::text AS tx, x FROM t_asis_648 ORDER BY id"
)
expect_equal(out$tx, rep("bytea", 2))
expect_equal(as.list(out$x), unclass(df$x))
})

test_that("zero-row data frames with a list column create a bytea column (#648)", {
skip_if_not(postgresHasDefault())
con <- postgresDefault()
on.exit(dbDisconnect(con))

df <- data.frame(id = integer())
df$x <- list()

dbWriteTable(con, "t_empty_648", df, temporary = TRUE)
out <- dbGetQuery(
con,
"SELECT format_type(atttypid, atttypmod) AS type FROM pg_attribute
WHERE attrelid = 't_empty_648'::regclass AND attname = 'x'"
)
expect_equal(out$type, "bytea")
expect_equal(nrow(dbReadTable(con, "t_empty_648")), 0L)
})

test_that("rejected list columns leave a user transaction usable (#648)", {
skip_if_not(postgresHasDefault())
con <- postgresDefault()
on.exit(dbDisconnect(con))

df <- data.frame(id = 1:2)
df$x <- list(1:2, "a")

dbBegin(con)
dbCreateTable(con, "t_tx_ok_648", c(id = "INTEGER"), temporary = TRUE)
expect_error(
dbWriteTable(con, "t_tx_648", df, temporary = TRUE),
"Lists must contain raw vectors or NULL"
)
expect_false(dbExistsTable(con, "t_tx_648"))
expect_true(dbIsValid(con))
dbAppendTable(con, "t_tx_ok_648", data.frame(id = 1L))
expect_equal(dbGetQuery(con, "SELECT id FROM t_tx_ok_648")$id, 1L)
dbCommit(con)
expect_equal(
dbGetQuery(con, "SELECT COUNT(*)::int AS n FROM t_tx_ok_648")$n,
1L
)
})