You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/03/31 01:17:31 UTC

[GitHub] [arrow] paleolimbot commented on a change in pull request #12751: ARROW-15989: [R] rbind & cbind for Table & RecordBatch

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



##########
File path: r/R/record-batch.R
##########
@@ -189,3 +189,36 @@ record_batch <- RecordBatch$create
 
 #' @export
 names.RecordBatch <- function(x) x$names()
+
+#' @export
+rbind.RecordBatch <- function(...) {
+  stop("Use Table$create to combine record batches")
+}
+
+#' @export
+cbind.RecordBatch <- function(...) {
+  batches <- list(...)
+
+  # Assert they have the same length
+  unequal_length_idx <- which.min(lapply(batches, function(x) x$num_rows == batches[[1]]$num_rows))
+  if (unequal_length_idx != 1) {
+    stop(
+      sprintf(
+        "Cannot cbind batches with unequal number of rows. Batch 1 has %i, batch %i has %i",
+        batches[[1]]$num_rows,
+        unequal_length_idx,
+        batches[[unequal_length_idx]]$num_rows
+      )
+    )
+  }
+
+  fields <- unlist(lapply(batches, function(tab) tab$schema$fields))
+  schema <- Schema$create(fields)
+  columns <- unlist(lapply(batches, function(tab) tab$columns))
+
+  # return new table
+  args <- columns
+  names(args) <- names(schema)
+  args$schema <- schema
+  do.call(RecordBatch$create, args)

Review comment:
       You should be able to do `RecordBatch$create(!!! args, schema = schema)` (because we use `rlang::list2()` to ingest `...`, which allows 'splicing' via `!!!`).




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org