Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions src/iceberg/arrow/arrow_io.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <limits>
#include <mutex>
#include <optional>
#include <string_view>
#include <vector>

#include <arrow/buffer.h>
Expand All @@ -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 {

Expand Down Expand Up @@ -484,26 +486,28 @@ class ArrowOutputFile : public OutputFile {
} // namespace

Result<std::string> 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<Error>{
{.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<std::shared_ptr<::arrow::io::RandomAccessFile>> OpenArrowInputStream(
Expand Down
34 changes: 30 additions & 4 deletions src/iceberg/test/arrow_io_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <arrow/filesystem/localfs.h>
#include <arrow/result.h>
#include <arrow/status.h>
#include <arrow/util/uri.h>
#include <gtest/gtest.h>

#include "iceberg/arrow/arrow_io_internal.h"
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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) {
Expand Down
Loading