Skip to content
Draft
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
2 changes: 1 addition & 1 deletion src/iceberg/catalog/rest/constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ inline const std::string kUserAgentPrefix = "iceberg-cpp/";
inline const std::string kUserAgent = "iceberg-cpp/" ICEBERG_VERSION_STRING;

inline const std::string kQueryParamParent = "parent";
inline const std::string kQueryParamPageToken = "page_token";
inline const std::string kQueryParamPageToken = "pageToken";

} // namespace iceberg::rest
38 changes: 19 additions & 19 deletions src/iceberg/catalog/rest/rest_catalog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -683,18 +683,16 @@ Result<std::vector<Namespace>> RestCatalog::ListNamespaces(
const Namespace& ns, auth::AuthSession& session) const {
ICEBERG_ENDPOINT_CHECK(supported_endpoints_, Endpoint::ListNamespaces());
ICEBERG_ASSIGN_OR_RAISE(auto path, paths_->Namespaces());
// An empty pageToken asks the server for the first page.
std::unordered_map<std::string, std::string> params{{kQueryParamPageToken, ""}};
if (!ns.levels.empty()) {
ICEBERG_ASSIGN_OR_RAISE(
params[kQueryParamParent],
EncodeNamespace(ns, config_.Get(RestCatalogProperties::kNamespaceSeparator)));
}
std::vector<Namespace> result;
std::string next_token;
std::unordered_set<std::string> seen_tokens;
while (true) {
std::unordered_map<std::string, std::string> params;
if (!ns.levels.empty()) {
ICEBERG_ASSIGN_OR_RAISE(
params[kQueryParamParent],
EncodeNamespace(ns, config_.Get(RestCatalogProperties::kNamespaceSeparator)));
}
if (!next_token.empty()) {
params[kQueryParamPageToken] = next_token;
}
ICEBERG_ASSIGN_OR_RAISE(const auto response,
client_->Get(path, params, /*headers=*/{},
*NamespaceErrorHandler::Instance(), session));
Expand All @@ -705,9 +703,11 @@ Result<std::vector<Namespace>> RestCatalog::ListNamespaces(
if (list_response.next_page_token.empty()) {
return result;
}
next_token = std::move(list_response.next_page_token);
if (!seen_tokens.insert(list_response.next_page_token).second) {
return IOError("ListNamespaces response repeated a pagination token");
}
params[kQueryParamPageToken] = std::move(list_response.next_page_token);
}
return result;
}

Status RestCatalog::CreateNamespace(
Expand Down Expand Up @@ -781,13 +781,11 @@ Result<std::vector<TableIdentifier>> RestCatalog::ListTables(
const Namespace& ns, auth::AuthSession& session) const {
ICEBERG_ENDPOINT_CHECK(supported_endpoints_, Endpoint::ListTables());
ICEBERG_ASSIGN_OR_RAISE(auto path, paths_->Tables(ns));
// An empty pageToken asks the server for the first page.
std::unordered_map<std::string, std::string> params{{kQueryParamPageToken, ""}};
std::vector<TableIdentifier> result;
std::string next_token;
std::unordered_set<std::string> seen_tokens;
while (true) {
std::unordered_map<std::string, std::string> params;
if (!next_token.empty()) {
params[kQueryParamPageToken] = next_token;
}
ICEBERG_ASSIGN_OR_RAISE(const auto response,
client_->Get(path, params, /*headers=*/{},
*TableErrorHandler::Instance(), session));
Expand All @@ -798,9 +796,11 @@ Result<std::vector<TableIdentifier>> RestCatalog::ListTables(
if (list_response.next_page_token.empty()) {
return result;
}
next_token = std::move(list_response.next_page_token);
if (!seen_tokens.insert(list_response.next_page_token).second) {
return IOError("ListTables response repeated a pagination token");
}
params[kQueryParamPageToken] = std::move(list_response.next_page_token);
}
return result;
}

Result<LoadTableResult> RestCatalog::CreateTableInternal(
Expand Down
100 changes: 89 additions & 11 deletions src/iceberg/test/rest_catalog_unit_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,8 @@ const std::string kCommitTableResponse =
// --------------------------------------------------------------------------
// Use the regular catalog initialization with a mock HTTP client.
// --------------------------------------------------------------------------
class RestCatalogLoadTableTest : public ::testing::Test {
class RestCatalogTestBase : public ::testing::Test {
protected:
void TearDown() override { installed_provider.reset(); }

void SetUp() override {
mock_client_ = std::make_shared<MockHttpClient>();
FileIORegistry::Register(
Expand All @@ -197,14 +195,6 @@ class RestCatalogLoadTableTest : public ::testing::Test {
-> Result<std::unique_ptr<FileIO>> {
return std::make_unique<NoOpFileIO>();
}});

identifier_ = TableIdentifier{.ns = Namespace{{"default"}}, .name = "my_table"};

all_plan_endpoints_ = {Endpoint::LoadTable(), Endpoint::UpdateTable(),
Endpoint::PlanTableScan(), Endpoint::FetchPlanningResult(),
Endpoint::CancelPlanning(), Endpoint::FetchScanTasks()};

no_plan_endpoint_set_ = {Endpoint::LoadTable()};
}

// Builds a RestCatalog with the given supported endpoints and an optional
Expand All @@ -228,11 +218,99 @@ class RestCatalogLoadTableTest : public ::testing::Test {
}

std::shared_ptr<MockHttpClient> mock_client_;
};

class RestCatalogLoadTableTest : public RestCatalogTestBase {
protected:
void TearDown() override { installed_provider.reset(); }

void SetUp() override {
RestCatalogTestBase::SetUp();
identifier_ = TableIdentifier{.ns = Namespace{{"default"}}, .name = "my_table"};

all_plan_endpoints_ = {Endpoint::LoadTable(), Endpoint::UpdateTable(),
Endpoint::PlanTableScan(), Endpoint::FetchPlanningResult(),
Endpoint::CancelPlanning(), Endpoint::FetchScanTasks()};

no_plan_endpoint_set_ = {Endpoint::LoadTable()};
}

TableIdentifier identifier_;
std::unordered_set<Endpoint> all_plan_endpoints_;
std::unordered_set<Endpoint> no_plan_endpoint_set_;
};

class RestCatalogPaginationTest : public RestCatalogTestBase {
protected:
using QueryParams = std::unordered_map<std::string, std::string>;
};

TEST_F(RestCatalogPaginationTest, ListNamespacesAcrossPages) {
ICEBERG_UNWRAP_OR_FAIL(auto root, MakeCatalog({Endpoint::ListNamespaces()}));
ICEBERG_UNWRAP_OR_FAIL(auto catalog, root->AsCatalog());
::testing::InSequence order;
EXPECT_CALL(*mock_client_,
Get("http://test-server/v1/namespaces",
::testing::Eq(QueryParams{{"pageToken", ""}, {"parent", "accounting"}}),
_, _, _))
.WillOnce(Return(HttpResponse(
200,
R"({"namespaces":[["accounting","tax"]],"next-page-token":"opaque+/="})")));
EXPECT_CALL(*mock_client_, Get("http://test-server/v1/namespaces",
::testing::Eq(QueryParams{{"pageToken", "opaque+/="},
{"parent", "accounting"}}),
_, _, _))
.WillOnce(Return(HttpResponse(
200, R"({"namespaces":[["accounting","payroll"]],"next-page-token":null})")));
ICEBERG_UNWRAP_OR_FAIL(auto namespaces,
catalog->ListNamespaces(Namespace{{"accounting"}}));
EXPECT_THAT(namespaces, ::testing::ElementsAre(Namespace{{"accounting", "tax"}},
Namespace{{"accounting", "payroll"}}));
}

TEST_F(RestCatalogPaginationTest, ListTablesAcrossPages) {
ICEBERG_UNWRAP_OR_FAIL(auto root, MakeCatalog({Endpoint::ListTables()}));
ICEBERG_UNWRAP_OR_FAIL(auto catalog, root->AsCatalog());
const Namespace ns{{"accounting"}};
::testing::InSequence order;
EXPECT_CALL(*mock_client_, Get("http://test-server/v1/namespaces/accounting/tables",
::testing::Eq(QueryParams{{"pageToken", ""}}), _, _, _))
.WillOnce(Return(HttpResponse(
200,
R"({"identifiers":[{"namespace":["accounting"],"name":"tax"}],"next-page-token":"page-2"})")));
EXPECT_CALL(*mock_client_,
Get("http://test-server/v1/namespaces/accounting/tables",
::testing::Eq(QueryParams{{"pageToken", "page-2"}}), _, _, _))
.WillOnce(Return(HttpResponse(
200, R"({"identifiers":[{"namespace":["accounting"],"name":"payroll"}]})")));
ICEBERG_UNWRAP_OR_FAIL(auto tables, catalog->ListTables(ns));
EXPECT_THAT(tables,
::testing::ElementsAre(TableIdentifier{.ns = ns, .name = "tax"},
TableIdentifier{.ns = ns, .name = "payroll"}));
}

TEST_F(RestCatalogPaginationTest, ListTablesRejectsCyclicPageTokens) {
ICEBERG_UNWRAP_OR_FAIL(auto root, MakeCatalog({Endpoint::ListTables()}));
ICEBERG_UNWRAP_OR_FAIL(auto catalog, root->AsCatalog());
constexpr auto kPath = "http://test-server/v1/namespaces/accounting/tables";
::testing::InSequence order;
EXPECT_CALL(*mock_client_,
Get(kPath, ::testing::Eq(QueryParams{{"pageToken", ""}}), _, _, _))
.WillOnce(
Return(HttpResponse(200, R"({"identifiers":[],"next-page-token":"page-2"})")));
EXPECT_CALL(*mock_client_,
Get(kPath, ::testing::Eq(QueryParams{{"pageToken", "page-2"}}), _, _, _))
.WillOnce(
Return(HttpResponse(200, R"({"identifiers":[],"next-page-token":"page-3"})")));
EXPECT_CALL(*mock_client_,
Get(kPath, ::testing::Eq(QueryParams{{"pageToken", "page-3"}}), _, _, _))
.WillOnce(
Return(HttpResponse(200, R"({"identifiers":[],"next-page-token":"page-2"})")));
EXPECT_THAT(catalog->ListTables(Namespace{{"accounting"}}),
::testing::AllOf(IsError(ErrorKind::kIOError),
HasErrorMessage("repeated a pagination token")));
}

class RestCatalogLabelsTest : public RestCatalogLoadTableTest,
public ::testing::WithParamInterface<bool> {
protected:
Expand Down