|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "paimon/core/io/arrow_ipc_file.h" |
| 20 | + |
| 21 | +#include <utility> |
| 22 | + |
| 23 | +#include "arrow/ipc/api.h" |
| 24 | +#include "arrow/util/compression.h" |
| 25 | +#include "paimon/common/utils/arrow/arrow_input_stream_adapter.h" |
| 26 | +#include "paimon/common/utils/arrow/arrow_output_stream_adapter.h" |
| 27 | +#include "paimon/common/utils/arrow/arrow_utils.h" |
| 28 | +#include "paimon/common/utils/arrow/status_utils.h" |
| 29 | +#include "paimon/common/utils/scope_guard.h" |
| 30 | +#include "paimon/fs/file_system.h" |
| 31 | +#include "paimon/macros.h" |
| 32 | + |
| 33 | +namespace paimon { |
| 34 | + |
| 35 | +ArrowIpcFileWriter::ArrowIpcFileWriter(const std::shared_ptr<FileSystem>& fs, std::string path, |
| 36 | + const std::shared_ptr<arrow::Schema>& schema, |
| 37 | + std::string compression, int32_t compression_level, |
| 38 | + bool use_threads, |
| 39 | + const std::shared_ptr<arrow::MemoryPool>& arrow_pool) |
| 40 | + : fs_(fs), |
| 41 | + path_(std::move(path)), |
| 42 | + schema_(schema), |
| 43 | + compression_(std::move(compression)), |
| 44 | + compression_level_(compression_level), |
| 45 | + use_threads_(use_threads), |
| 46 | + arrow_pool_(arrow_pool) {} |
| 47 | + |
| 48 | +ArrowIpcFileWriter::~ArrowIpcFileWriter() { |
| 49 | + [[maybe_unused]] Status status = Close(); |
| 50 | +} |
| 51 | + |
| 52 | +Result<std::unique_ptr<ArrowIpcFileWriter>> ArrowIpcFileWriter::Create( |
| 53 | + const std::shared_ptr<FileSystem>& fs, const std::string& path, |
| 54 | + const std::shared_ptr<arrow::Schema>& schema, const std::string& compression, |
| 55 | + int32_t compression_level, bool use_threads, |
| 56 | + const std::shared_ptr<arrow::MemoryPool>& arrow_pool) { |
| 57 | + if (!fs || !schema || !arrow_pool || path.empty()) { |
| 58 | + return Status::Invalid("invalid Arrow IPC file writer arguments"); |
| 59 | + } |
| 60 | + std::unique_ptr<ArrowIpcFileWriter> writer(new ArrowIpcFileWriter( |
| 61 | + fs, path, schema, compression, compression_level, use_threads, arrow_pool)); |
| 62 | + PAIMON_RETURN_NOT_OK(writer->Open()); |
| 63 | + return writer; |
| 64 | +} |
| 65 | + |
| 66 | +Status ArrowIpcFileWriter::Open() { |
| 67 | + auto cleanup_guard = ScopeGuard([&]() { |
| 68 | + arrow_writer_.reset(); |
| 69 | + arrow_output_stream_adapter_.reset(); |
| 70 | + if (out_stream_) { |
| 71 | + [[maybe_unused]] Status status = out_stream_->Close(); |
| 72 | + out_stream_.reset(); |
| 73 | + } |
| 74 | + [[maybe_unused]] Status status = fs_->Delete(path_); |
| 75 | + }); |
| 76 | + auto write_options = arrow::ipc::IpcWriteOptions::Defaults(); |
| 77 | + write_options.memory_pool = arrow_pool_.get(); |
| 78 | + write_options.use_threads = use_threads_; |
| 79 | + PAIMON_ASSIGN_OR_RAISE(arrow::Compression::type arrow_compression, |
| 80 | + ArrowUtils::GetCompressionType(compression_)); |
| 81 | + if (!arrow::util::Codec::SupportsCompressionLevel(arrow_compression)) { |
| 82 | + compression_level_ = arrow::util::Codec::UseDefaultCompressionLevel(); |
| 83 | + } |
| 84 | + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW( |
| 85 | + write_options.codec, arrow::util::Codec::Create(arrow_compression, compression_level_)); |
| 86 | + PAIMON_ASSIGN_OR_RAISE(out_stream_, fs_->Create(path_, /*overwrite=*/false)); |
| 87 | + arrow_output_stream_adapter_ = std::make_shared<ArrowOutputStreamAdapter>(out_stream_); |
| 88 | + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW( |
| 89 | + arrow_writer_, |
| 90 | + arrow::ipc::MakeFileWriter(arrow_output_stream_adapter_, schema_, write_options)); |
| 91 | + cleanup_guard.Release(); |
| 92 | + return Status::OK(); |
| 93 | +} |
| 94 | + |
| 95 | +Status ArrowIpcFileWriter::WriteBatch(const std::shared_ptr<arrow::RecordBatch>& batch) { |
| 96 | + if (closed_ || !arrow_writer_) { |
| 97 | + return Status::Invalid("Arrow IPC file writer is closed"); |
| 98 | + } |
| 99 | + if (!batch) { |
| 100 | + return Status::Invalid("Arrow IPC record batch is null"); |
| 101 | + } |
| 102 | + PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow_writer_->WriteRecordBatch(*batch)); |
| 103 | + return Status::OK(); |
| 104 | +} |
| 105 | + |
| 106 | +Status ArrowIpcFileWriter::Close() { |
| 107 | + if (closed_) { |
| 108 | + return Status::OK(); |
| 109 | + } |
| 110 | + if (arrow_writer_) { |
| 111 | + PAIMON_RETURN_NOT_OK_FROM_ARROW(arrow_writer_->Close()); |
| 112 | + arrow_writer_.reset(); |
| 113 | + } |
| 114 | + if (out_stream_) { |
| 115 | + PAIMON_RETURN_NOT_OK(out_stream_->Close()); |
| 116 | + out_stream_.reset(); |
| 117 | + } |
| 118 | + closed_ = true; |
| 119 | + return Status::OK(); |
| 120 | +} |
| 121 | + |
| 122 | +Result<int64_t> ArrowIpcFileWriter::GetFileSize() const { |
| 123 | + if (!closed_ && arrow_output_stream_adapter_) { |
| 124 | + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(int64_t size, arrow_output_stream_adapter_->Tell()); |
| 125 | + return size; |
| 126 | + } |
| 127 | + PAIMON_ASSIGN_OR_RAISE(FileStatus status, fs_->GetFileStatus(path_)); |
| 128 | + return status.GetLen(); |
| 129 | +} |
| 130 | + |
| 131 | +ArrowIpcFileReader::ArrowIpcFileReader(const std::shared_ptr<FileSystem>& fs, std::string path, |
| 132 | + bool use_threads, |
| 133 | + const std::shared_ptr<arrow::MemoryPool>& arrow_pool) |
| 134 | + : fs_(fs), path_(std::move(path)), use_threads_(use_threads), arrow_pool_(arrow_pool) {} |
| 135 | + |
| 136 | +ArrowIpcFileReader::~ArrowIpcFileReader() { |
| 137 | + Close(); |
| 138 | +} |
| 139 | + |
| 140 | +Result<std::unique_ptr<ArrowIpcFileReader>> ArrowIpcFileReader::Open( |
| 141 | + const std::shared_ptr<FileSystem>& fs, const std::string& path, bool use_threads, |
| 142 | + const std::shared_ptr<arrow::MemoryPool>& arrow_pool) { |
| 143 | + if (!fs || !arrow_pool || path.empty()) { |
| 144 | + return Status::Invalid("invalid Arrow IPC file reader arguments"); |
| 145 | + } |
| 146 | + std::unique_ptr<ArrowIpcFileReader> reader( |
| 147 | + new ArrowIpcFileReader(fs, path, use_threads, arrow_pool)); |
| 148 | + PAIMON_RETURN_NOT_OK(reader->DoOpen()); |
| 149 | + return reader; |
| 150 | +} |
| 151 | + |
| 152 | +Status ArrowIpcFileReader::DoOpen() { |
| 153 | + PAIMON_ASSIGN_OR_RAISE(in_stream_, fs_->Open(path_)); |
| 154 | + PAIMON_ASSIGN_OR_RAISE(FileStatus status, fs_->GetFileStatus(path_)); |
| 155 | + arrow_input_stream_adapter_ = |
| 156 | + std::make_shared<ArrowInputStreamAdapter>(in_stream_, status.GetLen(), arrow_pool_); |
| 157 | + auto read_options = arrow::ipc::IpcReadOptions::Defaults(); |
| 158 | + read_options.memory_pool = arrow_pool_.get(); |
| 159 | + read_options.use_threads = use_threads_; |
| 160 | + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW( |
| 161 | + arrow_reader_, |
| 162 | + arrow::ipc::RecordBatchFileReader::Open(arrow_input_stream_adapter_, read_options)); |
| 163 | + record_batch_count_ = arrow_reader_->num_record_batches(); |
| 164 | + return Status::OK(); |
| 165 | +} |
| 166 | + |
| 167 | +int32_t ArrowIpcFileReader::GetRecordBatchCount() const { |
| 168 | + return record_batch_count_; |
| 169 | +} |
| 170 | + |
| 171 | +Result<std::shared_ptr<arrow::RecordBatch>> ArrowIpcFileReader::ReadRecordBatch( |
| 172 | + int32_t batch_index) { |
| 173 | + if (closed_ || !arrow_reader_) { |
| 174 | + return Status::Invalid("Arrow IPC file reader is closed"); |
| 175 | + } |
| 176 | + if (batch_index < 0 || batch_index >= record_batch_count_) { |
| 177 | + return Status::Invalid("Arrow IPC record batch index out of range: ", batch_index); |
| 178 | + } |
| 179 | + PAIMON_ASSIGN_OR_RAISE_FROM_ARROW(std::shared_ptr<arrow::RecordBatch> batch, |
| 180 | + arrow_reader_->ReadRecordBatch(batch_index)); |
| 181 | + return batch; |
| 182 | +} |
| 183 | + |
| 184 | +void ArrowIpcFileReader::Close() { |
| 185 | + if (closed_) { |
| 186 | + return; |
| 187 | + } |
| 188 | + arrow_reader_.reset(); |
| 189 | + arrow_input_stream_adapter_.reset(); |
| 190 | + if (in_stream_) { |
| 191 | + [[maybe_unused]] Status status = in_stream_->Close(); |
| 192 | + in_stream_.reset(); |
| 193 | + } |
| 194 | + closed_ = true; |
| 195 | +} |
| 196 | + |
| 197 | +} // namespace paimon |
0 commit comments