Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions pkg-r/R/TblSqlSource.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,18 @@ TblSqlSource <- R6::R6Class(
#' Execute a SQL query and return results
#'
#' @param query SQL query string to execute
#' @return A data frame containing query results
execute_query = function(query) {
#' @param collect If `TRUE`, collects the results into a local data frame
#' using [dplyr::collect()]. If `FALSE` (default), returns a lazy SQL
#' tibble.
#' @return A data frame (if `collect = TRUE`) or a lazy SQL tibble (if
#' `collect = FALSE`)
execute_query = function(query, collect = FALSE) {
Comment thread
gadenbuie marked this conversation as resolved.
Outdated
sql_query <- self$prep_query(query)
dplyr::tbl(private$conn, dplyr::sql(sql_query))
result <- dplyr::tbl(private$conn, dplyr::sql(sql_query))
if (collect) {
result <- dplyr::collect(result)
}
result
},

#' @description
Expand Down
9 changes: 7 additions & 2 deletions pkg-r/man/TblSqlSource.Rd

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

25 changes: 25 additions & 0 deletions pkg-r/tests/testthat/test-TblSqlSource.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,31 @@ describe("TblSqlSource$new()", {
expect_equal(collected$value, c(30, 40, 50))
})

it("returns lazy tibble from execute_query() when collect = FALSE", {
source <- local_tbl_sql_source()

result <- source$execute_query(
"SELECT * FROM test_table WHERE value > 25",
collect = FALSE
)
expect_s3_class(result, "tbl_sql")
expect_s3_class(result, "tbl_lazy")
})

it("returns data frame from execute_query() when collect = TRUE", {
source <- local_tbl_sql_source()

result <- source$execute_query(
"SELECT * FROM test_table WHERE value > 25",
collect = TRUE
)
expect_s3_class(result, "data.frame")
expect_false(inherits(result, "tbl_sql"))
expect_false(inherits(result, "tbl_lazy"))
expect_equal(nrow(result), 3)
expect_equal(result$value, c(30, 40, 50))
})

it("returns data frame from test_query()", {
source <- local_tbl_sql_source()

Expand Down