From d830113ea4c30f14592cf7505e3eb011f2bb593e Mon Sep 17 00:00:00 2001 From: "zhuo.wang" Date: Fri, 4 Dec 2026 18:27:28 +0800 Subject: [PATCH] fix(rest): use the Iceberg pagination query parameter --- src/iceberg/catalog/rest/constant.h | 2 +- src/iceberg/catalog/rest/rest_catalog.cc | 38 ++++---- src/iceberg/test/rest_catalog_unit_test.cc | 100 ++++++++++++++++++--- 3 files changed, 109 insertions(+), 31 deletions(-) diff --git a/src/iceberg/catalog/rest/constant.h b/src/iceberg/catalog/rest/constant.h index 0a6e8d0c01..53ffc260d7 100644 --- a/src/iceberg/catalog/rest/constant.h +++ b/src/iceberg/catalog/rest/constant.h @@ -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 diff --git a/src/iceberg/catalog/rest/rest_catalog.cc b/src/iceberg/catalog/rest/rest_catalog.cc index a6ba928e50..83e31d0aeb 100644 --- a/src/iceberg/catalog/rest/rest_catalog.cc +++ b/src/iceberg/catalog/rest/rest_catalog.cc @@ -683,18 +683,16 @@ Result> 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 params{{kQueryParamPageToken, ""}}; + if (!ns.levels.empty()) { + ICEBERG_ASSIGN_OR_RAISE( + params[kQueryParamParent], + EncodeNamespace(ns, config_.Get(RestCatalogProperties::kNamespaceSeparator))); + } std::vector result; - std::string next_token; + std::unordered_set seen_tokens; while (true) { - std::unordered_map 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)); @@ -705,9 +703,11 @@ Result> 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( @@ -781,13 +781,11 @@ Result> 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 params{{kQueryParamPageToken, ""}}; std::vector result; - std::string next_token; + std::unordered_set seen_tokens; while (true) { - std::unordered_map params; - if (!next_token.empty()) { - params[kQueryParamPageToken] = next_token; - } ICEBERG_ASSIGN_OR_RAISE(const auto response, client_->Get(path, params, /*headers=*/{}, *TableErrorHandler::Instance(), session)); @@ -798,9 +796,11 @@ Result> 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 RestCatalog::CreateTableInternal( diff --git a/src/iceberg/test/rest_catalog_unit_test.cc b/src/iceberg/test/rest_catalog_unit_test.cc index 00b6f93a88..22992b15f8 100644 --- a/src/iceberg/test/rest_catalog_unit_test.cc +++ b/src/iceberg/test/rest_catalog_unit_test.cc @@ -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(); FileIORegistry::Register( @@ -197,14 +195,6 @@ class RestCatalogLoadTableTest : public ::testing::Test { -> Result> { return std::make_unique(); }}); - - 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 @@ -228,11 +218,99 @@ class RestCatalogLoadTableTest : public ::testing::Test { } std::shared_ptr 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 all_plan_endpoints_; std::unordered_set no_plan_endpoint_set_; }; +class RestCatalogPaginationTest : public RestCatalogTestBase { + protected: + using QueryParams = std::unordered_map; +}; + +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 { protected: