Skip to content
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
16 changes: 15 additions & 1 deletion r/R/table.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ names.Table <- function(x) x$ColumnNames()
#' does not copy array data, but instead creates new chunked arrays for each
#' column that point at existing array data.
#'
#' @param ... A [Table]
#' @param ... One or more [Table] or [RecordBatch] objects. RecordBatch objects
#' will be automatically converted to Tables.
#' @param unify_schemas If TRUE, the schemas of the tables will be first unified
#' with fields of the same name being merged, then each table will be promoted
#' to the unified schema before being concatenated. Otherwise, all tables should
Expand All @@ -176,6 +177,10 @@ names.Table <- function(x) x$ColumnNames()
#' prius <- arrow_table(name = "Prius", mpg = 58, cyl = 4, disp = 1.8)
#' combined <- concat_tables(tbl, prius)
#' tail(combined)$to_data_frame()
#'
#' # Can also pass RecordBatch objects
#' batch <- record_batch(name = "Volt", mpg = 53, cyl = 4, disp = 1.5)
#' combined2 <- concat_tables(tbl, batch)
#' @export
concat_tables <- function(..., unify_schemas = TRUE) {
tables <- list2(...)
Expand All @@ -184,6 +189,15 @@ concat_tables <- function(..., unify_schemas = TRUE) {
abort("Must pass at least one Table.")
}

# Convert any RecordBatch objects to Tables
tables <- lapply(tables, function(x) {
if (inherits(x, "RecordBatch")) {
arrow_table(x)
} else {
x
}
})

if (!unify_schemas) {
# assert they have same schema
schema <- tables[[1]]$schema
Expand Down
7 changes: 6 additions & 1 deletion r/man/concat_tables.Rd

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

14 changes: 14 additions & 0 deletions r/tests/testthat/test-Table.R
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,20 @@ test_that("Tables can be combined with concat_tables()", {
expect_equal(expected, concat_tables(expected))
})

test_that("concat_tables() handles RecordBatch objects (GH-47000)", {
# concat_tables() should automatically convert RecordBatch to Table
tbl <- arrow_table(a = 1:5, b = letters[1:5])
rb <- record_batch(a = 6:10, b = letters[6:10])

# Concatenating a Table with a RecordBatch should work (not segfault)
result <- concat_tables(tbl, rb)
expect_s3_class(result, "Table")
expect_equal(
result,
arrow_table(a = 1:10, b = letters[1:10])
)
})

test_that("Table supports rbind", {
expect_error(
rbind(arrow_table(a = 1:10), arrow_table(a = c("a", "b"))),
Expand Down
Loading