thisisnic commented on a change in pull request #10100: URL: https://github.com/apache/arrow/pull/10100#discussion_r617672819
########## File path: cpp/src/arrow/record_batch.cc ########## @@ -253,6 +253,27 @@ bool RecordBatch::ApproxEquals(const RecordBatch& other) const { return true; } +Result<std::shared_ptr<RecordBatch>> RecordBatch::SelectColumns( + const std::vector<int>& indices) const { + int n = static_cast<int>(indices.size()); + + std::vector<std::shared_ptr<Field>> fields(n); + std::vector<std::shared_ptr<Array>> columns(n); Review comment: Done! ########## File path: cpp/src/arrow/record_batch.cc ########## @@ -253,6 +253,27 @@ bool RecordBatch::ApproxEquals(const RecordBatch& other) const { return true; } +Result<std::shared_ptr<RecordBatch>> RecordBatch::SelectColumns( + const std::vector<int>& indices) const { + int n = static_cast<int>(indices.size()); + + std::vector<std::shared_ptr<Field>> fields(n); + std::vector<std::shared_ptr<Array>> columns(n); + + for (int i = 0; i < n; i++) { + int pos = indices[i]; + if (pos < 0 || pos > num_columns() - 1) { + return Status::Invalid("Invalid column index ", pos, " to select columns."); + } + fields[i] = schema()->field(pos); + columns[i] = column(pos); + } + + auto new_schema = + std::make_shared<arrow::Schema>(std::move(fields), schema()->metadata()); + return RecordBatch::Make(new_schema, num_rows(), columns); Review comment: Done! -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org