44
55#include < Storages/MergeTree/ExportList.h>
66#include < Access/AccessControl.h>
7+ #include < TableFunctions/TableFunctionFactory.h>
78#include < AggregateFunctions/AggregateFunctionCount.h>
89#include < Analyzer/QueryTreeBuilder.h>
910#include < Analyzer/Utils.h>
@@ -6444,9 +6445,45 @@ void MergeTreeData::exportPartToTable(const PartitionCommand & command, ContextP
64446445
64456446 const auto part_name = command.partition ->as <ASTLiteral &>().value .safeGet <String>();
64466447
6447- const auto database_name = query_context->resolveDatabase (command.to_database );
6448+ if (!command.to_table_function )
6449+ {
6450+ const auto database_name = query_context->resolveDatabase (command.to_database );
6451+ exportPartToTable (part_name, StorageID{database_name, command.to_table }, generateSnowflakeIDString (), query_context);
6452+
6453+ return ;
6454+ }
6455+
6456+ auto table_function_ast = command.to_table_function ;
6457+ auto table_function_ptr = TableFunctionFactory::instance ().get (command.to_table_function , query_context);
6458+
6459+ if (table_function_ptr->needStructureHint ())
6460+ {
6461+ const auto source_metadata_ptr = getInMemoryMetadataPtr ();
6462+
6463+ // / Grab only the readable columns from the source metadata to skip ephemeral columns
6464+ const auto readable_columns = ColumnsDescription (source_metadata_ptr->getColumns ().getReadable ());
6465+ table_function_ptr->setStructureHint (readable_columns);
6466+ }
6467+
6468+ if (command.partition_by_expr )
6469+ {
6470+ table_function_ptr->setPartitionBy (command.partition_by_expr );
6471+ }
6472+
6473+ auto dest_storage = table_function_ptr->execute (
6474+ table_function_ast,
6475+ query_context,
6476+ table_function_ptr->getName (),
6477+ /* cached_columns */ {},
6478+ /* use_global_context */ false ,
6479+ /* is_insert_query */ true );
6480+
6481+ if (!dest_storage)
6482+ {
6483+ throw Exception (ErrorCodes::BAD_ARGUMENTS, " Failed to reconstruct destination storage" );
6484+ }
64486485
6449- exportPartToTable (part_name, StorageID{database_name, command. to_table } , generateSnowflakeIDString (), query_context);
6486+ exportPartToTable (part_name, dest_storage , generateSnowflakeIDString (), query_context);
64506487}
64516488
64526489void MergeTreeData::exportPartToTable (
@@ -6464,6 +6501,17 @@ void MergeTreeData::exportPartToTable(
64646501 throw Exception (ErrorCodes::BAD_ARGUMENTS, " Exporting to the same table is not allowed" );
64656502 }
64666503
6504+ exportPartToTable (part_name, dest_storage, transaction_id, query_context, allow_outdated_parts, completion_callback);
6505+ }
6506+
6507+ void MergeTreeData::exportPartToTable (
6508+ const std::string & part_name,
6509+ const StoragePtr & dest_storage,
6510+ const String & transaction_id,
6511+ ContextPtr query_context,
6512+ bool allow_outdated_parts,
6513+ std::function<void (MergeTreePartExportManifest::CompletionCallbackResult)> completion_callback)
6514+ {
64676515 if (!dest_storage->supportsImport ())
64686516 throw Exception (ErrorCodes::NOT_IMPLEMENTED, " Destination storage {} does not support MergeTree parts or uses unsupported partitioning" , dest_storage->getName ());
64696517
@@ -6533,7 +6581,7 @@ void MergeTreeData::exportPartToTable(
65336581 {
65346582 const auto format_settings = getFormatSettings (query_context);
65356583 MergeTreePartExportManifest manifest (
6536- dest_storage-> getStorageID () ,
6584+ dest_storage,
65376585 part,
65386586 transaction_id,
65396587 query_context->getCurrentQueryId (),
@@ -6546,8 +6594,7 @@ void MergeTreeData::exportPartToTable(
65466594
65476595 if (!export_manifests.emplace (std::move (manifest)).second )
65486596 {
6549- throw Exception (ErrorCodes::ABORTED, " Data part '{}' is already being exported to table '{}'" ,
6550- part_name, dest_storage->getStorageID ().getFullTableName ());
6597+ throw Exception (ErrorCodes::ABORTED, " Data part '{}' is already being exported" , part_name);
65516598 }
65526599 }
65536600
@@ -9038,8 +9085,9 @@ std::vector<MergeTreeExportStatus> MergeTreeData::getExportsStatus() const
90389085
90399086 status.source_database = source_database;
90409087 status.source_table = source_table;
9041- status.destination_database = manifest.destination_storage_id .database_name ;
9042- status.destination_table = manifest.destination_storage_id .table_name ;
9088+ const auto destination_storage_id = manifest.destination_storage_ptr ->getStorageID ();
9089+ status.destination_database = destination_storage_id.database_name ;
9090+ status.destination_table = destination_storage_id.table_name ;
90439091 status.create_time = manifest.create_time ;
90449092 status.part_name = manifest.data_part ->name ;
90459093
0 commit comments