Skip to content
Merged
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
29 changes: 29 additions & 0 deletions cpp/src/arrow/record_batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,35 @@ Result<std::shared_ptr<RecordBatch>> RecordBatch::ReplaceSchema(
return RecordBatch::Make(std::move(schema), num_rows(), columns());
}

std::vector<std::string> RecordBatch::ColumnNames() const {
std::vector<std::string> names(num_columns());
for (int i = 0; i < num_columns(); ++i) {
names[i] = schema()->field(i)->name();
}
return names;
}

Result<std::shared_ptr<RecordBatch>> RecordBatch::RenameColumns(
const std::vector<std::string>& names) const {
int n = num_columns();

if (static_cast<int>(names.size()) != n) {
return Status::Invalid("tried to rename a record batch of ", n, " columns but only ",
names.size(), " names were provided");
}

ArrayVector columns(n);
FieldVector fields(n);

for (int i = 0; i < n; ++i) {
columns[i] = column(i);
fields[i] = schema()->field(i)->WithName(names[i]);
}

return RecordBatch::Make(::arrow::schema(std::move(fields)), num_rows(),
std::move(columns));
}

Result<std::shared_ptr<RecordBatch>> RecordBatch::SelectColumns(
const std::vector<int>& indices) const {
int n = static_cast<int>(indices.size());
Expand Down
7 changes: 7 additions & 0 deletions cpp/src/arrow/record_batch.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,13 @@ class ARROW_EXPORT RecordBatch {
/// \return PrettyPrint representation suitable for debugging
std::string ToString() const;

/// \brief Return names of all columns
std::vector<std::string> ColumnNames() const;

/// \brief Rename columns with provided names
Result<std::shared_ptr<RecordBatch>> RenameColumns(
const std::vector<std::string>& names) const;

/// \brief Return new record batch with specified columns
Result<std::shared_ptr<RecordBatch>> SelectColumns(
const std::vector<int>& indices) const;
Expand Down
26 changes: 26 additions & 0 deletions cpp/src/arrow/record_batch_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,32 @@ TEST_F(TestRecordBatch, RemoveColumn) {
AssertBatchesEqual(*new_batch, *batch4);
}

TEST_F(TestRecordBatch, RenameColumns) {
const int length = 10;

auto field1 = field("f1", int32());
auto field2 = field("f2", uint8());
auto field3 = field("f3", int16());

auto schema1 = ::arrow::schema({field1, field2, field3});

random::RandomArrayGenerator gen(42);

auto array1 = gen.ArrayOf(int32(), length);
auto array2 = gen.ArrayOf(uint8(), length);
auto array3 = gen.ArrayOf(int16(), length);

auto batch = RecordBatch::Make(schema1, length, {array1, array2, array3});
EXPECT_THAT(batch->ColumnNames(), testing::ElementsAre("f1", "f2", "f3"));

ASSERT_OK_AND_ASSIGN(auto renamed, batch->RenameColumns({"zero", "one", "two"}));
EXPECT_THAT(renamed->ColumnNames(), testing::ElementsAre("zero", "one", "two"));
EXPECT_THAT(renamed->columns(), testing::ElementsAre(array1, array2, array3));
ASSERT_OK(renamed->ValidateFull());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It wouldn't hurt to check that the renamed batch is otherwise equal to the original.

@judahrand judahrand Mar 4, 2024 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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


ASSERT_RAISES(Invalid, batch->RenameColumns({"hello", "world"}));
}

TEST_F(TestRecordBatch, SelectColumns) {
const int length = 10;

Expand Down
7 changes: 7 additions & 0 deletions python/pyarrow/includes/libarrow.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -961,8 +961,15 @@ cdef extern from "arrow/api.h" namespace "arrow" nogil:
shared_ptr[CArray] column(int i)
const c_string& column_name(int i)

CResult[shared_ptr[CRecordBatch]] AddColumn(
int i, shared_ptr[CField] field, shared_ptr[CArray] column)
CResult[shared_ptr[CRecordBatch]] RemoveColumn(int i)
CResult[shared_ptr[CRecordBatch]] SetColumn(
int i, shared_ptr[CField] field, shared_ptr[CArray] column)

const vector[shared_ptr[CArray]]& columns()

CResult[shared_ptr[CRecordBatch]] RenameColumns(const vector[c_string]&)
CResult[shared_ptr[CRecordBatch]] SelectColumns(const vector[int]&)

int num_columns()
Expand Down
Loading