| 597 | } |
| 598 | |
| 599 | Result<std::shared_ptr<Table>> Table::CombineChunks(MemoryPool* pool) const { |
| 600 | const int ncolumns = num_columns(); |
| 601 | std::vector<std::shared_ptr<ChunkedArray>> compacted_columns(ncolumns); |
| 602 | for (int i = 0; i < ncolumns; ++i) { |
| 603 | const auto& col = column(i); |
| 604 | if (col->num_chunks() <= 1) { |
| 605 | compacted_columns[i] = col; |
| 606 | continue; |
| 607 | } |
| 608 | |
| 609 | if (is_binary_like(col->type()->id())) { |
| 610 | // ARROW-5744 Allow binary columns to be combined into multiple chunks to avoid |
| 611 | // buffer overflow |
| 612 | ArrayVector chunks; |
| 613 | int chunk_i = 0; |
| 614 | while (chunk_i < col->num_chunks()) { |
| 615 | ArrayVector safe_chunks; |
| 616 | int64_t data_length = 0; |
| 617 | for (; chunk_i < col->num_chunks(); ++chunk_i) { |
| 618 | const auto& chunk = col->chunk(chunk_i); |
| 619 | data_length += checked_cast<const BinaryArray&>(*chunk).total_values_length(); |
| 620 | if (data_length >= kBinaryMemoryLimit) { |
| 621 | break; |
| 622 | } |
| 623 | safe_chunks.push_back(chunk); |
| 624 | } |
| 625 | chunks.emplace_back(); |
| 626 | ARROW_ASSIGN_OR_RAISE(chunks.back(), Concatenate(safe_chunks, pool)); |
| 627 | } |
| 628 | compacted_columns[i] = std::make_shared<ChunkedArray>(std::move(chunks)); |
| 629 | } else { |
| 630 | ARROW_ASSIGN_OR_RAISE(auto compacted, Concatenate(col->chunks(), pool)); |
| 631 | compacted_columns[i] = std::make_shared<ChunkedArray>(compacted); |
| 632 | } |
| 633 | } |
| 634 | return Table::Make(schema(), std::move(compacted_columns), num_rows_); |
| 635 | } |
| 636 | |
| 637 | Result<std::shared_ptr<RecordBatch>> Table::CombineChunksToBatch(MemoryPool* pool) const { |
| 638 | ARROW_ASSIGN_OR_RAISE(std::shared_ptr<Table> combined, CombineChunks(pool)); |