|
| 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 "paimon/core/index/global_index_meta.h" |
| 21 | + |
| 22 | +#include <memory> |
| 23 | +#include <string> |
| 24 | +#include <vector> |
| 25 | + |
| 26 | +#include "fmt/format.h" |
| 27 | +#include "fmt/ranges.h" |
| 28 | +#include "paimon/common/data/binary_array.h" |
| 29 | +#include "paimon/common/data/binary_row_writer.h" |
| 30 | + |
| 31 | +namespace paimon { |
| 32 | +GlobalIndexMeta::GlobalIndexMeta(int64_t _row_range_start, int64_t _row_range_end, |
| 33 | + int32_t _index_field_id, |
| 34 | + const std::optional<std::vector<int32_t>>& _extra_field_ids, |
| 35 | + const std::shared_ptr<Bytes>& _index_meta) |
| 36 | + : row_range_start(_row_range_start), |
| 37 | + row_range_end(_row_range_end), |
| 38 | + index_field_id(_index_field_id), |
| 39 | + extra_field_ids(_extra_field_ids), |
| 40 | + index_meta(_index_meta) {} |
| 41 | + |
| 42 | +bool GlobalIndexMeta::operator==(const GlobalIndexMeta& other) const { |
| 43 | + if (this == &other) { |
| 44 | + return true; |
| 45 | + } |
| 46 | + if ((index_meta && !other.index_meta) || (!index_meta && other.index_meta)) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + if (index_meta && other.index_meta && !(*index_meta == *other.index_meta)) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + return row_range_start == other.row_range_start && row_range_end == other.row_range_end && |
| 53 | + index_field_id == other.index_field_id && extra_field_ids == other.extra_field_ids; |
| 54 | +} |
| 55 | + |
| 56 | +std::string GlobalIndexMeta::ToString() const { |
| 57 | + std::string extra_field_ids_str = |
| 58 | + extra_field_ids == std::nullopt |
| 59 | + ? "null" |
| 60 | + : fmt::format("{}", fmt::join(extra_field_ids.value(), ", ")); |
| 61 | + |
| 62 | + std::string index_meta_str = |
| 63 | + index_meta == nullptr ? "null" : std::string(index_meta->data(), index_meta->size()); |
| 64 | + return fmt::format( |
| 65 | + "{{row_range_start={}, row_range_end={}, index_field_id={}, extra_field_ids={}, " |
| 66 | + "index_meta={}}}", |
| 67 | + row_range_start, row_range_end, index_field_id, extra_field_ids_str, index_meta_str); |
| 68 | +} |
| 69 | + |
| 70 | +BinaryRow GlobalIndexMeta::ToRow(MemoryPool* pool) const { |
| 71 | + BinaryRow row(5); |
| 72 | + BinaryRowWriter writer(&row, 32 * 1024, pool); |
| 73 | + writer.WriteLong(0, row_range_start); |
| 74 | + writer.WriteLong(1, row_range_end); |
| 75 | + writer.WriteInt(2, index_field_id); |
| 76 | + if (!extra_field_ids) { |
| 77 | + writer.SetNullAt(3); |
| 78 | + } else { |
| 79 | + writer.WriteArray(3, BinaryArray::FromIntArray(extra_field_ids.value(), pool)); |
| 80 | + } |
| 81 | + if (index_meta == nullptr) { |
| 82 | + writer.SetNullAt(4); |
| 83 | + } else { |
| 84 | + writer.WriteBinary(4, *index_meta); |
| 85 | + } |
| 86 | + writer.Complete(); |
| 87 | + return row; |
| 88 | +} |
| 89 | + |
| 90 | +Result<GlobalIndexMeta> GlobalIndexMeta::FromRow(const InternalRow& row) { |
| 91 | + int64_t row_range_start = row.GetLong(0); |
| 92 | + int64_t row_range_end = row.GetLong(1); |
| 93 | + int32_t index_field_id = row.GetInt(2); |
| 94 | + std::optional<std::vector<int32_t>> extra_field_ids; |
| 95 | + if (!row.IsNullAt(3)) { |
| 96 | + std::shared_ptr<InternalArray> array = row.GetArray(3); |
| 97 | + if (!array) { |
| 98 | + return Status::Invalid("GlobalIndexMeta FromRow failed with nullptr extra field ids"); |
| 99 | + } |
| 100 | + PAIMON_ASSIGN_OR_RAISE(extra_field_ids, array->ToIntArray()); |
| 101 | + } |
| 102 | + std::shared_ptr<Bytes> index_meta; |
| 103 | + if (!row.IsNullAt(4)) { |
| 104 | + index_meta = row.GetBinary(4); |
| 105 | + assert(index_meta); |
| 106 | + } |
| 107 | + return GlobalIndexMeta(row_range_start, row_range_end, index_field_id, extra_field_ids, |
| 108 | + index_meta); |
| 109 | +} |
| 110 | + |
| 111 | +const std::shared_ptr<arrow::DataType>& GlobalIndexMeta::DataType() { |
| 112 | + static std::shared_ptr<arrow::DataType> schema = arrow::struct_({ |
| 113 | + arrow::field("_ROW_RANGE_START", arrow::int64(), /*nullable=*/false), |
| 114 | + arrow::field("_ROW_RANGE_END", arrow::int64(), /*nullable=*/false), |
| 115 | + arrow::field("_INDEX_FIELD_ID", arrow::int32(), /*nullable=*/false), |
| 116 | + arrow::field("_EXTRA_FIELD_IDS", |
| 117 | + arrow::list(arrow::field("item", arrow::int32(), /*nullable=*/false)), |
| 118 | + /*nullable=*/true), |
| 119 | + arrow::field("_INDEX_META", arrow::binary(), /*nullable=*/true), |
| 120 | + }); |
| 121 | + return schema; |
| 122 | +} |
| 123 | + |
| 124 | +} // namespace paimon |
0 commit comments