diff --git a/r/src/table.cpp b/r/src/table.cpp index 3e8adf59750..997d8f137cb 100644 --- a/r/src/table.cpp +++ b/r/src/table.cpp @@ -150,6 +150,21 @@ std::shared_ptr Table__SelectColumns( namespace arrow { namespace r { +arrow::Status check_consistent_column_length( + const std::vector>& columns) { + if (columns.size()) { + int64_t num_rows = columns[0]->length(); + + for (const auto& column : columns) { + if (column->length() != num_rows) { + return arrow::Status::Invalid("All columns must have the same length"); + } + } + } + + return arrow::Status::OK(); +} + arrow::Status InferSchemaFromDots(SEXP lst, SEXP schema_sxp, int num_fields, std::shared_ptr& schema) { // maybe a schema was given @@ -327,6 +342,8 @@ std::shared_ptr Table__from_dots(SEXP lst, SEXP schema_sxp) { StopIfNotOk( arrow::r::CollectTableColumns(lst, schema, num_fields, infer_schema, columns)); + StopIfNotOk(arrow::r::check_consistent_column_length(columns)); + return arrow::Table::Make(schema, columns); } diff --git a/r/tests/testthat/test-RecordBatch.R b/r/tests/testthat/test-RecordBatch.R index a017823ce34..96abceb2994 100644 --- a/r/tests/testthat/test-RecordBatch.R +++ b/r/tests/testthat/test-RecordBatch.R @@ -465,3 +465,9 @@ test_that("RecordBatch name assignment", { expect_error(names(rb) <- NULL) expect_error(names(rb) <- c(TRUE, FALSE)) }) + +test_that("record_batch() with different length arrays", { + msg <- "All arrays must have the same length" + expect_error(record_batch(a=1:5, b = 42), msg) + expect_error(record_batch(a=1:5, b = 1:6), msg) +}) diff --git a/r/tests/testthat/test-Table.R b/r/tests/testthat/test-Table.R index 8e0f8eebef5..7a0b8bd6c02 100644 --- a/r/tests/testthat/test-Table.R +++ b/r/tests/testthat/test-Table.R @@ -469,3 +469,9 @@ test_that("Table name assignment", { expect_error(names(tab) <- NULL) expect_error(names(tab) <- c(TRUE, FALSE)) }) + +test_that("Table$create() with different length columns", { + msg <- "All columns must have the same length" + expect_error(Table$create(a=1:5, b = 42), msg) + expect_error(Table$create(a=1:5, b = 1:6), msg) +})