1#pragma once
2
3#include <ext/shared_ptr_helper.h>
4
5#include <Parsers/IAST_fwd.h>
6#include <Storages/IStorage.h>
7
8
9namespace DB
10{
11
12class StorageMaterializedView : public ext::shared_ptr_helper<StorageMaterializedView>, public IStorage
13{
14 friend struct ext::shared_ptr_helper<StorageMaterializedView>;
15public:
16 std::string getName() const override { return "MaterializedView"; }
17 std::string getTableName() const override { return table_name; }
18 std::string getDatabaseName() const override { return database_name; }
19
20 ASTPtr getInnerQuery() const { return inner_query->clone(); }
21
22 NameAndTypePair getColumn(const String & column_name) const override;
23 bool hasColumn(const String & column_name) const override;
24
25 bool supportsSampling() const override { return getTargetTable()->supportsSampling(); }
26 bool supportsPrewhere() const override { return getTargetTable()->supportsPrewhere(); }
27 bool supportsFinal() const override { return getTargetTable()->supportsFinal(); }
28 bool supportsIndexForIn() const override { return getTargetTable()->supportsIndexForIn(); }
29 bool mayBenefitFromIndexForIn(const ASTPtr & left_in_operand, const Context & query_context) const override
30 {
31 return getTargetTable()->mayBenefitFromIndexForIn(left_in_operand, query_context);
32 }
33
34 BlockOutputStreamPtr write(const ASTPtr & query, const Context & context) override;
35
36 void drop(TableStructureWriteLockHolder &) override;
37
38 void truncate(const ASTPtr &, const Context &, TableStructureWriteLockHolder &) override;
39
40 bool optimize(const ASTPtr & query, const ASTPtr & partition, bool final, bool deduplicate, const Context & context) override;
41
42 void alterPartition(const ASTPtr & query, const PartitionCommands & commands, const Context & context) override;
43
44 void mutate(const MutationCommands & commands, const Context & context) override;
45
46 void rename(const String & new_path_to_db, const String & new_database_name, const String & new_table_name, TableStructureWriteLockHolder &) override;
47
48 void shutdown() override;
49
50 void checkTableCanBeDropped() const override;
51 void checkPartitionCanBeDropped(const ASTPtr & partition) override;
52
53 QueryProcessingStage::Enum getQueryProcessingStage(const Context & context) const override;
54
55 StoragePtr getTargetTable() const;
56 StoragePtr tryGetTargetTable() const;
57
58 ActionLock getActionLock(StorageActionBlockType type) override;
59
60 BlockInputStreams read(
61 const Names & column_names,
62 const SelectQueryInfo & query_info,
63 const Context & context,
64 QueryProcessingStage::Enum processed_stage,
65 size_t max_block_size,
66 unsigned num_streams) override;
67
68 Strings getDataPaths() const override;
69
70private:
71 String select_database_name;
72 String select_table_name;
73 String target_database_name;
74 String target_table_name;
75 String table_name;
76 String database_name;
77 ASTPtr inner_query;
78 Context & global_context;
79 bool has_inner_table = false;
80
81 void checkStatementCanBeForwarded() const;
82
83protected:
84 StorageMaterializedView(
85 const String & table_name_,
86 const String & database_name_,
87 Context & local_context,
88 const ASTCreateQuery & query,
89 const ColumnsDescription & columns_,
90 bool attach_);
91};
92
93}
94