From 5bc7f67e616cdc81bc2827750d981995f6cdeb32 Mon Sep 17 00:00:00 2001 From: alesapin Date: Fri, 5 Jun 2020 14:54:54 +0300 Subject: [PATCH] Better metadata for select query and renames --- .../PushingToViewsBlockOutputStream.cpp | 2 +- src/Storages/IStorage.cpp | 41 ++++++-- src/Storages/IStorage.h | 38 ++++--- src/Storages/LiveView/StorageLiveView.h | 1 + src/Storages/MergeTree/MergeTreeData.cpp | 23 ++--- src/Storages/MergeTree/MergeTreeData.h | 2 - src/Storages/StorageInMemoryMetadata.cpp | 34 ------- src/Storages/StorageInMemoryMetadata.h | 30 ------ src/Storages/StorageMaterializedView.cpp | 98 ++++--------------- src/Storages/StorageMaterializedView.h | 7 -- src/Storages/StorageView.cpp | 10 +- src/Storages/StorageView.h | 3 - src/Storages/TTLDescription.cpp | 2 +- src/Storages/TTLDescription.h | 3 +- src/Storages/ya.make | 2 + 15 files changed, 103 insertions(+), 193 deletions(-) diff --git a/src/DataStreams/PushingToViewsBlockOutputStream.cpp b/src/DataStreams/PushingToViewsBlockOutputStream.cpp index 2c4792e184e..2c2e6972158 100644 --- a/src/DataStreams/PushingToViewsBlockOutputStream.cpp +++ b/src/DataStreams/PushingToViewsBlockOutputStream.cpp @@ -72,7 +72,7 @@ PushingToViewsBlockOutputStream::PushingToViewsBlockOutputStream( StoragePtr inner_table = materialized_view->getTargetTable(); auto inner_table_id = inner_table->getStorageID(); - query = materialized_view->getInnerQuery(); + query = materialized_view->getSelectQuery().inner_query; std::unique_ptr insert = std::make_unique(); insert->table_id = inner_table_id; diff --git a/src/Storages/IStorage.cpp b/src/Storages/IStorage.cpp index 496aa55d071..33daf0c298c 100644 --- a/src/Storages/IStorage.cpp +++ b/src/Storages/IStorage.cpp @@ -421,12 +421,12 @@ NamesAndTypesList IStorage::getVirtuals() const return {}; } -const StorageMetadataKeyField & IStorage::getPartitionKey() const +const KeyDescription & IStorage::getPartitionKey() const { return partition_key; } -void IStorage::setPartitionKey(const StorageMetadataKeyField & partition_key_) +void IStorage::setPartitionKey(const KeyDescription & partition_key_) { partition_key = partition_key_; } @@ -448,12 +448,12 @@ Names IStorage::getColumnsRequiredForPartitionKey() const return {}; } -const StorageMetadataKeyField & IStorage::getSortingKey() const +const KeyDescription & IStorage::getSortingKey() const { return sorting_key; } -void IStorage::setSortingKey(const StorageMetadataKeyField & sorting_key_) +void IStorage::setSortingKey(const KeyDescription & sorting_key_) { sorting_key = sorting_key_; } @@ -482,12 +482,12 @@ Names IStorage::getSortingKeyColumns() const return {}; } -const StorageMetadataKeyField & IStorage::getPrimaryKey() const +const KeyDescription & IStorage::getPrimaryKey() const { return primary_key; } -void IStorage::setPrimaryKey(const StorageMetadataKeyField & primary_key_) +void IStorage::setPrimaryKey(const KeyDescription & primary_key_) { primary_key = primary_key_; } @@ -516,12 +516,12 @@ Names IStorage::getPrimaryKeyColumns() const return {}; } -const StorageMetadataKeyField & IStorage::getSamplingKey() const +const KeyDescription & IStorage::getSamplingKey() const { return sampling_key; } -void IStorage::setSamplingKey(const StorageMetadataKeyField & sampling_key_) +void IStorage::setSamplingKey(const KeyDescription & sampling_key_) { sampling_key = sampling_key_; } @@ -654,4 +654,29 @@ ColumnDependencies IStorage::getColumnDependencies(const NameSet & updated_colum } +ASTPtr IStorage::getSettingsChanges() const +{ + return settings_changes->clone(); +} + +void IStorage::setSettingsChanges(const ASTPtr & settings_changes_) +{ + settings_changes = settings_changes_->clone(); +} + +const SelectQueryDescription & IStorage::getSelectQuery() const +{ + return select; +} + +void IStorage::setSelectQuery(const SelectQueryDescription & select_) +{ + select = select_; +} + +bool IStorage::hasSelectQuery() const +{ + return select.select_query != nullptr; +} + } diff --git a/src/Storages/IStorage.h b/src/Storages/IStorage.h index a637c9c6881..cd7b2ad8a0e 100644 --- a/src/Storages/IStorage.h +++ b/src/Storages/IStorage.h @@ -15,7 +15,9 @@ #include #include #include +#include #include +#include #include #include #include @@ -152,6 +154,15 @@ public: /// thread-unsafe part. lockStructure must be acquired const ConstraintsDescription & getConstraints() const; void setConstraints(ConstraintsDescription constraints_); + /// Storage settings + ASTPtr getSettingsChanges() const; + void setSettingsChanges(const ASTPtr & settings_changes_); + bool hasSettingsChanges() const { return settings_changes != nullptr; } + + const SelectQueryDescription & getSelectQuery() const; + void setSelectQuery(const SelectQueryDescription & select_); + bool hasSelectQuery() const; + /// Returns storage metadata copy. Direct modification of /// result structure doesn't affect storage. virtual StorageInMemoryMetadata getInMemoryMetadata() const; @@ -203,14 +214,17 @@ private: IndicesDescription secondary_indices; ConstraintsDescription constraints; - StorageMetadataKeyField partition_key; - StorageMetadataKeyField primary_key; - StorageMetadataKeyField sorting_key; - StorageMetadataKeyField sampling_key; + KeyDescription partition_key; + KeyDescription primary_key; + KeyDescription sorting_key; + KeyDescription sampling_key; TTLColumnsDescription column_ttls_by_name; TTLTableDescription table_ttl; + ASTPtr settings_changes; + SelectQueryDescription select; + private: RWLockImpl::LockHolder tryLockTimed( const RWLock & rwlock, RWLockImpl::Type type, const String & query_id, const SettingSeconds & acquire_timeout) const; @@ -443,10 +457,10 @@ public: virtual Strings getDataPaths() const { return {}; } /// Returns structure with partition key. - const StorageMetadataKeyField & getPartitionKey() const; + const KeyDescription & getPartitionKey() const; /// Set partition key for storage (methods bellow, are just wrappers for this /// struct). - void setPartitionKey(const StorageMetadataKeyField & partition_key_); + void setPartitionKey(const KeyDescription & partition_key_); /// Returns ASTExpressionList of partition key expression for storage or nullptr if there is none. ASTPtr getPartitionKeyAST() const { return partition_key.definition_ast; } /// Storage has user-defined (in CREATE query) partition key. @@ -458,10 +472,10 @@ public: /// Returns structure with sorting key. - const StorageMetadataKeyField & getSortingKey() const; + const KeyDescription & getSortingKey() const; /// Set sorting key for storage (methods bellow, are just wrappers for this /// struct). - void setSortingKey(const StorageMetadataKeyField & sorting_key_); + void setSortingKey(const KeyDescription & sorting_key_); /// Returns ASTExpressionList of sorting key expression for storage or nullptr if there is none. ASTPtr getSortingKeyAST() const { return sorting_key.definition_ast; } /// Storage has user-defined (in CREATE query) sorting key. @@ -475,10 +489,10 @@ public: Names getSortingKeyColumns() const; /// Returns structure with primary key. - const StorageMetadataKeyField & getPrimaryKey() const; + const KeyDescription & getPrimaryKey() const; /// Set primary key for storage (methods bellow, are just wrappers for this /// struct). - void setPrimaryKey(const StorageMetadataKeyField & primary_key_); + void setPrimaryKey(const KeyDescription & primary_key_); /// Returns ASTExpressionList of primary key expression for storage or nullptr if there is none. ASTPtr getPrimaryKeyAST() const { return primary_key.definition_ast; } /// Storage has user-defined (in CREATE query) sorting key. @@ -493,10 +507,10 @@ public: Names getPrimaryKeyColumns() const; /// Returns structure with sampling key. - const StorageMetadataKeyField & getSamplingKey() const; + const KeyDescription & getSamplingKey() const; /// Set sampling key for storage (methods bellow, are just wrappers for this /// struct). - void setSamplingKey(const StorageMetadataKeyField & sampling_key_); + void setSamplingKey(const KeyDescription & sampling_key_); /// Returns sampling expression AST for storage or nullptr if there is none. ASTPtr getSamplingKeyAST() const { return sampling_key.definition_ast; } /// Storage has user-defined (in CREATE query) sampling key. diff --git a/src/Storages/LiveView/StorageLiveView.h b/src/Storages/LiveView/StorageLiveView.h index fe62de224da..458e74eb506 100644 --- a/src/Storages/LiveView/StorageLiveView.h +++ b/src/Storages/LiveView/StorageLiveView.h @@ -165,6 +165,7 @@ public: const Context & context); private: + /// TODO move to common struct SelectQueryDescription StorageID select_table_id = StorageID::createEmpty(); /// Will be initialized in constructor ASTPtr inner_query; /// stored query : SELECT * FROM ( SELECT a FROM A) ASTPtr inner_subquery; /// stored query's innermost subquery if any diff --git a/src/Storages/MergeTree/MergeTreeData.cpp b/src/Storages/MergeTree/MergeTreeData.cpp index 63d163a593e..8b046673556 100644 --- a/src/Storages/MergeTree/MergeTreeData.cpp +++ b/src/Storages/MergeTree/MergeTreeData.cpp @@ -131,7 +131,6 @@ MergeTreeData::MergeTreeData( : IStorage(table_id_) , global_context(context_) , merging_params(merging_params_) - , settings_ast(metadata.settings_ast) , require_part_metadata(require_part_metadata_) , relative_data_path(relative_data_path_) , broken_part_callback(broken_part_callback_) @@ -145,6 +144,7 @@ MergeTreeData::MergeTreeData( if (relative_data_path.empty()) throw Exception("MergeTree storages require data path", ErrorCodes::INCORRECT_FILE_NAME); + setSettingsChanges(metadata.settings_ast); const auto settings = getSettings(); setProperties(metadata, /*only_check*/ false, attach); @@ -153,7 +153,7 @@ MergeTreeData::MergeTreeData( if (metadata.sample_by_ast != nullptr) { - StorageMetadataKeyField candidate_sampling_key = StorageMetadataKeyField::getKeyFromAST(metadata.sample_by_ast, getColumns(), global_context); + KeyDescription candidate_sampling_key = KeyDescription::getKeyFromAST(metadata.sample_by_ast, getColumns(), global_context); const auto & pk_sample_block = getPrimaryKey().sample_block; if (!pk_sample_block.has(candidate_sampling_key.column_names[0]) && !attach @@ -265,8 +265,8 @@ StorageInMemoryMetadata MergeTreeData::getInMemoryMetadata() const if (isSamplingKeyDefined()) metadata.sample_by_ast = getSamplingKeyAST()->clone(); - if (settings_ast) - metadata.settings_ast = settings_ast->clone(); + if (hasSettingsChanges()) + metadata.settings_ast = getSettingsChanges(); return metadata; } @@ -444,7 +444,7 @@ void MergeTreeData::setProperties(const StorageInMemoryMetadata & metadata, bool { setColumns(std::move(metadata.columns)); - StorageMetadataKeyField new_sorting_key; + KeyDescription new_sorting_key; new_sorting_key.definition_ast = metadata.order_by_ast; new_sorting_key.column_names = std::move(new_sorting_key_columns); new_sorting_key.expression_list_ast = std::move(new_sorting_key_expr_list); @@ -453,7 +453,7 @@ void MergeTreeData::setProperties(const StorageInMemoryMetadata & metadata, bool new_sorting_key.data_types = std::move(new_sorting_key_data_types); setSortingKey(new_sorting_key); - StorageMetadataKeyField new_primary_key; + KeyDescription new_primary_key; new_primary_key.definition_ast = metadata.primary_key_ast; new_primary_key.column_names = std::move(new_primary_key_columns); new_primary_key.expression_list_ast = std::move(new_primary_key_expr_list); @@ -472,7 +472,7 @@ namespace { ExpressionActionsPtr getCombinedIndicesExpression( - const StorageMetadataKeyField & key, + const KeyDescription & key, const IndicesDescription & indices, const ColumnsDescription & columns, const Context & context) @@ -523,7 +523,7 @@ ASTPtr MergeTreeData::extractKeyExpressionList(const ASTPtr & node) void MergeTreeData::initPartitionKey(ASTPtr partition_by_ast) { - StorageMetadataKeyField new_partition_key = StorageMetadataKeyField::getKeyFromAST(partition_by_ast, getColumns(), global_context); + KeyDescription new_partition_key = KeyDescription::getKeyFromAST(partition_by_ast, getColumns(), global_context); if (new_partition_key.expression_list_ast->children.empty()) return; @@ -1460,9 +1460,10 @@ void MergeTreeData::checkAlterIsPossible(const AlterCommands & commands, const S setTTLExpressions(metadata.columns, metadata.ttl_for_table_ast, /* only_check = */ true); - if (settings_ast) + if (hasSettingsChanges()) { - const auto & current_changes = settings_ast->as().changes; + + const auto & current_changes = getSettingsChanges()->as().changes; const auto & new_changes = metadata.settings_ast->as().changes; for (const auto & changed_setting : new_changes) { @@ -1601,7 +1602,7 @@ void MergeTreeData::changeSettings( MergeTreeSettings copy = *getSettings(); copy.applyChanges(new_changes); storage_settings.set(std::make_unique(copy)); - settings_ast = new_settings; + setSettingsChanges(new_settings); } } diff --git a/src/Storages/MergeTree/MergeTreeData.h b/src/Storages/MergeTree/MergeTreeData.h index 6df181e3f98..dcc6174ef5a 100644 --- a/src/Storages/MergeTree/MergeTreeData.h +++ b/src/Storages/MergeTree/MergeTreeData.h @@ -677,8 +677,6 @@ protected: friend struct ReplicatedMergeTreeTableMetadata; friend class StorageReplicatedMergeTree; - ASTPtr settings_ast; - bool require_part_metadata; String relative_data_path; diff --git a/src/Storages/StorageInMemoryMetadata.cpp b/src/Storages/StorageInMemoryMetadata.cpp index ee38637e118..1b7ec39b9e3 100644 --- a/src/Storages/StorageInMemoryMetadata.cpp +++ b/src/Storages/StorageInMemoryMetadata.cpp @@ -1,13 +1,5 @@ #include -#include -#include -#include -#include -#include -#include -#include - namespace DB { @@ -89,30 +81,4 @@ StorageInMemoryMetadata & StorageInMemoryMetadata::operator=(const StorageInMemo return *this; } -StorageMetadataKeyField StorageMetadataKeyField::getKeyFromAST(const ASTPtr & definition_ast, const ColumnsDescription & columns, const Context & context) -{ - StorageMetadataKeyField result; - result.definition_ast = definition_ast; - result.expression_list_ast = extractKeyExpressionList(definition_ast); - - if (result.expression_list_ast->children.empty()) - return result; - - const auto & children = result.expression_list_ast->children; - for (const auto & child : children) - result.column_names.emplace_back(child->getColumnName()); - - { - auto expr = result.expression_list_ast->clone(); - auto syntax_result = SyntaxAnalyzer(context).analyze(expr, columns.getAllPhysical()); - result.expression = ExpressionAnalyzer(expr, syntax_result, context).getActions(true); - result.sample_block = result.expression->getSampleBlock(); - } - - for (size_t i = 0; i < result.sample_block.columns(); ++i) - result.data_types.emplace_back(result.sample_block.getByPosition(i).type); - - return result; -} - } diff --git a/src/Storages/StorageInMemoryMetadata.h b/src/Storages/StorageInMemoryMetadata.h index 39bc8fd2b31..39374166d5e 100644 --- a/src/Storages/StorageInMemoryMetadata.h +++ b/src/Storages/StorageInMemoryMetadata.h @@ -3,7 +3,6 @@ #include #include #include -#include #include namespace DB @@ -44,33 +43,4 @@ struct StorageInMemoryMetadata StorageInMemoryMetadata & operator=(const StorageInMemoryMetadata & other); }; -/// Common structure for primary, partition and other storage keys -struct StorageMetadataKeyField -{ - /// User defined AST in CREATE/ALTER query. This field may be empty, but key - /// can exists because some of them maybe set implicitly (for example, - /// primary key in merge tree can be part of sorting key) - ASTPtr definition_ast; - - /// ASTExpressionList with key fields, example: (x, toStartOfMonth(date))). - ASTPtr expression_list_ast; - - /// Expression from expression_list_ast created by ExpressionAnalyzer. Useful, - /// when you need to get required columns for key, example: a, date, b. - ExpressionActionsPtr expression; - - /// Sample block with key columns (names, types, empty column) - Block sample_block; - - /// Column names in key definition, example: x, toStartOfMonth(date), a * b. - Names column_names; - - /// Types from sample block ordered in columns order. - DataTypes data_types; - - /// Parse key structure from key definition. Requires all columns, available - /// in storage. - static StorageMetadataKeyField getKeyFromAST(const ASTPtr & definition_ast, const ColumnsDescription & columns, const Context & context); -}; - } diff --git a/src/Storages/StorageMaterializedView.cpp b/src/Storages/StorageMaterializedView.cpp index 1b8c6acb49f..313e75a169e 100644 --- a/src/Storages/StorageMaterializedView.cpp +++ b/src/Storages/StorageMaterializedView.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include @@ -41,58 +42,6 @@ static inline String generateInnerTableName(const StorageID & view_id) return ".inner." + view_id.getTableName(); } -static StorageID extractDependentTableFromSelectQuery(ASTSelectQuery & query, const Context & context, bool add_default_db = true) -{ - if (add_default_db) - { - AddDefaultDatabaseVisitor visitor(context.getCurrentDatabase(), nullptr); - visitor.visit(query); - } - - if (auto db_and_table = getDatabaseAndTable(query, 0)) - { - return StorageID(db_and_table->database, db_and_table->table/*, db_and_table->uuid*/); - } - else if (auto subquery = extractTableExpression(query, 0)) - { - auto * ast_select = subquery->as(); - if (!ast_select) - throw Exception("Logical error while creating StorageMaterializedView. " - "Could not retrieve table name from select query.", - DB::ErrorCodes::LOGICAL_ERROR); - if (ast_select->list_of_selects->children.size() != 1) - throw Exception("UNION is not supported for MATERIALIZED VIEW", - ErrorCodes::QUERY_IS_NOT_SUPPORTED_IN_MATERIALIZED_VIEW); - - auto & inner_query = ast_select->list_of_selects->children.at(0); - - return extractDependentTableFromSelectQuery(inner_query->as(), context, false); - } - else - return StorageID::createEmpty(); -} - - -static void checkAllowedQueries(const ASTSelectQuery & query) -{ - if (query.prewhere() || query.final() || query.sampleSize()) - throw Exception("MATERIALIZED VIEW cannot have PREWHERE, SAMPLE or FINAL.", DB::ErrorCodes::QUERY_IS_NOT_SUPPORTED_IN_MATERIALIZED_VIEW); - - ASTPtr subquery = extractTableExpression(query, 0); - if (!subquery) - return; - - if (const auto * ast_select = subquery->as()) - { - if (ast_select->list_of_selects->children.size() != 1) - throw Exception("UNION is not supported for MATERIALIZED VIEW", ErrorCodes::QUERY_IS_NOT_SUPPORTED_IN_MATERIALIZED_VIEW); - - const auto & inner_query = ast_select->list_of_selects->children.at(0); - - checkAllowedQueries(inner_query->as()); - } -} - StorageMaterializedView::StorageMaterializedView( const StorageID & table_id_, @@ -117,13 +66,8 @@ StorageMaterializedView::StorageMaterializedView( if (query.select->list_of_selects->children.size() != 1) throw Exception("UNION is not supported for MATERIALIZED VIEW", ErrorCodes::QUERY_IS_NOT_SUPPORTED_IN_MATERIALIZED_VIEW); - select = query.select->clone(); - inner_query = query.select->list_of_selects->children.at(0); - - auto & select_query = inner_query->as(); - checkAllowedQueries(select_query); - - select_table_id = extractDependentTableFromSelectQuery(select_query, local_context); + auto select = SelectQueryDescription::getSelectQueryFromASTForMatView(query.select->clone(), local_context); + setSelectQuery(select); if (!has_inner_table) target_table_id = query.to_table_id; @@ -152,14 +96,14 @@ StorageMaterializedView::StorageMaterializedView( target_table_id = DatabaseCatalog::instance().getTable({manual_create_query->database, manual_create_query->table}, global_context)->getStorageID(); } - if (!select_table_id.empty()) - DatabaseCatalog::instance().addDependency(select_table_id, getStorageID()); + if (!select.select_table_id.empty()) + DatabaseCatalog::instance().addDependency(select.select_table_id, getStorageID()); } StorageInMemoryMetadata StorageMaterializedView::getInMemoryMetadata() const { StorageInMemoryMetadata result(getColumns(), getSecondaryIndices(), getConstraints()); - result.select = getSelectQuery(); + result.select = getSelectQuery().select_query; return result; } @@ -222,8 +166,9 @@ static void executeDropQuery(ASTDropQuery::Kind kind, Context & global_context, void StorageMaterializedView::drop() { auto table_id = getStorageID(); - if (!select_table_id.empty()) - DatabaseCatalog::instance().removeDependency(select_table_id, table_id); + const auto & select_query = getSelectQuery(); + if (!select_query.select_table_id.empty()) + DatabaseCatalog::instance().removeDependency(select_query.select_table_id, table_id); if (has_inner_table && tryGetTargetTable()) executeDropQuery(ASTDropQuery::Kind::Drop, global_context, target_table_id); @@ -262,21 +207,12 @@ void StorageMaterializedView::alter( /// start modify query if (context.getSettingsRef().allow_experimental_alter_materialized_view_structure) { - auto & new_select = metadata.select->as(); + auto new_select = SelectQueryDescription::getSelectQueryFromASTForMatView(metadata.select, context); + const auto & old_select = getSelectQuery(); - if (new_select.list_of_selects->children.size() != 1) - throw Exception("UNION is not supported for MATERIALIZED VIEW", ErrorCodes::QUERY_IS_NOT_SUPPORTED_IN_MATERIALIZED_VIEW); + DatabaseCatalog::instance().updateDependency(old_select.select_table_id, table_id, new_select.select_table_id, table_id); - auto & new_inner_query = new_select.list_of_selects->children.at(0); - auto & select_query = new_inner_query->as(); - checkAllowedQueries(select_query); - - auto new_select_table_id = extractDependentTableFromSelectQuery(select_query, context); - DatabaseCatalog::instance().updateDependency(select_table_id, table_id, new_select_table_id, table_id); - - select_table_id = new_select_table_id; - select = metadata.select; - inner_query = new_inner_query; + setSelectQuery(new_select); } /// end modify query @@ -349,15 +285,17 @@ void StorageMaterializedView::renameInMemory(const StorageID & new_table_id) } IStorage::renameInMemory(new_table_id); + const auto & select_query = getSelectQuery(); // TODO Actually we don't need to update dependency if MV has UUID, but then db and table name will be outdated - DatabaseCatalog::instance().updateDependency(select_table_id, old_table_id, select_table_id, getStorageID()); + DatabaseCatalog::instance().updateDependency(select_query.select_table_id, old_table_id, select_query.select_table_id, getStorageID()); } void StorageMaterializedView::shutdown() { + const auto & select_query = getSelectQuery(); /// Make sure the dependency is removed after DETACH TABLE - if (!select_table_id.empty()) - DatabaseCatalog::instance().removeDependency(select_table_id, getStorageID()); + if (!select_query.select_table_id.empty()) + DatabaseCatalog::instance().removeDependency(select_query.select_table_id, getStorageID()); } StoragePtr StorageMaterializedView::getTargetTable() const diff --git a/src/Storages/StorageMaterializedView.h b/src/Storages/StorageMaterializedView.h index fd8639abb6a..d5f81e2248e 100644 --- a/src/Storages/StorageMaterializedView.h +++ b/src/Storages/StorageMaterializedView.h @@ -19,8 +19,6 @@ public: std::string getName() const override { return "MaterializedView"; } bool isView() const override { return true; } - ASTPtr getSelectQuery() const { return select->clone(); } - ASTPtr getInnerQuery() const { return inner_query->clone(); } bool hasInnerTable() const { return has_inner_table; } StorageInMemoryMetadata getInMemoryMetadata() const override; @@ -76,14 +74,9 @@ public: Strings getDataPaths() const override; private: - /// Can be empty if SELECT query doesn't contain table - StorageID select_table_id = StorageID::createEmpty(); /// Will be initialized in constructor StorageID target_table_id = StorageID::createEmpty(); - ASTPtr select; - ASTPtr inner_query; - Context & global_context; bool has_inner_table = false; diff --git a/src/Storages/StorageView.cpp b/src/Storages/StorageView.cpp index c6b37a50aa9..2525ac48732 100644 --- a/src/Storages/StorageView.cpp +++ b/src/Storages/StorageView.cpp @@ -12,6 +12,7 @@ #include #include +#include #include @@ -45,7 +46,10 @@ StorageView::StorageView( if (!query.select) throw Exception("SELECT query is not specified for " + getName(), ErrorCodes::INCORRECT_QUERY); - inner_query = query.select->ptr(); + SelectQueryDescription description; + + description.inner_query = query.select->ptr(); + setSelectQuery(description); } @@ -59,7 +63,7 @@ Pipes StorageView::read( { Pipes pipes; - ASTPtr current_inner_query = inner_query; + ASTPtr current_inner_query = getSelectQuery().inner_query; if (context.getSettings().enable_optimize_predicate_expression) current_inner_query = getRuntimeViewQuery(*query_info.query->as(), context); @@ -119,7 +123,7 @@ static void replaceTableNameWithSubquery(ASTSelectQuery * select_query, ASTPtr & ASTPtr StorageView::getRuntimeViewQuery(ASTSelectQuery * outer_query, const Context & context, bool normalize) { - auto runtime_view_query = inner_query->clone(); + auto runtime_view_query = getSelectQuery().inner_query->clone(); /// TODO: remove getTableExpressions and getTablesWithColumns { diff --git a/src/Storages/StorageView.h b/src/Storages/StorageView.h index 86550db83ce..179da5a07b9 100644 --- a/src/Storages/StorageView.h +++ b/src/Storages/StorageView.h @@ -33,9 +33,6 @@ public: ASTPtr getRuntimeViewQuery(ASTSelectQuery * outer_query, const Context & context, bool normalize); -private: - ASTPtr inner_query; - protected: StorageView( const StorageID & table_id_, diff --git a/src/Storages/TTLDescription.cpp b/src/Storages/TTLDescription.cpp index da9691aab4a..3bef8894971 100644 --- a/src/Storages/TTLDescription.cpp +++ b/src/Storages/TTLDescription.cpp @@ -59,7 +59,7 @@ TTLDescription TTLDescription::getTTLFromAST( const ASTPtr & definition_ast, const ColumnsDescription & columns, const Context & context, - const StorageMetadataKeyField & primary_key) + const KeyDescription & primary_key) { TTLDescription result; const auto * ttl_element = definition_ast->as(); diff --git a/src/Storages/TTLDescription.h b/src/Storages/TTLDescription.h index 99a145b8acc..1ad6960ee3b 100644 --- a/src/Storages/TTLDescription.h +++ b/src/Storages/TTLDescription.h @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -73,7 +74,7 @@ struct TTLDescription /// Parse TTL structure from definition. Able to parse both column and table /// TTLs. - static TTLDescription getTTLFromAST(const ASTPtr & definition_ast, const ColumnsDescription & columns, const Context & context, const StorageMetadataKeyField & primary_key); + static TTLDescription getTTLFromAST(const ASTPtr & definition_ast, const ColumnsDescription & columns, const Context & context, const KeyDescription & primary_key); }; /// Mapping from column name to column TTL diff --git a/src/Storages/ya.make b/src/Storages/ya.make index a28dd393929..b10a6194b7c 100644 --- a/src/Storages/ya.make +++ b/src/Storages/ya.make @@ -176,6 +176,8 @@ SRCS( VirtualColumnUtils.cpp extractKeyExpressionList.cpp TTLDescription.cpp + KeyDescription.cpp + SelectQueryDescription.cpp ) END()