4444#include " paimon/common/utils/arrow/status_utils.h"
4545#include " paimon/common/utils/date_time_utils.h"
4646#include " paimon/common/utils/scope_guard.h"
47+ #include " paimon/core/bucket/bucket_function.h"
48+ #include " paimon/core/bucket/default_bucket_function.h"
49+ #include " paimon/core/bucket/hive_bucket_function.h"
50+ #include " paimon/core/bucket/mod_bucket_function.h"
4751#include " paimon/data/decimal.h"
4852#include " paimon/data/timestamp.h"
4953#include " paimon/memory/memory_pool.h"
@@ -236,8 +240,21 @@ static Result<WriteFunction> WriteBucketRow(int32_t col_id,
236240}
237241} // namespace
238242
243+ BucketIdCalculator::BucketIdCalculator (int32_t num_buckets,
244+ std::unique_ptr<BucketFunction> bucket_function,
245+ const std::shared_ptr<MemoryPool>& pool)
246+ : num_buckets_(num_buckets), bucket_function_(std::move(bucket_function)), pool_(pool) {}
247+
248+ BucketIdCalculator::~BucketIdCalculator () = default ;
249+
239250Result<std::unique_ptr<BucketIdCalculator>> BucketIdCalculator::Create (
240251 bool is_pk_table, int32_t num_buckets, const std::shared_ptr<MemoryPool>& pool) {
252+ return Create (is_pk_table, num_buckets, std::make_unique<DefaultBucketFunction>(), pool);
253+ }
254+
255+ Result<std::unique_ptr<BucketIdCalculator>> BucketIdCalculator::Create (
256+ bool is_pk_table, int32_t num_buckets, std::unique_ptr<BucketFunction> bucket_function,
257+ const std::shared_ptr<MemoryPool>& pool) {
241258 if (num_buckets == 0 || num_buckets < -2 ) {
242259 return Status::Invalid (" num buckets must be -1 or -2 or greater than 0" );
243260 }
@@ -249,12 +266,22 @@ Result<std::unique_ptr<BucketIdCalculator>> BucketIdCalculator::Create(
249266 if (!is_pk_table && num_buckets == -2 ) {
250267 return Status::Invalid (" Append table not support PostponeBucketMode" );
251268 }
252- return std::unique_ptr<BucketIdCalculator>(new BucketIdCalculator (num_buckets, pool));
269+ return std::unique_ptr<BucketIdCalculator>(
270+ new BucketIdCalculator (num_buckets, std::move (bucket_function), pool));
271+ }
272+
273+ Result<std::unique_ptr<BucketIdCalculator>> BucketIdCalculator::CreateMod (
274+ bool is_pk_table, int32_t num_buckets, FieldType bucket_key_type,
275+ const std::shared_ptr<MemoryPool>& pool) {
276+ PAIMON_ASSIGN_OR_RAISE (auto mod_func, ModBucketFunction::Create (bucket_key_type));
277+ return Create (is_pk_table, num_buckets, std::move (mod_func), pool);
253278}
254279
255- Result<std::unique_ptr<BucketIdCalculator>> BucketIdCalculator::Create (bool is_pk_table,
256- int32_t num_buckets) {
257- return Create (is_pk_table, num_buckets, GetDefaultPool ());
280+ Result<std::unique_ptr<BucketIdCalculator>> BucketIdCalculator::CreateHive (
281+ bool is_pk_table, int32_t num_buckets, const std::vector<HiveFieldInfo>& field_infos,
282+ const std::shared_ptr<MemoryPool>& pool) {
283+ PAIMON_ASSIGN_OR_RAISE (auto hive_func, HiveBucketFunction::Create (field_infos));
284+ return Create (is_pk_table, num_buckets, std::move (hive_func), pool);
258285}
259286
260287Status BucketIdCalculator::CalculateBucketIds (ArrowArray* bucket_keys, ArrowSchema* bucket_schema,
@@ -298,7 +325,7 @@ Status BucketIdCalculator::CalculateBucketIds(ArrowArray* bucket_keys, ArrowSche
298325 write_functions[col](row, &row_writer);
299326 }
300327 row_writer.Complete ();
301- bucket_ids[row] = std::abs (bucket_row. HashCode () % num_buckets_);
328+ bucket_ids[row] = bucket_function_-> Bucket (bucket_row, num_buckets_);
302329 }
303330 guard.Release ();
304331 return Status::OK ();
0 commit comments