| 1366 | |
| 1367 | |
| 1368 | std::optional<QueryPipeline> StorageDistributed::distributedWrite(const ASTInsertQuery & query, ContextPtr local_context) |
| 1369 | { |
| 1370 | const Settings & settings = local_context->getSettingsRef(); |
| 1371 | if (settings[Setting::max_distributed_depth] && local_context->getClientInfo().distributed_depth >= settings[Setting::max_distributed_depth]) |
| 1372 | throw Exception(ErrorCodes::TOO_LARGE_DISTRIBUTED_DEPTH, "Maximum distributed depth exceeded"); |
| 1373 | |
| 1374 | auto & select = query.select->as<ASTSelectWithUnionQuery &>(); |
| 1375 | |
| 1376 | StoragePtr src_storage; |
| 1377 | |
| 1378 | /// Distributed write only works in the most trivial case INSERT ... SELECT |
| 1379 | /// without any unions or joins on the right side |
| 1380 | if (select.list_of_selects->children.size() == 1) |
| 1381 | { |
| 1382 | if (auto * select_query = select.list_of_selects->children.at(0)->as<ASTSelectQuery>()) |
| 1383 | { |
| 1384 | if (local_context->getSettingsRef()[Setting::enable_global_with_statement]) |
| 1385 | ApplyWithAliasVisitor::visit(select.list_of_selects->children.at(0)); |
| 1386 | ApplyWithSubqueryVisitor(local_context).visit(select.list_of_selects->children.at(0)); |
| 1387 | |
| 1388 | JoinedTables joined_tables(Context::createCopy(local_context), *select_query); |
| 1389 | |
| 1390 | if (joined_tables.tablesCount() == 1) |
| 1391 | { |
| 1392 | src_storage = joined_tables.getLeftTableStorage(); |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | if (!src_storage) |
| 1398 | return {}; |
| 1399 | |
| 1400 | if (auto src_distributed = std::dynamic_pointer_cast<StorageDistributed>(src_storage)) |
| 1401 | { |
| 1402 | return distributedWriteBetweenDistributedTables(*src_distributed, query, local_context); |
| 1403 | } |
| 1404 | if (auto src_storage_cluster = std::dynamic_pointer_cast<IStorageCluster>(src_storage)) |
| 1405 | { |
| 1406 | return distributedWriteFromClusterStorage(*src_storage_cluster, query, local_context); |
| 1407 | } |
| 1408 | |
| 1409 | return {}; |
| 1410 | } |
| 1411 | |
| 1412 | |
| 1413 | void StorageDistributed::checkAlterIsPossible(const AlterCommands & commands, ContextPtr local_context) const |
nothing calls this directly
no test coverage detected