| 1 | #pragma once |
| 2 | |
| 3 | #include <ext/shared_ptr_helper.h> |
| 4 | |
| 5 | #include <Common/OptimizedRegularExpression.h> |
| 6 | #include <Storages/IStorage.h> |
| 7 | |
| 8 | |
| 9 | namespace DB |
| 10 | { |
| 11 | |
| 12 | /** A table that represents the union of an arbitrary number of other tables. |
| 13 | * All tables must have the same structure. |
| 14 | */ |
| 15 | class StorageMerge : public ext::shared_ptr_helper<StorageMerge>, public IStorage |
| 16 | { |
| 17 | friend struct ext::shared_ptr_helper<StorageMerge>; |
| 18 | public: |
| 19 | std::string getName() const override { return "Merge" ; } |
| 20 | std::string getTableName() const override { return table_name; } |
| 21 | std::string getDatabaseName() const override { return database_name; } |
| 22 | |
| 23 | bool isRemote() const override; |
| 24 | |
| 25 | /// The check is delayed to the read method. It checks the support of the tables used. |
| 26 | bool supportsSampling() const override { return true; } |
| 27 | bool supportsPrewhere() const override { return true; } |
| 28 | bool supportsFinal() const override { return true; } |
| 29 | bool supportsIndexForIn() const override { return true; } |
| 30 | |
| 31 | /// Consider columns coming from the underlying tables |
| 32 | NameAndTypePair getColumn(const String & column_name) const override; |
| 33 | bool hasColumn(const String & column_name) const override; |
| 34 | |
| 35 | QueryProcessingStage::Enum getQueryProcessingStage(const Context &) const override; |
| 36 | |
| 37 | BlockInputStreams read( |
| 38 | const Names & column_names, |
| 39 | const SelectQueryInfo & query_info, |
| 40 | const Context & context, |
| 41 | QueryProcessingStage::Enum processed_stage, |
| 42 | size_t max_block_size, |
| 43 | unsigned num_streams) override; |
| 44 | |
| 45 | void rename(const String & /*new_path_to_db*/, const String & new_database_name, const String & new_table_name, TableStructureWriteLockHolder &) override |
| 46 | { |
| 47 | table_name = new_table_name; |
| 48 | database_name = new_database_name; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | void checkAlterIsPossible(const AlterCommands & commands, const Settings & /* settings */) override; |
| 53 | |
| 54 | /// you need to add and remove columns in the sub-tables manually |
| 55 | /// the structure of sub-tables is not checked |
| 56 | void alter(const AlterCommands & params, const Context & context, TableStructureWriteLockHolder & table_lock_holder) override; |
| 57 | |
| 58 | bool mayBenefitFromIndexForIn(const ASTPtr & left_in_operand, const Context & query_context) const override; |
| 59 | |
| 60 | private: |
| 61 | String table_name; |
| 62 | String database_name; |
| 63 | String source_database; |
| 64 | OptimizedRegularExpression table_name_regexp; |
| 65 | Context global_context; |
| 66 | |
| 67 | using StorageListWithLocks = std::list<std::pair<StoragePtr, TableStructureReadLockHolder>>; |
| 68 | |
| 69 | StorageListWithLocks getSelectedTables(const String & query_id) const; |
| 70 | |
| 71 | StorageMerge::StorageListWithLocks getSelectedTables(const ASTPtr & query, bool has_virtual_column, bool get_lock, const String & query_id) const; |
| 72 | |
| 73 | template <typename F> |
| 74 | StoragePtr getFirstTable(F && predicate) const; |
| 75 | |
| 76 | DatabaseTablesIteratorPtr getDatabaseIterator(const Context & context) const; |
| 77 | |
| 78 | protected: |
| 79 | StorageMerge( |
| 80 | const std::string & database_name_, |
| 81 | const std::string & table_name_, |
| 82 | const ColumnsDescription & columns_, |
| 83 | const String & source_database_, |
| 84 | const String & table_name_regexp_, |
| 85 | const Context & context_); |
| 86 | |
| 87 | Block (const Names & column_names, const SelectQueryInfo & query_info, |
| 88 | const Context & context, QueryProcessingStage::Enum processed_stage); |
| 89 | |
| 90 | BlockInputStreams createSourceStreams(const SelectQueryInfo & query_info, const QueryProcessingStage::Enum & processed_stage, |
| 91 | const UInt64 max_block_size, const Block & , const StoragePtr & storage, |
| 92 | const TableStructureReadLockHolder & struct_lock, Names & real_column_names, |
| 93 | Context & modified_context, size_t streams_num, bool has_table_virtual_column, |
| 94 | bool concat_streams = false); |
| 95 | |
| 96 | void convertingSourceStream(const Block & , const Context & context, ASTPtr & query, |
| 97 | BlockInputStreamPtr & source_stream, QueryProcessingStage::Enum processed_stage); |
| 98 | }; |
| 99 | |
| 100 | } |
| 101 | |