2020-02-11 18:27:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-02-17 17:08:31 +00:00
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
#include <Core/Block.h>
|
2020-02-11 18:27:52 +00:00
|
|
|
#include <Interpreters/IJoin.h>
|
2020-04-07 09:48:47 +00:00
|
|
|
#include <Interpreters/TableJoin.h>
|
2020-10-10 16:31:10 +00:00
|
|
|
|
2020-02-11 18:27:52 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-02-20 19:13:12 +00:00
|
|
|
/// Used when setting 'join_algorithm' set to JoinAlgorithm::AUTO.
|
|
|
|
/// Starts JOIN with join-in-memory algorithm and switches to join-on-disk on the fly if there's no memory to place right table.
|
2022-10-26 16:09:11 +00:00
|
|
|
/// Current join-in-memory and join-on-disk are JoinAlgorithm::HASH and JoinAlgorithm::PARTIAL_MERGE joins respectively.
|
2020-02-11 18:27:52 +00:00
|
|
|
class JoinSwitcher : public IJoin
|
|
|
|
{
|
|
|
|
public:
|
2022-10-26 16:09:11 +00:00
|
|
|
JoinSwitcher(std::shared_ptr<TableJoin> table_join_, const Block & right_sample_block_);
|
2020-02-11 18:27:52 +00:00
|
|
|
|
2023-08-03 15:55:15 +00:00
|
|
|
std::string getName() const override { return "JoinSwitcher"; }
|
2021-03-25 18:11:54 +00:00
|
|
|
const TableJoin & getTableJoin() const override { return *table_join; }
|
|
|
|
|
2020-02-20 19:13:12 +00:00
|
|
|
/// Add block of data from right hand of JOIN into current join object.
|
|
|
|
/// If join-in-memory memory limit exceeded switches to join-on-disk and continue with it.
|
|
|
|
/// @returns false, if join-on-disk disk limit exceeded
|
2023-07-05 17:03:18 +00:00
|
|
|
bool addBlockToJoin(const Block & block, bool check_limits) override;
|
2020-02-11 18:27:52 +00:00
|
|
|
|
2021-09-13 13:35:17 +00:00
|
|
|
void checkTypesOfKeys(const Block & block) const override
|
|
|
|
{
|
|
|
|
join->checkTypesOfKeys(block);
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:27:52 +00:00
|
|
|
void joinBlock(Block & block, std::shared_ptr<ExtraBlock> & not_processed) override
|
|
|
|
{
|
|
|
|
join->joinBlock(block, not_processed);
|
|
|
|
}
|
|
|
|
|
2021-07-14 10:02:23 +00:00
|
|
|
const Block & getTotals() const override
|
2020-02-11 18:27:52 +00:00
|
|
|
{
|
2021-07-14 10:02:23 +00:00
|
|
|
return join->getTotals();
|
2020-02-11 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void setTotals(const Block & block) override
|
|
|
|
{
|
|
|
|
join->setTotals(block);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t getTotalRowCount() const override
|
|
|
|
{
|
|
|
|
return join->getTotalRowCount();
|
|
|
|
}
|
|
|
|
|
2020-02-17 17:08:31 +00:00
|
|
|
size_t getTotalByteCount() const override
|
|
|
|
{
|
|
|
|
return join->getTotalByteCount();
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:27:52 +00:00
|
|
|
bool alwaysReturnsEmptySet() const override
|
|
|
|
{
|
|
|
|
return join->alwaysReturnsEmptySet();
|
|
|
|
}
|
|
|
|
|
2022-10-18 11:43:01 +00:00
|
|
|
IBlocksStreamPtr
|
2021-09-10 14:52:44 +00:00
|
|
|
getNonJoinedBlocks(const Block & left_sample_block, const Block & result_sample_block, UInt64 max_block_size) const override
|
2020-02-11 18:27:52 +00:00
|
|
|
{
|
2021-09-10 14:52:44 +00:00
|
|
|
return join->getNonJoinedBlocks(left_sample_block, result_sample_block, max_block_size);
|
2020-02-11 18:27:52 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 11:43:01 +00:00
|
|
|
IBlocksStreamPtr getDelayedBlocks() override
|
2022-06-17 22:38:18 +00:00
|
|
|
{
|
2022-10-05 12:40:32 +00:00
|
|
|
return join->getDelayedBlocks();
|
2022-06-17 22:38:18 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 17:47:40 +00:00
|
|
|
bool hasDelayedBlocks() const override
|
|
|
|
{
|
|
|
|
return join->hasDelayedBlocks();
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:27:52 +00:00
|
|
|
private:
|
|
|
|
JoinPtr join;
|
2020-02-18 14:19:13 +00:00
|
|
|
SizeLimits limits;
|
2020-02-20 17:19:16 +00:00
|
|
|
bool switched;
|
2020-02-17 17:08:31 +00:00
|
|
|
mutable std::mutex switch_mutex;
|
2020-04-07 09:48:47 +00:00
|
|
|
std::shared_ptr<TableJoin> table_join;
|
2020-02-18 12:41:23 +00:00
|
|
|
const Block right_sample_block;
|
2020-02-17 17:08:31 +00:00
|
|
|
|
2020-02-20 19:13:12 +00:00
|
|
|
/// Change join-in-memory to join-on-disk moving right hand JOIN data from one to another.
|
|
|
|
/// Throws an error if join-on-disk do not support JOIN kind or strictness.
|
2022-09-27 12:33:09 +00:00
|
|
|
bool switchJoin();
|
2020-02-11 18:27:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|