Skip to content

Commit b7fadf5

Browse files
authored
chore: consolidate Avro and Parquet register api (#191)
1 parent 58d926f commit b7fadf5

18 files changed

+121
-41
lines changed

example/demo_example.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919

2020
#include <iostream>
2121

22-
#include "iceberg/avro/avro_reader.h"
22+
#include "iceberg/avro/avro_register.h"
2323
#include "iceberg/file_reader.h"
24+
#include "iceberg/parquet/parquet_register.h"
2425

2526
int main() {
26-
iceberg::avro::AvroReader::Register();
27+
iceberg::avro::RegisterAll();
28+
iceberg::parquet::RegisterAll();
2729
auto open_result = iceberg::ReaderFactoryRegistry::Open(
2830
iceberg::FileFormatType::kAvro, {.path = "non-existing-file.avro"});
2931
if (!open_result.has_value()) {

src/iceberg/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,12 @@ if(ICEBERG_BUILD_BUNDLE)
109109
avro/avro_data_util.cc
110110
avro/avro_reader.cc
111111
avro/avro_writer.cc
112-
avro/avro_schema_util.cc
113112
avro/avro_register.cc
113+
avro/avro_schema_util.cc
114114
avro/avro_stream_internal.cc
115115
parquet/parquet_data_util.cc
116116
parquet/parquet_reader.cc
117+
parquet/parquet_register.cc
117118
parquet/parquet_schema_util.cc)
118119

119120
# Libraries to link with exported libiceberg_bundle.{so,a}.

src/iceberg/avro/avro_reader.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "iceberg/arrow/arrow_error_transform_internal.h"
3535
#include "iceberg/arrow/arrow_fs_file_io_internal.h"
3636
#include "iceberg/avro/avro_data_util_internal.h"
37+
#include "iceberg/avro/avro_register.h"
3738
#include "iceberg/avro/avro_schema_util_internal.h"
3839
#include "iceberg/avro/avro_stream_internal.h"
3940
#include "iceberg/name_mapping.h"
@@ -247,7 +248,7 @@ Status AvroReader::Open(const ReaderOptions& options) {
247248

248249
Status AvroReader::Close() { return impl_->Close(); }
249250

250-
void AvroReader::Register() {
251+
void RegisterReader() {
251252
static ReaderFactoryRegistry avro_reader_register(
252253
FileFormatType::kAvro,
253254
[]() -> Result<std::unique_ptr<Reader>> { return std::make_unique<AvroReader>(); });

src/iceberg/avro/avro_reader.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ class ICEBERG_BUNDLE_EXPORT AvroReader : public Reader {
3939

4040
Result<ArrowSchema> Schema() final;
4141

42-
/// \brief Register this Avro reader implementation.
43-
static void Register();
44-
4542
private:
4643
class Impl;
4744
std::unique_ptr<Impl> impl_;

src/iceberg/avro/avro_register.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424
namespace iceberg::avro {
2525

2626
void RegisterLogicalTypes() {
27-
static std::once_flag flag{};
28-
std::call_once(flag, []() {
29-
// Register the map logical type with the avro custom logical type registry.
30-
// See https://github.com/apache/avro/pull/3326 for details.
31-
::avro::CustomLogicalTypeRegistry::instance().registerType(
32-
"map", [](const std::string&) { return std::make_shared<MapLogicalType>(); });
33-
});
27+
// Register the map logical type with the avro custom logical type registry.
28+
// See https://github.com/apache/avro/pull/3326 for details.
29+
::avro::CustomLogicalTypeRegistry::instance().registerType(
30+
"map", [](const std::string&) { return std::make_shared<MapLogicalType>(); });
31+
}
32+
33+
void RegisterAll() {
34+
RegisterLogicalTypes();
35+
RegisterReader();
36+
RegisterWriter();
3437
}
3538

3639
} // namespace iceberg::avro

src/iceberg/avro/avro_register.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,23 @@
1919

2020
#pragma once
2121

22+
/// \file iceberg/avro/avro_register.h
23+
/// \brief Provide functions to register Avro implementations.
24+
2225
#include "iceberg/iceberg_bundle_export.h"
2326

2427
namespace iceberg::avro {
2528

29+
/// \brief Register all the logical types.
2630
ICEBERG_BUNDLE_EXPORT void RegisterLogicalTypes();
2731

32+
/// \brief Register Avro reader implementation.
33+
ICEBERG_BUNDLE_EXPORT void RegisterReader();
34+
35+
/// \brief Register Avro writer implementation.
36+
ICEBERG_BUNDLE_EXPORT void RegisterWriter();
37+
38+
/// \brief Register all the logical types, Avro reader, and Avro writer.
39+
ICEBERG_BUNDLE_EXPORT void RegisterAll();
40+
2841
} // namespace iceberg::avro

src/iceberg/avro/avro_schema_util.cc

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,7 @@ namespace iceberg::avro {
4545

4646
namespace {
4747

48-
constexpr std::string_view kIcebergFieldNameProp = "iceberg-field-name";
49-
constexpr std::string_view kFieldIdProp = "field-id";
50-
constexpr std::string_view kKeyIdProp = "key-id";
51-
constexpr std::string_view kValueIdProp = "value-id";
52-
constexpr std::string_view kElementIdProp = "element-id";
53-
constexpr std::string_view kAdjustToUtcProp = "adjust-to-utc";
54-
5548
::avro::LogicalType GetMapLogicalType() {
56-
RegisterLogicalTypes();
5749
return ::avro::LogicalType(std::make_shared<MapLogicalType>());
5850
}
5951

src/iceberg/avro/avro_writer.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
#include "iceberg/arrow/arrow_error_transform_internal.h"
3232
#include "iceberg/arrow/arrow_fs_file_io_internal.h"
33+
#include "iceberg/avro/avro_register.h"
3334
#include "iceberg/avro/avro_schema_util_internal.h"
3435
#include "iceberg/avro/avro_stream_internal.h"
3536
#include "iceberg/schema.h"
@@ -133,7 +134,7 @@ std::optional<int64_t> AvroWriter::length() {
133134

134135
std::vector<int64_t> AvroWriter::split_offsets() { return {}; }
135136

136-
void AvroWriter::Register() {
137+
void RegisterWriter() {
137138
static WriterFactoryRegistry avro_writer_register(
138139
FileFormatType::kAvro,
139140
[]() -> Result<std::unique_ptr<Writer>> { return std::make_unique<AvroWriter>(); });

src/iceberg/avro/avro_writer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ class ICEBERG_BUNDLE_EXPORT AvroWriter : public Writer {
4343

4444
std::vector<int64_t> split_offsets() final;
4545

46-
/// \brief Register this Avro writer implementation.
47-
static void Register();
48-
4946
private:
5047
class Impl;
5148
std::unique_ptr<Impl> impl_;

src/iceberg/avro/constants.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,12 @@ constexpr std::string_view kElement = "element";
3131
constexpr std::string_view kKey = "key";
3232
constexpr std::string_view kValue = "value";
3333

34+
// Avro custom attributes constants
35+
constexpr std::string_view kIcebergFieldNameProp = "iceberg-field-name";
36+
constexpr std::string_view kFieldIdProp = "field-id";
37+
constexpr std::string_view kKeyIdProp = "key-id";
38+
constexpr std::string_view kValueIdProp = "value-id";
39+
constexpr std::string_view kElementIdProp = "element-id";
40+
constexpr std::string_view kAdjustToUtcProp = "adjust-to-utc";
41+
3442
} // namespace iceberg::avro

0 commit comments

Comments
 (0)