2020-02-17 17:08:31 +00:00
|
|
|
#include <Common/typeid_cast.h>
|
|
|
|
#include <Interpreters/JoinSwitcher.h>
|
2020-04-07 09:48:47 +00:00
|
|
|
#include <Interpreters/HashJoin.h>
|
2020-02-17 17:21:03 +00:00
|
|
|
#include <Interpreters/MergeJoin.h>
|
2022-08-04 15:20:19 +00:00
|
|
|
#include <Interpreters/JoinUtils.h>
|
2020-02-17 17:08:31 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2022-10-26 16:09:11 +00:00
|
|
|
JoinSwitcher::JoinSwitcher(std::shared_ptr<TableJoin> table_join_, const Block & right_sample_block_)
|
2020-02-19 19:11:23 +00:00
|
|
|
: limits(table_join_->sizeLimits())
|
|
|
|
, switched(false)
|
2020-02-17 17:21:03 +00:00
|
|
|
, table_join(table_join_)
|
|
|
|
, right_sample_block(right_sample_block_.cloneEmpty())
|
|
|
|
{
|
2020-04-07 09:48:47 +00:00
|
|
|
join = std::make_shared<HashJoin>(table_join, right_sample_block);
|
2020-02-18 14:19:13 +00:00
|
|
|
|
|
|
|
if (!limits.hasLimits())
|
|
|
|
limits.max_bytes = table_join->defaultMaxBytes();
|
2020-02-17 17:21:03 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 17:03:18 +00:00
|
|
|
bool JoinSwitcher::addBlockToJoin(const Block & block, bool)
|
2020-02-17 17:08:31 +00:00
|
|
|
{
|
|
|
|
std::lock_guard lock(switch_mutex);
|
|
|
|
|
|
|
|
if (switched)
|
2023-07-05 17:03:18 +00:00
|
|
|
return join->addBlockToJoin(block);
|
2020-02-17 17:08:31 +00:00
|
|
|
|
|
|
|
/// HashJoin with external limits check
|
|
|
|
|
2023-07-05 17:03:18 +00:00
|
|
|
join->addBlockToJoin(block, false);
|
2020-02-17 17:08:31 +00:00
|
|
|
size_t rows = join->getTotalRowCount();
|
|
|
|
size_t bytes = join->getTotalByteCount();
|
|
|
|
|
|
|
|
if (!limits.softCheck(rows, bytes))
|
2022-09-27 12:33:09 +00:00
|
|
|
return switchJoin();
|
2020-02-17 17:08:31 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-27 12:33:09 +00:00
|
|
|
bool JoinSwitcher::switchJoin()
|
2020-02-17 17:08:31 +00:00
|
|
|
{
|
2022-09-27 12:33:09 +00:00
|
|
|
HashJoin * hash_join = assert_cast<HashJoin *>(join.get());
|
2022-12-30 15:54:39 +00:00
|
|
|
BlocksList right_blocks = hash_join->releaseJoinedBlocks(true);
|
2020-02-17 17:08:31 +00:00
|
|
|
|
2022-06-16 12:09:23 +00:00
|
|
|
/// Destroy old join & create new one.
|
2022-10-26 16:09:11 +00:00
|
|
|
join = std::make_shared<MergeJoin>(table_join, right_sample_block);
|
2020-02-17 17:08:31 +00:00
|
|
|
|
2022-09-27 12:33:09 +00:00
|
|
|
bool success = true;
|
2022-06-16 12:09:23 +00:00
|
|
|
for (const Block & saved_block : right_blocks)
|
2023-07-05 17:03:18 +00:00
|
|
|
success = success && join->addBlockToJoin(saved_block);
|
2020-02-17 17:08:31 +00:00
|
|
|
|
|
|
|
switched = true;
|
2022-09-27 12:33:09 +00:00
|
|
|
return success;
|
2020-02-17 17:08:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|