|
| 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 | +#include <avro/NodeImpl.hh> |
| 31 | + |
| 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/schema_internal.h" |
| 37 | +#include "iceberg/util/checked_cast.h" |
| 38 | +#include "iceberg/util/macros.h" |
| 39 | + |
| 40 | +namespace iceberg::avro { |
| 41 | + |
| 42 | +namespace { |
| 43 | + |
| 44 | +Result<std::unique_ptr<AvroOutputStream>> CreateOutputStream(const WriterOptions& options, |
| 45 | + int64_t buffer_size) { |
| 46 | + auto io = internal::checked_pointer_cast<arrow::ArrowFileSystemFileIO>(options.io); |
| 47 | + auto result = io->fs()->OpenOutputStream(options.path); |
| 48 | + if (!result.ok()) { |
| 49 | + return IOError("Failed to open file {} for {}", options.path, |
| 50 | + result.status().message()); |
| 51 | + } |
| 52 | + return std::make_unique<AvroOutputStream>(result.MoveValueUnsafe(), buffer_size); |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace |
| 56 | + |
| 57 | +// A stateful context to keep track of the writing progress. |
| 58 | +struct WriteContext {}; |
| 59 | + |
| 60 | +class AvroWriter::Impl { |
| 61 | + public: |
| 62 | + Status Open(const WriterOptions& options) { |
| 63 | + write_arrow_schema_ = options.schema; |
| 64 | + ICEBERG_ASSIGN_OR_RAISE(write_schema_, FromArrowSchema(options.schema, std::nullopt)); |
| 65 | + |
| 66 | + auto root = std::make_shared<::avro::NodeRecord>(); |
| 67 | + ToAvroNodeVisitor visitor; |
| 68 | + for (const auto& field : write_schema_->fields()) { |
| 69 | + ::avro::NodePtr node; |
| 70 | + ICEBERG_RETURN_UNEXPECTED(visitor.Visit(field, &node)); |
| 71 | + root->addLeaf(node); |
| 72 | + } |
| 73 | + avro_schema_ = std::make_shared<::avro::ValidSchema>(root); |
| 74 | + |
| 75 | + // Open the output stream and adapt to the avro interface. |
| 76 | + constexpr int64_t kDefaultBufferSize = 1024 * 1024; |
| 77 | + ICEBERG_ASSIGN_OR_RAISE(auto output_stream, |
| 78 | + CreateOutputStream(options, kDefaultBufferSize)); |
| 79 | + |
| 80 | + writer_ = std::make_unique<::avro::DataFileWriter<::avro::GenericDatum>>( |
| 81 | + std::move(output_stream), *avro_schema_); |
| 82 | + return {}; |
| 83 | + } |
| 84 | + |
| 85 | + Status Write(ArrowArray /*data*/) { |
| 86 | + if (!context_) { |
| 87 | + ICEBERG_RETURN_UNEXPECTED(InitWriteContext()); |
| 88 | + } |
| 89 | + // TODO(xiao.dong) convert data and write to avro |
| 90 | + return {}; |
| 91 | + } |
| 92 | + |
| 93 | + Status Close() { |
| 94 | + if (writer_ != nullptr) { |
| 95 | + writer_->close(); |
| 96 | + writer_.reset(); |
| 97 | + } |
| 98 | + context_.reset(); |
| 99 | + return {}; |
| 100 | + } |
| 101 | + |
| 102 | + private: |
| 103 | + Status InitWriteContext() { return {}; } |
| 104 | + |
| 105 | + private: |
| 106 | + ArrowSchema write_arrow_schema_; |
| 107 | + // The schema to write. |
| 108 | + std::shared_ptr<::iceberg::Schema> write_schema_; |
| 109 | + // The avro schema to write. |
| 110 | + std::shared_ptr<::avro::ValidSchema> avro_schema_; |
| 111 | + // The avro writer to write the data into a datum. |
| 112 | + std::unique_ptr<::avro::DataFileWriter<::avro::GenericDatum>> writer_; |
| 113 | + // The context to keep track of the writing progress. |
| 114 | + std::unique_ptr<WriteContext> context_; |
| 115 | +}; |
| 116 | + |
| 117 | +AvroWriter::~AvroWriter() = default; |
| 118 | + |
| 119 | +Status AvroWriter::Write(ArrowArray data) { return impl_->Write(data); } |
| 120 | + |
| 121 | +Status AvroWriter::Open(const WriterOptions& options) { |
| 122 | + impl_ = std::make_unique<Impl>(); |
| 123 | + return impl_->Open(options); |
| 124 | +} |
| 125 | + |
| 126 | +Status AvroWriter::Close() { return impl_->Close(); } |
| 127 | + |
| 128 | +void AvroWriter::Register() { |
| 129 | + static WriterFactoryRegistry avro_writer_register( |
| 130 | + FileFormatType::kAvro, |
| 131 | + []() -> Result<std::unique_ptr<Writer>> { return std::make_unique<AvroWriter>(); }); |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace iceberg::avro |
0 commit comments