|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <Interpreters/Streaming/HashJoin.h> |
| 4 | + |
| 5 | +namespace DB |
| 6 | +{ |
| 7 | +namespace Streaming |
| 8 | +{ |
| 9 | +AsofHashJoin::AsofHashJoin( |
| 10 | + std::shared_ptr<TableJoin> table_join_, |
| 11 | + JoinStreamDescriptionPtr left_join_stream_desc_, |
| 12 | + JoinStreamDescriptionPtr right_join_stream_desc_) |
| 13 | + : HashJoin(table_join_, left_join_stream_desc_, right_join_stream_desc_) |
| 14 | + , asof_type(*table_join->getAsofType()) |
| 15 | + , asof_inequality(table_join->getAsofInequality()) |
| 16 | +{ |
| 17 | +} |
| 18 | + |
| 19 | +void AsofHashJoin::joinLeftBlock(Block & block) |
| 20 | +{ |
| 21 | + doJoinBlockWithHashTable<true>(block, hash_blocks); |
| 22 | +} |
| 23 | + |
| 24 | +void AsofHashJoin::insertRightBlock(Block block) |
| 25 | +{ |
| 26 | + /// FIXME, there are quite some block copies |
| 27 | + /// FIXME, all_key_columns shall hold shared_ptr to columns instead of raw ptr |
| 28 | + /// then we can update `source_block` in place |
| 29 | + /// key columns are from source `block` |
| 30 | + ColumnRawPtrMap all_key_columns = JoinCommon::materializeColumnsInplaceMap(block, table_join->getAllNames(JoinTableSide::Right)); |
| 31 | + |
| 32 | + /// We have copy of source `block` to `block_to_save` after prepare, so `block_to_save` is good to get moved to the buffered stream data |
| 33 | + Block block_to_save = prepareBlockToSave(block, right_data.buffered_data->sample_block); |
| 34 | + |
| 35 | + /// FIXME, multiple disjuncts OR clause |
| 36 | + ColumnRawPtrs key_columns; |
| 37 | + const Names & key_names = table_join->getClauses().front().key_names_right; |
| 38 | + key_columns.reserve(key_names.size()); |
| 39 | + for (const auto & name : key_names) |
| 40 | + key_columns.push_back(all_key_columns[name]); |
| 41 | + |
| 42 | + /// We will insert to the map only keys, where all components are not NULL. |
| 43 | + ConstNullMapPtr null_map{}; |
| 44 | + ColumnPtr null_map_holder = extractNestedColumnsAndNullMap(key_columns, null_map); |
| 45 | + |
| 46 | + /// If LEFT, RIGHT or FULL save blocks with nulls for NotJoinedBlocks |
| 47 | + UInt8 save_nullmap = 0; |
| 48 | + if (isRightOrFull(table_join->kind()) && null_map) |
| 49 | + { |
| 50 | + /// Save rows with NULL keys |
| 51 | + for (size_t i = 0; !save_nullmap && i < null_map->size(); ++i) |
| 52 | + save_nullmap |= (*null_map)[i]; |
| 53 | + } |
| 54 | + |
| 55 | + /// Add `block_to_save` to target stream data |
| 56 | + /// Note `block_to_save` may be empty for cases in which the query doesn't care other non-key columns. |
| 57 | + /// For example, SELECT count() FROM stream_a JOIN stream_b ON i=ii; |
| 58 | + auto start_row = buffered_hash_data->addOrConcatDataBlock(std::move(block_to_save)); |
| 59 | + auto rows = buffered_hash_data->lastDataBlock().rows(); |
| 60 | + |
| 61 | + switch (hash_method_type) |
| 62 | + { |
| 63 | +#define M(TYPE) \ |
| 64 | + case HashType::TYPE: \ |
| 65 | + return insertFromBlockImplType< \ |
| 66 | + Strictness::Asof, \ |
| 67 | + typename KeyGetterForType<HashType::TYPE, std::remove_reference_t<decltype(*(buffered_hash_data->maps->TYPE))>>::Type>( \ |
| 68 | + join, \ |
| 69 | + *(buffered_hash_data->maps->TYPE), \ |
| 70 | + rows, \ |
| 71 | + key_columns, \ |
| 72 | + key_sizes[0], \ |
| 73 | + &buffered_hash_data->blocks, \ |
| 74 | + start_row, \ |
| 75 | + null_map, \ |
| 76 | + buffered_hash_data->pool); \ |
| 77 | + break; |
| 78 | + APPLY_FOR_HASH_KEY_VARIANTS(M) |
| 79 | +#undef M |
| 80 | + } |
| 81 | + insertFromBlockImpl<strictness_>( |
| 82 | + hash_method_type, |
| 83 | + map, |
| 84 | + rows, |
| 85 | + key_columns, |
| 86 | + key_sizes[0], |
| 87 | + &target_hash_blocks->blocks, |
| 88 | + start_row, |
| 89 | + null_map, |
| 90 | + target_hash_blocks->pool); |
| 91 | + |
| 92 | + if (save_nullmap) |
| 93 | + /// FIXME, we will need account the allocated bytes for null_map_holder / not_joined_map as well |
| 94 | + buffered_hash_data->blocks_nullmaps.emplace_back(&buffered_hash_data->lastDataBlock(), null_map_holder); |
| 95 | + |
| 96 | + checkLimits(); |
| 97 | +} |
| 98 | +} |
| 99 | +} |
0 commit comments