dongxiao1198 commented on code in PR #216: URL: https://github.com/apache/iceberg-cpp/pull/216#discussion_r2422427303
########## src/iceberg/manifest_adapter.cc: ########## @@ -0,0 +1,685 @@ +/* + * 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/manifest_adapter.h" + +#include <nanoarrow/nanoarrow.h> + +#include "iceberg/arrow/nanoarrow_error_transform_internal.h" +#include "iceberg/manifest_entry.h" +#include "iceberg/manifest_list.h" +#include "iceberg/schema.h" +#include "iceberg/schema_internal.h" +#include "iceberg/util/checked_cast.h" +#include "iceberg/util/macros.h" + +#define NANOARROW_RETURN_IF_FAILED(status) \ + if (status != NANOARROW_OK) [[unlikely]] { \ + return InvalidArrowData("nanoarrow error: {}", status); \ + } + +namespace { +static constexpr int64_t kBlockSizeInBytesV1 = 64 * 1024 * 1024L; +} + +namespace iceberg { + +Status ManifestAdapter::StartAppending() { + if (size_ > 0) { + return InvalidArgument("Adapter buffer not empty, cannot start appending."); + } + array_ = {}; + size_ = 0; + ArrowError error; + ICEBERG_NANOARROW_RETURN_IF_NOT_OK(ArrowArrayInitFromSchema(&array_, &schema_, &error), + error); + NANOARROW_RETURN_IF_FAILED(ArrowArrayStartAppending(&array_)); + return {}; +} + +Result<ArrowArray*> ManifestAdapter::FinishAppending() { + ArrowError error; + ICEBERG_NANOARROW_RETURN_IF_NOT_OK(ArrowArrayFinishBuildingDefault(&array_, &error), + error); + return &array_; +} + +Status ManifestAdapter::AppendField(ArrowArray* arrowArray, int64_t value) { + NANOARROW_RETURN_IF_FAILED(ArrowArrayAppendInt(arrowArray, value)); + return {}; +} + +Status ManifestAdapter::AppendField(ArrowArray* arrowArray, uint64_t value) { + NANOARROW_RETURN_IF_FAILED(ArrowArrayAppendUInt(arrowArray, value)); + return {}; +} + +Status ManifestAdapter::AppendField(ArrowArray* arrowArray, double value) { + NANOARROW_RETURN_IF_FAILED(ArrowArrayAppendDouble(arrowArray, value)); + return {}; +} + +Status ManifestAdapter::AppendField(ArrowArray* arrowArray, std::string_view value) { + ArrowStringView view(value.data(), value.size()); + NANOARROW_RETURN_IF_FAILED(ArrowArrayAppendString(arrowArray, view)); + return {}; +} + +Status ManifestAdapter::AppendField(ArrowArray* arrowArray, + const std::span<const uint8_t>& value) { + ArrowBufferViewData data; + data.as_char = reinterpret_cast<const char*>(value.data()); + ArrowBufferView view(data, value.size()); + NANOARROW_RETURN_IF_FAILED(ArrowArrayAppendBytes(arrowArray, view)); + return {}; +} + +ManifestEntryAdapter::~ManifestEntryAdapter() { + if (array_.release != nullptr) { + ArrowArrayRelease(&array_); + } + if (schema_.release != nullptr) { + ArrowSchemaRelease(&schema_); + } +} + +Result<std::shared_ptr<StructType>> ManifestEntryAdapter::GetManifestEntryStructType() { + if (partition_spec_ == nullptr) { + return ManifestEntry::TypeFromPartitionType(nullptr); + } + ICEBERG_ASSIGN_OR_RAISE(auto partition_schema, partition_spec_->GetPartitionSchema()); + return ManifestEntry::TypeFromPartitionType(std::move(partition_schema)); +} + +Status ManifestEntryAdapter::AppendPartition( + ArrowArray* arrow_array, const std::shared_ptr<StructType>& partition_type, + const std::vector<Literal>& partitions) { + if (arrow_array->n_children != partition_type->fields().size()) [[unlikely]] { + return InvalidManifest("Arrow array of partition does not match partition type."); + } + if (partitions.size() != partition_type->fields().size()) [[unlikely]] { + return InvalidManifest("Literal list of partition does not match partition type."); + } + auto fields = partition_type->fields(); + + for (int32_t i = 0; i < fields.size(); i++) { + const auto& partition = partitions[i]; + const auto& field = fields[i]; + auto array = arrow_array->children[i]; + if (partition.IsNull()) { + NANOARROW_RETURN_IF_FAILED(ArrowArrayAppendNull(array, 1)); + continue; + } + switch (field.type()->type_id()) { Review Comment: current case was based on the previous reader case. I have added a TODO to add more reader writer case together -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
