2015-01-27 21:24:24 +00:00
|
|
|
#pragma once
|
|
|
|
|
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-08-04 15:20:19 +00:00
|
|
|
#include <Interpreters/JoinUtils.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
|
|
|
*/
|
2022-05-03 06:43:28 +00:00
|
|
|
class StorageJoin final : public StorageSetOrJoinBase
|
2015-01-27 21:24:24 +00:00
|
|
|
{
|
|
|
|
public:
|
2022-04-19 20:47:29 +00:00
|
|
|
StorageJoin(
|
|
|
|
DiskPtr disk_,
|
|
|
|
const String & relative_path_,
|
|
|
|
const StorageID & table_id_,
|
|
|
|
const Names & key_names_,
|
|
|
|
bool use_nulls_,
|
|
|
|
SizeLimits limits_,
|
2022-07-29 16:30:50 +00:00
|
|
|
JoinKind kind_,
|
|
|
|
JoinStrictness strictness_,
|
2022-04-19 20:47:29 +00:00
|
|
|
const ColumnsDescription & columns_,
|
|
|
|
const ConstraintsDescription & constraints_,
|
|
|
|
const String & comment,
|
|
|
|
bool overwrite,
|
|
|
|
bool persistent_);
|
|
|
|
|
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;
|
2023-01-30 17:38:28 +00:00
|
|
|
void mutate(const MutationCommands & commands, ContextPtr context) override;
|
2021-04-18 09:38:50 +00:00
|
|
|
|
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.
|
2023-08-07 03:15:20 +00:00
|
|
|
HashJoinPtr getJoinLocked(std::shared_ptr<TableJoin> analyzed_join, ContextPtr context, const Names & required_columns_names) 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
|
|
|
|
2023-06-07 18:33:08 +00:00
|
|
|
SinkToStoragePtr write(const ASTPtr & query, const StorageMetadataPtr & metadata_snapshot, ContextPtr context, bool async_insert) 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,
|
2021-07-09 03:15:41 +00:00
|
|
|
const StorageSnapshotPtr & storage_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,
|
2022-10-07 10:46:45 +00:00
|
|
|
size_t num_streams) override;
|
2018-11-30 14:49:35 +00:00
|
|
|
|
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();
|
2022-08-15 04:56:29 +00:00
|
|
|
Block block = metadata_snapshot->getSampleBlock();
|
2023-01-11 16:17:21 +00:00
|
|
|
convertRightBlock(block);
|
2022-02-11 20:26:46 +00:00
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool useNulls() const { return use_nulls; }
|
|
|
|
|
2022-12-07 15:02:37 +00:00
|
|
|
const Names & getKeyNames() const { return key_names; }
|
|
|
|
|
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;
|
2022-07-29 16:30:50 +00:00
|
|
|
JoinKind kind; /// LEFT | INNER ...
|
|
|
|
JoinStrictness 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();
|
2023-03-06 10:54:43 +00:00
|
|
|
|
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;
|
2023-03-06 10:54:43 +00:00
|
|
|
/// Same as tryLockTimedWithContext, but returns `nullptr` if lock is already acquired by current query.
|
2023-03-13 10:49:51 +00:00
|
|
|
static RWLockImpl::LockHolder tryLockForCurrentQueryTimedWithContext(const RWLock & lock, RWLockImpl::Type type, ContextPtr context);
|
2023-01-11 16:17:21 +00:00
|
|
|
|
|
|
|
void convertRightBlock(Block & block) const;
|
2015-01-27 21:24:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|