|
19 | 19 |
|
20 | 20 | #include "iceberg/data/data_writer.h" |
21 | 21 |
|
| 22 | +#include "iceberg/file_writer.h" |
| 23 | +#include "iceberg/manifest/manifest_entry.h" |
| 24 | +#include "iceberg/util/macros.h" |
| 25 | + |
22 | 26 | namespace iceberg { |
23 | 27 |
|
24 | 28 | class DataWriter::Impl { |
25 | 29 | public: |
| 30 | + static Result<std::unique_ptr<Impl>> Make(DataWriterOptions options) { |
| 31 | + WriterOptions writer_options; |
| 32 | + writer_options.path = options.path; |
| 33 | + writer_options.schema = options.schema; |
| 34 | + writer_options.io = options.io; |
| 35 | + writer_options.properties = WriterProperties::FromMap(options.properties); |
| 36 | + |
| 37 | + ICEBERG_ASSIGN_OR_RAISE( |
| 38 | + auto writer, WriterFactoryRegistry::Open(options.format, writer_options)); |
| 39 | + |
| 40 | + return std::unique_ptr<Impl>( |
| 41 | + new Impl(std::move(options), std::move(writer))); |
| 42 | + } |
| 43 | + |
| 44 | + Status Write(ArrowArray* data) { |
| 45 | + if (!writer_) { |
| 46 | + return InvalidArgument("Writer not initialized"); |
| 47 | + } |
| 48 | + return writer_->Write(data); |
| 49 | + } |
| 50 | + |
| 51 | + Result<int64_t> Length() const { |
| 52 | + if (!writer_) { |
| 53 | + return InvalidArgument("Writer not initialized"); |
| 54 | + } |
| 55 | + return writer_->length(); |
| 56 | + } |
| 57 | + |
| 58 | + Status Close() { |
| 59 | + if (!writer_) { |
| 60 | + return InvalidArgument("Writer not initialized"); |
| 61 | + } |
| 62 | + if (closed_) { |
| 63 | + // Idempotent: no-op if already closed |
| 64 | + return {}; |
| 65 | + } |
| 66 | + ICEBERG_RETURN_UNEXPECTED(writer_->Close()); |
| 67 | + closed_ = true; |
| 68 | + return {}; |
| 69 | + } |
| 70 | + |
| 71 | + Result<FileWriter::WriteResult> Metadata() { |
| 72 | + if (!closed_) { |
| 73 | + return InvalidArgument("Cannot get metadata before closing the writer"); |
| 74 | + } |
| 75 | + |
| 76 | + ICEBERG_ASSIGN_OR_RAISE(auto metrics, writer_->metrics()); |
| 77 | + ICEBERG_ASSIGN_OR_RAISE(auto length, writer_->length()); |
| 78 | + auto split_offsets = writer_->split_offsets(); |
| 79 | + |
| 80 | + auto data_file = std::make_shared<DataFile>(); |
| 81 | + data_file->content = DataFile::Content::kData; |
| 82 | + data_file->file_path = options_.path; |
| 83 | + data_file->file_format = options_.format; |
| 84 | + data_file->partition = options_.partition; |
| 85 | + data_file->record_count = metrics.row_count.value_or(0); |
| 86 | + data_file->file_size_in_bytes = length; |
| 87 | + data_file->sort_order_id = options_.sort_order_id; |
| 88 | + data_file->split_offsets = std::move(split_offsets); |
| 89 | + |
| 90 | + // Convert metrics maps from unordered_map to map |
| 91 | + for (const auto& [col_id, size] : metrics.column_sizes) { |
| 92 | + data_file->column_sizes[col_id] = size; |
| 93 | + } |
| 94 | + for (const auto& [col_id, count] : metrics.value_counts) { |
| 95 | + data_file->value_counts[col_id] = count; |
| 96 | + } |
| 97 | + for (const auto& [col_id, count] : metrics.null_value_counts) { |
| 98 | + data_file->null_value_counts[col_id] = count; |
| 99 | + } |
| 100 | + for (const auto& [col_id, count] : metrics.nan_value_counts) { |
| 101 | + data_file->nan_value_counts[col_id] = count; |
| 102 | + } |
| 103 | + |
| 104 | + // Serialize literal bounds to binary format |
| 105 | + for (const auto& [col_id, literal] : metrics.lower_bounds) { |
| 106 | + ICEBERG_ASSIGN_OR_RAISE(auto serialized, literal.Serialize()); |
| 107 | + data_file->lower_bounds[col_id] = std::move(serialized); |
| 108 | + } |
| 109 | + for (const auto& [col_id, literal] : metrics.upper_bounds) { |
| 110 | + ICEBERG_ASSIGN_OR_RAISE(auto serialized, literal.Serialize()); |
| 111 | + data_file->upper_bounds[col_id] = std::move(serialized); |
| 112 | + } |
| 113 | + |
| 114 | + FileWriter::WriteResult result; |
| 115 | + result.data_files.push_back(std::move(data_file)); |
| 116 | + return result; |
| 117 | + } |
| 118 | + |
| 119 | + private: |
| 120 | + Impl(DataWriterOptions options, std::unique_ptr<Writer> writer) |
| 121 | + : options_(std::move(options)), writer_(std::move(writer)) {} |
| 122 | + |
| 123 | + DataWriterOptions options_; |
| 124 | + std::unique_ptr<Writer> writer_; |
| 125 | + bool closed_ = false; |
26 | 126 | }; |
27 | 127 |
|
| 128 | +DataWriter::DataWriter(std::unique_ptr<Impl> impl) : impl_(std::move(impl)) {} |
| 129 | + |
28 | 130 | DataWriter::~DataWriter() = default; |
29 | 131 |
|
30 | | -Status DataWriter::Write(ArrowArray* data) { return NotImplemented(""); } |
| 132 | +Result<std::unique_ptr<DataWriter>> DataWriter::Make(const DataWriterOptions& options) { |
| 133 | + ICEBERG_ASSIGN_OR_RAISE(auto impl, Impl::Make(options)); |
| 134 | + return std::unique_ptr<DataWriter>(new DataWriter(std::move(impl))); |
| 135 | +} |
| 136 | + |
| 137 | +Status DataWriter::Write(ArrowArray* data) { return impl_->Write(data); } |
31 | 138 |
|
32 | | -Result<int64_t> DataWriter::Length() const { return NotImplemented(""); } |
| 139 | +Result<int64_t> DataWriter::Length() const { return impl_->Length(); } |
33 | 140 |
|
34 | | -Status DataWriter::Close() { return NotImplemented(""); } |
| 141 | +Status DataWriter::Close() { return impl_->Close(); } |
35 | 142 |
|
36 | | -Result<FileWriter::WriteResult> DataWriter::Metadata() { return NotImplemented(""); } |
| 143 | +Result<FileWriter::WriteResult> DataWriter::Metadata() { return impl_->Metadata(); } |
37 | 144 |
|
38 | 145 | } // namespace iceberg |
0 commit comments