|
| 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 | +#pragma once |
| 21 | + |
| 22 | +#include <cassert> |
| 23 | +#include <cstdint> |
| 24 | +#include <list> |
| 25 | +#include <memory> |
| 26 | +#include <optional> |
| 27 | +#include <string> |
| 28 | +#include <utility> |
| 29 | +#include <vector> |
| 30 | + |
| 31 | +#include "paimon/common/data/internal_array.h" |
| 32 | +#include "paimon/common/data/internal_row.h" |
| 33 | +#include "paimon/common/utils/linked_hash_map.h" |
| 34 | +#include "paimon/core/index/deletion_vector_meta.h" |
| 35 | +#include "paimon/core/index/index_file_meta.h" |
| 36 | +#include "paimon/core/index/index_file_meta_v2_deserializer.h" |
| 37 | +#include "paimon/core/utils/object_serializer.h" |
| 38 | +#include "paimon/result.h" |
| 39 | + |
| 40 | +namespace paimon { |
| 41 | +class MemoryPool; |
| 42 | + |
| 43 | +class IndexFileMetaV4Deserializer : public ObjectSerializer<std::shared_ptr<IndexFileMeta>> { |
| 44 | + public: |
| 45 | + static const std::shared_ptr<arrow::DataType>& GlobalIndexMetaDataType() { |
| 46 | + static std::shared_ptr<arrow::DataType> schema = arrow::struct_({ |
| 47 | + arrow::field("_ROW_RANGE_START", arrow::int64(), /*nullable=*/false), |
| 48 | + arrow::field("_ROW_RANGE_END", arrow::int64(), /*nullable=*/false), |
| 49 | + arrow::field("_INDEX_FIELD_ID", arrow::int32(), /*nullable=*/false), |
| 50 | + arrow::field("_EXTRA_FIELD_IDS", |
| 51 | + arrow::list(arrow::field("item", arrow::int32(), /*nullable=*/false)), |
| 52 | + /*nullable=*/true), |
| 53 | + arrow::field("_INDEX_META", arrow::binary(), /*nullable=*/true), |
| 54 | + }); |
| 55 | + return schema; |
| 56 | + } |
| 57 | + |
| 58 | + static const std::shared_ptr<arrow::DataType>& DataType() { |
| 59 | + static std::shared_ptr<arrow::DataType> schema = arrow::struct_({ |
| 60 | + arrow::field("_INDEX_TYPE", arrow::utf8(), false), |
| 61 | + arrow::field("_FILE_NAME", arrow::utf8(), false), |
| 62 | + arrow::field("_FILE_SIZE", arrow::int64(), false), |
| 63 | + arrow::field("_ROW_COUNT", arrow::int64(), false), |
| 64 | + arrow::field("_DELETIONS_VECTORS_RANGES", |
| 65 | + arrow::list(arrow::field("item", DeletionVectorMeta::DataType(), true)), |
| 66 | + true), |
| 67 | + arrow::field("_EXTERNAL_PATH", arrow::utf8(), true), |
| 68 | + arrow::field("_GLOBAL_INDEX", GlobalIndexMetaDataType(), true), |
| 69 | + }); |
| 70 | + return schema; |
| 71 | + } |
| 72 | + |
| 73 | + explicit IndexFileMetaV4Deserializer(const std::shared_ptr<MemoryPool>& pool) |
| 74 | + : ObjectSerializer<std::shared_ptr<IndexFileMeta>>(DataType(), pool) {} |
| 75 | + |
| 76 | + Result<BinaryRow> ToRow(const std::shared_ptr<IndexFileMeta>& meta) const override { |
| 77 | + assert(false); |
| 78 | + return Status::Invalid("IndexFileMetaV4Deserializer to row is not valid"); |
| 79 | + } |
| 80 | + |
| 81 | + Result<std::shared_ptr<IndexFileMeta>> FromRow(const InternalRow& row) const override { |
| 82 | + auto file_type = row.GetString(0); |
| 83 | + auto file_name = row.GetString(1); |
| 84 | + auto file_size = row.GetLong(2); |
| 85 | + auto row_count = row.GetLong(3); |
| 86 | + std::optional<LinkedHashMap<std::string, DeletionVectorMeta>> dv_ranges; |
| 87 | + if (!row.IsNullAt(4)) { |
| 88 | + dv_ranges = IndexFileMetaV2Deserializer::RowArrayDataToDvRanges(row.GetArray(4).get()); |
| 89 | + } |
| 90 | + std::optional<std::string> external_path; |
| 91 | + if (!row.IsNullAt(5)) { |
| 92 | + external_path = row.GetString(5).ToString(); |
| 93 | + } |
| 94 | + std::optional<GlobalIndexMeta> global_index_meta; |
| 95 | + if (!row.IsNullAt(6)) { |
| 96 | + std::shared_ptr<InternalRow> global_index_meta_row = |
| 97 | + row.GetRow(6, GlobalIndexMetaDataType()->num_fields()); |
| 98 | + assert(global_index_meta_row); |
| 99 | + int64_t row_range_start = global_index_meta_row->GetLong(0); |
| 100 | + int64_t row_range_end = global_index_meta_row->GetLong(1); |
| 101 | + int32_t index_field_id = global_index_meta_row->GetInt(2); |
| 102 | + std::optional<std::vector<int32_t>> extra_field_ids; |
| 103 | + if (!global_index_meta_row->IsNullAt(3)) { |
| 104 | + std::shared_ptr<InternalArray> array = global_index_meta_row->GetArray(3); |
| 105 | + if (!array) { |
| 106 | + return Status::Invalid( |
| 107 | + "GlobalIndexMeta FromRow failed with nullptr extra field ids"); |
| 108 | + } |
| 109 | + PAIMON_ASSIGN_OR_RAISE(extra_field_ids, array->ToIntArray()); |
| 110 | + } |
| 111 | + std::shared_ptr<Bytes> index_meta; |
| 112 | + if (!global_index_meta_row->IsNullAt(4)) { |
| 113 | + index_meta = global_index_meta_row->GetBinary(4); |
| 114 | + assert(index_meta); |
| 115 | + } |
| 116 | + global_index_meta = GlobalIndexMeta(row_range_start, row_range_end, index_field_id, |
| 117 | + extra_field_ids, index_meta); |
| 118 | + } |
| 119 | + return std::make_shared<IndexFileMeta>(file_type.ToString(), file_name.ToString(), |
| 120 | + file_size, row_count, dv_ranges, external_path, |
| 121 | + global_index_meta); |
| 122 | + } |
| 123 | +}; |
| 124 | + |
| 125 | +} // namespace paimon |
0 commit comments