|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "iceberg/avro/avro_writer.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | + |
| 24 | +#include <arrow/array/builder_base.h> |
| 25 | +#include <arrow/c/bridge.h> |
| 26 | +#include <arrow/record_batch.h> |
| 27 | +#include <arrow/result.h> |
| 28 | +#include <avro/DataFile.hh> |
| 29 | +#include <avro/GenericDatum.hh> |
| 30 | + |
| 31 | +#include "iceberg/arrow/arrow_error_transform_internal.h" |
| 32 | +#include "iceberg/arrow/arrow_fs_file_io_internal.h" |
| 33 | +#include "iceberg/avro/avro_schema_util_internal.h" |
| 34 | +#include "iceberg/avro/avro_stream_internal.h" |
| 35 | +#include "iceberg/schema.h" |
| 36 | +#include "iceberg/util/checked_cast.h" |
| 37 | +#include "iceberg/util/macros.h" |
| 38 | + |
| 39 | +namespace iceberg::avro { |
| 40 | + |
| 41 | +namespace { |
| 42 | + |
| 43 | +Result<std::unique_ptr<AvroOutputStream>> CreateOutputStream(const WriterOptions& options, |
| 44 | + int64_t buffer_size) { |
| 45 | + auto io = internal::checked_pointer_cast<arrow::ArrowFileSystemFileIO>(options.io); |
| 46 | + ICEBERG_ARROW_ASSIGN_OR_RETURN(auto output, io->fs()->OpenOutputStream(options.path)); |
| 47 | + return std::make_unique<AvroOutputStream>(output, buffer_size); |
| 48 | +} |
| 49 | + |
| 50 | +} // namespace |
| 51 | + |
| 52 | +// A stateful context to keep track of the writing progress. |
| 53 | +struct WriteContext {}; |
| 54 | + |
| 55 | +class AvroWriter::Impl { |
| 56 | + public: |
| 57 | + Status Open(const WriterOptions& options) { |
| 58 | + write_schema_ = options.schema; |
| 59 | + |
| 60 | + ::avro::NodePtr root; |
| 61 | + ICEBERG_RETURN_UNEXPECTED(ToAvroNodeVisitor{}.Visit(*write_schema_, &root)); |
| 62 | + |
| 63 | + avro_schema_ = std::make_shared<::avro::ValidSchema>(root); |
| 64 | + |
| 65 | + // Open the output stream and adapt to the avro interface. |
| 66 | + constexpr int64_t kDefaultBufferSize = 1024 * 1024; |
| 67 | + ICEBERG_ASSIGN_OR_RAISE(auto output_stream, |
| 68 | + CreateOutputStream(options, kDefaultBufferSize)); |
| 69 | + |
| 70 | + writer_ = std::make_unique<::avro::DataFileWriter<::avro::GenericDatum>>( |
| 71 | + std::move(output_stream), *avro_schema_); |
| 72 | + return {}; |
| 73 | + } |
| 74 | + |
| 75 | + Status Write(ArrowArray /*data*/) { |
| 76 | + // TODO(xiao.dong) convert data and write to avro |
| 77 | + // total_bytes_+= written_bytes; |
| 78 | + return {}; |
| 79 | + } |
| 80 | + |
| 81 | + Status Close() { |
| 82 | + if (writer_ != nullptr) { |
| 83 | + writer_->close(); |
| 84 | + writer_.reset(); |
| 85 | + } |
| 86 | + return {}; |
| 87 | + } |
| 88 | + |
| 89 | + bool Closed() const { return writer_ == nullptr; } |
| 90 | + |
| 91 | + int64_t length() { return total_bytes_; } |
| 92 | + |
| 93 | + private: |
| 94 | + int64_t total_bytes_ = 0; |
| 95 | + // The schema to write. |
| 96 | + std::shared_ptr<::iceberg::Schema> write_schema_; |
| 97 | + // The avro schema to write. |
| 98 | + std::shared_ptr<::avro::ValidSchema> avro_schema_; |
| 99 | + // The avro writer to write the data into a datum. |
| 100 | + std::unique_ptr<::avro::DataFileWriter<::avro::GenericDatum>> writer_; |
| 101 | +}; |
| 102 | + |
| 103 | +AvroWriter::~AvroWriter() = default; |
| 104 | + |
| 105 | +Status AvroWriter::Write(ArrowArray data) { return impl_->Write(data); } |
| 106 | + |
| 107 | +Status AvroWriter::Open(const WriterOptions& options) { |
| 108 | + impl_ = std::make_unique<Impl>(); |
| 109 | + return impl_->Open(options); |
| 110 | +} |
| 111 | + |
| 112 | +Status AvroWriter::Close() { |
| 113 | + if (!impl_->Closed()) { |
| 114 | + return impl_->Close(); |
| 115 | + } |
| 116 | + return {}; |
| 117 | +} |
| 118 | + |
| 119 | +std::optional<Metrics> AvroWriter::metrics() { |
| 120 | + if (impl_->Closed()) { |
| 121 | + // TODO(xiao.dong) implement metrics |
| 122 | + return {}; |
| 123 | + } |
| 124 | + return std::nullopt; |
| 125 | +} |
| 126 | + |
| 127 | +std::optional<int64_t> AvroWriter::length() { |
| 128 | + if (impl_->Closed()) { |
| 129 | + return impl_->length(); |
| 130 | + } |
| 131 | + return std::nullopt; |
| 132 | +} |
| 133 | + |
| 134 | +std::vector<int64_t> AvroWriter::split_offsets() { return {}; } |
| 135 | + |
| 136 | +void AvroWriter::Register() { |
| 137 | + static WriterFactoryRegistry avro_writer_register( |
| 138 | + FileFormatType::kAvro, |
| 139 | + []() -> Result<std::unique_ptr<Writer>> { return std::make_unique<AvroWriter>(); }); |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace iceberg::avro |
0 commit comments