paleolimbot commented on a change in pull request #12751:
URL: https://github.com/apache/arrow/pull/12751#discussion_r839924183



##########
File path: r/R/table.R
##########
@@ -149,6 +149,64 @@ Table$create <- function(..., schema = NULL) {
 #' @export
 names.Table <- function(x) x$ColumnNames()
 
+#' @export
+rbind.Table <- function(...) {
+  tables <- list(...)
+
+  # assert they have same schema
+  schema <- tables[[1]]$schema
+  unequal_schema_idx <- which.min(lapply(tables, function(x) x$schema == 
schema))
+  if (unequal_schema_idx != 1) {
+    stop(paste0(
+      sprintf("Schema at index %i does not match the first schema\n", 
unequal_schema_idx),
+      "Schema 1:\n",
+      tables[[1]]$schema$ToString(),
+      sprintf("\nSchema %i:\n", unequal_schema_idx),
+      tables[[unequal_schema_idx]]$schema$ToString()
+    ))
+  }
+
+  # create chunked array from each column
+  columns <- vector(mode = "list", length = tables[[1]]$num_columns)
+  for (i in seq_len(length(columns))) {
+    columns[[i]] <- do.call(c, lapply(tables, function(tab) tab[[i]]))
+  }
+
+  # return new table
+  args <- columns
+  names(args) <- names(schema)
+  args$schema <- schema
+  do.call(Table$create, args)
+}
+
+#' @export
+cbind.Table <- function(...) {
+  tables <- list(...)
+
+  # Assert they have the same length
+  unequal_length_idx <- which.min(lapply(tables, function(x) x$num_rows == 
tables[[1]]$num_rows))

Review comment:
       That output is correct (`tables` is a `list()` of size 2, and you've 
provided it with one argument)...you'd need to do:
   
   ``` r
   library(arrow, warn.conflicts = FALSE)
   tables <- list(Table$create(a = 1:10), Table$create(a = c("a", "b")))
   size_proxy <- lapply(tables, function(tbl) seq_len(tbl$num_rows))
   vctrs::vec_size_common(!!! size_proxy)
   #> Error in `stop_vctrs()`:
   #> ! Can't recycle `..1` (size 10) to match `..2` (size 2).
   ```
   
   `seq_len()` is good here because it doesn't actually allocate if the values 
are never accessed:
   
   ``` r
   x <- seq_len(1e9)
   lobstr::obj_size(x)
   #> 680 B
   vctrs::vec_size_common(x)
   #> [1] 1000000000
   lobstr::obj_size(x)
   #> 680 B
   x[1] <- 27
   lobstr::obj_size(x)
   #> 8,000,000,048 B
   ```
   
   You can still totally roll your own error if this gives a confusing one for 
the final behaviour!




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to