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

fix: rel_sql(rel, "{{sql}}") works even on a read-only database #138

Merged
merged 3 commits into from
Apr 29, 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
4 changes: 3 additions & 1 deletion src/duckdb/src/main/relation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ unique_ptr<QueryResult> Relation::Query(const string &sql) {
}

unique_ptr<QueryResult> Relation::Query(const string &name, const string &sql) {
CreateView(name);
bool replace = true;
bool temp = IsReadOnly();
CreateView(name, replace, temp);
Comment on lines +327 to +329
Copy link
Collaborator

Choose a reason for hiding this comment

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

@Tmonster: I realized too late that this patches the duckdb sources. Has this been upstreamed by now? Do you want to?

I can work around with a patch file for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

has not been upstreamed yet, and might not be until after v1.0 comes out. I think a patch file in duckdb-r is the way to go for now. I will still create an upstream PR though so you can eventually remove that patch.

return Query(sql);
}

Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-readonly.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,31 @@ test_that("read_only flag and shutdown works as expected", {

expect_true(TRUE)
})

test_that("read_only flag does not throw error when rel_sql is called", {
unlink("tmp.duckdb")
con <- dbConnect(duckdb(), "tmp.duckdb")
# just need a db to open in read only mode
dbExecute(con, "create table t1 as select range a from range(2)")
dbDisconnect(con)

con <- dbConnect(duckdb(), "tmp.duckdb", read_only=TRUE)
rel <- duckdb:::rel_from_df(con, mtcars)
ans <- data.frame(cyl=c(4), disp=c(71.1))
expect_rel2 <- duckdb:::rel_sql(rel, "SELECT cyl, disp FROM _ where disp = 71.1")
expect_equivalent(expect_rel2, ans)
dbDisconnect(con)
})

test_that("read_only flag still throws error when table is attempted to be created", {
unlink("tmp.duckdb")
con <- dbConnect(duckdb(), "tmp.duckdb")
# just need a db to open in read only mode
dbExecute(con, "create table t1 as select range a from range(2)")
dbDisconnect(con)

con <- dbConnect(duckdb(), "tmp.duckdb", read_only=TRUE)
rel <- duckdb:::rel_from_df(con, mtcars)
expect_error(duckdb:::rel_sql(rel, "create table t2 as (SELECT cyl, disp FROM _ )"))
dbDisconnect(con)
})
Binary file added tests/testthat/tmp.duckdb
Binary file not shown.
Loading