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

Conversation

JSchoenbachler
Copy link
Contributor

@JSchoenbachler JSchoenbachler commented Jan 19, 2021

This is a pull request to close #241

The only change is to replace the dbAppendTable function with the relevant pieces of dbWriteTable when append & copy = TRUE.

Copy link
Member

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

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

Thanks for working on this!

R/tables.R Outdated
Comment on lines 206 to 207
connection_copy_data(conn@ptr, sql, value)
}
Copy link
Member

Choose a reason for hiding this comment

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

Need to return a value here. Test with DBItest::test_some("append_table_return"). Also, factors should give a warning: DBItest::test_some("append_roundtrip_factor")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@krlmlr I have it now returning nrow(values) and a warning if factors are supplied. Let me know if there is anything else I need to do!

Copy link
Member

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

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

Thanks, this looks great, tests pass. Can we reuse code?

R/tables.R Outdated Show resolved Hide resolved
Co-authored-by: Kirill Müller <krlmlr@users.noreply.github.com>
@JSchoenbachler
Copy link
Contributor Author

@krlmlr Approved suggestion and committed to branch!

Copy link
Member

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

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

Looks like we need warn = TRUE.

R/tables.R Outdated Show resolved Hide resolved
Co-authored-by: Kirill Müller <krlmlr@users.noreply.github.com>
@JSchoenbachler
Copy link
Contributor Author

Commit is done!

@krlmlr
Copy link
Member

krlmlr commented Jan 23, 2021

Thanks! I think dbAppendTable() needs a copy = TRUE argument, similarly to dbWriteTable(), because COPY doesn't work everywhere.

I should have noticed earlier. Would you like to include this in this PR? If not, no worries, I can take this on.

Also, dbWriteTable() ultimately should forward to dbAppendTable() and also dbCreateTable(). We could do that in a separate PR.

@JSchoenbachler
Copy link
Contributor Author

@krlmlr Could you review the changes I made in the most recent commit and let me know if that is the way you want those things addressed? The only thing I think I didn't add was the addition of using dbCreateTable().

@krlmlr
Copy link
Member

krlmlr commented Jan 26, 2021

Thanks. Could you please run devtools::document()? Please watch out for failures in the automated tests.

@JSchoenbachler
Copy link
Contributor Author

@krlmlr All checks passed. Had to add a param for warn. I will add the dbCreateTable function call.

@JSchoenbachler
Copy link
Contributor Author

@krlmlr wait, I just checked and it already looks like dbWriteTable() calls dbCreateTable() on line 103 of tables.R. Was I misunderstanding what you were asking?

@JSchoenbachler
Copy link
Contributor Author

@krlmlr Hey just wanted to make sure you had seen the above message asking for clarification!

Copy link
Member

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

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

Thanks, I was mistaken, dbWriteTable() seems fine. Added a few more comments, would you like to do another round?

@@ -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) {

)
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)

if (!copy) {
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?


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?

R/tables.R Show resolved Hide resolved
Base automatically changed from master to main March 12, 2021 04:02
@krlmlr
Copy link
Member

krlmlr commented Sep 6, 2021

@JSchoenbachler: We are in the process of relicensing RPostgres under MIT, #312. Are you ok with maintaining your contribution under that license?

@JSchoenbachler
Copy link
Contributor Author

@krlmlr yes, that is fine! Sorry I haven't looked at this in a long time.

@krlmlr
Copy link
Member

krlmlr commented Sep 8, 2021

No worries! I can pick this up in the next few days, feel free to add more commits in the meantime.

@krlmlr krlmlr mentioned this pull request Sep 11, 2021
@krlmlr krlmlr closed this in #322 Sep 11, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dbWriteTable vs dbAppendTable google cloud Postgres
2 participants