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

Use dbWriteTable Copy in dbAppendTable to Eliminate Slowdown #286

Closed
wants to merge 7 commits into from
Closed
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
53 changes: 26 additions & 27 deletions R/tables.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#' and uses `COPY name FROM stdin`. This is fast, but not supported by
#' all postgres servers (e.g. Amazon's redshift). If `FALSE`, generates
#' a single SQL string. This is slower, but always supported.
#' @param warn If `TRUE`, warns user when converting from factor to string.
#'
#' @examples
#' # For running the examples on systems without PostgreSQL connection:
Expand Down Expand Up @@ -108,22 +109,7 @@ setMethod("dbWriteTable", c("PqConnection", "character", "data.frame"),
}

if (nrow(value) > 0) {
krlmlr marked this conversation as resolved.
Show resolved Hide resolved
if (!copy) {
value <- sqlData(conn, value, row.names = FALSE)

sql <- sqlAppendTable(conn, name, value, row.names = FALSE)
dbExecute(conn, sql)
} else {
value <- sql_data_copy(value, row.names = FALSE)

fields <- dbQuoteIdentifier(conn, names(value))
sql <- paste0(
"COPY ", dbQuoteIdentifier(conn, name),
" (", paste(fields, collapse = ", "), ")",
" FROM STDIN"
)
connection_copy_data(conn@ptr, sql, value)
}
dbAppendTable(conn, name, value, copy, warn = FALSE)
}

invisible(TRUE)
Expand Down Expand Up @@ -188,20 +174,33 @@ format_keep_na <- function(x, ...) {
#' @rdname postgres-tables
#' @export
setMethod("dbAppendTable", c("PqConnection"),
function(conn, name, value, ..., row.names = NULL) {
function(conn, name, value, copy = TRUE, warn = TRUE, ..., row.names = NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need a warn argument here, dbAppendTable() is a new generic.

Suggested change
function(conn, name, value, copy = TRUE, warn = TRUE, ..., row.names = NULL) {
function(conn, name, value, ..., copy = TRUE, row.names = NULL) {

stopifnot(is.null(row.names))

query <- sqlAppendTableTemplate(
con = conn,
table = name,
values = value,
row.names = row.names,
prefix = "$",
pattern = "1",
...
)
row.names <- FALSE

value = factor_to_string(value, warn = warn)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
value = factor_to_string(value, warn = warn)
value = factor_to_string(value, warn = TRUE)


value <- sqlRownamesToColumn(value, row.names)

if (!copy) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens here if the input has zero rows? Do we have a test for this in DBItest?

value <- sqlData(conn, value, row.names = FALSE)

sql <- sqlAppendTable(conn, name, value, row.names = FALSE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the previous approach via sqlAppendTableTemplate() works with Redshift? Can we implement a dbAppendTable() method for a RedshiftConnection that uses sqlAppendTable() and keep using sqlAppendTableTemplate() in the PqConnection method?

dbExecute(conn, sql)
} else {
value <- sql_data_copy(value, row.names = FALSE)

fields <- dbQuoteIdentifier(conn, names(value))
sql <- paste0(
"COPY ", dbQuoteIdentifier(conn, name),
" (", paste(fields, collapse = ", "), ")",
" FROM STDIN"
)
connection_copy_data(conn@ptr, sql, value)
}

dbExecute(conn, query, params = unname(as.list(value)))
nrow(value)
}
)

Expand Down
12 changes: 11 additions & 1 deletion man/postgres-tables.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.