-
Notifications
You must be signed in to change notification settings - Fork 12
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
Duckdb assign and copy improvements #74
Open
egillax
wants to merge
8
commits into
OHDSI:develop
Choose a base branch
from
egillax:duckdb_assign_and_copy
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@egillax -- since the behavior is different on windoz vs *ix machines, do we have a benchmark on both types? |
Here it is (a tiny bit slower than attaching): library(Andromeda)
#> Loading required package: dplyr
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
andromeda <- andromeda()
n <- 16e5
n_subjects <- 100e3
n_cols <- 5e3
covariates <- data.frame(
row = sample(1:n_subjects, n, replace = TRUE),
col = sample(1:n_cols, n, replace = TRUE),
c = rnorm(n)
)
covariateRef <- data.frame(
a = 1:3e3,
b = letters[1:3e3],
c = rnorm(3e3)
)
analysisRef <- data.frame(
a = 1:3e1,
b = letters[1:3e1],
c = rnorm(3e1)
)
andromeda$covariates <- covariates
andromeda$covariateRef <- covariateRef
andromeda$analysisRef <- analysisRef
newAndromeda <- Andromeda::andromeda()
value <- andromeda$covariates
oldWay <- function() {
doBatchedAppend <- function(batch) {
duckdb::dbWriteTable(conn = newAndromeda, name = "old", value = batch, overwrite = FALSE, append = TRUE)
return(TRUE)
}
dummy <- batchApply(value, doBatchedAppend)
if (length(dummy) == 0) {
duckdb::dbWriteTable(conn = newAndromeda, name = "old", value = dplyr::collect(value), overwrite = FALSE, append = TRUE)
}
}
newWay <- function() {
valueFile <- dbplyr::remote_con(value)@dbname
DBI::dbExecute(newAndromeda, paste0("ATTACH DATABASE '", valueFile, "' AS old"))
DBI::dbExecute(newAndromeda, paste0("CREATE TABLE new AS SELECT * FROM old.covariates"))
DBI::dbExecute(newAndromeda, "DETACH DATABASE old")
}
parquetWay <- function() {
tempFile <- tempfile(fileext = ".parquet")
DBI::dbExecute(
dbplyr::remote_con(value),
sprintf(
"COPY (%s) TO '%s' (FORMAT 'parquet')",
dbplyr::sql_render(value), tempFile
)
)
DBI::dbExecute(
newAndromeda,
sprintf(
"CREATE OR REPLACE TABLE %s AS SELECT * FROM read_parquet('%s')",
"old",
tempFile
)
)
unlink(tempFile)
}
microbenchmark::microbenchmark(oldWay(),
newWay(),
parquetWay(),
times = 10,
setup = {
newAndromeda$old <- NULL
newAndromeda$new <- NULL
}
)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> oldWay() 794.35739 808.35575 842.31538 851.49410 865.55682 881.60433 10
#> newWay() 31.91771 32.64865 35.34855 35.83560 36.58089 39.37339 10
#> parquetWay() 59.34795 59.74585 61.57503 60.65007 63.11939 67.11480 10 Created on 2025-02-11 with reprex v2.1.1 For completeness here is the benchmark with a lazy query (which uses a temporary parquet file like on windows): lazy query benchmarklibrary(Andromeda)
#> Loading required package: dplyr
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
andromeda <- andromeda()
n <- 16e5
n_subjects <- 100e3
n_cols <- 5e3
covariates <- data.frame(
row = sample(1:n_subjects, n, replace = TRUE),
col = sample(1:n_cols, n, replace = TRUE),
c = rnorm(n)
)
covariateRef <- data.frame(
a = 1:3e3,
b = letters[1:3e3],
c = rnorm(3e3)
)
analysisRef <- data.frame(
a = 1:3e1,
b = letters[1:3e1],
c = rnorm(3e1)
)
population <- data.frame(
row = 1:floor(n_subjects / 2)
)
andromeda$covariates <- covariates
andromeda$covariateRef <- covariateRef
andromeda$analysisRef <- analysisRef
andromeda$population <- population
newAndromeda <- Andromeda::andromeda()
# create the query, join with population which only includes half of the subjects
value <- andromeda$covariates %>% dplyr::inner_join(andromeda$population, by = "row")
oldWay <- function() {
doBatchedAppend <- function(batch) {
duckdb::dbWriteTable(conn = newAndromeda, name = "old", value = batch, overwrite = FALSE, append = TRUE)
return(TRUE)
}
dummy <- batchApply(value, doBatchedAppend)
if (length(dummy) == 0) {
duckdb::dbWriteTable(conn = newAndromeda, name = "old", value = dplyr::collect(value), overwrite = FALSE, append = TRUE)
}
}
newWay <- function() {
valueFile <- dbplyr::remote_con(value)@dbname
DBI::dbExecute(newAndromeda, paste0("ATTACH DATABASE '", valueFile, "' AS old"))
DBI::dbExecute(newAndromeda, paste0("CREATE TABLE new AS SELECT * FROM old.covariates"))
DBI::dbExecute(newAndromeda, "DETACH DATABASE old")
}
parquetWay <- function() {
tempFile <- tempfile(fileext = ".parquet")
DBI::dbExecute(
dbplyr::remote_con(value),
sprintf(
"COPY (%s) TO '%s' (FORMAT 'parquet')",
dbplyr::sql_render(value), tempFile
)
)
DBI::dbExecute(
newAndromeda,
sprintf(
"CREATE OR REPLACE TABLE %s AS SELECT * FROM read_parquet('%s')",
"old",
tempFile
)
)
unlink(tempFile)
}
microbenchmark::microbenchmark(oldWay(),
parquetWay(),
times = 10,
setup = {
newAndromeda$old <- NULL
newAndromeda$new <- NULL
}
)
#> Unit: milliseconds
#> expr min lq mean median uq max neval
#> oldWay() 428.4652 436.4600 452.3760 439.7648 454.9417 518.0835 10
#> parquetWay() 122.6131 123.2617 124.5367 124.1885 126.1864 127.1723 10 Created on 2025-02-11 with reprex v2.1.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #73
Use
attach
andcopy
operations instead ofbatchApply
incopyAndromeda
and assignment ("[[<-"
)In "[[<-" attach/copy didn't work for
lazy_queries
: materialize query to parquet file and then assign into databaseUse
identical
instead ofall.equal
, it's faster and more fit for purpose.Set
"wal_autocheckpoint = '0KB'"
to immediately commit changes to the database instead of having them in the wal file. Had an issue with this indeepPlp
and this seems to match the previous behaviour which disabledjournal_mode
for sqliteHad to modify one test because
table
is a reserved word induckdb
so the test failed. Renamed thetable
toiris
.Microbenchmark reprex for identical vs all.equal
Created on 2025-02-11 with reprex v2.1.1
Microbenchmark reprex for attach method vs `batchApply` for `[[<-`
Created on 2025-02-11 with reprex v2.1.1