ClickHouse/src/Interpreters/JoinSwitcher.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

58 lines
1.5 KiB
C++
Raw Normal View History

#include <Common/typeid_cast.h>
#include <Interpreters/JoinSwitcher.h>
#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>
namespace DB
{
2022-10-26 16:09:11 +00:00
JoinSwitcher::JoinSwitcher(std::shared_ptr<TableJoin> table_join_, const Block & right_sample_block_)
: 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())
{
join = std::make_shared<HashJoin>(table_join, right_sample_block);
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)
{
std::lock_guard lock(switch_mutex);
if (switched)
2023-07-05 17:03:18 +00:00
return join->addBlockToJoin(block);
/// HashJoin with external limits check
2023-07-05 17:03:18 +00:00
join->addBlockToJoin(block, false);
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();
return true;
}
2022-09-27 12:33:09 +00:00
bool JoinSwitcher::switchJoin()
{
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);
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);
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);
switched = true;
2022-09-27 12:33:09 +00:00
return success;
}
}