|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#pragma once |
| 20 | +#include <cstdint> |
| 21 | +#include <memory> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "paimon/bucket/bucket_function_type.h" |
| 25 | +#include "paimon/memory/memory_pool.h" |
| 26 | +#include "paimon/result.h" |
| 27 | +#include "paimon/status.h" |
| 28 | +#include "paimon/visibility.h" |
| 29 | + |
| 30 | +struct ArrowSchema; |
| 31 | +struct ArrowArray; |
| 32 | + |
| 33 | +namespace paimon { |
| 34 | +class BucketFunction; |
| 35 | +class MemoryPool; |
| 36 | + |
| 37 | +/// Calculator for determining bucket ids based on the given bucket keys. |
| 38 | +/// |
| 39 | +/// @note `BucketIdCalculator` is compatible with the Java implementation and uses |
| 40 | +/// hash-based distribution to ensure even data distribution across buckets. |
| 41 | +class PAIMON_EXPORT BucketIdCalculator { |
| 42 | + public: |
| 43 | + /// Create `BucketIdCalculator` with default bucket function. |
| 44 | + /// @param is_pk_table Whether this is for a primary key table. |
| 45 | + /// @param num_buckets Number of buckets. |
| 46 | + /// @param pool Memory pool for memory allocation. |
| 47 | + static Result<std::unique_ptr<BucketIdCalculator>> Create( |
| 48 | + bool is_pk_table, int32_t num_buckets, const std::shared_ptr<MemoryPool>& pool); |
| 49 | + |
| 50 | + /// Create `BucketIdCalculator` with a custom bucket function. |
| 51 | + /// @param is_pk_table Whether this is for a primary key table. |
| 52 | + /// @param num_buckets Number of buckets. |
| 53 | + /// @param bucket_function The bucket function to use for bucket assignment. |
| 54 | + /// @param pool Memory pool for memory allocation. |
| 55 | + static Result<std::unique_ptr<BucketIdCalculator>> Create( |
| 56 | + bool is_pk_table, int32_t num_buckets, std::unique_ptr<BucketFunction> bucket_function, |
| 57 | + const std::shared_ptr<MemoryPool>& pool); |
| 58 | + |
| 59 | + /// Create `BucketIdCalculator` with MOD bucket function. |
| 60 | + /// @param is_pk_table Whether this is for a primary key table. |
| 61 | + /// @param num_buckets Number of buckets. |
| 62 | + /// @param bucket_key_type The type of the single bucket key field. Must be INT or BIGINT. |
| 63 | + /// @param pool Memory pool for memory allocation. |
| 64 | + static Result<std::unique_ptr<BucketIdCalculator>> CreateMod( |
| 65 | + bool is_pk_table, int32_t num_buckets, FieldType bucket_key_type, |
| 66 | + const std::shared_ptr<MemoryPool>& pool); |
| 67 | + |
| 68 | + /// Create `BucketIdCalculator` with HIVE bucket function. |
| 69 | + /// @param is_pk_table Whether this is for a primary key table. |
| 70 | + /// @param num_buckets Number of buckets. |
| 71 | + /// @param field_infos The detailed type info of all fields in the bucket key row. |
| 72 | + /// @param pool Memory pool for memory allocation. |
| 73 | + static Result<std::unique_ptr<BucketIdCalculator>> CreateHive( |
| 74 | + bool is_pk_table, int32_t num_buckets, const std::vector<HiveFieldInfo>& field_infos, |
| 75 | + const std::shared_ptr<MemoryPool>& pool); |
| 76 | + |
| 77 | + /// Calculate bucket ids for the given bucket keys. |
| 78 | + /// @param bucket_keys Arrow struct array containing the bucket key values. |
| 79 | + /// @param bucket_schema Arrow schema describing the structure of bucket_keys. |
| 80 | + /// @param bucket_ids Output array to store calculated bucket ids. |
| 81 | + /// @note 1. bucket_keys is a struct array, the order of fields needs to be consistent with |
| 82 | + /// "bucket-key" options in table schema. 2. bucket_keys and bucket_schema match each other. 3. |
| 83 | + /// bucket_ids is allocated enough space, at least >= bucket_keys->length |
| 84 | + Status CalculateBucketIds(ArrowArray* bucket_keys, ArrowSchema* bucket_schema, |
| 85 | + int32_t* bucket_ids) const; |
| 86 | + |
| 87 | + /// Destructor |
| 88 | + ~BucketIdCalculator(); |
| 89 | + |
| 90 | + private: |
| 91 | + BucketIdCalculator(int32_t num_buckets, std::unique_ptr<BucketFunction> bucket_function, |
| 92 | + const std::shared_ptr<MemoryPool>& pool); |
| 93 | + |
| 94 | + private: |
| 95 | + int32_t num_buckets_; |
| 96 | + std::unique_ptr<BucketFunction> bucket_function_; |
| 97 | + std::shared_ptr<MemoryPool> pool_; |
| 98 | +}; |
| 99 | +} // namespace paimon |
0 commit comments