@@ -598,15 +598,35 @@ bool ReplicatedMergeTreeSinkImpl<false>::writeExistingPart(MergeTreeData::Mutabl
598598 int error = 0 ;
599599 String error_message;
600600 // / Set a special error code if the block is duplicate
601- // / And remove attaching_ prefix
602601 if (deduplicate && deduplicated)
603602 {
604603 error = ErrorCodes::INSERT_WAS_DEDUPLICATED;
605604 error_message = " The part was deduplicated" ;
606- if (!endsWith (part->getDataPartStorage ().getRelativePath (), " detached/attaching_" + part->name + " /" ))
607- throw Exception (ErrorCodes::LOGICAL_ERROR, " Unexpected relative path for a deduplicated part: {}" , part->getDataPartStorage ().getRelativePath ());
608- fs::path new_relative_path = fs::path (" detached" ) / part->getNewName (part->info );
609- part->renameTo (new_relative_path, false );
605+
606+ const auto & relative_path = part->getDataPartStorage ().getRelativePath ();
607+ const auto part_dir = fs::path (relative_path).parent_path ().filename ().string ();
608+
609+ if (relative_path.ends_with (" detached/attaching_" + part->name + " /" ))
610+ {
611+ // / Part came from ATTACH PART - rename back to detached/ (remove attaching_ prefix)
612+ fs::path new_relative_path = fs::path (" detached" ) / part->getNewName (part->info );
613+ part->renameTo (new_relative_path, false );
614+ }
615+ else if (part_dir.starts_with (" tmp_restore_" + part->name ))
616+ {
617+ // / Part came from RESTORE with a temporary directory.
618+ // / Just remove the temporary part since it's a duplicate.
619+ LOG_DEBUG (log, " Removing deduplicated part {} from temporary path {}" , part->name , relative_path);
620+ part->removeIfNeeded ();
621+ }
622+ else
623+ {
624+ throw Exception (
625+ ErrorCodes::LOGICAL_ERROR,
626+ " Unexpected deduplicated part with relative path '{}' and part directory '{}'. "
627+ " Expected relative path to end with 'detached/attaching_{}/' or part directory to start with 'tmp_restore_{}'." ,
628+ relative_path, part_dir, part->name , part->name );
629+ }
610630 }
611631 PartLog::addNewPart (storage.getContext (), PartLog::PartLogEntry (part, watch.elapsed (), profile_events_scope.getSnapshot ()), {block_id}, ExecutionStatus (error, error_message));
612632 return deduplicated;
0 commit comments