You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/04/21 14:50:20 UTC

[GitHub] [arrow] pitrou commented on a change in pull request #10100: ARROW-11660: [C++] Move RecordBatch::SelectColumns method from R to C++ library

pitrou commented on a change in pull request #10100:
URL: https://github.com/apache/arrow/pull/10100#discussion_r617619087



##########
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:
       Nit, but you can use the `FieldVector` and `ArrayVector` type aliases, respectively.

##########
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:
       `std::move(columns)` will avoid a vector copy here.




-- 
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:
users@infra.apache.org