|
| 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 "manifest_reader_internal.h" |
| 21 | + |
| 22 | +#include <array> |
| 23 | + |
| 24 | +#include <nanoarrow/nanoarrow.h> |
| 25 | + |
| 26 | +#include "iceberg/manifest_entry.h" |
| 27 | +#include "iceberg/manifest_list.h" |
| 28 | +#include "iceberg/schema.h" |
| 29 | +#include "iceberg/schema_internal.h" |
| 30 | +#include "iceberg/type.h" |
| 31 | + |
| 32 | +namespace iceberg { |
| 33 | + |
| 34 | +#define ARROW_RETURN_IF_NOT_OK(status, error) \ |
| 35 | + if (status != NANOARROW_OK) { \ |
| 36 | + return InvalidArrowData("NanoArrow error: {}", error.message); \ |
| 37 | + } |
| 38 | + |
| 39 | +Result<std::vector<std::unique_ptr<ManifestFile>>> ParseManifestListEntry( |
| 40 | + ArrowSchema* schema, ArrowArray* array_in, const Schema& iceberg_schema) { |
| 41 | + if (schema->n_children != array_in->n_children) { |
| 42 | + return InvalidArgument("Columns size not match between schema:{} and array:{}", |
| 43 | + schema->n_children, array_in->n_children); |
| 44 | + } |
| 45 | + if (iceberg_schema.fields().size() != array_in->n_children) { |
| 46 | + return InvalidArgument("Columns size not match between schema:{} and array:{}", |
| 47 | + iceberg_schema.fields().size(), array_in->n_children); |
| 48 | + } |
| 49 | + |
| 50 | + ArrowError error; |
| 51 | + ArrowArrayView array_view; |
| 52 | + auto status = ArrowArrayViewInitFromSchema(&array_view, schema, &error); |
| 53 | + ARROW_RETURN_IF_NOT_OK(status, error); |
| 54 | + status = ArrowArrayViewSetArray(&array_view, array_in, &error); |
| 55 | + ARROW_RETURN_IF_NOT_OK(status, error); |
| 56 | + status = ArrowArrayViewValidate(&array_view, NANOARROW_VALIDATION_LEVEL_FULL, &error); |
| 57 | + ARROW_RETURN_IF_NOT_OK(status, error); |
| 58 | + |
| 59 | + std::vector<std::unique_ptr<ManifestFile>> manifest_files; |
| 60 | + manifest_files.resize(array_in->length); |
| 61 | + for (auto& manifest_file : manifest_files) { |
| 62 | + manifest_file = std::make_unique<ManifestFile>(); |
| 63 | + } |
| 64 | + |
| 65 | + for (int64_t idx = 0; idx < array_in->n_children; idx++) { |
| 66 | + const auto& field = iceberg_schema.GetFieldByIndex(idx); |
| 67 | + if (!field.has_value()) { |
| 68 | + ArrowArrayRelease(array_in); |
| 69 | + ArrowArrayViewReset(&array_view); |
| 70 | + return InvalidArgument("Field not found in schema: {}", idx); |
| 71 | + } |
| 72 | + auto field_name = field.value().get().name(); |
| 73 | + auto view_of_column = array_view.children[idx]; |
| 74 | + |
| 75 | +#define PARSE_PRIMITIVE_FIELD(field_name) \ |
| 76 | + for (size_t row_idx = 0; row_idx < view_of_column->length; row_idx++) { \ |
| 77 | + if (!ArrowArrayViewIsNull(view_of_column, row_idx)) { \ |
| 78 | + auto value = ArrowArrayViewGetIntUnsafe(view_of_column, row_idx); \ |
| 79 | + manifest_files[row_idx]->field_name = value; \ |
| 80 | + } \ |
| 81 | + } |
| 82 | + |
| 83 | + if (field_name == ManifestFile::kManifestPath.name()) { |
| 84 | + for (size_t row_idx = 0; row_idx < view_of_column->length; row_idx++) { |
| 85 | + if (!ArrowArrayViewIsNull(view_of_column, row_idx)) { |
| 86 | + auto value = ArrowArrayViewGetStringUnsafe(view_of_column, row_idx); |
| 87 | + std::string path_str(value.data, value.size_bytes); |
| 88 | + manifest_files[row_idx]->manifest_path = path_str; |
| 89 | + } |
| 90 | + } |
| 91 | + } else if (field_name == ManifestFile::kManifestLength.name()) { |
| 92 | + PARSE_PRIMITIVE_FIELD(manifest_length); |
| 93 | + } else if (field_name == ManifestFile::kPartitionSpecId.name()) { |
| 94 | + PARSE_PRIMITIVE_FIELD(partition_spec_id); |
| 95 | + } else if (field_name == ManifestFile::kContent.name()) { |
| 96 | + for (size_t row_idx = 0; row_idx < view_of_column->length; row_idx++) { |
| 97 | + if (!ArrowArrayViewIsNull(view_of_column, row_idx)) { |
| 98 | + auto value = ArrowArrayViewGetIntUnsafe(view_of_column, row_idx); |
| 99 | + manifest_files[row_idx]->content = static_cast<ManifestFile::Content>(value); |
| 100 | + } |
| 101 | + } |
| 102 | + } else if (field_name == ManifestFile::kSequenceNumber.name()) { |
| 103 | + PARSE_PRIMITIVE_FIELD(sequence_number); |
| 104 | + } else if (field_name == ManifestFile::kMinSequenceNumber.name()) { |
| 105 | + PARSE_PRIMITIVE_FIELD(min_sequence_number); |
| 106 | + } else if (field_name == ManifestFile::kAddedSnapshotId.name()) { |
| 107 | + PARSE_PRIMITIVE_FIELD(added_snapshot_id); |
| 108 | + } else if (field_name == ManifestFile::kAddedFilesCount.name()) { |
| 109 | + PARSE_PRIMITIVE_FIELD(added_files_count); |
| 110 | + } else if (field_name == ManifestFile::kExistingFilesCount.name()) { |
| 111 | + PARSE_PRIMITIVE_FIELD(existing_files_count); |
| 112 | + } else if (field_name == ManifestFile::kDeletedFilesCount.name()) { |
| 113 | + PARSE_PRIMITIVE_FIELD(deleted_files_count); |
| 114 | + } else if (field_name == ManifestFile::kAddedRowsCount.name()) { |
| 115 | + PARSE_PRIMITIVE_FIELD(added_rows_count); |
| 116 | + } else if (field_name == ManifestFile::kExistingRowsCount.name()) { |
| 117 | + PARSE_PRIMITIVE_FIELD(existing_rows_count); |
| 118 | + } else if (field_name == ManifestFile::kDeletedRowsCount.name()) { |
| 119 | + PARSE_PRIMITIVE_FIELD(deleted_rows_count); |
| 120 | + } else if (field_name == ManifestFile::kPartitions.name()) { |
| 121 | + // view_of_column is list<struct<PartitionFieldSummary>> |
| 122 | + auto manifest_count = view_of_column->length; |
| 123 | + if (view_of_column->storage_type != ArrowType::NANOARROW_TYPE_LIST) { |
| 124 | + return InvalidArgument("partitions field should be a list."); |
| 125 | + } |
| 126 | + auto view_of_list_iterm = view_of_column->children[0]; |
| 127 | + // view_of_list_iterm is struct<PartitionFieldSummary> |
| 128 | + if (view_of_list_iterm->storage_type != ArrowType::NANOARROW_TYPE_STRUCT) { |
| 129 | + return InvalidArgument("partitions list field should be a list."); |
| 130 | + } |
| 131 | + if (view_of_list_iterm->n_children != 4) { |
| 132 | + return InvalidArgument("PartitionFieldSummary should have 4 fields."); |
| 133 | + } |
| 134 | + if (view_of_list_iterm->children[0]->storage_type != |
| 135 | + ArrowType::NANOARROW_TYPE_BOOL) { |
| 136 | + return InvalidArgument("contains_null should have be bool type column."); |
| 137 | + } |
| 138 | + auto contains_null = view_of_list_iterm->children[0]; |
| 139 | + if (view_of_list_iterm->children[1]->storage_type != |
| 140 | + ArrowType::NANOARROW_TYPE_BOOL) { |
| 141 | + return InvalidArgument("contains_nan should have be bool type column."); |
| 142 | + } |
| 143 | + auto contains_nan = view_of_list_iterm->children[1]; |
| 144 | + if (view_of_list_iterm->children[2]->storage_type != |
| 145 | + ArrowType::NANOARROW_TYPE_BINARY) { |
| 146 | + return InvalidArgument("lower_bound should have be binary type column."); |
| 147 | + } |
| 148 | + auto lower_bound_list = view_of_list_iterm->children[2]; |
| 149 | + if (view_of_list_iterm->children[3]->storage_type != |
| 150 | + ArrowType::NANOARROW_TYPE_BINARY) { |
| 151 | + return InvalidArgument("upper_bound should have be binary type column."); |
| 152 | + } |
| 153 | + auto upper_bound_list = view_of_list_iterm->children[3]; |
| 154 | + for (int64_t manifest_idx = 0; manifest_idx < manifest_count; manifest_idx++) { |
| 155 | + auto offset = ArrowArrayViewListChildOffset(view_of_column, manifest_idx); |
| 156 | + auto next_offset = |
| 157 | + ArrowArrayViewListChildOffset(view_of_column, manifest_idx + 1); |
| 158 | + // partitions from offset to next_offset belongs to manifest_idx |
| 159 | + auto& manifest_file = manifest_files[manifest_idx]; |
| 160 | + for (int64_t partition_idx = offset; partition_idx < next_offset; |
| 161 | + partition_idx++) { |
| 162 | + PartitionFieldSummary partition_field_summary; |
| 163 | + if (!ArrowArrayViewIsNull(contains_null, partition_idx)) { |
| 164 | + partition_field_summary.contains_null = |
| 165 | + ArrowArrayViewGetIntUnsafe(contains_null, partition_idx); |
| 166 | + } |
| 167 | + if (!ArrowArrayViewIsNull(contains_nan, partition_idx)) { |
| 168 | + partition_field_summary.contains_nan = |
| 169 | + ArrowArrayViewGetIntUnsafe(contains_nan, partition_idx); |
| 170 | + } |
| 171 | + if (!ArrowArrayViewIsNull(lower_bound_list, partition_idx)) { |
| 172 | + auto buffer = ArrowArrayViewGetBytesUnsafe(lower_bound_list, partition_idx); |
| 173 | + partition_field_summary.lower_bound = std::vector<uint8_t>( |
| 174 | + buffer.data.as_char, buffer.data.as_char + buffer.size_bytes); |
| 175 | + } |
| 176 | + if (!ArrowArrayViewIsNull(upper_bound_list, partition_idx)) { |
| 177 | + auto buffer = ArrowArrayViewGetBytesUnsafe(upper_bound_list, partition_idx); |
| 178 | + partition_field_summary.upper_bound = std::vector<uint8_t>( |
| 179 | + buffer.data.as_char, buffer.data.as_char + buffer.size_bytes); |
| 180 | + } |
| 181 | + |
| 182 | + manifest_file->partitions.emplace_back(partition_field_summary); |
| 183 | + } |
| 184 | + } |
| 185 | + } else if (field_name == ManifestFile::kKeyMetadata.name()) { |
| 186 | + for (size_t row_idx = 0; row_idx < view_of_column->length; row_idx++) { |
| 187 | + if (!ArrowArrayViewIsNull(view_of_column, row_idx)) { |
| 188 | + auto value = ArrowArrayViewGetUIntUnsafe(view_of_column, row_idx); |
| 189 | + manifest_files[row_idx]->key_metadata.push_back(value); |
| 190 | + } |
| 191 | + } |
| 192 | + } else if (field_name == ManifestFile::kFirstRowId.name()) { |
| 193 | + PARSE_PRIMITIVE_FIELD(first_row_id); |
| 194 | + } else { |
| 195 | + return InvalidArgument("Unsupported type: {}", field_name); |
| 196 | + } |
| 197 | + } |
| 198 | +#undef PARSE_PRIMITIVE_FIELD |
| 199 | + ArrowArrayRelease(array_in); |
| 200 | + ArrowArrayViewReset(&array_view); |
| 201 | + return manifest_files; |
| 202 | +} // namespace iceberg |
| 203 | + |
| 204 | +Result<std::vector<std::unique_ptr<ManifestEntry>>> ManifestReaderImpl::Entries() const { |
| 205 | + return {}; |
| 206 | +} |
| 207 | + |
| 208 | +Result<std::vector<std::unique_ptr<ManifestFile>>> ManifestListReaderImpl::Files() const { |
| 209 | + std::vector<std::unique_ptr<ManifestFile>> manifest_files; |
| 210 | + auto arrow_schema = reader_->Schema(); |
| 211 | + if (!arrow_schema.has_value()) { |
| 212 | + return InvalidArgument("Get schema failed in reader:{}", |
| 213 | + arrow_schema.error().message); |
| 214 | + } |
| 215 | + auto schema = FromArrowSchema(arrow_schema.value(), std::nullopt); |
| 216 | + if (!schema.has_value()) { |
| 217 | + return InvalidArgument("Parse iceberg schema failed:{}", schema.error().message); |
| 218 | + } |
| 219 | + while (true) { |
| 220 | + auto result = reader_->Next(); |
| 221 | + if (!result.has_value()) { |
| 222 | + return InvalidArgument("Failed to read manifest list entry:{}", |
| 223 | + result.error().message); |
| 224 | + } |
| 225 | + if (result.value().has_value()) { |
| 226 | + auto parse_result = ParseManifestListEntry( |
| 227 | + &arrow_schema.value(), &result.value().value(), *schema.value()); |
| 228 | + if (!parse_result.has_value()) { |
| 229 | + return InvalidArgument("Failed to parse manifest list entry:{}", |
| 230 | + parse_result.error().message); |
| 231 | + } |
| 232 | + manifest_files.insert(manifest_files.end(), |
| 233 | + std::make_move_iterator(parse_result.value().begin()), |
| 234 | + std::make_move_iterator(parse_result.value().end())); |
| 235 | + } else { |
| 236 | + break; |
| 237 | + } |
| 238 | + } |
| 239 | + return manifest_files; |
| 240 | +} |
| 241 | + |
| 242 | +} // namespace iceberg |
0 commit comments