| 392 | std::shared_ptr<::arrow::Schema> schema() const override { return schema_; } |
| 393 | |
| 394 | Status WriteTable(const Table& table, int64_t chunk_size) override { |
| 395 | RETURN_NOT_OK(CheckClosed()); |
| 396 | RETURN_NOT_OK(table.Validate()); |
| 397 | |
| 398 | if (chunk_size <= 0 && table.num_rows() > 0) { |
| 399 | return Status::Invalid("chunk size per row_group must be greater than 0"); |
| 400 | } else if (!table.schema()->Equals(*schema_, false)) { |
| 401 | return Status::Invalid("table schema does not match this writer's. table:'", |
| 402 | table.schema()->ToString(), "' this:'", schema_->ToString(), |
| 403 | "'"); |
| 404 | } else if (chunk_size > this->properties().max_row_group_length()) { |
| 405 | chunk_size = this->properties().max_row_group_length(); |
| 406 | } |
| 407 | |
| 408 | auto WriteRowGroup = [&](int64_t offset, int64_t size) { |
| 409 | RETURN_NOT_OK(NewRowGroup()); |
| 410 | for (int i = 0; i < table.num_columns(); i++) { |
| 411 | RETURN_NOT_OK(WriteColumnChunk(table.column(i), offset, size)); |
| 412 | } |
| 413 | return Status::OK(); |
| 414 | }; |
| 415 | |
| 416 | if (table.num_rows() == 0) { |
| 417 | // Append a row group with 0 rows |
| 418 | RETURN_NOT_OK( |
| 419 | WriteRowGroup(0, 0).OrElse([&](auto&&) { PARQUET_IGNORE_NOT_OK(Close()); })); |
| 420 | } |
| 421 | |
| 422 | for (int chunk = 0; chunk * chunk_size < table.num_rows(); chunk++) { |
| 423 | int64_t offset = chunk * chunk_size; |
| 424 | RETURN_NOT_OK(WriteRowGroup(offset, std::min(chunk_size, table.num_rows() - offset)) |
| 425 | .OrElse([&](auto&&) { PARQUET_IGNORE_NOT_OK(Close()); })); |
| 426 | } |
| 427 | return Status::OK(); |
| 428 | } |
| 429 | |
| 430 | Status NewBufferedRowGroup() override { |
| 431 | RETURN_NOT_OK(CheckClosed()); |