ClickHouse/src/Storages/StorageMerge.h

132 lines
4.6 KiB
C++
Raw Normal View History

2012-05-30 05:53:09 +00:00
#pragma once
2021-06-15 19:55:21 +00:00
#include <common/shared_ptr_helper.h>
#include <Common/OptimizedRegularExpression.h>
#include <Storages/IStorage.h>
2014-01-10 13:24:50 +00:00
2012-05-30 05:53:09 +00:00
namespace DB
{
2017-04-16 15:00:33 +00:00
/** A table that represents the union of an arbitrary number of other tables.
* All tables must have the same structure.
2012-05-30 05:53:09 +00:00
*/
2021-06-15 19:55:21 +00:00
class StorageMerge final : public shared_ptr_helper<StorageMerge>, public IStorage, WithContext
2012-05-30 05:53:09 +00:00
{
2021-06-15 19:55:21 +00:00
friend struct shared_ptr_helper<StorageMerge>;
2012-05-30 05:53:09 +00:00
public:
std::string getName() const override { return "Merge"; }
bool isRemote() const override;
2017-04-16 15:00:33 +00:00
/// The check is delayed to the read method. It checks the support of the tables used.
bool supportsSampling() const override { return true; }
bool supportsPrewhere() const override { return true; }
bool supportsFinal() const override { return true; }
bool supportsIndexForIn() const override { return true; }
2020-12-22 16:40:53 +00:00
bool supportsSubcolumns() const override { return true; }
QueryProcessingStage::Enum
getQueryProcessingStage(ContextPtr, QueryProcessingStage::Enum, const StorageMetadataPtr &, SelectQueryInfo &) const override;
2020-08-03 13:54:14 +00:00
Pipe read(
const Names & column_names,
const StorageMetadataPtr & /*metadata_snapshot*/,
SelectQueryInfo & query_info,
ContextPtr context,
QueryProcessingStage::Enum processed_stage,
size_t max_block_size,
2017-06-02 15:54:39 +00:00
unsigned num_streams) override;
void checkAlterIsPossible(const AlterCommands & commands, ContextPtr context) const override;
2019-12-26 18:17:05 +00:00
2017-04-16 15:00:33 +00:00
/// you need to add and remove columns in the sub-tables manually
/// the structure of sub-tables is not checked
void alter(const AlterCommands & params, ContextPtr context, TableLockHolder & table_lock_holder) override;
bool mayBenefitFromIndexForIn(
const ASTPtr & left_in_operand, ContextPtr query_context, const StorageMetadataPtr & metadata_snapshot) const override;
2018-03-16 09:00:04 +00:00
2012-05-30 05:53:09 +00:00
private:
2021-07-02 02:11:28 +00:00
using DbToTableSetMap = std::map<String, std::set<String>>;
2021-06-25 13:51:17 +00:00
2021-06-04 14:48:48 +00:00
std::optional<OptimizedRegularExpression> source_database_regexp;
std::optional<OptimizedRegularExpression> source_table_regexp;
2021-06-25 08:00:30 +00:00
std::optional<DbToTableSetMap> source_databases_and_tables;
2021-06-25 13:51:17 +00:00
String source_database_name_or_regexp;
bool database_is_regexp = false;
2021-06-07 09:14:29 +00:00
/// (Database, Table, Lock, TableName)
using StorageWithLockAndName = std::tuple<String, StoragePtr, TableLockHolder, String>;
2019-12-30 18:20:43 +00:00
using StorageListWithLocks = std::list<StorageWithLockAndName>;
2021-06-07 09:14:29 +00:00
using DatabaseTablesIterators = std::vector<DatabaseTablesIteratorPtr>;
StorageMerge::StorageListWithLocks getSelectedTables(
2021-06-25 08:00:30 +00:00
ContextPtr query_context,
const ASTPtr & query = nullptr,
bool filter_by_database_virtual_column = false,
bool filter_by_table_virtual_column = false) const;
template <typename F>
StoragePtr getFirstTable(F && predicate) const;
2021-06-25 13:51:17 +00:00
DatabaseTablesIteratorPtr getDatabaseIterator(const String & database_name, ContextPtr context) const;
2021-06-07 09:14:29 +00:00
DatabaseTablesIterators getDatabaseIterators(ContextPtr context) const;
2020-04-27 13:55:30 +00:00
NamesAndTypesList getVirtuals() const override;
ColumnSizeByName getColumnSizes() const override;
protected:
StorageMerge(
2019-12-04 16:06:55 +00:00
const StorageID & table_id_,
const ColumnsDescription & columns_,
2021-04-23 12:18:23 +00:00
const String & comment,
2021-06-25 13:51:17 +00:00
const String & source_database_name_or_regexp_,
bool database_is_regexp_,
2021-06-25 08:00:30 +00:00
const DbToTableSetMap & source_databases_and_tables_,
ContextPtr context_);
StorageMerge(
const StorageID & table_id_,
const ColumnsDescription & columns_,
2021-04-23 12:18:23 +00:00
const String & comment,
2021-06-25 13:51:17 +00:00
const String & source_database_name_or_regexp_,
bool database_is_regexp_,
const String & source_table_regexp_,
ContextPtr context_);
2021-06-24 23:25:06 +00:00
struct AliasData
{
String name;
DataTypePtr type;
ASTPtr expression;
};
using Aliases = std::vector<AliasData>;
2020-08-06 12:24:05 +00:00
Pipe createSources(
const StorageMetadataPtr & metadata_snapshot,
SelectQueryInfo & query_info,
const QueryProcessingStage::Enum & processed_stage,
UInt64 max_block_size,
const Block & header,
2021-06-26 12:50:15 +00:00
const Aliases & aliases,
const StorageWithLockAndName & storage_with_lock,
Names & real_column_names,
2021-05-31 14:49:02 +00:00
ContextMutablePtr modified_context,
size_t streams_num,
2021-06-07 09:14:29 +00:00
bool has_database_virtual_column,
bool has_table_virtual_column,
bool concat_streams = false);
void convertingSourceStream(
2021-06-24 23:25:06 +00:00
const Block & header, const StorageMetadataPtr & metadata_snapshot, const Aliases & aliases,
ContextPtr context, ASTPtr & query,
Pipe & pipe, QueryProcessingStage::Enum processed_stage);
2012-05-30 05:53:09 +00:00
};
}