Skip to content

Commit 9528c0b

Browse files
committed
fix
1 parent e13f5b6 commit 9528c0b

6 files changed

Lines changed: 174 additions & 17 deletions

File tree

‎include/paimon/catalog/table.h‎

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333

3434
namespace paimon {
3535

36+
class TableScan;
37+
class TableRead;
38+
3639
/// A table provides basic abstraction for table type.
3740
class PAIMON_EXPORT Table {
3841
public:
@@ -47,19 +50,21 @@ class PAIMON_EXPORT Table {
4750
const Identifier& identifier);
4851

4952
/// @param uuid Metastore UUID, or empty if unavailable.
50-
Table(const std::shared_ptr<Schema>& schema, const std::string& database,
51-
const std::string& table_name, const std::string& uuid = "")
52-
: schema_(schema), database_(database), table_name_(table_name), uuid_(uuid) {}
53+
Table(const std::shared_ptr<Schema>& schema, const Identifier& identifier,
54+
const std::string& uuid = "")
55+
: schema_(schema), identifier_(identifier), uuid_(uuid) {}
5356

5457
~Table() = default;
5558

5659
/// A name to identify this table.
5760
std::string Name() const {
58-
return table_name_;
61+
return identifier_.GetObjectName();
5962
}
6063

6164
/// Full name of the table, default is database.tableName.
62-
std::string FullName() const;
65+
std::string FullName() const {
66+
return identifier_.GetFullName();
67+
}
6368

6469
/// Returns the metastore UUID, falling back to `FullName()`.
6570
/// @note Use `CatalogUuid()` for commit requests to avoid sending a table name as an ID.
@@ -78,10 +83,34 @@ class PAIMON_EXPORT Table {
7883
return schema_;
7984
}
8085

86+
/// Creates a scan of this table from a finished scan context, standing in for
87+
/// `TableScan::Create`.
88+
/// @param context The finished scan context.
89+
/// @return The scan, or the error `TableScan::Create` reports.
90+
Result<std::unique_ptr<TableScan>> NewScan(std::unique_ptr<ScanContext> context) const;
91+
92+
/// Creates a read of this table from a finished read context, standing in for
93+
/// `TableRead::Create`.
94+
/// @param context The finished read context.
95+
/// @return The read, or the error `TableRead::Create` reports.
96+
Result<std::unique_ptr<TableRead>> NewRead(std::unique_ptr<ReadContext> context) const;
97+
98+
/// Creates a write of this table from a finished write context, standing in for
99+
/// `FileStoreWrite::Create`.
100+
/// @param context The finished write context.
101+
/// @return The write, or the error `FileStoreWrite::Create` reports.
102+
Result<std::unique_ptr<FileStoreWrite>> NewWrite(std::unique_ptr<WriteContext> context) const;
103+
104+
/// Creates a commit of this table from a finished commit context, standing in for
105+
/// `FileStoreCommit::Create`.
106+
/// @param context The finished commit context.
107+
/// @return The commit, or the error `FileStoreCommit::Create` reports.
108+
Result<std::unique_ptr<FileStoreCommit>> NewCommit(
109+
std::unique_ptr<CommitContext> context) const;
110+
81111
private:
82112
std::shared_ptr<Schema> schema_;
83-
std::string database_;
84-
std::string table_name_;
113+
Identifier identifier_;
85114
std::string uuid_;
86115
};
87116

‎src/paimon/core/catalog/file_system_catalog.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,7 @@ Result<std::shared_ptr<Table>> FileSystemCatalog::GetTable(const Identifier& ide
333333
PAIMON_ASSIGN_OR_RAISE(bool is_system_table, identifier.IsSystemTable());
334334
if (is_system_table) {
335335
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<Schema> schema, LoadTableSchema(identifier));
336-
return std::make_shared<Table>(schema, identifier.GetDatabaseName(),
337-
identifier.GetObjectName());
336+
return std::make_shared<Table>(schema, identifier);
338337
}
339338
PAIMON_ASSIGN_OR_RAISE(std::string table_path, GetTableLocation(identifier));
340339
// `Table::Create` reads the same schema file and rejects a format table there.

‎src/paimon/core/table/table.cpp‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,19 @@
2121
#include <optional>
2222

2323
#include "fmt/format.h"
24+
#include "paimon/commit_context.h"
2425
#include "paimon/common/utils/checked_cast.h"
2526
#include "paimon/core/catalog/catalog_utils.h"
2627
#include "paimon/core/schema/schema_manager.h"
2728
#include "paimon/core/schema/table_schema.h"
29+
#include "paimon/file_store_commit.h"
30+
#include "paimon/file_store_write.h"
2831
#include "paimon/fs/file_system.h"
32+
#include "paimon/read_context.h"
33+
#include "paimon/scan_context.h"
34+
#include "paimon/table/source/table_read.h"
35+
#include "paimon/table/source/table_scan.h"
36+
#include "paimon/write_context.h"
2937

3038
namespace paimon {
3139

@@ -52,12 +60,25 @@ Result<std::shared_ptr<Table>> Table::Create(const std::shared_ptr<FileSystem>&
5260
CatalogUtils::CheckManagedTableType(identifier, *latest_schema, "Table::Create"));
5361

5462
auto schema = checked_pointer_cast<Schema>(*latest_schema);
55-
return std::make_shared<Table>(schema, identifier.GetDatabaseName(),
56-
identifier.GetObjectName());
63+
return std::make_shared<Table>(schema, identifier);
5764
}
5865

59-
std::string Table::FullName() const {
60-
return database_ == Identifier::kUnknownDatabase ? table_name_ : database_ + "." + table_name_;
66+
Result<std::unique_ptr<TableScan>> Table::NewScan(std::unique_ptr<ScanContext> context) const {
67+
return TableScan::Create(std::move(context));
68+
}
69+
70+
Result<std::unique_ptr<TableRead>> Table::NewRead(std::unique_ptr<ReadContext> context) const {
71+
return TableRead::Create(std::move(context));
72+
}
73+
74+
Result<std::unique_ptr<FileStoreWrite>> Table::NewWrite(
75+
std::unique_ptr<WriteContext> context) const {
76+
return FileStoreWrite::Create(std::move(context));
77+
}
78+
79+
Result<std::unique_ptr<FileStoreCommit>> Table::NewCommit(
80+
std::unique_ptr<CommitContext> context) const {
81+
return FileStoreCommit::Create(std::move(context));
6182
}
6283

6384
} // namespace paimon

‎src/paimon/core/table/table_test.cpp‎

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,38 @@
2525

2626
#include "arrow/api.h"
2727
#include "gtest/gtest.h"
28+
#include "paimon/commit_context.h"
2829
#include "paimon/core/schema/schema_manager.h"
2930
#include "paimon/core/schema/table_schema.h"
31+
#include "paimon/file_store_commit.h"
32+
#include "paimon/file_store_write.h"
33+
#include "paimon/read_context.h"
34+
#include "paimon/scan_context.h"
3035
#include "paimon/schema/schema.h"
36+
#include "paimon/table/source/table_read.h"
37+
#include "paimon/table/source/table_scan.h"
38+
#include "paimon/testing/mock/mock_catalog.h"
3139
#include "paimon/testing/utils/testharness.h"
40+
#include "paimon/write_context.h"
3241

3342
namespace paimon::test {
3443

44+
namespace {
45+
46+
// Creates a managed table directory and opens it as a `Table` under `identifier`.
47+
Result<std::shared_ptr<Table>> CreateManagedTable(const std::shared_ptr<FileSystem>& file_system,
48+
const std::string& path,
49+
const Identifier& identifier) {
50+
SchemaManager schema_manager(file_system, path);
51+
auto schema = arrow::schema({arrow::field("id", arrow::int32(), /*nullable=*/false)});
52+
PAIMON_ASSIGN_OR_RAISE([[maybe_unused]] std::unique_ptr<TableSchema> created,
53+
schema_manager.CreateTable(schema, /*partition_keys=*/{},
54+
/*primary_keys=*/{}, /*options=*/{}));
55+
return Table::Create(file_system, path, identifier);
56+
}
57+
58+
} // namespace
59+
3560
TEST(TableTest, TestCreateWithUnknownDatabase) {
3661
auto dir = UniqueTestDirectory::Create();
3762
ASSERT_TRUE(dir);
@@ -123,4 +148,89 @@ TEST(TableTest, TestCreateFailedWithNonExistSchema) {
123148
"load table schema for Identifier{database='unknown', object='tbl1'} failed");
124149
}
125150

151+
TEST(TableTest, TestNewScanCreatesATableScan) {
152+
auto dir = UniqueTestDirectory::Create();
153+
ASSERT_TRUE(dir);
154+
const Identifier identifier("db", "tbl");
155+
ASSERT_OK_AND_ASSIGN(std::shared_ptr<Table> table,
156+
CreateManagedTable(dir->GetFileSystem(), dir->Str(), identifier));
157+
158+
auto catalog = std::make_shared<MockVersionManagedCatalog>();
159+
catalog->SetFileSystem(dir->GetFileSystem());
160+
161+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<ScanContext> ctx,
162+
ScanContextBuilder(dir->Str()).WithCatalog(catalog, identifier).Finish());
163+
ASSERT_OK_AND_ASSIGN([[maybe_unused]] std::unique_ptr<TableScan> scan,
164+
table->NewScan(std::move(ctx)));
165+
}
166+
167+
TEST(TableTest, TestNewReadCreatesATableRead) {
168+
auto dir = UniqueTestDirectory::Create();
169+
ASSERT_TRUE(dir);
170+
const Identifier identifier("db", "tbl");
171+
ASSERT_OK_AND_ASSIGN(std::shared_ptr<Table> table,
172+
CreateManagedTable(dir->GetFileSystem(), dir->Str(), identifier));
173+
174+
auto catalog = std::make_shared<MockVersionManagedCatalog>();
175+
catalog->SetFileSystem(dir->GetFileSystem());
176+
177+
ASSERT_OK_AND_ASSIGN(std::unique_ptr<ReadContext> ctx,
178+
ReadContextBuilder(dir->Str()).WithCatalog(catalog, identifier).Finish());
179+
ASSERT_OK_AND_ASSIGN([[maybe_unused]] std::unique_ptr<TableRead> read,
180+
table->NewRead(std::move(ctx)));
181+
}
182+
183+
TEST(TableTest, TestNewWriteCreatesAFileStoreWrite) {
184+
auto dir = UniqueTestDirectory::Create();
185+
ASSERT_TRUE(dir);
186+
const Identifier identifier("db", "tbl");
187+
ASSERT_OK_AND_ASSIGN(std::shared_ptr<Table> table,
188+
CreateManagedTable(dir->GetFileSystem(), dir->Str(), identifier));
189+
190+
auto catalog = std::make_shared<MockVersionManagedCatalog>();
191+
catalog->SetFileSystem(dir->GetFileSystem());
192+
// A catalog write reads the current schema from the catalog on the main branch.
193+
catalog->SetTableSchema(table->LatestSchema());
194+
195+
ASSERT_OK_AND_ASSIGN(
196+
std::unique_ptr<WriteContext> ctx,
197+
WriteContextBuilder(dir->Str(), "commit-user").WithCatalog(catalog, identifier).Finish());
198+
ASSERT_OK_AND_ASSIGN([[maybe_unused]] std::unique_ptr<FileStoreWrite> write,
199+
table->NewWrite(std::move(ctx)));
200+
}
201+
202+
TEST(TableTest, TestNewCommitCreatesAFileStoreCommit) {
203+
auto dir = UniqueTestDirectory::Create();
204+
ASSERT_TRUE(dir);
205+
const Identifier identifier("db", "tbl");
206+
ASSERT_OK_AND_ASSIGN(std::shared_ptr<Table> table,
207+
CreateManagedTable(dir->GetFileSystem(), dir->Str(), identifier));
208+
209+
auto catalog = std::make_shared<MockVersionManagedCatalog>();
210+
catalog->SetFileSystem(dir->GetFileSystem());
211+
// Like a write, a catalog commit reads the current schema from the catalog.
212+
catalog->SetTableSchema(table->LatestSchema());
213+
214+
ASSERT_OK_AND_ASSIGN(
215+
std::unique_ptr<CommitContext> ctx,
216+
CommitContextBuilder(dir->Str(), "commit-user").WithCatalog(catalog, identifier).Finish());
217+
ASSERT_OK_AND_ASSIGN([[maybe_unused]] std::unique_ptr<FileStoreCommit> commit,
218+
table->NewCommit(std::move(ctx)));
219+
}
220+
221+
TEST(TableTest, TestNewScanReadWriteCommitRejectANullContext) {
222+
auto dir = UniqueTestDirectory::Create();
223+
ASSERT_TRUE(dir);
224+
ASSERT_OK_AND_ASSIGN(
225+
std::shared_ptr<Table> table,
226+
CreateManagedTable(dir->GetFileSystem(), dir->Str(), Identifier("db", "tbl")));
227+
228+
// The operators report the null context themselves; the table stands in without swallowing
229+
// the error, so behavior matches `TableScan::Create` and friends.
230+
ASSERT_NOK_WITH_MSG(table->NewScan(nullptr), "scan context is null pointer");
231+
ASSERT_NOK_WITH_MSG(table->NewRead(nullptr), "read context is null pointer");
232+
ASSERT_NOK_WITH_MSG(table->NewWrite(nullptr), "write context is null pointer");
233+
ASSERT_NOK_WITH_MSG(table->NewCommit(nullptr), "commit context is null pointer");
234+
}
235+
126236
} // namespace paimon::test

‎src/paimon/rest/rest_catalog.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,7 @@ Result<std::shared_ptr<Table>> RestCatalog::GetTable(const Identifier& identifie
448448
PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<Schema> schema, LoadTableSchema(identifier, &uuid));
449449
PAIMON_RETURN_NOT_OK(
450450
CatalogUtils::CheckManagedTableType(identifier, schema, "Catalog::GetTable"));
451-
return std::make_shared<Table>(schema, identifier.GetDatabaseName(), identifier.GetObjectName(),
452-
uuid);
451+
return std::make_shared<Table>(schema, identifier, uuid);
453452
}
454453

455454
std::string RestCatalog::GetRootPath() const {

‎src/paimon/testing/mock/mock_catalog.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ class MockVersionManagedCatalog : public Catalog, public VersionManagedCatalog {
9595

9696
Result<std::shared_ptr<Table>> GetTable(const Identifier& identifier) const override {
9797
++get_table_calls_;
98-
return std::make_shared<Table>(table_schema_, identifier.GetDatabaseName(),
99-
identifier.GetObjectName(), table_uuid_);
98+
return std::make_shared<Table>(table_schema_, identifier, table_uuid_);
10099
}
101100

102101
void SetTableSchema(const std::shared_ptr<Schema>& table_schema) {

0 commit comments

Comments
 (0)