| 163 | |
| 164 | |
| 165 | StoragePtr InterpreterInsertQuery::getTable(ASTInsertQuery & query) |
| 166 | { |
| 167 | if (query.table_function) |
| 168 | { |
| 169 | const auto & factory = TableFunctionFactory::instance(); |
| 170 | TableFunctionPtr table_function_ptr = factory.get(query.table_function, getContext()); |
| 171 | return table_function_ptr->execute(query.table_function, getContext(), table_function_ptr->getName()); |
| 172 | } |
| 173 | |
| 174 | if (getContext()->getServerType() == ServerType::cnch_worker && !query.select) |
| 175 | { |
| 176 | query.table_id = getContext()->resolveStorageID(query.table_id); |
| 177 | auto storage = DatabaseCatalog::instance().tryGetTable(query.table_id, getContext()); |
| 178 | if (auto * cnch_table = dynamic_cast<StorageCnchMergeTree *>(storage.get())) |
| 179 | { |
| 180 | auto create_query = cnch_table->getCreateQueryForCloudTable( |
| 181 | cnch_table->getCreateTableSql(), |
| 182 | query.table_id.table_name, |
| 183 | nullptr, |
| 184 | false, |
| 185 | std::nullopt, |
| 186 | Strings{}, |
| 187 | query.table_id.database_name); |
| 188 | LOG_TRACE(&Poco::Logger::get(__PRETTY_FUNCTION__), "Worker side create query: {}", create_query); |
| 189 | |
| 190 | NameSet view_create_sqls = genViewDependencyCreateQueries(storage, getContext()); |
| 191 | if (!view_create_sqls.empty()) |
| 192 | { |
| 193 | ContextMutablePtr mutable_context = const_pointer_cast<Context>(getContext()); |
| 194 | if (!mutable_context->tryGetCnchWorkerResource()) |
| 195 | mutable_context->initCnchWorkerResource(); |
| 196 | view_create_sqls.insert(create_query); |
| 197 | for (const auto & create_sql : view_create_sqls) |
| 198 | mutable_context->getCnchWorkerResource()->executeCreateQuery(mutable_context, create_sql); |
| 199 | if (auto worker_txn = dynamic_pointer_cast<CnchWorkerTransaction>(mutable_context->getCurrentTransaction()); worker_txn) |
| 200 | { |
| 201 | if (view_create_sqls.size() > 1) |
| 202 | { |
| 203 | worker_txn->enableExplicitCommit(); |
| 204 | worker_txn->setExplicitCommitStorageID(storage->getStorageID()); |
| 205 | } |
| 206 | |
| 207 | mutable_context->setCurrentTransaction(worker_txn); |
| 208 | } |
| 209 | return mutable_context->getCnchWorkerResource()->getTable(query.table_id); |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | query.table_id = getContext()->resolveStorageID(query.table_id); |
| 214 | return DatabaseCatalog::instance().getTable(query.table_id, getContext()); |
| 215 | } |
| 216 | } |
| 217 | else |
| 218 | return storage; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | query.table_id = getContext()->resolveStorageID(query.table_id); |
no test coverage detected