2015-01-27 21:24:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-10-02 07:13:14 +00:00
|
|
|
#include <base/shared_ptr_helper.h>
|
2016-08-26 21:25:05 +00:00
|
|
|
|
2021-09-29 17:30:07 +00:00
|
|
|
#include <Common/RWLock.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Storages/StorageSet.h>
|
2021-09-29 17:30:07 +00:00
|
|
|
#include <Storages/TableLockHolder.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Parsers/ASTTablesInSelectQuery.h>
|
2022-02-11 20:26:46 +00:00
|
|
|
#include <Interpreters/join_common.h>
|
2015-01-27 21:24:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2020-04-07 09:48:47 +00:00
|
|
|
class TableJoin;
|
|
|
|
class HashJoin;
|
|
|
|
using HashJoinPtr = std::shared_ptr<HashJoin>;
|
2017-01-14 09:00:19 +00:00
|
|
|
|
2017-04-16 15:00:33 +00:00
|
|
|
/** Allows you save the state for later use on the right side of the JOIN.
|
|
|
|
* When inserted into a table, the data will be inserted into the state,
|
|
|
|
* and also written to the backup file, to restore after the restart.
|
|
|
|
* Reading from the table is not possible directly - only specifying on the right side of JOIN is possible.
|
2015-01-28 00:38:10 +00:00
|
|
|
*
|
2017-04-16 15:00:33 +00:00
|
|
|
* When using, JOIN must be of the appropriate type (ANY|ALL LEFT|INNER ...).
|
2015-01-27 21:24:24 +00:00
|
|
|
*/
|
2021-06-15 19:55:21 +00:00
|
|
|
class StorageJoin final : public shared_ptr_helper<StorageJoin>, public StorageSetOrJoinBase
|
2015-01-27 21:24:24 +00:00
|
|
|
{
|
2021-06-15 19:55:21 +00:00
|
|
|
friend struct shared_ptr_helper<StorageJoin>;
|
2015-01-27 21:24:24 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
String getName() const override { return "Join"; }
|
|
|
|
|
2021-04-10 23:33:54 +00:00
|
|
|
void truncate(const ASTPtr &, const StorageMetadataPtr & metadata_snapshot, ContextPtr, TableExclusiveLockHolder &) override;
|
2018-06-09 16:09:37 +00:00
|
|
|
|
2021-05-31 23:22:05 +00:00
|
|
|
/// Only delete is supported.
|
2021-04-18 09:38:50 +00:00
|
|
|
void checkMutationIsPossible(const MutationCommands & commands, const Settings & settings) const override;
|
|
|
|
void mutate(const MutationCommands & commands, ContextPtr context) override;
|
|
|
|
|
2021-02-25 09:31:22 +00:00
|
|
|
/// Return instance of HashJoin holding lock that protects from insertions to StorageJoin.
|
|
|
|
/// HashJoin relies on structure of hash table that's why we need to return it with locked mutex.
|
2021-09-30 08:47:15 +00:00
|
|
|
HashJoinPtr getJoinLocked(std::shared_ptr<TableJoin> analyzed_join, ContextPtr context) const;
|
2021-02-25 09:31:22 +00:00
|
|
|
|
2021-02-25 11:21:06 +00:00
|
|
|
/// Get result type for function "joinGet(OrNull)"
|
2021-02-25 09:31:22 +00:00
|
|
|
DataTypePtr joinGetCheckAndGetReturnType(const DataTypes & data_types, const String & column_name, bool or_null) const;
|
2021-02-25 11:21:06 +00:00
|
|
|
|
|
|
|
/// Execute function "joinGet(OrNull)" on data block.
|
|
|
|
/// Takes rwlock for read to prevent parallel StorageJoin updates during processing data block
|
|
|
|
/// (but not during processing whole query, it's safe for joinGet that doesn't involve `used_flags` from HashJoin)
|
2021-09-29 17:30:07 +00:00
|
|
|
ColumnWithTypeAndName joinGet(const Block & block, const Block & block_with_columns_to_add, ContextPtr context) const;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2021-07-23 14:25:35 +00:00
|
|
|
SinkToStoragePtr write(const ASTPtr & query, const StorageMetadataPtr & metadata_snapshot, ContextPtr context) override;
|
2021-05-31 23:22:05 +00:00
|
|
|
|
2020-08-03 13:54:14 +00:00
|
|
|
Pipe read(
|
2018-11-30 14:49:35 +00:00
|
|
|
const Names & column_names,
|
2020-06-15 19:08:58 +00:00
|
|
|
const StorageMetadataPtr & /*metadata_snapshot*/,
|
2020-09-20 17:52:17 +00:00
|
|
|
SelectQueryInfo & query_info,
|
2021-04-10 23:33:54 +00:00
|
|
|
ContextPtr context,
|
2018-11-30 14:49:35 +00:00
|
|
|
QueryProcessingStage::Enum processed_stage,
|
2019-02-18 23:38:44 +00:00
|
|
|
size_t max_block_size,
|
2018-11-30 14:49:35 +00:00
|
|
|
unsigned num_streams) override;
|
|
|
|
|
2020-11-25 13:47:32 +00:00
|
|
|
std::optional<UInt64> totalRows(const Settings & settings) const override;
|
|
|
|
std::optional<UInt64> totalBytes(const Settings & settings) const override;
|
2020-10-23 18:11:55 +00:00
|
|
|
|
2022-02-11 20:26:46 +00:00
|
|
|
Block getRightSampleBlock() const
|
|
|
|
{
|
|
|
|
auto metadata_snapshot = getInMemoryMetadataPtr();
|
|
|
|
Block block = metadata_snapshot->getSampleBlock().sortColumns();
|
|
|
|
if (use_nulls && isLeftOrFull(kind))
|
|
|
|
{
|
|
|
|
for (auto & col : block)
|
|
|
|
{
|
|
|
|
JoinCommon::convertColumnToNullable(col);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useNulls() const { return use_nulls; }
|
|
|
|
|
2015-01-27 21:24:24 +00:00
|
|
|
private:
|
2018-11-30 14:49:35 +00:00
|
|
|
Block sample_block;
|
2019-05-28 06:12:20 +00:00
|
|
|
const Names key_names;
|
2018-11-30 14:49:35 +00:00
|
|
|
bool use_nulls;
|
|
|
|
SizeLimits limits;
|
2017-04-01 07:20:54 +00:00
|
|
|
ASTTableJoin::Kind kind; /// LEFT | INNER ...
|
|
|
|
ASTTableJoin::Strictness strictness; /// ANY | ALL
|
2020-03-29 10:07:51 +00:00
|
|
|
bool overwrite;
|
2017-04-01 07:20:54 +00:00
|
|
|
|
2020-04-07 09:48:47 +00:00
|
|
|
std::shared_ptr<TableJoin> table_join;
|
2019-09-09 19:43:37 +00:00
|
|
|
HashJoinPtr join;
|
2021-02-24 16:19:04 +00:00
|
|
|
|
|
|
|
/// Protect state for concurrent use in insertFromBlock and joinBlock.
|
2021-02-25 09:31:22 +00:00
|
|
|
/// Lock is stored in HashJoin instance during query and blocks concurrent insertions.
|
2021-09-29 17:30:07 +00:00
|
|
|
mutable RWLock rwlock = RWLockImpl::create();
|
2021-05-31 23:22:05 +00:00
|
|
|
mutable std::mutex mutate_mutex;
|
2021-04-18 09:38:50 +00:00
|
|
|
|
2021-09-30 08:47:15 +00:00
|
|
|
void insertBlock(const Block & block, ContextPtr context) override;
|
2019-11-01 11:03:35 +00:00
|
|
|
void finishInsert() override {}
|
2021-09-29 17:30:07 +00:00
|
|
|
size_t getSize(ContextPtr context) const override;
|
2021-09-30 08:47:15 +00:00
|
|
|
RWLockImpl::LockHolder tryLockTimedWithContext(const RWLock & lock, RWLockImpl::Type type, ContextPtr context) const;
|
2017-11-03 21:50:22 +00:00
|
|
|
|
|
|
|
protected:
|
2017-04-01 07:20:54 +00:00
|
|
|
StorageJoin(
|
2020-12-15 16:45:13 +00:00
|
|
|
DiskPtr disk_,
|
2019-10-25 19:07:47 +00:00
|
|
|
const String & relative_path_,
|
2019-12-04 16:06:55 +00:00
|
|
|
const StorageID & table_id_,
|
2017-04-01 07:20:54 +00:00
|
|
|
const Names & key_names_,
|
2018-11-30 14:49:35 +00:00
|
|
|
bool use_nulls_,
|
|
|
|
SizeLimits limits_,
|
2021-04-23 12:18:23 +00:00
|
|
|
ASTTableJoin::Kind kind_,
|
|
|
|
ASTTableJoin::Strictness strictness_,
|
2018-12-30 15:54:45 +00:00
|
|
|
const ColumnsDescription & columns_,
|
2019-08-24 21:20:20 +00:00
|
|
|
const ConstraintsDescription & constraints_,
|
2021-04-23 12:18:23 +00:00
|
|
|
const String & comment,
|
2019-10-25 19:07:47 +00:00
|
|
|
bool overwrite,
|
2020-09-18 12:58:27 +00:00
|
|
|
bool persistent_);
|
2015-01-27 21:24:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|