You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "Light-City (via GitHub)" <gi...@apache.org> on 2023/09/27 00:27:21 UTC
[GitHub] [arrow] Light-City opened a new pull request, #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Light-City opened a new pull request, #37896:
URL: https://github.com/apache/arrow/pull/37896
### Rationale for this change
User scenario: When we use acero plan, many smaller batches may be generated through agg and hashjoin. In addition, due to the mpp database, there is data distribution. When there are many segments, each segment data is compared at this time. Small, in order to improve performance, we hope to merge multiple fragmented small batches into one large batch for calculation together.
### What changes are included in this PR?
record_batch.cc
record_batch.h
record_batch_test.cc
### Are these changes tested?
yes, see record_batch_test.cc
### Are there any user-facing changes?
yes
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "conbench-apache-arrow[bot] (via GitHub)" <gi...@apache.org>.
conbench-apache-arrow[bot] commented on PR #37896:
URL: https://github.com/apache/arrow/pull/37896#issuecomment-1766414607
After merging your PR, Conbench analyzed the 6 benchmarking runs that have been run so far on merge-commit c5bce96ba626ad2558e10acddf8ef19ea06940e8.
There were no benchmark performance regressions. 🎉
The [full Conbench report](https://github.com/apache/arrow/runs/17781254336) has more details. It also includes information about 8 possible false positives for unstable benchmarks that are known to sometimes produce them.
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358104332
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
Review Comment:
Sorry, you are correct, it is handled 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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358106818
##########
cpp/src/arrow/record_batch_test.cc:
##########
@@ -555,4 +555,33 @@ TEST_F(TestRecordBatch, ReplaceSchema) {
ASSERT_RAISES(Invalid, b1->ReplaceSchema(schema));
}
+TEST_F(TestRecordBatch, ConcatenateRecordBatches) {
+ int length = 10;
+
+ auto f0 = field("f0", int32());
+ auto f1 = field("f1", uint8());
+
+ auto schema = ::arrow::schema({f0, f1});
+
+ random::RandomArrayGenerator gen(42);
+
+ auto b1 = gen.BatchOf(schema->fields(), length);
+
+ length = 5;
+
+ auto b2 = gen.BatchOf(schema->fields(), length);
+
+ ASSERT_OK_AND_ASSIGN(auto batch, ConcatenateRecordBatches({b1, b2}));
+ ASSERT_EQ(batch->num_rows(), b1->num_rows() + b2->num_rows());
+
+ f0 = field("fd0", int32());
+ f1 = field("fd1", uint8());
+
+ schema = ::arrow::schema({f0, f1});
+
+ auto b3 = gen.BatchOf(schema->fields(), length);
+
+ ASSERT_RAISES(Invalid, ConcatenateRecordBatches({b1, b3}));
Review Comment:
yes, i add this test.
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Light-City commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339393603
##########
cpp/src/arrow/record_batch.h:
##########
@@ -350,4 +350,12 @@ class ARROW_EXPORT RecordBatchReader {
Iterator<std::shared_ptr<RecordBatch>> batches, std::shared_ptr<Schema> schema);
};
+/// \brief Concatenate recordbatches
Review Comment:
ok
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "js8544 (via GitHub)" <gi...@apache.org>.
js8544 commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358099505
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
Review Comment:
```cpp
for (int i = 0; i < batches.size(); ++i) {
length += batches[i]->num_rows();
...
}
```
Doesn't this include the cols=0 case?
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358336370
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,34 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ if (!schema->Equals(batches[i]->schema())) {
+ return Status::Invalid(
+ "Schema of RecordBatch index ", i, " is ", batches[i]->schema()->ToString(),
+ ", which does not match index 0 recordbatch schema: ", schema->ToString());
+ }
+ }
+
+ std::vector<std::shared_ptr<Array>> concatenated_columns;
Review Comment:
ok
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358097865
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
Review Comment:
The code looks very refreshing, but it does not handle the case of cols = 0. In this case, cols = 0, length is not obtained.
For a batch, there may be cols = 0, the schema is empty, but there is length. This kind of batch is used to process count(*) and does not store the actual columns, only the number of rows.
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Light-City commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339398242
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
Review Comment:
no, My pseudocode is below, with the else branch
```cpp
if (cols == 0) {
// special case: null batch, no data, just length
for (size_t i = 0; i < batches.size(); ++i) {
length += batches[i]->num_rows();
}
} else {
for (int col = 0; col < cols; ++col)
}
```
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Light-City commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339404947
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
Review Comment:
Can it be changed to this method?
Within the col loop, the length of col=0 is assigned, and the lengths of the subsequent columns are verified against the first column.
```cpp
if (col == 0) {
length = array->length();
} else if (length != array->length()) {
return Status::Invalid(
"column index ", i, " length is ", array->length(),
", does not match the length of previous columns: ", length);
}
```
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
Review Comment:
Can it be changed to this method?
Within the col loop, the length of col=0 is assigned, and the lengths of the subsequent columns are verified against the first column.
```cpp
if (col == 0) {
length = array->length();
} else if (length != array->length()) {
return Status::Invalid(
"column index ", i, " length is ", array->length(),
", does not match the length of previous columns: ", length);
}
```
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358336077
##########
cpp/src/arrow/record_batch_test.cc:
##########
@@ -555,4 +555,38 @@ TEST_F(TestRecordBatch, ReplaceSchema) {
ASSERT_RAISES(Invalid, b1->ReplaceSchema(schema));
}
+TEST_F(TestRecordBatch, ConcatenateRecordBatches) {
+ int length = 10;
+
+ auto f0 = field("f0", int32());
+ auto f1 = field("f1", uint8());
+
+ auto schema = ::arrow::schema({f0, f1});
+
+ random::RandomArrayGenerator gen(42);
+
+ auto b1 = gen.BatchOf(schema->fields(), length);
+
+ length = 5;
+
+ auto b2 = gen.BatchOf(schema->fields(), length);
+
+ ASSERT_OK_AND_ASSIGN(auto batch, ConcatenateRecordBatches({b1, b2}));
+ ASSERT_EQ(batch->num_rows(), b1->num_rows() + b2->num_rows());
Review Comment:
ok, I've added it
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,34 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ if (!schema->Equals(batches[i]->schema())) {
+ return Status::Invalid(
+ "Schema of RecordBatch index ", i, " is ", batches[i]->schema()->ToString(),
+ ", which does not match index 0 recordbatch schema: ", schema->ToString());
+ }
+ }
+
+ std::vector<std::shared_ptr<Array>> concatenated_columns;
+ for (int col = 0; col < cols; ++col) {
+ ArrayVector column_arrays;
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "js8544 (via GitHub)" <gi...@apache.org>.
js8544 commented on PR #37896:
URL: https://github.com/apache/arrow/pull/37896#issuecomment-1761405129
I think the C++ Windows Checks are failing due to this change but I couldn't figure out why...
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Light-City commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339405109
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
+ for (int col = 0; col < cols; ++col) {
+ ArrayVector data;
+ for (size_t i = 0; i < batches.size(); ++i) {
+ auto cur_schema = batches[i]->schema();
+ if (!schema->Equals(cur_schema)) {
+ return Status::Invalid(
+ "RecordBatch index ", i, " schema is ", cur_schema->ToString(),
+ ", did not match index 0 recordbatch schema: ", schema->ToString());
+ }
+ auto column_data = batches[i]->column(col);
+ data.push_back(column_data);
+ }
+ auto array = Concatenate(data, pool).ValueOrDie();
+ length = array->length();
Review Comment:
Can it be changed to this method?
Within the col loop, the length of col=0 is assigned, and the lengths of the subsequent columns are verified against the first column.
```cpp
if (col == 0) {
length = array->length();
} else if (length != array->length()) {
return Status::Invalid(
"column index ", i, " length is ", array->length(),
", does not match the length of previous columns: ", length);
}
```
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Light-City commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339398242
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
Review Comment:
no, My pseudocode is below, with the else branch
if (cols == 0) {
// special case: null batch, no data, just length
for (size_t i = 0; i < batches.size(); ++i) {
length += batches[i]->num_rows();
}
} else {
for (int col = 0; col < cols; ++col)
}
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on PR #37896:
URL: https://github.com/apache/arrow/pull/37896#issuecomment-1761950887
CI failure seems unrelated
+1
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "js8544 (via GitHub)" <gi...@apache.org>.
js8544 commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358070444
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
Review Comment:
Will the following change make any difference on the result?
```cpp
Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
const RecordBatchVector& batches, MemoryPool* pool) {
int64_t length = 0;
size_t n = batches.size();
if (n == 0) {
return Status::Invalid("Must pass at least one recordbatch");
}
int cols = batches[0]->num_columns();
auto schema = batches[0]->schema();
for (int i = 0; i < batches.size(); ++i) {
length += batches[i]->num_rows();
if (!schema->Equals(batches[i]->schema())) {
return Status::Invalid(
"Schema of RecordBatch index ", i, " is ", batches[i]->schema()->ToString(),
", which does not match index 0 recordbatch schema: ", schema->ToString());
}
}
std::vector<std::shared_ptr<Array>> concatenated_columns;
for (int col = 0; col < cols; ++col) {
ArrayVector column_arrays;
for (const auto & batch : batches) {
column_arrays.emplace_back(std::move(batch->column(col)));
}
ARROW_ASSIGN_OR_RAISE(auto concatenated_column, Concatenate(column_arrays, pool))
concatenated_columns.emplace_back(std::move(concatenated_column));
}
return RecordBatch::Make(std::move(schema), length, std::move(concatenated_columns));
}
```
I assumed:
1. Schema checking can be moved out of the nested loop.
2. The result length is just the sum of all batch lengths, no matter how many (or zero) columns there are.
3. Each column has the same length in a RecordBatch, so no checking of column length is needed here.
4. Variable names like `column_data`, `data`, `columns` are ambiguous so I changed them to more descriptive names.
5. More data can be std::moved to avoid copy.
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on PR #37896:
URL: https://github.com/apache/arrow/pull/37896#issuecomment-1761437718
> I think the C++ Windows Checks are failing due to this change but I couldn't figure out why...
+1, I am also very confused, mac and linux are compiled correctly
But Windows is
```
[345/901] Linking CXX executable release\arrow-table-test.exe
FAILED: release/arrow-table-test.exe
cmd.exe /C "cd . && D:\a\_temp\msys64\clang64\bin\c++.exe -Qunused-arguments -fcolor-diagnostics -Wa,-mbig-obj -Wall -Wextra -Wdocumentation -Wshorten-64-to-32 -Wno-missing-braces -Wno-unused-parameter -Wno-constant-logical-operand -Wno-return-stack-address -Wdate-time -Wno-unknown-warning-option -Wno-pass-failed -mxsave -msse4.2 -O3 -DNDEBUG -O2 @CMakeFiles\arrow-table-test.rsp -o release\arrow-table-test.exe -Wl,--out-implib,release\libarrow-table-test.dll.a -Wl,--major-image-version,0,--minor-image-version,0 && cd ."
ld.lld: error: undefined symbol: arrow::ConcatenateRecordBatches(std::__1::vector<std::__1::shared_ptr<arrow::RecordBatch>, std::__1::allocator<std::__1::shared_ptr<arrow::RecordBatch>>> const&, arrow::MemoryPool*)
>>> referenced by src/arrow/CMakeFiles/arrow-table-test.dir/record_batch_test.cc.obj:(arrow::TestRecordBatch_ConcatenateRecordBatches_Test::TestBody())
>>> referenced by src/arrow/CMakeFiles/arrow-table-test.dir/record_batch_test.cc.obj:(arrow::TestRecordBatch_ConcatenateRecordBatches_Test::TestBody())
```
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] github-actions[bot] commented on pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #37896:
URL: https://github.com/apache/arrow/pull/37896#issuecomment-1736477818
:warning: GitHub issue #37895 **has been automatically assigned in GitHub** to PR creator.
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] bkietz commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339037926
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
Review Comment:
Instead, please make this deep copy the batch to conform with ConcatenateArrays https://github.com/apache/arrow/issues/37878
##########
cpp/src/arrow/record_batch.h:
##########
@@ -350,4 +350,12 @@ class ARROW_EXPORT RecordBatchReader {
Iterator<std::shared_ptr<RecordBatch>> batches, std::shared_ptr<Schema> schema);
};
+/// \brief Concatenate recordbatches
Review Comment:
Nit: either use "record batches" or "RecordBatch"
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
+ for (int col = 0; col < cols; ++col) {
+ ArrayVector data;
+ for (size_t i = 0; i < batches.size(); ++i) {
+ auto cur_schema = batches[i]->schema();
+ if (!schema->Equals(cur_schema)) {
+ return Status::Invalid(
+ "RecordBatch index ", i, " schema is ", cur_schema->ToString(),
+ ", did not match index 0 recordbatch schema: ", schema->ToString());
+ }
+ auto column_data = batches[i]->column(col);
+ data.push_back(column_data);
+ }
+ auto array = Concatenate(data, pool).ValueOrDie();
+ length = array->length();
+ columns.push_back(array);
+ }
+ }
+ return RecordBatch::Make(std::move(schema), length, columns);
Review Comment:
```suggestion
return RecordBatch::Make(std::move(schema), length, std::move(columns));
```
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
+ for (int col = 0; col < cols; ++col) {
+ ArrayVector data;
+ for (size_t i = 0; i < batches.size(); ++i) {
+ auto cur_schema = batches[i]->schema();
+ if (!schema->Equals(cur_schema)) {
+ return Status::Invalid(
+ "RecordBatch index ", i, " schema is ", cur_schema->ToString(),
+ ", did not match index 0 recordbatch schema: ", schema->ToString());
+ }
+ auto column_data = batches[i]->column(col);
+ data.push_back(column_data);
+ }
+ auto array = Concatenate(data, pool).ValueOrDie();
Review Comment:
Instead of aborting, please raise the error. We have a helper macro for this:
```suggestion
ARROW_ASSIGN_OR_RAISE(auto array, Concatenate(data, pool));
```
##########
cpp/src/arrow/record_batch_test.cc:
##########
@@ -555,4 +555,42 @@ TEST_F(TestRecordBatch, ReplaceSchema) {
ASSERT_RAISES(Invalid, b1->ReplaceSchema(schema));
}
+TEST_F(TestRecordBatch, ConcatenateRecordBatches) {
+ int length = 10;
+
+ auto f0 = field("f0", int32());
+ auto f1 = field("f1", uint8());
+
+ auto schema = ::arrow::schema({f0, f1});
+
+ random::RandomArrayGenerator gen(42);
+
+ auto a0 = gen.ArrayOf(int32(), length);
Review Comment:
I think you could use `gen.BatchOf` instead for less boilerplate
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
Review Comment:
I don't think you need a special case for zero columns:
```suggestion
// special case: null batch, no data, just length
for (size_t i = 0; i < batches.size(); ++i) {
length += batches[i]->num_rows();
}
```
Then the loop `for (int col = 0; col < cols; ++col)` will just never be entered if `cols == 0`
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
+ for (int col = 0; col < cols; ++col) {
+ ArrayVector data;
+ for (size_t i = 0; i < batches.size(); ++i) {
+ auto cur_schema = batches[i]->schema();
+ if (!schema->Equals(cur_schema)) {
+ return Status::Invalid(
+ "RecordBatch index ", i, " schema is ", cur_schema->ToString(),
+ ", did not match index 0 recordbatch schema: ", schema->ToString());
+ }
+ auto column_data = batches[i]->column(col);
+ data.push_back(column_data);
+ }
+ auto array = Concatenate(data, pool).ValueOrDie();
+ length = array->length();
Review Comment:
Instead, please compute the length explicitly before this loop as in the zero columns case
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Light-City commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339399986
##########
cpp/src/arrow/record_batch_test.cc:
##########
@@ -555,4 +555,42 @@ TEST_F(TestRecordBatch, ReplaceSchema) {
ASSERT_RAISES(Invalid, b1->ReplaceSchema(schema));
}
+TEST_F(TestRecordBatch, ConcatenateRecordBatches) {
+ int length = 10;
+
+ auto f0 = field("f0", int32());
+ auto f1 = field("f1", uint8());
+
+ auto schema = ::arrow::schema({f0, f1});
+
+ random::RandomArrayGenerator gen(42);
+
+ auto a0 = gen.ArrayOf(int32(), length);
Review Comment:
This function is really good, I will replace it with
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358346261
##########
cpp/src/arrow/record_batch.h:
##########
@@ -350,4 +350,13 @@ class ARROW_EXPORT RecordBatchReader {
Iterator<std::shared_ptr<RecordBatch>> batches, std::shared_ptr<Schema> schema);
};
+/// \brief Concatenate record batches
+///
+/// \param[in] batches a vector of record batches to be concatenated
+/// \param[in] pool memory to store the result will be allocated from this memory pool
+/// \return the concatenated record batch
Review Comment:
Nit: can we port some comment from `ConcatenateTables`? (And we don't have a `ConcatenateTablesOptions` and force schema equal)
```c++
/// \brief Construct a new table from multiple input tables.
///
/// The new table is assembled from existing column chunks without copying,
/// if schemas are identical. If schemas do not match exactly and
/// unify_schemas is enabled in options (off by default), an attempt is
/// made to unify them, and then column chunks are converted to their
/// respective unified datatype, which will probably incur a copy.
/// :func:`arrow::PromoteTableToSchema` is used to unify schemas.
///
/// Tables are concatenated in order they are provided in and the order of
/// rows within tables will be preserved.
///
/// \param[in] tables a std::vector of Tables to be concatenated
/// \param[in] options specify how to unify schema of input tables
/// \param[in] memory_pool MemoryPool to be used if null-filled arrays need to
/// be created or if existing column chunks need to endure type conversion
/// \return new Table
```
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "mapleFU (via GitHub)" <gi...@apache.org>.
mapleFU commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358276774
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,34 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ if (!schema->Equals(batches[i]->schema())) {
+ return Status::Invalid(
+ "Schema of RecordBatch index ", i, " is ", batches[i]->schema()->ToString(),
+ ", which does not match index 0 recordbatch schema: ", schema->ToString());
+ }
+ }
+
+ std::vector<std::shared_ptr<Array>> concatenated_columns;
Review Comment:
should we call `reserve` here?
##########
cpp/src/arrow/record_batch_test.cc:
##########
@@ -555,4 +555,38 @@ TEST_F(TestRecordBatch, ReplaceSchema) {
ASSERT_RAISES(Invalid, b1->ReplaceSchema(schema));
}
+TEST_F(TestRecordBatch, ConcatenateRecordBatches) {
+ int length = 10;
+
+ auto f0 = field("f0", int32());
+ auto f1 = field("f1", uint8());
+
+ auto schema = ::arrow::schema({f0, f1});
+
+ random::RandomArrayGenerator gen(42);
+
+ auto b1 = gen.BatchOf(schema->fields(), length);
+
+ length = 5;
+
+ auto b2 = gen.BatchOf(schema->fields(), length);
+
+ ASSERT_OK_AND_ASSIGN(auto batch, ConcatenateRecordBatches({b1, b2}));
+ ASSERT_EQ(batch->num_rows(), b1->num_rows() + b2->num_rows());
Review Comment:
Would you mind test content equals `{b1 concat b2}` ? (using `Slice` or `Concatenate` )
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,34 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ if (!schema->Equals(batches[i]->schema())) {
+ return Status::Invalid(
+ "Schema of RecordBatch index ", i, " is ", batches[i]->schema()->ToString(),
+ ", which does not match index 0 recordbatch schema: ", schema->ToString());
+ }
+ }
+
+ std::vector<std::shared_ptr<Array>> concatenated_columns;
+ for (int col = 0; col < cols; ++col) {
+ ArrayVector column_arrays;
Review Comment:
ditto
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on PR #37896:
URL: https://github.com/apache/arrow/pull/37896#issuecomment-1761448774
> I think the C++ Windows Checks are failing due to this change but I couldn't figure out why...
Found that ARROW_EXPORT is missing, this should be the reason
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Light-City commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339396545
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
+ for (int col = 0; col < cols; ++col) {
+ ArrayVector data;
+ for (size_t i = 0; i < batches.size(); ++i) {
+ auto cur_schema = batches[i]->schema();
+ if (!schema->Equals(cur_schema)) {
+ return Status::Invalid(
+ "RecordBatch index ", i, " schema is ", cur_schema->ToString(),
+ ", did not match index 0 recordbatch schema: ", schema->ToString());
+ }
+ auto column_data = batches[i]->column(col);
+ data.push_back(column_data);
+ }
+ auto array = Concatenate(data, pool).ValueOrDie();
+ length = array->length();
+ columns.push_back(array);
+ }
+ }
+ return RecordBatch::Make(std::move(schema), length, columns);
Review Comment:
ok
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Light-City commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339396874
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
+ for (int col = 0; col < cols; ++col) {
+ ArrayVector data;
+ for (size_t i = 0; i < batches.size(); ++i) {
+ auto cur_schema = batches[i]->schema();
+ if (!schema->Equals(cur_schema)) {
+ return Status::Invalid(
+ "RecordBatch index ", i, " schema is ", cur_schema->ToString(),
+ ", did not match index 0 recordbatch schema: ", schema->ToString());
+ }
+ auto column_data = batches[i]->column(col);
+ data.push_back(column_data);
+ }
+ auto array = Concatenate(data, pool).ValueOrDie();
Review Comment:
Indeed, return value handling is missing
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Light-City commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339407325
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
Review Comment:
The reason why the special case of column 0 is processed here is that if we want to implement count(*), then this RecordBatch does not store data, that is, only the length. This scenario is triggered when small batches need to be merged.
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [arrow] Light-City commented on a diff in pull request #37896: GH-37895: [C++] Feature: support concatenate recordbatches.
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1339406024
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
Review Comment:
For a single batch, I removed this processing and let it follow the normal logic below
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358370962
##########
cpp/src/arrow/record_batch.h:
##########
@@ -350,4 +350,13 @@ class ARROW_EXPORT RecordBatchReader {
Iterator<std::shared_ptr<RecordBatch>> batches, std::shared_ptr<Schema> schema);
};
+/// \brief Concatenate record batches
+///
+/// \param[in] batches a vector of record batches to be concatenated
+/// \param[in] pool memory to store the result will be allocated from this memory pool
+/// \return the concatenated record batch
Review Comment:
yes, I add some comment.
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "Light-City (via GitHub)" <gi...@apache.org>.
Light-City commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358106021
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
Review Comment:
Thanks for the modification. This code modification has been submitted.
note:
```
column_arrays.emplace_back(std::move(batch->column(col)));
```
Change to
```
column_arrays.emplace_back(batch->column(col));
```
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "js8544 (via GitHub)" <gi...@apache.org>.
js8544 commented on code in PR #37896:
URL: https://github.com/apache/arrow/pull/37896#discussion_r1358074430
##########
cpp/src/arrow/record_batch_test.cc:
##########
@@ -555,4 +555,33 @@ TEST_F(TestRecordBatch, ReplaceSchema) {
ASSERT_RAISES(Invalid, b1->ReplaceSchema(schema));
}
+TEST_F(TestRecordBatch, ConcatenateRecordBatches) {
+ int length = 10;
+
+ auto f0 = field("f0", int32());
+ auto f1 = field("f1", uint8());
+
+ auto schema = ::arrow::schema({f0, f1});
+
+ random::RandomArrayGenerator gen(42);
+
+ auto b1 = gen.BatchOf(schema->fields(), length);
+
+ length = 5;
+
+ auto b2 = gen.BatchOf(schema->fields(), length);
+
+ ASSERT_OK_AND_ASSIGN(auto batch, ConcatenateRecordBatches({b1, b2}));
+ ASSERT_EQ(batch->num_rows(), b1->num_rows() + b2->num_rows());
+
+ f0 = field("fd0", int32());
+ f1 = field("fd1", uint8());
+
+ schema = ::arrow::schema({f0, f1});
+
+ auto b3 = gen.BatchOf(schema->fields(), length);
+
+ ASSERT_RAISES(Invalid, ConcatenateRecordBatches({b1, b3}));
Review Comment:
It's desirable to have tests on edge cases such as zero-length RecordBatch and zero-column ones.
##########
cpp/src/arrow/record_batch.cc:
##########
@@ -432,4 +433,43 @@ RecordBatchReader::~RecordBatchReader() {
ARROW_WARN_NOT_OK(this->Close(), "Implicitly called RecordBatchReader::Close failed");
}
+Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
+ const RecordBatchVector& batches, MemoryPool* pool) {
+ int64_t length = 0;
+ size_t n = batches.size();
+ if (n == 0) {
+ return Status::Invalid("Must pass at least one recordbatch");
+ }
+ if (n == 1) {
+ return batches[0];
+ }
+ int cols = batches[0]->num_columns();
+ auto schema = batches[0]->schema();
+ std::vector<std::shared_ptr<Array>> columns;
+ if (cols == 0) {
+ // special case: null batch, no data, just length
+ for (size_t i = 0; i < batches.size(); ++i) {
+ length += batches[i]->num_rows();
+ }
+ } else {
Review Comment:
Will the following change make any difference on the result?
```cpp
Result<std::shared_ptr<RecordBatch>> ConcatenateRecordBatches(
const RecordBatchVector& batches, MemoryPool* pool) {
int64_t length = 0;
size_t n = batches.size();
if (n == 0) {
return Status::Invalid("Must pass at least one recordbatch");
}
int cols = batches[0]->num_columns();
auto schema = batches[0]->schema();
for (int i = 0; i < batches.size(); ++i) {
length += batches[i]->num_rows();
if (!schema->Equals(batches[i]->schema())) {
return Status::Invalid(
"Schema of RecordBatch index ", i, " is ", batches[i]->schema()->ToString(),
", which does not match index 0 recordbatch schema: ", schema->ToString());
}
}
std::vector<std::shared_ptr<Array>> concatenated_columns;
for (int col = 0; col < cols; ++col) {
ArrayVector column_arrays;
for (const auto & batch : batches) {
column_arrays.emplace_back(std::move(batch->column(col)));
}
ARROW_ASSIGN_OR_RAISE(auto concatenated_column, Concatenate(column_arrays, pool))
concatenated_columns.emplace_back(std::move(concatenated_column));
}
return RecordBatch::Make(std::move(schema), length, std::move(concatenated_columns));
}
```
I assumed:
1. Schema checking can be moved out of the nested loop.
2. The result length is just the sum of all batch lengths, no matter how many (or zero) columns there are.
3. Each column has the same length in a RecordBatch, so no checking of column length is needed here.
4. Variable names like `column_data`, `data`, `columns` are ambiguous so I changed them to more descriptive names.
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] GH-37895: [C++] Feature: support concatenate recordbatches. [arrow]
Posted by "bkietz (via GitHub)" <gi...@apache.org>.
bkietz merged PR #37896:
URL: https://github.com/apache/arrow/pull/37896
--
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.
To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org