MCPcopy Create free account
hub / github.com/ClickHouse/ClickHouse / executeToTableImpl

Method executeToTableImpl

src/Interpreters/InterpreterDropQuery.cpp:156–373  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

154}
155
156BlockIO InterpreterDropQuery::executeToTableImpl(const ContextPtr & context_, ASTDropQuery & query, DatabasePtr & db, UUID & uuid_to_wait)
157{
158 if (query.kind == ASTDropQuery::Kind::Detach && query.isTemporary())
159 throw Exception(ErrorCodes::SYNTAX_ERROR, "DETACH of TEMPORARY tables are not supported");
160
161 /// NOTE: it does not contain UUID, we will resolve it with locked DDLGuard
162 auto table_id = StorageID(query);
163 if (query.isTemporary() || table_id.database_name.empty())
164 {
165 if (context_->tryResolveStorageID(table_id, Context::ResolveExternal))
166 return executeToTemporaryTable(table_id.getTableName(), query.kind);
167 query.setDatabase(table_id.database_name = context_->getCurrentDatabase());
168 }
169
170 if (query.isTemporary())
171 {
172 if (query.if_exists)
173 return {};
174 throw Exception(ErrorCodes::UNKNOWN_TABLE, "Temporary table {} doesn't exist", backQuoteIfNeed(table_id.table_name));
175 }
176
177 auto ddl_guard = (!query.no_ddl_lock ? DatabaseCatalog::instance().getDDLGuard(table_id.database_name, table_id.table_name, nullptr) : nullptr);
178
179 /// If table was already dropped by anyone, an exception will be thrown
180 auto [database, table] = query.if_exists ? DatabaseCatalog::instance().tryGetDatabaseAndTable(table_id, context_)
181 : DatabaseCatalog::instance().getDatabaseAndTable(table_id, context_);
182
183 if (database && table)
184 {
185 const auto & settings = getContext()->getSettingsRef();
186 if (query.if_empty)
187 {
188 if (auto rows = table->totalRows(getContext()); rows > 0)
189 throw Exception(ErrorCodes::TABLE_NOT_EMPTY, "Table {} is not empty", backQuoteIfNeed(table_id.table_name));
190 }
191 checkStorageSupportsTransactionsIfNeeded(table, context_);
192
193 auto & ast_drop_query = query.as<ASTDropQuery &>();
194
195 if (ast_drop_query.is_view && !table->isView())
196 throw Exception(ErrorCodes::INCORRECT_QUERY,
197 "Table {} is not a View",
198 table_id.getNameForLogs());
199
200 if (ast_drop_query.is_dictionary && !table->isDictionary())
201 throw Exception(ErrorCodes::INCORRECT_QUERY,
202 "Table {} is not a Dictionary",
203 table_id.getNameForLogs());
204
205 bool secondary_query = getContext()->isDDLOrOnClusterInternal();
206
207 /// Don't ignore DROP for refreshable materialized views: TRUNCATE doesn't stop
208 /// the periodic refresh task, so the orphaned view would keep refreshing indefinitely,
209 /// consuming background pool threads and potentially overwhelming the server.
210 auto * materialized_view = dynamic_cast<StorageMaterializedView *>(table.get());
211 bool is_refreshable_view = materialized_view && materialized_view->isRefreshable();
212
213 /// Prevents recursive drop from drop database query. The original query must specify a table.

Callers

nothing calls this directly

Calls 15

backQuoteIfNeedFunction · 0.85
maybeRemoveOnClusterFunction · 0.85
executeDDLQueryOnClusterFunction · 0.85
tryResolveStorageIDMethod · 0.80
getCurrentDatabaseMethod · 0.80
getDDLGuardMethod · 0.80
getDatabaseAndTableMethod · 0.80
isRefreshableMethod · 0.80
releaseTableLockMethod · 0.80
lockExclusivelyMethod · 0.80

Tested by

no test coverage detected