yiguolei commented on code in PR #14480:
URL: https://github.com/apache/doris/pull/14480#discussion_r1028951585


##########
be/src/vec/common/hash_table/partitioned_hash_table.h:
##########
@@ -0,0 +1,666 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+// This file is copied from
+// 
https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/HashTable/TwoLevelHashTable.h
+// and modified by Doris
+#pragma once
+
+#include "vec/common/hash_table/hash_table.h"
+
+/** Partitioned hash table.
+  * Represents 16 (or 1ULL << BITS_FOR_SUB_TABLE) small hash tables (sub table 
count of the first level).
+  * To determine which one to use, one of the bytes of the hash function is 
taken.
+  *
+  * Usually works a little slower than a simple hash table.
+  * However, it has advantages in some cases:
+  * - if you need to merge two hash tables together, then you can easily 
parallelize it by sub tables;
+  * - delay during resizes is amortized, since the small hash tables will be 
resized separately;
+  * - in theory, resizes are cache-local in a larger range of sizes.
+  */
+
+template <size_t initial_size_degree = 8>
+struct PartitionedHashTableGrower : public 
HashTableGrowerWithPrecalculation<initial_size_degree> {
+    /// Increase the size of the hash table.
+    void increase_size() { this->increase_size_degree(this->size_degree() >= 
15 ? 1 : 2); }
+};
+
+template <typename Key, typename Cell, typename Hash, typename Grower, 
typename Allocator,
+          typename ImplTable = HashTable<Key, Cell, Hash, Grower, Allocator>,
+          bool ENABLE_PARTITIONED = false, size_t BITS_FOR_SUB_TABLE = 4>
+class PartitionedHashTable : private boost::noncopyable,
+                             protected Hash /// empty base optimization
+{
+public:
+    using Impl = ImplTable;
+
+    using key_type = typename Impl::key_type;
+    using mapped_type = typename Impl::mapped_type;
+    using value_type = typename Impl::value_type;
+    using cell_type = typename Impl::cell_type;
+
+    using LookupResult = typename Impl::LookupResult;
+    using ConstLookupResult = typename Impl::ConstLookupResult;
+
+protected:
+    friend class const_iterator;
+    friend class iterator;
+
+    using HashValue = size_t;
+    using Self = PartitionedHashTable;
+
+private:
+    static constexpr size_t NUM_LEVEL1_SUB_TABLES = 1ULL << BITS_FOR_SUB_TABLE;
+    static constexpr size_t MAX_SUB_TABLE = NUM_LEVEL1_SUB_TABLES - 1;
+
+    //factor that will trigger growing the hash table on insert.
+    static constexpr float MAX_SUB_TABLE_OCCUPANCY_FRACTION = 0.5f;
+
+    static const int PARTITIONED_BUCKET_THRESHOLD = 8388608;
+
+    Impl level0_sub_table;
+    Impl level1_sub_tables[NUM_LEVEL1_SUB_TABLES];
+
+    bool _is_partitioned = false;
+
+    // if ENABLE_PARTITIONED, the threshold of bucket count of level0 hash 
table above
+    // which the hash table is converted to partioned hash table
+    int _partitioned_threshold = PARTITIONED_BUCKET_THRESHOLD;
+
+public:
+    PartitionedHashTable() {
+        if constexpr (ENABLE_PARTITIONED) {
+            
level0_sub_table.set_partitioned_threshold(PARTITIONED_BUCKET_THRESHOLD);
+        }
+    }
+
+    explicit PartitionedHashTable(size_t size_hint) {
+        if constexpr (ENABLE_PARTITIONED) {
+            
level0_sub_table.set_partitioned_threshold(PARTITIONED_BUCKET_THRESHOLD);
+            if (level0_sub_table.check_if_need_partition(size_hint)) {
+                _is_partitioned = true;
+
+                for (size_t i = 0; i < NUM_LEVEL1_SUB_TABLES; ++i) {
+                    level1_sub_tables[i] = std::move(Impl(size_hint / 
NUM_LEVEL1_SUB_TABLES));
+                }
+            } else {
+                level0_sub_table = std::move(Impl(size_hint));
+            }
+        } else {
+            level0_sub_table = std::move(Impl(size_hint));
+        }
+    }
+
+    PartitionedHashTable(PartitionedHashTable&& rhs) { *this = std::move(rhs); 
}
+
+    PartitionedHashTable& operator=(PartitionedHashTable&& rhs) {
+        std::swap(_is_partitioned, rhs._is_partitioned);
+        std::swap(_partitioned_threshold, rhs._partitioned_threshold);
+
+        level0_sub_table = std::move(rhs.level0_sub_table);
+        for (size_t i = 0; i < NUM_LEVEL1_SUB_TABLES; ++i) {
+            level1_sub_tables[i] = std::move(rhs.level1_sub_tables[i]);
+        }
+        return *this;
+    }
+
+    size_t hash(const Key& x) const { return Hash::operator()(x); }
+
+    float get_factor() const { return MAX_SUB_TABLE_OCCUPANCY_FRACTION; }
+
+    bool should_be_shrink(int64_t valid_row) const {
+        if constexpr (ENABLE_PARTITIONED) {
+            if (_is_partitioned) {
+                return false;
+            } else {
+                return level0_sub_table.should_be_shrink(valid_row);
+            }
+        } else {
+            return level0_sub_table.should_be_shrink(valid_row);
+        }
+    }
+
+    template <typename Func>
+    void ALWAYS_INLINE for_each_value(Func&& func) {
+        if constexpr (ENABLE_PARTITIONED) {
+            if (_is_partitioned) {
+                for (auto i = 0u; i < NUM_LEVEL1_SUB_TABLES; ++i) {
+                    level1_sub_tables[i].for_each_value(func);
+                }
+            } else {
+                level0_sub_table.for_each_value(func);
+            }
+        } else {
+            level0_sub_table.for_each_value(func);
+        }
+    }
+
+    size_t get_size() {
+        size_t count = 0;
+        if constexpr (ENABLE_PARTITIONED) {
+            if (_is_partitioned) {
+                for (auto i = 0u; i < this->NUM_LEVEL1_SUB_TABLES; ++i) {
+                    for (auto& v : this->level1_sub_tables[i]) {
+                        count += v.get_second().get_row_count();
+                    }
+                }
+            } else {
+                count = level0_sub_table.get_size();
+            }
+        } else {
+            count = level0_sub_table.get_size();
+        }
+        return count;
+    }
+
+    void init_buf_size(size_t reserve_for_num_elements) {
+        if constexpr (ENABLE_PARTITIONED) {
+            if (_is_partitioned) {
+                for (auto& impl : level1_sub_tables) {
+                    impl.init_buf_size(reserve_for_num_elements / 
NUM_LEVEL1_SUB_TABLES);
+                }
+            } else {
+                if 
(level0_sub_table.check_if_need_partition(reserve_for_num_elements)) {
+                    level0_sub_table.clear_and_shrink();
+                    _is_partitioned = true;
+
+                    for (size_t i = 0; i < NUM_LEVEL1_SUB_TABLES; ++i) {
+                        
level1_sub_tables[i].init_buf_size(reserve_for_num_elements /
+                                                           
NUM_LEVEL1_SUB_TABLES);
+                    }
+                } else {
+                    level0_sub_table.init_buf_size(reserve_for_num_elements);
+                }
+            }
+        } else {
+            level0_sub_table.init_buf_size(reserve_for_num_elements);
+        }
+    }
+
+    void delete_zero_key(Key key) {
+        if constexpr (ENABLE_PARTITIONED) {

Review Comment:
   ENABLE_PARTITIONED this template variable is useless.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org
For additional commands, e-mail: commits-h...@doris.apache.org

Reply via email to