|
2 | 2 | #include <Interpreters/HashJoin/HashJoin.h> |
3 | 3 | #include <Common/ColumnsHashing.h> |
4 | 4 | #include <Common/HashTable/Prefetching.h> |
| 5 | +#include <Common/SipHash.h> |
5 | 6 | #include <Columns/ColumnLowCardinality.h> |
| 7 | +#include <Columns/ColumnString.h> |
6 | 8 | #include <Columns/ColumnsNumber.h> |
| 9 | +#include <base/PackedStringRef.h> |
7 | 10 |
|
8 | 11 | #include <algorithm> |
| 12 | +#include <bit> |
9 | 13 | #include <concepts> |
10 | 14 | #include <span> |
11 | 15 |
|
@@ -457,6 +461,98 @@ struct HashMethodHashedJoinKey : public ColumnsHashing::columns_hashing_impl::Ha |
457 | 461 | void buildKeys(size_t begin, size_t end, UInt128 * keys, const UInt8 * skip) { key.buildRange(begin, end, keys, skip); } |
458 | 462 | }; |
459 | 463 |
|
| 464 | +/// The 16-byte key of a single `String` join key in the `key_packed_string` and `two_level_key_packed_string` maps. |
| 465 | +/// |
| 466 | +/// - Up to 15 bytes: the `UnhashedPackedStringRef` of the string. It is exact: byte 15 holds the length, and the bytes |
| 467 | +/// past the end are zero. The empty string is the zero key. |
| 468 | +/// - 16 bytes or more: the unkeyed `sipHash128Padded` of the string with bit 7 of byte 15 set. No short key has that |
| 469 | +/// bit set, so a hashed key never equals a short one. Two long strings whose other 127 hash bits match share a key. |
| 470 | +/// |
| 471 | +/// `build` and `buildRange` read up to 16 bytes from the start of a string; the chars of a `ColumnString` have that |
| 472 | +/// padding. |
| 473 | +struct PackedStringJoinKey |
| 474 | +{ |
| 475 | + static constexpr size_t max_inline_size = UnhashedPackedStringRef::MAX_SMALL_LEN; |
| 476 | + |
| 477 | + /// Bit 7 of byte 15, which is in `items[1]` on both byte orders. |
| 478 | + static constexpr UInt64 hashed_flag = UInt64(UnhashedPackedStringRef::LARGE_TAG_BYTE) |
| 479 | + << (std::endian::native == std::endian::little ? 56 : 0); |
| 480 | + |
| 481 | + static ALWAYS_INLINE UInt128 build(const char * data, size_t size) |
| 482 | + { |
| 483 | + if (size <= max_inline_size) |
| 484 | + return std::bit_cast<UInt128>(UnhashedPackedStringRef::buildShort(data, size)); |
| 485 | + |
| 486 | + UInt128 key = sipHash128Padded(data, size); |
| 487 | + key.items[1] |= hashed_flag; |
| 488 | + return key; |
| 489 | + } |
| 490 | + |
| 491 | + /// `keys[i - begin]` = the key of row `i` of a `ColumnString`, for the rows [begin, end). The rows `i` with |
| 492 | + /// `skip[i] != 0` are not hashed, and their keys are unspecified. On aarch64 and on x86 with AVX2 it hashes long |
| 493 | + /// strings 8 at a time: 4 in NEON lanes and 4 in general registers, or all 8 in AVX2 lanes. |
| 494 | + static void buildRange( |
| 495 | + const char * chars, const IColumn::Offset * offsets, size_t begin, size_t end, UInt128 * __restrict keys, const UInt8 * skip); |
| 496 | +}; |
| 497 | + |
| 498 | +/// Key getter of `key_packed_string` and `two_level_key_packed_string`. It builds the `PackedStringJoinKey` of a row, |
| 499 | +/// or reads keys built ahead, see `PreparedJoinKeys`. |
| 500 | +template <typename Value, typename Mapped> |
| 501 | +struct HashMethodPackedStringJoinKey : public ColumnsHashing::columns_hashing_impl::HashMethodBase< |
| 502 | + HashMethodPackedStringJoinKey<Value, Mapped>, |
| 503 | + Value, |
| 504 | + Mapped, |
| 505 | + /*consecutive_keys_optimization=*/false, |
| 506 | + use_offset>, |
| 507 | + public PreparedJoinKeys<HashMethodPackedStringJoinKey<Value, Mapped>> |
| 508 | +{ |
| 509 | + using Base = ColumnsHashing::columns_hashing_impl:: |
| 510 | + HashMethodBase<HashMethodPackedStringJoinKey<Value, Mapped>, Value, Mapped, false, use_offset>; |
| 511 | + |
| 512 | + /// Hashing a long string is not cheap, but `true` lets the join loops prefetch: after `prepareRows`, they build |
| 513 | + /// each key once. |
| 514 | + static constexpr bool has_cheap_key_calculation = true; |
| 515 | + |
| 516 | + const IColumn::Offset * offsets; |
| 517 | + const UInt8 * chars; |
| 518 | + |
| 519 | + /// The number of `emplaceKey` calls, and how many of their keys are hashed. |
| 520 | + size_t emplaced_keys = 0; |
| 521 | + size_t hashed_keys = 0; |
| 522 | + |
| 523 | + HashMethodPackedStringJoinKey(const ColumnRawPtrs & key_columns, const Sizes &, const ColumnsHashing::HashMethodContextPtr &) |
| 524 | + : HashMethodPackedStringJoinKey(assert_cast<const ColumnString &>(*key_columns[0])) |
| 525 | + { |
| 526 | + } |
| 527 | + |
| 528 | + explicit HashMethodPackedStringJoinKey(const ColumnString & column) |
| 529 | + : PreparedJoinKeys<HashMethodPackedStringJoinKey>(column.size()) |
| 530 | + , offsets(column.getOffsets().data()) |
| 531 | + , chars(column.getChars().data()) |
| 532 | + { |
| 533 | + } |
| 534 | + |
| 535 | + ALWAYS_INLINE UInt128 buildKey(size_t row) const |
| 536 | + { |
| 537 | + return PackedStringJoinKey::build( |
| 538 | + reinterpret_cast<const char *>(chars) + offsets[ssize_t(row) - 1], offsets[row] - offsets[ssize_t(row) - 1]); |
| 539 | + } |
| 540 | + |
| 541 | + void buildKeys(size_t begin, size_t end, UInt128 * keys, const UInt8 * skip) const |
| 542 | + { |
| 543 | + PackedStringJoinKey::buildRange(reinterpret_cast<const char *>(chars), offsets, begin, end, keys, skip); |
| 544 | + } |
| 545 | + |
| 546 | + template <typename Data> |
| 547 | + ALWAYS_INLINE typename Base::EmplaceResult emplaceKey(Data & data, size_t row, Arena & pool) |
| 548 | + { |
| 549 | + auto result = Base::emplaceKey(data, row, pool); |
| 550 | + ++emplaced_keys; |
| 551 | + hashed_keys += (result.getKey().items[1] & PackedStringJoinKey::hashed_flag) != 0; |
| 552 | + return result; |
| 553 | + } |
| 554 | +}; |
| 555 | + |
460 | 556 | template <typename Value, typename Mapped> struct KeyGetterForTypeImpl<HashJoin::Type::key8, Value, Mapped> |
461 | 557 | { |
462 | 558 | using Type = ColumnsHashing::HashMethodOneNumber<Value, Mapped, UInt8, false, use_offset>; |
@@ -555,6 +651,14 @@ template <typename Value, typename Mapped> struct KeyGetterForTypeImpl<HashJoin: |
555 | 651 | { |
556 | 652 | using Type = ColumnsHashing::HashMethodOneNumber<Value, Mapped, UInt16, false, use_offset>; |
557 | 653 | }; |
| 654 | +template <typename Value, typename Mapped> struct KeyGetterForTypeImpl<HashJoin::Type::two_level_key_packed_string, Value, Mapped> |
| 655 | +{ |
| 656 | + using Type = HashMethodPackedStringJoinKey<Value, Mapped>; |
| 657 | +}; |
| 658 | +template <typename Value, typename Mapped> struct KeyGetterForTypeImpl<HashJoin::Type::key_packed_string, Value, Mapped> |
| 659 | +{ |
| 660 | + using Type = HashMethodPackedStringJoinKey<Value, Mapped>; |
| 661 | +}; |
558 | 662 | #define KEYGETTER_RANGE_IMPL(TYPE, FIELD_TYPE) \ |
559 | 663 | template <typename Value, typename Mapped> \ |
560 | 664 | struct KeyGetterForTypeImpl<HashJoin::Type::TYPE, Value, Mapped> \ |
|
0 commit comments