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

Add dbWriteTable method for the CSV #254

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

artemklevtsov
Copy link

To load CSV directly:

tmp <- tempfile(fileext = ".csv")
utils::write.csv(iris, tmp)

DBI::dbWriteTable(
  conn = conn,
  name = "iris",
  value = tmp,
  temporary = TRUE
)

@artemklevtsov
Copy link
Author

artemklevtsov commented Sep 28, 2020

I think the LOAD LOCAL DATA INFILE statement should solve the performance issues. Far as I can see, this has already been implemented. Didn't that solve the problem?

My goal in this PR was just to reduce memory usage for more optimal ETL.

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. Instead of adding yet another dbWriteTable() method, I'd rather implement a new postgresAppendTableCSV() that does everything minus table creation/removal. We can later decide if we add a new dbAppendTable() method or implement a new dbAppendTableCSV() generic.

src/DbConnection.cpp Outdated Show resolved Hide resolved
@krlmlr
Copy link
Member

krlmlr commented Dec 22, 2020

Would you like to help with a postgresAppendTableCSV() method?

@artemklevtsov
Copy link
Author

Sure. Can you provide more details? I didn't find anything in this repo.

@krlmlr
Copy link
Member

krlmlr commented Dec 22, 2020

DBI now has dbCreateTable() and dbAppendTable(). If we want to implement a new method for writing CSV files, we should implement postgresAppendTableCSV() -- without duplicationg the logic of creating tables. Later we could think about providing a dbAppendTable() method that forwards to postgresAppendTableCSV().

@artemklevtsov
Copy link
Author

Fast draft the postgresAppendTableCSV may be looks like:

postgresAppendTableCSV <- function(conn, name, value, fields = NULL, sep = ",", header = TRUE, na.strings = "NA", encoding = "UTF-8") {
  sql <- paste0(
    "COPY ", dbQuoteIdentifier(conn, name),
    " (",  paste(dbQuoteIdentifier(conn, fields), collapse = ","), ") ",
    "FROM STDIN ",
    "(FORMAT CSV DELIMITER '", sep, "', HEADER '", header, "' NULL '", na.strings, "' ENCODING '", encoding, "')"
  )
  connection_copy_file(conn@ptr, sql, path.expand(value))
  
  invisible(TRUE)
}

Base automatically changed from master to main March 12, 2021 04:02
@krlmlr krlmlr added this to the 1.4.1 milestone Dec 5, 2021
@krlmlr krlmlr modified the milestones: 1.4.1, 1.4.2, 1.4.3 Dec 5, 2021
@krlmlr krlmlr marked this pull request as draft April 1, 2024 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants