|
| 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 | +#include "iceberg/inspect/row_builder_internal.h" |
| 21 | + |
| 22 | +#include <utility> |
| 23 | + |
| 24 | +#include <nanoarrow/nanoarrow.h> |
| 25 | + |
| 26 | +#include "iceberg/arrow/nanoarrow_status_internal.h" |
| 27 | +#include "iceberg/arrow_c_data_guard_internal.h" |
| 28 | +#include "iceberg/schema.h" |
| 29 | +#include "iceberg/schema_internal.h" |
| 30 | + |
| 31 | +namespace iceberg { |
| 32 | + |
| 33 | +Result<ArrowRowBuilder> ArrowRowBuilder::Make(const Schema& schema) { |
| 34 | + ArrowSchema arrow_schema; |
| 35 | + ICEBERG_RETURN_UNEXPECTED(ToArrowSchema(schema, &arrow_schema)); |
| 36 | + internal::ArrowSchemaGuard schema_guard(&arrow_schema); |
| 37 | + |
| 38 | + auto array = std::make_unique<ArrowArray>(); |
| 39 | + ArrowError error; |
| 40 | + ICEBERG_NANOARROW_RETURN_UNEXPECTED_WITH_ERROR( |
| 41 | + ArrowArrayInitFromSchema(array.get(), &arrow_schema, &error), error); |
| 42 | + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayStartAppending(array.get())); |
| 43 | + |
| 44 | + return ArrowRowBuilder(std::move(array)); |
| 45 | +} |
| 46 | + |
| 47 | +ArrowRowBuilder::ArrowRowBuilder(std::unique_ptr<ArrowArray>&& array) noexcept |
| 48 | + : array_(std::move(array)) {} |
| 49 | + |
| 50 | +ArrowRowBuilder::ArrowRowBuilder(ArrowRowBuilder&& other) noexcept |
| 51 | + : array_(std::move(other.array_)) {} |
| 52 | + |
| 53 | +ArrowRowBuilder& ArrowRowBuilder::operator=(ArrowRowBuilder&& other) noexcept { |
| 54 | + if (this != &other) { |
| 55 | + if (array_ != nullptr && array_->release != nullptr) { |
| 56 | + ArrowArrayRelease(array_.get()); |
| 57 | + } |
| 58 | + array_ = std::move(other.array_); |
| 59 | + } |
| 60 | + return *this; |
| 61 | +} |
| 62 | + |
| 63 | +ArrowRowBuilder::~ArrowRowBuilder() { |
| 64 | + if (array_ != nullptr && array_->release != nullptr) { |
| 65 | + ArrowArrayRelease(array_.get()); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +int64_t ArrowRowBuilder::num_columns() const { |
| 70 | + return array_ == nullptr ? 0 : array_->n_children; |
| 71 | +} |
| 72 | + |
| 73 | +ArrowArray* ArrowRowBuilder::column(int64_t index) { |
| 74 | + if (array_ == nullptr || index < 0 || index >= array_->n_children) { |
| 75 | + return nullptr; |
| 76 | + } |
| 77 | + return array_->children[index]; |
| 78 | +} |
| 79 | + |
| 80 | +Status ArrowRowBuilder::FinishRow() { |
| 81 | + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayFinishElement(array_.get())); |
| 82 | + return {}; |
| 83 | +} |
| 84 | + |
| 85 | +Result<ArrowArray> ArrowRowBuilder::Finish() && { |
| 86 | + ArrowError error; |
| 87 | + ICEBERG_NANOARROW_RETURN_UNEXPECTED_WITH_ERROR( |
| 88 | + ArrowArrayFinishBuildingDefault(array_.get(), &error), error); |
| 89 | + ArrowArray result = *array_; |
| 90 | + array_->release = nullptr; |
| 91 | + return result; |
| 92 | +} |
| 93 | + |
| 94 | +Status AppendNull(ArrowArray* array) { |
| 95 | + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayAppendNull(array, 1)); |
| 96 | + return {}; |
| 97 | +} |
| 98 | + |
| 99 | +Status AppendBoolean(ArrowArray* array, bool value) { |
| 100 | + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayAppendInt(array, value ? 1 : 0)); |
| 101 | + return {}; |
| 102 | +} |
| 103 | + |
| 104 | +Status AppendInt(ArrowArray* array, int64_t value) { |
| 105 | + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayAppendInt(array, value)); |
| 106 | + return {}; |
| 107 | +} |
| 108 | + |
| 109 | +Status AppendString(ArrowArray* array, std::string_view value) { |
| 110 | + ArrowStringView view(value.data(), static_cast<int64_t>(value.size())); |
| 111 | + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayAppendString(array, view)); |
| 112 | + return {}; |
| 113 | +} |
| 114 | + |
| 115 | +Status AppendStringMap(ArrowArray* array, |
| 116 | + const std::unordered_map<std::string, std::string>& entries) { |
| 117 | + // A nanoarrow map array is a list of struct<key, value>. children[0] is the |
| 118 | + // entries struct, whose children[0]/children[1] are the key/value builders. |
| 119 | + ArrowArray* struct_array = array->children[0]; |
| 120 | + ArrowArray* key_array = struct_array->children[0]; |
| 121 | + ArrowArray* value_array = struct_array->children[1]; |
| 122 | + |
| 123 | + for (const auto& [key, value] : entries) { |
| 124 | + ICEBERG_RETURN_UNEXPECTED(AppendString(key_array, key)); |
| 125 | + ICEBERG_RETURN_UNEXPECTED(AppendString(value_array, value)); |
| 126 | + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayFinishElement(struct_array)); |
| 127 | + } |
| 128 | + |
| 129 | + // Finish the (possibly empty) map element on the outer list. |
| 130 | + ICEBERG_NANOARROW_RETURN_UNEXPECTED(ArrowArrayFinishElement(array)); |
| 131 | + return {}; |
| 132 | +} |
| 133 | + |
| 134 | +} // namespace iceberg |
0 commit comments