Skip to content
Open
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 .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
25 changes: 21 additions & 4 deletions R/bq-download.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,28 @@ bq_table_download <-
))
}

params <- set_row_params(
nrow = bq_table_nrow(x),
n_max = n_max,
start_index = start_index
# For views, bq_table_nrow() returns 0 even when the view has data
# because BigQuery doesn't populate numRows for views. We need to
# handle views differently.
is_view <- tryCatch(
bq_table_meta(x, "type")$type == "VIEW",
error = function(e) FALSE
)

if (is_view) {
# For views, we can't rely on numRows, so we use the user-specified n_max
# and let the download process handle pagination naturally
params <- list(
n_max = if (is.infinite(n_max)) 1e9 else n_max, # Use large number for Inf
start_index = start_index
)
} else {
params <- set_row_params(
nrow = bq_table_nrow(x),
n_max = n_max,
start_index = start_index
)
}
n_max <- params$n_max
start_index <- params$start_index

Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-dbi-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,31 @@ test_that("DBI::sqlData return TRUE, FALSE for logical class", {
DBI::SQL(c("TRUE", "FALSE", "NULL"))
)
})

test_that("dbReadTable works with views (issue #633)", {
skip_if_no_auth()

tb <- bq_test_table()
con <- DBI::dbConnect(bq_dataset(tb$project, tb$dataset))

# Create a test table with some data
test_data <- data.frame(x = 1:3, y = c("a", "b", "c"))
DBI::dbWriteTable(con, tb$table, test_data)

# Create a view based on the table
view_name <- paste0(tb$table, "_view")
view_sql <- sprintf("CREATE VIEW %s AS SELECT * FROM %s", view_name, tb$table)
DBI::dbExecute(con, view_sql)
defer(DBI::dbExecute(con, sprintf("DROP VIEW %s", view_name)))

# Test that dbReadTable works with the view (this was returning 0 rows before the fix)
result_view <- DBI::dbReadTable(con, view_name)
expect_equal(nrow(result_view), 3)
expect_equal(ncol(result_view), 2)
expect_equal(result_view$x, c(1, 2, 3))

# Verify the table still works normally
result_table <- DBI::dbReadTable(con, tb$table)
expect_equal(nrow(result_table), 3)
expect_equal(result_table$x, c(1, 2, 3))
})
Loading