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 random tbl name #26

Merged
merged 3 commits into from
Aug 18, 2024
Merged
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: duckdbfs
Title: High Performance Remote File System, Database and 'Geospatial' Access Using 'duckdb'
Version: 0.0.5
Version: 0.0.6
Authors@R:
c(person("Carl", "Boettiger", , "[email protected]", c("aut", "cre"),
comment = c(ORCID = "0000-0002-1642-628X")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# duckdbfs 0.0.6

* bugfix open_dataset() uses random table name by default, avoid naming collisions.

# duckdbfs 0.0.5

* bugfix `write_dataset()` no longer adds `**` into paths when writing some partitions.
Expand Down
2 changes: 1 addition & 1 deletion R/open_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ open_dataset <- function(sources,
unify_schemas = FALSE,
format = c("parquet", "csv", "tsv", "sf"),
conn = cached_connection(),
tblname = tbl_name(sources),
tblname = tmp_tbl_name(),
mode = "VIEW",
filename = FALSE,
recursive = TRUE,
Expand Down
31 changes: 9 additions & 22 deletions R/write_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,44 +35,31 @@ write_dataset <- function(dataset,
tblname <- as.character(remote_name(dataset, conn))
}

path <- parse_uri(path, conn = conn, recursive = FALSE)

## local writes use different notation to allow overwrites:
allow_overwrite <- character(0)
if(overwrite){
allow_overwrite <- paste("OVERWRITE_OR_IGNORE")
allow_overwrite <- "OVERWRITE_OR_IGNORE"
}

path <- parse_uri(path, conn = conn, recursive = FALSE)
if(grepl("^s3://", path)) {
duckdb_s3_config(conn = conn, ...)
if(overwrite){
# allow_overwrite <- paste("ALLOW_OVERWRITE", overwrite)
}
}


format <- toupper(format)
partition_by <- character(0)
if(length(partitioning) > 0) {
partition_by <- paste0("PARTITION_BY (",
paste(partitioning, collapse=", "),
"), ")
}
comma <- character(0)
if (length(c(partition_by, allow_overwrite) > 0)){
comma <- ", "
") ")
}
options <- paste0(
paste("FORMAT", "'parquet'"), comma,
partition_by,
allow_overwrite
)

query <- paste("COPY", tblname, "TO",
paste0("'", path, "'"),
paste0("(", options, ")"), ";")

format <- toupper(format)
format_by <- glue::glue("FORMAT {format}")
options_vec <- c(format_by, partition_by, allow_overwrite)
options <- glue::glue_collapse(options_vec, sep = ", ")

copy <- glue::glue("COPY {tblname} TO '{path}' ")
query <- glue::glue(copy, "({options})", ";")
status <- DBI::dbSendQuery(conn, query)
invisible(path)
}
Expand Down
2 changes: 1 addition & 1 deletion man/open_dataset.Rd

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

11 changes: 9 additions & 2 deletions tests/testthat/test-write_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ test_that("write_dataset", {
## Write from a query string
path2 <- file.path(tempdir(), "spatial2.parquet")

tbl |>
dplyr::mutate(new = "test") |>
dataset <- tbl |>
dplyr::mutate(new = "test")
dataset |>
write_dataset(path2)

})
Expand All @@ -48,6 +49,12 @@ test_that("write_dataset partitions", {
parts <- list.files(path)
expect_true(any(grepl("cyl=4", parts)))

path <- file.path(tempdir(), "mtcars2")
mtcars |> write_dataset(path, partitioning = "cyl", overwrite=FALSE)
expect_true(file.exists(path))
df <- open_dataset(path)
expect_s3_class(df, "tbl")

})


Expand Down
Loading