Skip to content

Commit 7f50025

Browse files
author
shuxu.li
committed
feat: Refresh method support for table
1 parent d481ed7 commit 7f50025

File tree

5 files changed

+111
-46
lines changed

5 files changed

+111
-46
lines changed

src/iceberg/catalog.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ namespace iceberg {
3838
/// specified by the Iceberg Rest Catalog API.
3939
class ICEBERG_EXPORT Catalog {
4040
public:
41+
class TableBuilder;
42+
4143
virtual ~Catalog() = default;
4244

4345
/// \brief Return the name for this catalog
@@ -182,8 +184,8 @@ class ICEBERG_EXPORT Catalog {
182184
/// \param identifier a table identifier
183185
/// \param schema a schema
184186
/// \return the builder to create a table or start a create/replace transaction
185-
virtual std::unique_ptr<class TableBuilder> BuildTable(
186-
const TableIdentifier& identifier, const Schema& schema) const = 0;
187+
virtual std::unique_ptr<TableBuilder> BuildTable(const TableIdentifier& identifier,
188+
const Schema& schema) const = 0;
187189

188190
/// \brief A builder used to create valid tables or start create/replace transactions
189191
class TableBuilder {

src/iceberg/catalog/in_memory_catalog.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ Result<std::shared_ptr<Table>> InMemoryCatalog::RegisterTable(
441441
return LoadTable(identifier);
442442
}
443443

444-
std::unique_ptr<TableBuilder> InMemoryCatalog::BuildTable(
444+
std::unique_ptr<Catalog::TableBuilder> InMemoryCatalog::BuildTable(
445445
const TableIdentifier& identifier, const Schema& schema) const {
446446
throw IcebergError("not implemented");
447447
}

src/iceberg/catalog/in_memory_catalog.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ class ICEBERG_EXPORT InMemoryCatalog
9191
const TableIdentifier& identifier,
9292
const std::string& metadata_file_location) override;
9393

94-
std::unique_ptr<iceberg::TableBuilder> BuildTable(const TableIdentifier& identifier,
95-
const Schema& schema) const override;
94+
std::unique_ptr<TableBuilder> BuildTable(const TableIdentifier& identifier,
95+
const Schema& schema) const override;
9696

9797
private:
9898
std::string catalog_name_;

test/in_memory_catalog_test.cc

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
* under the License.
1818
*/
1919

20+
#include "iceberg/catalog/in_memory_catalog.h"
21+
2022
#include <filesystem>
2123

2224
#include <arrow/filesystem/localfs.h>
2325
#include <gmock/gmock.h>
2426
#include <gtest/gtest.h>
2527

2628
#include "iceberg/arrow/arrow_fs_file_io_internal.h"
29+
#include "iceberg/schema.h"
2730
#include "iceberg/table.h"
2831
#include "iceberg/table_metadata.h"
2932
#include "matchers.h"
@@ -115,42 +118,53 @@ TEST_F(InMemoryCatalogTest, RegisterTable) {
115118
}
116119

117120
TEST_F(InMemoryCatalogTest, RefreshTable) {
118-
TableIdentifier tableIdent{.ns = {}, .name = "t1"};
119-
std::shared_ptr<MockInMemoryCatalog> mock_catalog =
120-
std::make_shared<MockInMemoryCatalog>(
121-
"mock_catalog", file_io_, "/tmp/warehouse/",
122-
std::unordered_map<std::string, std::string>());
123-
auto table_location = GenerateTestTableLocation(tableIdent.name);
124-
auto buildTable = [this, &tableIdent, &mock_catalog, &table_location](
125-
int64_t snapshot_id, int64_t version) -> std::unique_ptr<Table> {
126-
std::unique_ptr<TableMetadata> metadata;
127-
ReadTableMetadata("TableMetadataV2Valid.json", &metadata);
128-
metadata->current_snapshot_id = snapshot_id;
129-
return std::make_unique<Table>(
130-
tableIdent, std::move(metadata),
131-
std::format("{}v{}.metadata.json", table_location, version), file_io_,
132-
std::static_pointer_cast<Catalog>(mock_catalog));
133-
};
134-
135-
auto table_v0 = buildTable(3051729675574597004, 0);
136-
auto table_v1 = buildTable(3055729675574597004, 1);
137-
EXPECT_CALL(*mock_catalog, LoadTable(::testing::_))
138-
.WillOnce(::testing::Return(
139-
::testing::ByMove(Result<std::unique_ptr<Table>>(std::move(table_v0)))))
140-
.WillOnce(::testing::Return(
141-
::testing::ByMove(Result<std::unique_ptr<Table>>(std::move(table_v1)))));
121+
TableIdentifier table_ident{.ns = {}, .name = "t1"};
122+
auto schema = std::make_shared<Schema>(
123+
std::vector<SchemaField>{SchemaField::MakeRequired(1, "x", int64())},
124+
/*schema_id=*/1);
142125

143-
auto metadata_location = std::format("{}v{}.metadata.json", table_location, 0);
144-
auto table = mock_catalog->RegisterTable(tableIdent, metadata_location);
145-
EXPECT_THAT(table, IsOk());
146-
ASSERT_TRUE(table.value()->current_snapshot().has_value());
147-
ASSERT_EQ(table.value()->current_snapshot().value()->snapshot_id, 3051729675574597004);
126+
std::shared_ptr<FileIO> io;
148127

149-
// refresh table to new snapshot
150-
auto status = table.value()->Refresh();
151-
EXPECT_THAT(status, IsOk());
152-
ASSERT_TRUE(table.value()->current_snapshot().has_value());
153-
ASSERT_EQ(table.value()->current_snapshot().value()->snapshot_id, 3055729675574597004);
128+
auto catalog = std::make_shared<MockCatalog>();
129+
// Mock 1st call to LoadTable
130+
EXPECT_CALL(*catalog, LoadTable(::testing::_))
131+
.WillOnce(::testing::Return(
132+
std::make_unique<Table>(table_ident,
133+
std::make_shared<TableMetadata>(TableMetadata{
134+
.schemas = {schema},
135+
.current_schema_id = 1,
136+
.current_snapshot_id = 1,
137+
.snapshots = {std::make_shared<Snapshot>(Snapshot{
138+
.snapshot_id = 1,
139+
.sequence_number = 1,
140+
})}}),
141+
"s3://location/1.json", io, catalog)));
142+
auto load_table_result = catalog->LoadTable(table_ident);
143+
ASSERT_THAT(load_table_result, IsOk());
144+
auto loaded_table = std::move(load_table_result.value());
145+
ASSERT_EQ(loaded_table->current_snapshot().value()->snapshot_id, 1);
146+
147+
// Mock 2nd call to LoadTable
148+
EXPECT_CALL(*catalog, LoadTable(::testing::_))
149+
.WillOnce(::testing::Return(
150+
std::make_unique<Table>(table_ident,
151+
std::make_shared<TableMetadata>(TableMetadata{
152+
.schemas = {schema},
153+
.current_schema_id = 1,
154+
.current_snapshot_id = 2,
155+
.snapshots = {std::make_shared<Snapshot>(Snapshot{
156+
.snapshot_id = 1,
157+
.sequence_number = 1,
158+
}),
159+
std::make_shared<Snapshot>(Snapshot{
160+
.snapshot_id = 2,
161+
.sequence_number = 2,
162+
})}}),
163+
"s3://location/2.json", io, catalog)));
164+
auto refreshed_result = loaded_table->Refresh();
165+
ASSERT_THAT(refreshed_result, IsOk());
166+
// check table is refreshed
167+
ASSERT_EQ(loaded_table->current_snapshot().value()->snapshot_id, 2);
154168
}
155169

156170
TEST_F(InMemoryCatalogTest, DropTable) {

test/mock_catalog.h

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,67 @@
1919

2020
#pragma once
2121

22-
#include "iceberg/catalog/in_memory_catalog.h"
22+
#include "iceberg/catalog.h"
2323

2424
namespace iceberg {
2525

26-
class MockInMemoryCatalog : public InMemoryCatalog {
26+
class MockCatalog : public Catalog {
2727
public:
28-
MockInMemoryCatalog(std::string const& name, std::shared_ptr<FileIO> const& file_io,
29-
std::string const& warehouse_location,
30-
std::unordered_map<std::string, std::string> const& properties)
31-
: InMemoryCatalog(name, file_io, warehouse_location, properties) {}
28+
MockCatalog() = default;
29+
~MockCatalog() override = default;
3230

33-
MOCK_METHOD(Result<std::unique_ptr<Table>>, LoadTable, (const TableIdentifier&),
31+
MOCK_METHOD(std::string_view, name, (), (const, override));
32+
33+
MOCK_METHOD(Status, CreateNamespace,
34+
(const Namespace&, (const std::unordered_map<std::string, std::string>&)),
35+
(override));
36+
37+
MOCK_METHOD((Result<std::vector<Namespace>>), ListNamespaces, (const Namespace&),
38+
(const, override));
39+
40+
MOCK_METHOD((Result<std::unordered_map<std::string, std::string>>),
41+
GetNamespaceProperties, (const Namespace&), (const, override));
42+
43+
MOCK_METHOD(Status, UpdateNamespaceProperties,
44+
(const Namespace&, (const std::unordered_map<std::string, std::string>&),
45+
(const std::unordered_set<std::string>&)),
3446
(override));
47+
48+
MOCK_METHOD(Status, DropNamespace, (const Namespace&), (override));
49+
50+
MOCK_METHOD(Result<bool>, NamespaceExists, (const Namespace&), (const, override));
51+
52+
MOCK_METHOD((Result<std::vector<TableIdentifier>>), ListTables, (const Namespace&),
53+
(const, override));
54+
55+
MOCK_METHOD((Result<std::unique_ptr<Table>>), CreateTable,
56+
(const TableIdentifier&, const Schema&, const PartitionSpec&,
57+
const std::string&, (const std::unordered_map<std::string, std::string>&)),
58+
(override));
59+
60+
MOCK_METHOD((Result<std::unique_ptr<Table>>), UpdateTable,
61+
(const TableIdentifier&,
62+
(const std::vector<std::unique_ptr<UpdateRequirement>>&),
63+
(const std::vector<std::unique_ptr<MetadataUpdate>>&)),
64+
(override));
65+
66+
MOCK_METHOD((Result<std::shared_ptr<Transaction>>), StageCreateTable,
67+
(const TableIdentifier&, const Schema&, const PartitionSpec&,
68+
const std::string&, (const std::unordered_map<std::string, std::string>&)),
69+
(override));
70+
71+
MOCK_METHOD(Result<bool>, TableExists, (const TableIdentifier&), (const, override));
72+
73+
MOCK_METHOD(Status, DropTable, (const TableIdentifier&, bool), (override));
74+
75+
MOCK_METHOD((Result<std::unique_ptr<Table>>), LoadTable, (const TableIdentifier&),
76+
(override));
77+
78+
MOCK_METHOD((Result<std::shared_ptr<Table>>), RegisterTable,
79+
(const TableIdentifier&, const std::string&), (override));
80+
81+
MOCK_METHOD((std::unique_ptr<Catalog::TableBuilder>), BuildTable,
82+
(const TableIdentifier&, const Schema&), (const, override));
3583
};
84+
3685
} // namespace iceberg

0 commit comments

Comments
 (0)