-
Notifications
You must be signed in to change notification settings - Fork 70
feat: implement basic parquet writer and add roundtrip tests #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+473
−35
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
31c06e5
basic support parquet writer
HuaHuaY eb8d8b9
add some tests
HuaHuaY e4cc3e2
fix some review comments
HuaHuaY 5e630c4
implement length() and split_offsets()
HuaHuaY e7b2bbf
fix length() and pass length() to reader_options
HuaHuaY 02a05cc
fix review
HuaHuaY 117b480
fix review
HuaHuaY a036f82
fix review
HuaHuaY 30d1559
fix review
HuaHuaY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,3 +23,6 @@ cmake-build-release/ | |
|
|
||
| # intellij files | ||
| .idea | ||
|
|
||
| # vscode files | ||
| .vscode | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <string_view> | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| constexpr std::string_view kParquetFieldIdKey = "PARQUET:field_id"; | ||
|
|
||
| } // namespace iceberg |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #include "iceberg/parquet/parquet_writer.h" | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include <arrow/c/bridge.h> | ||
| #include <arrow/record_batch.h> | ||
| #include <arrow/util/key_value_metadata.h> | ||
| #include <parquet/arrow/schema.h> | ||
| #include <parquet/arrow/writer.h> | ||
| #include <parquet/file_writer.h> | ||
| #include <parquet/properties.h> | ||
|
|
||
| #include "iceberg/arrow/arrow_error_transform_internal.h" | ||
| #include "iceberg/arrow/arrow_fs_file_io_internal.h" | ||
| #include "iceberg/schema_internal.h" | ||
| #include "iceberg/util/checked_cast.h" | ||
| #include "iceberg/util/macros.h" | ||
|
|
||
| namespace iceberg::parquet { | ||
|
|
||
| namespace { | ||
|
|
||
| Result<std::shared_ptr<::arrow::io::OutputStream>> OpenOutputStream( | ||
| const WriterOptions& options) { | ||
| auto io = internal::checked_pointer_cast<arrow::ArrowFileSystemFileIO>(options.io); | ||
| ICEBERG_ARROW_ASSIGN_OR_RETURN(auto output, io->fs()->OpenOutputStream(options.path)); | ||
| return output; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| class ParquetWriter::Impl { | ||
| public: | ||
| Status Open(const WriterOptions& options) { | ||
| auto writer_properties = | ||
| ::parquet::WriterProperties::Builder().memory_pool(pool_)->build(); | ||
| auto arrow_writer_properties = ::parquet::default_arrow_writer_properties(); | ||
|
|
||
| ArrowSchema c_schema; | ||
| ICEBERG_RETURN_UNEXPECTED(ToArrowSchema(*options.schema, &c_schema)); | ||
| ICEBERG_ARROW_ASSIGN_OR_RETURN(arrow_schema_, ::arrow::ImportSchema(&c_schema)); | ||
|
|
||
| std::shared_ptr<::parquet::SchemaDescriptor> schema_descriptor; | ||
| ICEBERG_ARROW_RETURN_NOT_OK( | ||
| ::parquet::arrow::ToParquetSchema(arrow_schema_.get(), *writer_properties, | ||
| *arrow_writer_properties, &schema_descriptor)); | ||
| auto schema_node = std::static_pointer_cast<::parquet::schema::GroupNode>( | ||
| schema_descriptor->schema_root()); | ||
|
|
||
| ICEBERG_ASSIGN_OR_RAISE(output_stream_, OpenOutputStream(options)); | ||
| auto file_writer = ::parquet::ParquetFileWriter::Open(output_stream_, schema_node, | ||
| writer_properties); | ||
| ICEBERG_ARROW_RETURN_NOT_OK(::parquet::arrow::FileWriter::Make( | ||
| pool_, std::move(file_writer), arrow_schema_, arrow_writer_properties, &writer_)); | ||
HuaHuaY marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| Status Write(ArrowArray array) { | ||
| ICEBERG_ARROW_ASSIGN_OR_RETURN(auto batch, | ||
| ::arrow::ImportRecordBatch(&array, arrow_schema_)); | ||
|
|
||
| ICEBERG_ARROW_RETURN_NOT_OK(writer_->WriteRecordBatch(*batch)); | ||
|
|
||
| return {}; | ||
| } | ||
|
|
||
| // Close the writer and release resources | ||
| Status Close() { | ||
| if (writer_ == nullptr) { | ||
| return {}; // Already closed | ||
| } | ||
|
|
||
| ICEBERG_ARROW_RETURN_NOT_OK(writer_->Close()); | ||
| auto& metadata = writer_->metadata(); | ||
| split_offsets_.reserve(metadata->num_row_groups()); | ||
| for (int i = 0; i < metadata->num_row_groups(); ++i) { | ||
| split_offsets_.push_back(metadata->RowGroup(i)->file_offset()); | ||
| } | ||
| writer_.reset(); | ||
|
|
||
| ICEBERG_ARROW_ASSIGN_OR_RETURN(total_bytes_, output_stream_->Tell()); | ||
| ICEBERG_ARROW_RETURN_NOT_OK(output_stream_->Close()); | ||
HuaHuaY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return {}; | ||
| } | ||
|
|
||
| bool Closed() const { return writer_ == nullptr; } | ||
|
|
||
| int64_t length() const { return total_bytes_; } | ||
|
|
||
| std::vector<int64_t> split_offsets() const { return split_offsets_; } | ||
|
|
||
| private: | ||
| // TODO(gangwu): make memory pool configurable | ||
| ::arrow::MemoryPool* pool_ = ::arrow::default_memory_pool(); | ||
| // Schema to write from the Parquet file. | ||
| std::shared_ptr<::arrow::Schema> arrow_schema_; | ||
| // The output stream to write Parquet file. | ||
| std::shared_ptr<::arrow::io::OutputStream> output_stream_; | ||
HuaHuaY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Parquet file writer to write ArrowArray. | ||
| std::unique_ptr<::parquet::arrow::FileWriter> writer_; | ||
| // Total length of the written Parquet file. | ||
| int64_t total_bytes_; | ||
HuaHuaY marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // Row group start offsets in the Parquet file. | ||
| std::vector<int64_t> split_offsets_; | ||
| }; | ||
|
|
||
| ParquetWriter::~ParquetWriter() = default; | ||
|
|
||
| Status ParquetWriter::Open(const WriterOptions& options) { | ||
| impl_ = std::make_unique<Impl>(); | ||
| return impl_->Open(options); | ||
| } | ||
|
|
||
| Status ParquetWriter::Write(ArrowArray array) { return impl_->Write(array); } | ||
|
|
||
| Status ParquetWriter::Close() { return impl_->Close(); } | ||
|
|
||
| std::optional<Metrics> ParquetWriter::metrics() { | ||
| if (!impl_->Closed()) { | ||
| return std::nullopt; | ||
| } | ||
| return {}; | ||
HuaHuaY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| std::optional<int64_t> ParquetWriter::length() { | ||
HuaHuaY marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!impl_->Closed()) { | ||
| return std::nullopt; | ||
| } | ||
| return impl_->length(); | ||
| } | ||
|
|
||
| std::vector<int64_t> ParquetWriter::split_offsets() { | ||
| if (!impl_->Closed()) { | ||
| return {}; | ||
| } | ||
| return impl_->split_offsets(); | ||
| } | ||
|
|
||
| void RegisterWriter() { | ||
| static WriterFactoryRegistry parquet_writer_register( | ||
| FileFormatType::kParquet, []() -> Result<std::unique_ptr<Writer>> { | ||
| return std::make_unique<ParquetWriter>(); | ||
| }); | ||
| } | ||
|
|
||
| } // namespace iceberg::parquet | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "iceberg/file_writer.h" | ||
| #include "iceberg/iceberg_bundle_export.h" | ||
|
|
||
| namespace iceberg::parquet { | ||
|
|
||
| /// \brief A writer that writes ArrowArray to Parquet files. | ||
| class ICEBERG_BUNDLE_EXPORT ParquetWriter : public Writer { | ||
| public: | ||
| ParquetWriter() = default; | ||
|
|
||
| ~ParquetWriter() override; | ||
|
|
||
| Status Open(const WriterOptions& options) final; | ||
|
|
||
| Status Close() final; | ||
|
|
||
| Status Write(ArrowArray array) final; | ||
|
|
||
| std::optional<Metrics> metrics() final; | ||
|
|
||
| std::optional<int64_t> length() final; | ||
|
|
||
| std::vector<int64_t> split_offsets() final; | ||
|
|
||
| private: | ||
| class Impl; | ||
| std::unique_ptr<Impl> impl_; | ||
| }; | ||
|
|
||
| } // namespace iceberg::parquet |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.