Skip to content
Closed
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
17 changes: 17 additions & 0 deletions r/src/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ std::shared_ptr<arrow::Table> Table__SelectColumns(
namespace arrow {
namespace r {

arrow::Status check_consistent_column_length(
const std::vector<std::shared_ptr<arrow::ChunkedArray>>& 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<arrow::Schema>& schema) {
// maybe a schema was given
Expand Down Expand Up @@ -327,6 +342,8 @@ std::shared_ptr<arrow::Table> 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));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this will conflict with #9615 but I believe the resolution is trivial: just insert this line as the second to last line in the new definition of Table__from_dots


return arrow::Table::Make(schema, columns);
}

Expand Down
6 changes: 6 additions & 0 deletions r/tests/testthat/test-RecordBatch.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
6 changes: 6 additions & 0 deletions r/tests/testthat/test-Table.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})