Skip to content

Commit

Permalink
Merge pull request #138 from Tmonster/rel_sql_on_read_only_dbs
Browse files Browse the repository at this point in the history
  • Loading branch information
krlmlr committed Apr 29, 2024
2 parents 9fcd063 + 64a7b34 commit 901d38d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
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);
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.

0 comments on commit 901d38d

Please sign in to comment.