|
| 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 | +#pragma once |
| 21 | + |
| 22 | +/// \file iceberg/file_reader.h |
| 23 | +/// Reader interface for file formats like Parquet, Avro and ORC. |
| 24 | + |
| 25 | +#include <functional> |
| 26 | +#include <memory> |
| 27 | +#include <optional> |
| 28 | +#include <variant> |
| 29 | + |
| 30 | +#include "iceberg/arrow_c_data.h" |
| 31 | +#include "iceberg/file_format.h" |
| 32 | +#include "iceberg/result.h" |
| 33 | +#include "iceberg/type_fwd.h" |
| 34 | + |
| 35 | +namespace iceberg { |
| 36 | + |
| 37 | +/// \brief Base reader class to read data from different file formats. |
| 38 | +class ICEBERG_EXPORT Reader { |
| 39 | + public: |
| 40 | + virtual ~Reader() = default; |
| 41 | + |
| 42 | + /// \brief Read next data from file. |
| 43 | + /// |
| 44 | + /// \return std::monostate if the reader has no more data, otherwise `ArrowArray` or |
| 45 | + /// `StructLike` depending on the data layout by the reader implementation. |
| 46 | + using Data = |
| 47 | + std::variant<std::monostate, ArrowArray, std::reference_wrapper<const StructLike>>; |
| 48 | + virtual Result<Data> Next() = 0; |
| 49 | + |
| 50 | + enum class DataLayout { kArrowArray, kStructLike }; |
| 51 | + |
| 52 | + /// \brief Get the data layout returned by `Next()` of the reader. |
| 53 | + virtual DataLayout data_layout() const = 0; |
| 54 | +}; |
| 55 | + |
| 56 | +/// \brief Wrapper of `Reader` to always return `StructLike`. |
| 57 | +/// |
| 58 | +/// If the data layout of the wrapped reader is `ArrowArray`, the data will be converted |
| 59 | +/// to `StructLike`; otherwise, the data will be returned as is without any cost. |
| 60 | +class ICEBERG_EXPORT StructLikeReader : public Reader { |
| 61 | + public: |
| 62 | + explicit StructLikeReader(std::unique_ptr<Reader> reader); |
| 63 | + |
| 64 | + /// \brief Always read data into `StructLike` or monostate if no more data. |
| 65 | + Result<Data> Next() final; |
| 66 | + |
| 67 | + DataLayout data_layout() const final { return DataLayout::kStructLike; } |
| 68 | +}; |
| 69 | + |
| 70 | +/// \brief Wrapper of `Reader` to always return `ArrowArray`. |
| 71 | +/// |
| 72 | +/// If the data layout of the wrapped reader is `StructLike`, the data will be converted |
| 73 | +/// to `ArrowArray`; otherwise, the data will be returned as is without any cost. |
| 74 | +class ICEBERG_EXPORT BatchReader : public Reader { |
| 75 | + public: |
| 76 | + explicit BatchReader(std::unique_ptr<Reader> reader); |
| 77 | + |
| 78 | + /// \brief Always read data into `ArrowArray` or monostate if no more data. |
| 79 | + Result<Data> Next() final; |
| 80 | + |
| 81 | + DataLayout data_layout() const final { return DataLayout::kArrowArray; } |
| 82 | +}; |
| 83 | + |
| 84 | +/// \brief A split of the file to read. |
| 85 | +struct ICEBERG_EXPORT Split { |
| 86 | + /// \brief The offset of the split. |
| 87 | + size_t offset; |
| 88 | + /// \brief The length of the split. |
| 89 | + size_t length; |
| 90 | +}; |
| 91 | + |
| 92 | +/// \brief Options for creating a reader. |
| 93 | +struct ICEBERG_EXPORT ReaderOptions { |
| 94 | + /// \brief The path to the file to read. |
| 95 | + std::string path; |
| 96 | + /// \brief The total length of the file. |
| 97 | + std::optional<size_t> length; |
| 98 | + /// \brief The split to read. |
| 99 | + std::optional<Split> split; |
| 100 | + /// \brief The batch size to read. Only applies to implementations that support |
| 101 | + /// batching. |
| 102 | + int64_t batch_size; |
| 103 | + /// \brief FileIO instance to open the file. Reader implementations should down cast it |
| 104 | + /// to the specific FileIO implementation. By default, the `iceberg-bundle` library uses |
| 105 | + /// `ArrowFileSystemFileIO` as the default implementation. |
| 106 | + std::shared_ptr<class FileIO> io; |
| 107 | + /// \brief The projection schema to read from the file. |
| 108 | + std::shared_ptr<class Schema> projection; |
| 109 | + /// \brief The filter to apply to the data. Reader implementations may ignore this if |
| 110 | + /// the file format does not support filtering. |
| 111 | + std::shared_ptr<class Expression> filter; |
| 112 | +}; |
| 113 | + |
| 114 | +/// \brief Factory function to create a reader of a specific file format. |
| 115 | +using ReaderFactory = |
| 116 | + std::function<Result<std::unique_ptr<Reader>>(const ReaderOptions&)>; |
| 117 | + |
| 118 | +/// \brief Registry of reader factories for different file formats. |
| 119 | +struct ICEBERG_EXPORT ReaderFactoryRegistry { |
| 120 | + /// \brief Register a factory function for a specific file format. |
| 121 | + ReaderFactoryRegistry(FileFormatType format_type, ReaderFactory factory); |
| 122 | + |
| 123 | + /// \brief Get the factory function for a specific file format. |
| 124 | + static ReaderFactory& GetFactory(FileFormatType format_type); |
| 125 | + |
| 126 | + /// \brief Create a reader for a specific file format. |
| 127 | + static Result<std::unique_ptr<Reader>> Create(FileFormatType format_type, |
| 128 | + const ReaderOptions& options); |
| 129 | +}; |
| 130 | + |
| 131 | +/// \brief Macro to register a reader factory for a specific file format. |
| 132 | +#define ICEBERG_REGISTER_READER_FACTORY(format_type, reader_factory) \ |
| 133 | + static ::iceberg::ReaderFactoryRegistry register_reader_factory_##format_type( \ |
| 134 | + ::iceberg::FileFormatType::k##format_type, reader_factory); |
| 135 | + |
| 136 | +} // namespace iceberg |
0 commit comments