wjones127 commented on a change in pull request #12751:
URL: https://github.com/apache/arrow/pull/12751#discussion_r837979911
##########
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:
This feels a little silly to me, so would welcome suggestions for a
better way to write this :)
--
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]