33
44#include < Storages/MergeTree/ExportList.h>
55#include < Access/AccessControl.h>
6+ #include < TableFunctions/TableFunctionFactory.h>
67#include < AggregateFunctions/AggregateFunctionCount.h>
78#include < Analyzer/QueryTreeBuilder.h>
89#include < Analyzer/Utils.h>
@@ -6215,9 +6216,45 @@ void MergeTreeData::exportPartToTable(const PartitionCommand & command, ContextP
62156216
62166217 const auto part_name = command.partition ->as <ASTLiteral &>().value .safeGet <String>();
62176218
6218- const auto database_name = query_context->resolveDatabase (command.to_database );
6219+ if (!command.to_table_function )
6220+ {
6221+ const auto database_name = query_context->resolveDatabase (command.to_database );
6222+ exportPartToTable (part_name, StorageID{database_name, command.to_table }, generateSnowflakeIDString (), query_context);
6223+
6224+ return ;
6225+ }
6226+
6227+ auto table_function_ast = command.to_table_function ;
6228+ auto table_function_ptr = TableFunctionFactory::instance ().get (command.to_table_function , query_context);
6229+
6230+ if (table_function_ptr->needStructureHint ())
6231+ {
6232+ const auto source_metadata_ptr = getInMemoryMetadataPtr ();
6233+
6234+ // / Grab only the readable columns from the source metadata to skip ephemeral columns
6235+ const auto readable_columns = ColumnsDescription (source_metadata_ptr->getColumns ().getReadable ());
6236+ table_function_ptr->setStructureHint (readable_columns);
6237+ }
6238+
6239+ if (command.partition_by_expr )
6240+ {
6241+ table_function_ptr->setPartitionBy (command.partition_by_expr );
6242+ }
6243+
6244+ auto dest_storage = table_function_ptr->execute (
6245+ table_function_ast,
6246+ query_context,
6247+ table_function_ptr->getName (),
6248+ /* cached_columns */ {},
6249+ /* use_global_context */ false ,
6250+ /* is_insert_query */ true );
6251+
6252+ if (!dest_storage)
6253+ {
6254+ throw Exception (ErrorCodes::BAD_ARGUMENTS, " Failed to reconstruct destination storage" );
6255+ }
62196256
6220- exportPartToTable (part_name, StorageID{database_name, command. to_table } , generateSnowflakeIDString (), query_context);
6257+ exportPartToTable (part_name, dest_storage , generateSnowflakeIDString (), query_context);
62216258}
62226259
62236260void MergeTreeData::exportPartToTable (
@@ -6235,6 +6272,17 @@ void MergeTreeData::exportPartToTable(
62356272 throw Exception (ErrorCodes::BAD_ARGUMENTS, " Exporting to the same table is not allowed" );
62366273 }
62376274
6275+ exportPartToTable (part_name, dest_storage, transaction_id, query_context, allow_outdated_parts, completion_callback);
6276+ }
6277+
6278+ void MergeTreeData::exportPartToTable (
6279+ const std::string & part_name,
6280+ const StoragePtr & dest_storage,
6281+ const String & transaction_id,
6282+ ContextPtr query_context,
6283+ bool allow_outdated_parts,
6284+ std::function<void (MergeTreePartExportManifest::CompletionCallbackResult)> completion_callback)
6285+ {
62386286 if (!dest_storage->supportsImport ())
62396287 throw Exception (ErrorCodes::NOT_IMPLEMENTED, " Destination storage {} does not support MergeTree parts or uses unsupported partitioning" , dest_storage->getName ());
62406288
@@ -6304,7 +6352,7 @@ void MergeTreeData::exportPartToTable(
63046352 {
63056353 const auto format_settings = getFormatSettings (query_context);
63066354 MergeTreePartExportManifest manifest (
6307- dest_storage-> getStorageID () ,
6355+ dest_storage,
63086356 part,
63096357 transaction_id,
63106358 query_context->getCurrentQueryId (),
@@ -6317,8 +6365,7 @@ void MergeTreeData::exportPartToTable(
63176365
63186366 if (!export_manifests.emplace (std::move (manifest)).second )
63196367 {
6320- throw Exception (ErrorCodes::ABORTED, " Data part '{}' is already being exported to table '{}'" ,
6321- part_name, dest_storage->getStorageID ().getFullTableName ());
6368+ throw Exception (ErrorCodes::ABORTED, " Data part '{}' is already being exported" , part_name);
63226369 }
63236370 }
63246371
@@ -8751,8 +8798,9 @@ std::vector<MergeTreeExportStatus> MergeTreeData::getExportsStatus() const
87518798
87528799 status.source_database = source_database;
87538800 status.source_table = source_table;
8754- status.destination_database = manifest.destination_storage_id .database_name ;
8755- status.destination_table = manifest.destination_storage_id .table_name ;
8801+ const auto destination_storage_id = manifest.destination_storage_ptr ->getStorageID ();
8802+ status.destination_database = destination_storage_id.database_name ;
8803+ status.destination_table = destination_storage_id.table_name ;
87568804 status.create_time = manifest.create_time ;
87578805 status.part_name = manifest.data_part ->name ;
87588806
0 commit comments