This is an automated email from the ASF dual-hosted git repository.
HappenLee pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push:
new c827c4d6ff7 [improvement](be) Pack short string keys in hash joins
(#70748)
c827c4d6ff7 is described below
commit c827c4d6ff7b8977bcb9c4c685c332643f5f3b3a
Author: HappenLee <[email protected]>
AuthorDate: Mon Nov 23 10:04:20 2026 +0800
[improvement](be) Pack short string keys in hash joins (#70748)
### What problem does this PR solve?
A single short string equality key currently uses a pointer and length
for each build and probe row. Matching a candidate can require another
read of the build string. Scan the build keys once and choose a 4-, 8-,
or 16-byte packed integer key when the values fit. This reuses the
existing numeric hash and the join table's bucket chains.
Build strings with trailing zero bytes or a length above 16 keep the
existing string path. A probe string that is too long or ends in a zero
byte can have no exact match in a packed table. Route it to an empty
bucket distinct from the SQL NULL bucket. Hash collisions still use
exact key equality.
Single-thread Doris INNER JOIN kernel, 1,048,576 build and probe rows,
100% matches, median of ten alternating runs. Total time includes shape
selection, key preparation, hashing, build, and probe.
| String length | Key width | Before | After |
|---|---:|---:|---:|
| 4 bytes | 4 bytes | 117.02 ms | 54.58 ms |
| 8 bytes | 8 bytes | 197.01 ms | 81.27 ms |
| 12 bytes | 16 bytes | 201.85 ms | 101.73 ms |
| 16 bytes | 16 bytes | 187.21 ms | 89.12 ms |
### Release note
None
### Check List (For Author)
- Test
- [x] Standalone Doris JOIN kernel check under ASAN and UBSAN. Covered
all widths, embedded and trailing zero bytes, arbitrary bytes, bucket
collisions, oversized probe keys, NULL keys, duplicates, and output
continuations.
- [x] Syntax checks for both JOIN operators, all probe instantiations,
and the new BE test.
- [ ] Full BE unit suite. The local test runner stopped at missing
Google Cloud static archives during build.
- Behavior changed: No change to JOIN results.
- Does this need documentation: No.
---
be/src/exec/common/hash_table/join_hash_table.h | 6 +-
.../common/hash_table/packed_string_join_key.h | 102 ++++++++++++++++++
be/src/exec/common/join_utils.h | 65 +++++++++++-
be/src/exec/operator/hashjoin_build_sink.cpp | 20 ++++
.../operator/join/process_hash_table_probe_impl.h | 5 +-
.../hash_table/packed_string_join_key_test.cpp | 115 +++++++++++++++++++++
6 files changed, 310 insertions(+), 3 deletions(-)
diff --git a/be/src/exec/common/hash_table/join_hash_table.h
b/be/src/exec/common/hash_table/join_hash_table.h
index 28a7ef14986..aacd5b4b8ee 100644
--- a/be/src/exec/common/hash_table/join_hash_table.h
+++ b/be/src/exec/common/hash_table/join_hash_table.h
@@ -60,7 +60,7 @@ public:
} else {
bucket_size = hash_join_table_calc_bucket_size(num_elem + 1);
}
- first.resize(bucket_size + 1);
+ first.resize(no_match_bucket(bucket_size) + 1);
next.resize(num_elem);
if constexpr (JoinOpType == TJoinOp::FULL_OUTER_JOIN ||
@@ -73,6 +73,10 @@ public:
uint32_t get_bucket_size() const { return bucket_size; }
+ // A non-null probe key that cannot match any build key uses an empty
bucket.
+ // bucket_size itself is reserved for NULL and may contain build rows.
+ static uint32_t no_match_bucket(uint32_t bucket_size) { return bucket_size
+ 1; }
+
size_t size() const { return next.size(); }
DorisVector<uint8_t>& get_visited() { return visited; }
diff --git a/be/src/exec/common/hash_table/packed_string_join_key.h
b/be/src/exec/common/hash_table/packed_string_join_key.h
new file mode 100644
index 00000000000..6a8e6ff5f6c
--- /dev/null
+++ b/be/src/exec/common/hash_table/packed_string_join_key.h
@@ -0,0 +1,102 @@
+// 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.
+
+#pragma once
+
+#include <algorithm>
+#include <array>
+#include <bit>
+#include <cstddef>
+#include <cstdint>
+#include <cstring>
+
+namespace doris::packed_string_join {
+
+inline constexpr size_t MAX_WIDTH = 16;
+
+inline bool fits(const uint8_t* chars, size_t end, size_t length, size_t
max_length) {
+ // Zero padding would make strings that differ only by trailing zero bytes
equal.
+ return length <= max_length && (length == 0 || chars[end - 1] != 0);
+}
+
+template <typename Offset>
+size_t max_build_key_length(const uint8_t* chars, const Offset* offsets,
size_t rows) {
+ size_t max_length = 1;
+ for (size_t i = 1; i < rows; ++i) {
+ const size_t length = offsets[i + 1] - offsets[i];
+ if (!fits(chars, offsets[i + 1], length, MAX_WIDTH)) {
+ return 0;
+ }
+ max_length = std::max(max_length, length);
+ }
+ return max_length;
+}
+
+template <typename Key>
+inline constexpr auto MASKS = [] {
+ std::array<Key, sizeof(Key) + 1> masks {};
+ std::array<uint8_t, sizeof(Key)> bytes {};
+ for (size_t i = 0; i < sizeof(Key); ++i) {
+ bytes[i] = 0xff;
+ masks[i + 1] = std::bit_cast<Key>(bytes);
+ }
+ return masks;
+}();
+
+// The string column's right padding permits a full-width load for a nonempty
key.
+template <typename Key>
+inline Key pack(const uint8_t* chars, size_t begin, size_t length) {
+ if (length == 0) {
+ return Key {};
+ }
+ Key key;
+ std::memcpy(&key, chars + begin, sizeof(Key));
+ return key & MASKS<Key>[length];
+}
+
+template <bool Build, typename Key, typename Offset, typename Hash>
+void prepare(const Offset* offsets, const uint8_t* chars, const uint8_t*
null_map, uint32_t rows,
+ uint32_t bucket_size, uint32_t no_match_bucket, size_t
max_length, Key* keys,
+ uint32_t* buckets, Hash&& hash) {
+ if constexpr (Build) {
+ // The build table does not insert its mock row at index zero.
+ keys[0] = Key {};
+ buckets[0] = 0;
+ }
+ for (uint32_t i = Build ? 1 : 0; i < rows; ++i) {
+ if (null_map && null_map[i]) {
+ keys[i] = Key {};
+ buckets[i] = bucket_size;
+ continue;
+ }
+ const size_t end = offsets[i + 1];
+ const size_t begin = offsets[i];
+ const size_t length = end - begin;
+ if constexpr (!Build) {
+ if (!fits(chars, end, length, max_length)) {
+ keys[i] = Key {};
+ buckets[i] = no_match_bucket;
+ continue;
+ }
+ }
+ const Key key = pack<Key>(chars, begin, length);
+ keys[i] = key;
+ buckets[i] = hash(key) & (bucket_size - 1);
+ }
+}
+
+} // namespace doris::packed_string_join
diff --git a/be/src/exec/common/join_utils.h b/be/src/exec/common/join_utils.h
index fcbe7136528..ddc7f23aaee 100644
--- a/be/src/exec/common/join_utils.h
+++ b/be/src/exec/common/join_utils.h
@@ -24,6 +24,7 @@
#include "exec/common/hash_table/hash_key_type.h"
#include "exec/common/hash_table/hash_map_context.h"
#include "exec/common/hash_table/join_hash_table.h"
+#include "exec/common/hash_table/packed_string_join_key.h"
#include "exec/common/join_op_utils.h" // IWYU pragma: export
(JoinOpVariants/AsofIndexGroup moved there)
namespace doris {
@@ -73,6 +74,67 @@ using SerializedHashTableContext =
using MethodOneString =
MethodStringNoCache<JoinHashMap<StringRef,
HashCRC32Return32<StringRef>, false>>;
+template <typename F>
+decltype(auto) with_join_string_column(const IColumn& column, F&& f) {
+ const auto& nested = is_column_nullable(column)
+ ? assert_cast<const
ColumnNullable&>(column).get_nested_column()
+ : column;
+ if (nested.is_column_string64()) {
+ return f(assert_cast<const ColumnString64&>(nested));
+ }
+ return f(assert_cast<const ColumnString&>(nested));
+}
+
+template <typename Key>
+class PackedStringJoinContext : public PrimaryTypeHashTableContext<Key> {
+ using Base = PrimaryTypeHashTableContext<Key>;
+ using Table = typename Base::HashMapType;
+
+public:
+ explicit PackedStringJoinContext(size_t max_length) :
_max_length(max_length) {}
+
+ size_t serialized_keys_size(bool is_build) const override {
+ return (is_build ? _build_keys.size() : _probe_keys.size()) *
sizeof(Key);
+ }
+
+ size_t estimated_size(const ColumnRawPtrs& key_columns, uint32_t rows,
bool is_join,
+ bool is_build, uint32_t bucket_size) override {
+ return sizeof(Key) * rows +
+ Base::estimated_size(key_columns, rows, is_join, is_build,
bucket_size);
+ }
+
+ void init_serialized_keys(const ColumnRawPtrs& key_columns, uint32_t rows,
+ const uint8_t* null_map, bool is_join, bool
is_build,
+ uint32_t bucket_size) override {
+ DCHECK(is_join);
+ auto& keys = is_build ? _build_keys : _probe_keys;
+ keys.resize(rows);
+ Base::keys = keys.data();
+ Base::bucket_nums.resize(rows);
+
+ with_join_string_column(*key_columns[0], [&](const auto& column) {
+ const auto* offsets = column.get_offsets().data() - 1;
+ const auto* chars = column.get_chars().data();
+ auto prepare = [&]<bool Build>() {
+ packed_string_join::prepare<Build>(offsets, chars, null_map,
rows, bucket_size,
+
Table::no_match_bucket(bucket_size), _max_length,
+ keys.data(),
Base::bucket_nums.data(),
+ HashCRC32Return32<Key> {});
+ };
+ if (is_build) {
+ prepare.template operator()<true>();
+ } else {
+ prepare.template operator()<false>();
+ }
+ });
+ }
+
+private:
+ size_t _max_length;
+ DorisVector<Key> _build_keys;
+ DorisVector<Key> _probe_keys;
+};
+
using HashTableVariants = std::variant<
std::monostate, SerializedHashTableContext,
PrimaryTypeHashTableContext<UInt8>,
PrimaryTypeHashTableContext<UInt16>,
PrimaryTypeHashTableContext<UInt32>,
@@ -83,7 +145,8 @@ using HashTableVariants = std::variant<
FixedKeyHashTableContext<UInt64>, FixedKeyHashTableContext<UInt72>,
FixedKeyHashTableContext<UInt96>, FixedKeyHashTableContext<UInt104>,
FixedKeyHashTableContext<UInt128>, FixedKeyHashTableContext<UInt136>,
- FixedKeyHashTableContext<UInt256>, MethodOneString>;
+ FixedKeyHashTableContext<UInt256>, MethodOneString,
PackedStringJoinContext<UInt32>,
+ PackedStringJoinContext<UInt64>, PackedStringJoinContext<UInt128>>;
struct JoinDataVariants {
HashTableVariants method_variant;
diff --git a/be/src/exec/operator/hashjoin_build_sink.cpp
b/be/src/exec/operator/hashjoin_build_sink.cpp
index 25faef50b1c..54a47d9dac2 100644
--- a/be/src/exec/operator/hashjoin_build_sink.cpp
+++ b/be/src/exec/operator/hashjoin_build_sink.cpp
@@ -702,6 +702,26 @@ Status
HashJoinBuildSinkLocalState::_hash_table_init(RuntimeState* state,
_shared_state->hash_table_variant_vector[p._use_shared_hash_table ? _task_idx :
0]);
}
+ if (data_types.size() == 1 &&
is_string_type(data_types[0]->get_primitive_type())) {
+ const size_t max_length = with_join_string_column(*raw_ptrs[0],
[](const auto& column) {
+ return packed_string_join::max_build_key_length(
+ column.get_chars().data(), column.get_offsets().data() -
1, column.size());
+ });
+ if (max_length != 0) {
+ for (auto& variant_ptr : variant_ptrs) {
+ auto& variant = variant_ptr->method_variant;
+ if (max_length <= sizeof(UInt32)) {
+
variant.emplace<PackedStringJoinContext<UInt32>>(max_length);
+ } else if (max_length <= sizeof(UInt64)) {
+
variant.emplace<PackedStringJoinContext<UInt64>>(max_length);
+ } else {
+
variant.emplace<PackedStringJoinContext<UInt128>>(max_length);
+ }
+ }
+ return Status::OK();
+ }
+ }
+
for (auto& variant_ptr : variant_ptrs) {
RETURN_IF_ERROR(init_hash_method<JoinDataVariants>(variant_ptr.get(),
data_types, true));
}
diff --git a/be/src/exec/operator/join/process_hash_table_probe_impl.h
b/be/src/exec/operator/join/process_hash_table_probe_impl.h
index d093b6d7dea..b9467e279a0 100644
--- a/be/src/exec/operator/join/process_hash_table_probe_impl.h
+++ b/be/src/exec/operator/join/process_hash_table_probe_impl.h
@@ -1143,5 +1143,8 @@ struct ExtractType<T(U)> {
INSTANTIATION(JoinOpType, (FixedKeyHashTableContext<UInt128>)); \
INSTANTIATION(JoinOpType, (FixedKeyHashTableContext<UInt136>)); \
INSTANTIATION(JoinOpType, (FixedKeyHashTableContext<UInt256>)); \
- INSTANTIATION(JoinOpType, (MethodOneString));
+ INSTANTIATION(JoinOpType, (MethodOneString)); \
+ INSTANTIATION(JoinOpType, (PackedStringJoinContext<UInt32>)); \
+ INSTANTIATION(JoinOpType, (PackedStringJoinContext<UInt64>)); \
+ INSTANTIATION(JoinOpType, (PackedStringJoinContext<UInt128>));
} // namespace doris
diff --git a/be/test/exec/common/hash_table/packed_string_join_key_test.cpp
b/be/test/exec/common/hash_table/packed_string_join_key_test.cpp
new file mode 100644
index 00000000000..8f2db124a7d
--- /dev/null
+++ b/be/test/exec/common/hash_table/packed_string_join_key_test.cpp
@@ -0,0 +1,115 @@
+// 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.
+
+#include <gtest/gtest.h>
+
+#include <string>
+#include <string_view>
+
+#include "core/column/column_string.h"
+#include "exec/common/join_utils.h"
+
+namespace doris {
+namespace {
+
+template <typename StringColumn>
+size_t max_key_length(const StringColumn& column) {
+ return packed_string_join::max_build_key_length(column.get_chars().data(),
+
column.get_offsets().data() - 1, column.size());
+}
+
+template <typename Key, typename StringColumn>
+void check_packed_join() {
+ auto build = StringColumn::create();
+ auto probe = StringColumn::create();
+ auto append = [](auto& column, std::string_view value) {
+ column->insert_data(value.data(), value.size());
+ };
+ const std::string full(sizeof(Key), 'x');
+ const std::string trailing_zero("a\0", 2);
+ const std::string internal_zero("a\0b", 3);
+ const std::string binary("\xff", 1);
+ for (std::string_view value :
+ {std::string_view(""), std::string_view(""), std::string_view("a"),
std::string_view(full),
+ std::string_view(internal_zero), std::string_view(binary),
std::string_view("")}) {
+ append(build, value);
+ }
+ const size_t max_length = max_key_length(*build);
+ ASSERT_EQ(max_length, sizeof(Key));
+ PackedStringJoinContext<Key> context(max_length);
+ constexpr uint8_t build_nulls[] {1, 0, 0, 0, 0, 0, 1};
+ auto& table = *context.hash_table;
+ table.template prepare_build<TJoinOp::INNER_JOIN>(build->size(), 16, true,
0);
+ const auto bucket_size = table.get_bucket_size();
+ const auto build_rows = static_cast<uint32_t>(build->size());
+ context.init_serialized_keys({build.get()}, build_rows, build_nulls, true,
true, bucket_size);
+ table.build(context.keys, context.bucket_nums.data(), build_rows, true);
+ EXPECT_EQ(context.serialized_keys_size(true), build->size() * sizeof(Key));
+
+ const std::string too_long(sizeof(Key) + 1, 'x');
+ append(probe, "");
+ append(probe, "a");
+ append(probe, full);
+ append(probe, trailing_zero);
+ append(probe, internal_zero);
+ append(probe, binary);
+ append(probe, too_long);
+ append(probe, "");
+ constexpr uint8_t probe_nulls[] {0, 0, 0, 0, 0, 0, 0, 1};
+ const auto probe_rows = static_cast<uint32_t>(probe->size());
+ context.init_serialized_keys({probe.get()}, probe_rows, probe_nulls, true,
false, bucket_size);
+ EXPECT_EQ(context.bucket_nums[3], table.no_match_bucket(bucket_size));
+ EXPECT_EQ(context.bucket_nums[6], table.no_match_bucket(bucket_size));
+ EXPECT_EQ(context.bucket_nums[7], bucket_size);
+ table.pre_build_idxs(context.bucket_nums);
+ EXPECT_EQ(context.bucket_nums[3], 0);
+ EXPECT_EQ(context.bucket_nums[6], 0);
+ EXPECT_EQ(context.bucket_nums[7], 6);
+
+ uint32_t probe_indexes[16] {};
+ uint32_t build_indexes[16] {};
+ bool probe_visited = false;
+ const auto [probe_pos, build_pos, matched] = table.template
find_batch<TJoinOp::INNER_JOIN>(
+ context.keys, context.bucket_nums.data(), 0, 0, probe_rows,
probe_indexes,
+ probe_visited, build_indexes, nullptr, false, false, false);
+ EXPECT_EQ(probe_pos, probe->size());
+ EXPECT_EQ(build_pos, 0);
+ ASSERT_EQ(matched, 6);
+ EXPECT_EQ((std::vector<uint32_t>(probe_indexes, probe_indexes + matched)),
+ (std::vector<uint32_t> {0, 1, 2, 4, 5, 7}));
+ EXPECT_EQ((std::vector<uint32_t>(build_indexes, build_indexes + matched)),
+ (std::vector<uint32_t> {1, 2, 3, 4, 5, 6}));
+
+ append(build, trailing_zero);
+ EXPECT_EQ(max_key_length(*build), 0);
+}
+
+TEST(PackedStringJoinKeyTest, KeyWidthsAndNoMatchBucket) {
+ check_packed_join<UInt32, ColumnString>();
+ check_packed_join<UInt64, ColumnString>();
+ check_packed_join<UInt128, ColumnString64>();
+}
+
+TEST(PackedStringJoinKeyTest, BuildWithLongValueKeepsStringKeys) {
+ auto build = ColumnString::create();
+ build->insert_default();
+ build->insert_data("abcdefghijklmnopq", 17);
+ EXPECT_EQ(max_key_length(*build), 0);
+}
+
+} // namespace
+} // namespace doris
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]