@@ -264,18 +264,44 @@ std::string normalizePathToStorageRoot(const std::string & table_location, const
264264 rel_path = rel_path.substr (1 );
265265
266266 // Check if the relative path already starts with the table location key (so the key is already relative to storage root)
267- if (!rel_path.empty () && rel_path.starts_with (base.key ))
267+ // This handles cases where the path in metadata is already relative to storage root, not relative to table location
268+ if (!rel_path.empty () && !base.key .empty ())
268269 {
269- std::filesystem::path fs_path (rel_path);
270- std::filesystem::path normalized = fs_path.lexically_normal ();
271-
272- std::string result = normalized.string ();
273- std::replace (result.begin (), result.end (), ' \\ ' , ' /' );
274-
275- while (!result.empty () && result.front () == ' /' )
276- result = result.substr (1 );
270+ // Check if path starts with base.key (exact match or followed by '/')
271+ if (rel_path == base.key || rel_path.starts_with (base.key + " /" ))
272+ {
273+ // Path already includes table location prefix, use it as-is
274+ std::filesystem::path fs_path (rel_path);
275+ std::filesystem::path normalized = fs_path.lexically_normal ();
276+
277+ std::string result = normalized.string ();
278+ std::replace (result.begin (), result.end (), ' \\ ' , ' /' );
279+
280+ while (!result.empty () && result.front () == ' /' )
281+ result = result.substr (1 );
282+
283+ return result;
284+ }
277285
278- return result;
286+ std::filesystem::path base_fs_path (base.key );
287+ if (base_fs_path.begin () != base_fs_path.end ())
288+ {
289+ std::string first_segment = base_fs_path.begin ()->string ();
290+ if (rel_path.starts_with (first_segment + " /" ) || rel_path == first_segment)
291+ {
292+ // Path starts with first segment of table location, likely relative to storage root
293+ std::filesystem::path fs_path (rel_path);
294+ std::filesystem::path normalized = fs_path.lexically_normal ();
295+
296+ std::string result = normalized.string ();
297+ std::replace (result.begin (), result.end (), ' \\ ' , ' /' );
298+
299+ while (!result.empty () && result.front () == ' /' )
300+ result = result.substr (1 );
301+
302+ return result;
303+ }
304+ }
279305 }
280306
281307 std::string combined = base.key ;
0 commit comments