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