diff --git a/dbms/src/Interpreters/InterpreterCreateQuery.cpp b/dbms/src/Interpreters/InterpreterCreateQuery.cpp index bdf30b86da6..2233afebaf3 100644 --- a/dbms/src/Interpreters/InterpreterCreateQuery.cpp +++ b/dbms/src/Interpreters/InterpreterCreateQuery.cpp @@ -488,6 +488,7 @@ BlockIO InterpreterCreateQuery::createTable(ASTCreateQuery & create) /// For `view` type tables, you may need `sample_block` to get the columns. if (create.select && (!create.attach || (!create.columns && (create.is_view || create.is_materialized_view)))) { + create.select->setDatabaseIfNeeded(database_name); interpreter_select = std::make_unique(create.select->ptr(), context); as_select_sample = interpreter_select->getSampleBlock(); } diff --git a/dbms/src/Storages/StorageMaterializedView.cpp b/dbms/src/Storages/StorageMaterializedView.cpp index b07780ead7e..3e95b75a5c4 100644 --- a/dbms/src/Storages/StorageMaterializedView.cpp +++ b/dbms/src/Storages/StorageMaterializedView.cpp @@ -72,9 +72,6 @@ StorageMaterializedView::StorageMaterializedView( if (!query.inner_storage) throw Exception("ENGINE of MaterializedView should be specified explicitly", ErrorCodes::INCORRECT_QUERY); - /// If the internal query does not specify a database, retrieve it from the context and write it to the query. - query.select->setDatabaseIfNeeded(database_name); - extractDependentTable(*query.select, select_database_name, select_table_name); if (!select_table_name.empty()) diff --git a/dbms/src/Storages/StorageView.cpp b/dbms/src/Storages/StorageView.cpp index 507b9e13a4f..3cf5b392098 100644 --- a/dbms/src/Storages/StorageView.cpp +++ b/dbms/src/Storages/StorageView.cpp @@ -12,7 +12,7 @@ namespace DB namespace ErrorCodes { - extern const int LOGICAL_ERROR; + extern const int INCORRECT_QUERY; } @@ -28,8 +28,9 @@ StorageView::StorageView( : IStorage{materialized_columns_, alias_columns_, column_defaults_}, table_name(table_name_), database_name(database_name_), context(context_), columns(columns_) { - /// If the internal query does not specify a database, retrieve it from the context and write it to the query. - query.select->setDatabaseIfNeeded(database_name); + if (!query.select) + throw Exception("SELECT query is not specified for " + getName(), ErrorCodes::INCORRECT_QUERY); + inner_query = query.select->ptr(); }