diff --git a/src/iceberg/arrow/arrow_io.cc b/src/iceberg/arrow/arrow_io.cc index 998f8c5ce..b821e8962 100644 --- a/src/iceberg/arrow/arrow_io.cc +++ b/src/iceberg/arrow/arrow_io.cc @@ -22,6 +22,7 @@ #include #include #include +#include #include #include @@ -36,6 +37,7 @@ #include "iceberg/arrow/arrow_io_util.h" #include "iceberg/arrow/arrow_status_internal.h" #include "iceberg/util/macros.h" +#include "iceberg/util/string_util.h" namespace iceberg::arrow { @@ -484,26 +486,28 @@ class ArrowOutputFile : public OutputFile { } // namespace Result ArrowFileSystemFileIO::ResolvePath(const std::string& file_location) { - const auto pos = file_location.find("://"); - if (pos == std::string::npos) { + const std::string_view uri(file_location); + // Absolute local file URIs have one slash or an empty authority (three slashes). + // Do not fall back to alias handling when the filesystem rejects either form. + if (StringUtils::EqualsIgnoreCase(uri.substr(0, 6), "file:/") && + (!uri.substr(6).starts_with('/') || uri.substr(6).starts_with("//"))) { + ICEBERG_ARROW_ASSIGN_OR_RETURN( + auto path, arrow_fs_->PathFromUri("file:" + file_location.substr(5))); + return path; + } + + const auto pos = uri.find("://"); + if (pos == uri.npos) { return file_location; } - - auto path = arrow_fs_->PathFromUri(file_location); - if (path.ok()) { + if (auto path = arrow_fs_->PathFromUri(file_location); path.ok()) { return std::move(path).ValueOrDie(); } - - // Foreign alias (s3a/s3n): validate via Arrow's parser, then percent-decode the - // scheme-less key (substring keeps a Windows drive letter's ':' that host() drops). - if (auto parsed = ::arrow::util::Uri::FromString(file_location); !parsed.ok()) { - const auto& status = parsed.status(); - return ::iceberg::unexpected{ - {.kind = ToErrorKind(status), .message = status.ToString()}}; - } - std::string bucket_key = file_location.substr(pos + 3); - bucket_key = bucket_key.substr(0, bucket_key.find_first_of("?#")); - return ::arrow::util::UriUnescape(bucket_key); + // Keep drive letters in alias keys. + ::arrow::util::Uri parsed; + ICEBERG_ARROW_RETURN_NOT_OK(parsed.Parse(file_location)); + const auto key = uri.substr(pos + 3); + return ::arrow::util::UriUnescape(key.substr(0, key.find_first_of("?#"))); } Result> OpenArrowInputStream( diff --git a/src/iceberg/test/arrow_io_test.cc b/src/iceberg/test/arrow_io_test.cc index 7bc9ebba5..fdb4254eb 100644 --- a/src/iceberg/test/arrow_io_test.cc +++ b/src/iceberg/test/arrow_io_test.cc @@ -27,6 +27,7 @@ #include #include #include +#include #include #include "iceberg/arrow/arrow_io_internal.h" @@ -356,6 +357,18 @@ TEST_F(LocalFileIOTest, ReadWriteFile) { EXPECT_THAT(read_res, HasValue(::testing::Eq("hello world"))); } +TEST_F(LocalFileIOTest, ReadsLocalFileUriWithOneOrThreeSlashes) { + ASSERT_THAT(file_io_->WriteFile(temp_filepath_, "metadata bytes"), IsOk()); + const auto uri = ::arrow::util::UriFromAbsolutePath(temp_filepath_); + ASSERT_TRUE(uri.ok()) << uri.status(); + ASSERT_TRUE(uri->starts_with("file:///")); + + for (const auto prefix : {"file:", "FiLe:", "file://", "FiLe://"}) { + EXPECT_THAT(file_io_->ReadFile(prefix + uri->substr(7), std::nullopt), + HasValue(::testing::Eq("metadata bytes"))); + } +} + TEST_F(LocalFileIOTest, DeleteFile) { auto write_res = file_io_->WriteFile(temp_filepath_, "hello world"); EXPECT_THAT(write_res, IsOk()); @@ -459,10 +472,23 @@ TEST_F(LocalFileIOTest, ResolvesForeignSchemeToUnderlyingPath) { EXPECT_THAT(with_query, HasValue(::testing::Eq("hello world"))); } -TEST_F(LocalFileIOTest, PropagatesNonSchemeMismatchUriError) { - auto read_res = file_io_->ReadFile("file:///tmp/%ZZ", std::nullopt); - EXPECT_THAT(read_res, IsError(ErrorKind::kUnknownError)); - EXPECT_THAT(read_res, HasErrorMessage("Cannot parse URI")); +TEST_F(LocalFileIOTest, RejectsMalformedLocalFileUris) { + for (const auto uri : {"file:/tmp/%ZZ", "file:///tmp/%ZZ"}) { + SCOPED_TRACE(uri); + auto read_res = file_io_->ReadFile(uri, std::nullopt); + EXPECT_THAT(read_res, IsError(ErrorKind::kUnknownError)); + EXPECT_THAT(read_res, HasErrorMessage("Cannot parse URI")); + } +} + +TEST(ArrowFileIOTest, PropagatesBackendRejectionOfLocalFileUris) { + auto file_io = arrow::ArrowFileSystemFileIO::MakeMockFileIO(); + for (const auto uri : + {"file:/example", "FiLe:/example", "file:///example", "FiLe:///example"}) { + SCOPED_TRACE(uri); + EXPECT_THAT(file_io->NewInputFile(uri), + HasErrorMessage("The filesystem expected a URI with one of the schemes")); + } } TEST_F(LocalFileIOTest, FallbackDecodesPercentEncodingInKey) {