Skip to content

Commit 95940ad

Browse files
authored
feat(catalog, predicate, schema): Add utility APIs (alibaba#64)
* feat(catalog): Support getting db/tbl location * feat(predicate): Add Not for PredicateBuilder * feat(schema): Support GetFieldType from Schema * fix review
1 parent ca51b80 commit 95940ad

9 files changed

Lines changed: 78 additions & 8 deletions

File tree

include/paimon/catalog/catalog.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ class PAIMON_EXPORT Catalog {
108108
/// @return A result containing true if the table exists, false otherwise, or an error status.
109109
virtual Result<bool> TableExists(const Identifier& identifier) const = 0;
110110

111+
/// Returns the expected location of a specified database.
112+
///
113+
/// @note This does not check whether the database actually exists.
114+
///
115+
/// @param db_name The name of the database to get the location for.
116+
/// @return A string representing the expected location of the database.
117+
virtual std::string GetDatabaseLocation(const std::string& db_name) const = 0;
118+
119+
/// Returns the expected location of a specified table.
120+
///
121+
/// @note This does not check whether the table actually exists.
122+
///
123+
/// @param identifier The table identifier containing database and table name.
124+
/// @return A string representing the expected location of the table.
125+
virtual std::string GetTableLocation(const Identifier& identifier) const = 0;
126+
111127
/// Loads the latest schema of a specified table.
112128
///
113129
/// @note System tables will not be supported.

include/paimon/predicate/predicate_builder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,12 @@ class PAIMON_EXPORT PredicateBuilder {
126126
/// @param predicates A vector of shared pointers to the predicates, which must not be empty.
127127
static Result<std::shared_ptr<Predicate>> Or(
128128
const std::vector<std::shared_ptr<Predicate>>& predicates);
129+
130+
/// Create a NOT predicate negating the result of another predicate.
131+
///
132+
/// Creates a logical NOT operation that inverts the truth value of the input predicate.
133+
///
134+
/// @param predicate A shared pointer to the predicate to be negated, which must not be nullptr.
135+
static Result<std::shared_ptr<Predicate>> Not(const std::shared_ptr<Predicate>& predicate);
129136
};
130137
} // namespace paimon

include/paimon/schema/schema.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ class PAIMON_EXPORT Schema {
5050
/// @return A vector of field names.
5151
virtual std::vector<std::string> FieldNames() const = 0;
5252

53+
/// Get the type of specific field.
54+
///
55+
/// @param field_name The name of the field to query.
56+
/// @return Result containing the FieldType of the specified field, or an error status on
57+
/// failure.
58+
virtual Result<FieldType> GetFieldType(const std::string& field_name) const = 0;
59+
5360
/// Get the unique identifier of this table schema.
5461
/// @return The schema id
5562
virtual int64_t Id() const = 0;

src/paimon/common/predicate/predicate_builder.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,12 @@ Result<std::shared_ptr<Predicate>> PredicateBuilder::Or(
152152
}
153153
return std::make_shared<CompoundPredicateImpl>(Or::Instance(), predicates);
154154
}
155+
156+
Result<std::shared_ptr<Predicate>> PredicateBuilder::Not(
157+
const std::shared_ptr<Predicate>& predicate) {
158+
if (!predicate) {
159+
return Status::Invalid("There must not be nullptr to construct a NOT predicate");
160+
}
161+
return predicate->Negate();
162+
}
155163
} // namespace paimon

src/paimon/core/catalog/file_system_catalog.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ Result<bool> FileSystemCatalog::TableExists(const Identifier& identifier) const
9292
return latest_schema != std::nullopt;
9393
}
9494

95+
std::string FileSystemCatalog::GetDatabaseLocation(const std::string& db_name) const {
96+
return NewDatabasePath(warehouse_, db_name);
97+
}
98+
99+
std::string FileSystemCatalog::GetTableLocation(const Identifier& identifier) const {
100+
return NewDataTablePath(warehouse_, identifier);
101+
}
102+
95103
Status FileSystemCatalog::CreateTable(const Identifier& identifier, ArrowSchema* c_schema,
96104
const std::vector<std::string>& partition_keys,
97105
const std::vector<std::string>& primary_keys,

src/paimon/core/catalog/file_system_catalog.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class FileSystemCatalog : public Catalog {
5151
Result<std::vector<std::string>> ListTables(const std::string& db_name) const override;
5252
Result<bool> DatabaseExists(const std::string& db_name) const override;
5353
Result<bool> TableExists(const Identifier& identifier) const override;
54+
std::string GetDatabaseLocation(const std::string& db_name) const override;
55+
std::string GetTableLocation(const Identifier& identifier) const override;
5456
Result<std::shared_ptr<Schema>> LoadTableSchema(const Identifier& identifier) const override;
5557

5658
private:

src/paimon/core/catalog/file_system_catalog_test.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ TEST(FileSystemCatalogTest, TestDatabaseExists) {
5353
ASSERT_OK_AND_ASSIGN(std::vector<std::string> db_names, catalog.ListDatabases());
5454
ASSERT_EQ(1, db_names.size());
5555
ASSERT_EQ(db_names[0], "db1");
56+
ASSERT_EQ(catalog.GetDatabaseLocation("db1"), PathUtil::JoinPath(dir->Str(), "db1.db"));
5657
}
5758

5859
TEST(FileSystemCatalogTest, TestInvalidCreateDatabase) {
@@ -297,11 +298,13 @@ TEST(FileSystemCatalogTest, TestCreateTableWhileTableExist) {
297298
arrow::Schema typed_schema(fields);
298299
::ArrowSchema schema;
299300
ASSERT_TRUE(arrow::ExportSchema(typed_schema, &schema).ok());
300-
ASSERT_OK(catalog.CreateTable(Identifier("db1", "tbl1"), &schema, {"f1"}, {}, options,
301+
Identifier identifier("db1", "tbl1");
302+
ASSERT_OK(catalog.CreateTable(identifier, &schema, {"f1"}, {}, options,
301303
/*ignore_if_exists=*/true));
302304
ASSERT_OK_AND_ASSIGN(auto fs, FileSystemFactory::Get("local", dir->Str(), {}));
303-
ASSERT_OK(fs->Delete(PathUtil::JoinPath(dir->Str(), "db1.db/tbl1/schema/schema-0")));
304-
ASSERT_OK(catalog.CreateTable(Identifier("db1", "tbl1"), &schema, {"f1"}, {}, options,
305+
ASSERT_OK(fs->Delete(
306+
PathUtil::JoinPath(catalog.GetTableLocation(identifier), "schema/schema-0")));
307+
ASSERT_OK(catalog.CreateTable(identifier, &schema, {"f1"}, {}, options,
305308
/*ignore_if_exists=*/false));
306309
}
307310
}
@@ -336,13 +339,13 @@ TEST(FileSystemCatalogTest, TestValidateTableSchema) {
336339
arrow::Schema typed_schema(fields);
337340
::ArrowSchema schema;
338341
ASSERT_TRUE(arrow::ExportSchema(typed_schema, &schema).ok());
339-
ASSERT_OK(catalog.CreateTable(Identifier("db1", "tbl1"), &schema, {"f1"}, {}, options,
342+
Identifier identifier("db1", "tbl1");
343+
ASSERT_OK(catalog.CreateTable(identifier, &schema, {"f1"}, {}, options,
340344
/*ignore_if_exists=*/false));
341345

342346
ASSERT_NOK_WITH_MSG(catalog.LoadTableSchema(Identifier("db0", "tbl0")),
343347
"Identifier{database=\'db0\', table=\'tbl0\'} not exist");
344-
ASSERT_OK_AND_ASSIGN(std::shared_ptr<Schema> table_schema,
345-
catalog.LoadTableSchema(Identifier("db1", "tbl1")));
348+
ASSERT_OK_AND_ASSIGN(std::shared_ptr<Schema> table_schema, catalog.LoadTableSchema(identifier));
346349
ASSERT_EQ(0, table_schema->Id());
347350
ASSERT_EQ(3, table_schema->HighestFieldId());
348351
ASSERT_EQ(1, table_schema->PartitionKeys().size());
@@ -353,8 +356,20 @@ TEST(FileSystemCatalogTest, TestValidateTableSchema) {
353356
std::vector<std::string> expected_field_names = {"f0", "f1", "f2", "f3"};
354357
ASSERT_EQ(field_names, expected_field_names);
355358

359+
FieldType type;
360+
ASSERT_OK_AND_ASSIGN(type, table_schema->GetFieldType("f0"));
361+
ASSERT_EQ(type, FieldType::STRING);
362+
ASSERT_OK_AND_ASSIGN(type, table_schema->GetFieldType("f1"));
363+
ASSERT_EQ(type, FieldType::INT);
364+
ASSERT_OK_AND_ASSIGN(type, table_schema->GetFieldType("f2"));
365+
ASSERT_EQ(type, FieldType::INT);
366+
ASSERT_OK_AND_ASSIGN(type, table_schema->GetFieldType("f3"));
367+
ASSERT_EQ(type, FieldType::DOUBLE);
368+
ASSERT_NOK(table_schema->GetFieldType("f4"));
369+
356370
ASSERT_OK_AND_ASSIGN(auto fs, FileSystemFactory::Get("local", dir->Str(), {}));
357-
std::string schema_path = PathUtil::JoinPath(dir->Str(), "db1.db/tbl1/schema/schema-0");
371+
std::string schema_path =
372+
PathUtil::JoinPath(catalog.GetTableLocation(identifier), "schema/schema-0");
358373
std::string expected_json_schema;
359374
ASSERT_OK(fs->ReadFile(schema_path, &expected_json_schema));
360375

@@ -366,7 +381,7 @@ TEST(FileSystemCatalogTest, TestValidateTableSchema) {
366381
ASSERT_TRUE(typed_schema.Equals(loaded_schema));
367382

368383
ASSERT_OK(fs->Delete(schema_path));
369-
ASSERT_NOK_WITH_MSG(catalog.LoadTableSchema(Identifier("db1", "tbl1")),
384+
ASSERT_NOK_WITH_MSG(catalog.LoadTableSchema(identifier),
370385
"Identifier{database=\'db1\', table=\'tbl1\'} not exist");
371386

372387
ASSERT_NOK_WITH_MSG(catalog.LoadTableSchema(Identifier("db1", "tbl$11")),

src/paimon/core/schema/table_schema.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ std::vector<std::string> TableSchema::FieldNames() const {
186186
return field_names;
187187
}
188188

189+
Result<FieldType> TableSchema::GetFieldType(const std::string& field_name) const {
190+
PAIMON_ASSIGN_OR_RAISE(DataField field, GetField(field_name));
191+
return FieldTypeUtils::ConvertToFieldType(field.Type()->id());
192+
}
193+
189194
Result<DataField> TableSchema::GetField(const std::string& field_name) const {
190195
for (const auto& field : Fields()) {
191196
if (field.Name() == field_name) {

src/paimon/core/schema/table_schema.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class TableSchema : public Schema, public Jsonizable<TableSchema> {
6666

6767
std::vector<std::string> FieldNames() const override;
6868

69+
Result<FieldType> GetFieldType(const std::string& field_name) const override;
70+
6971
int64_t Id() const override {
7072
return id_;
7173
}

0 commit comments

Comments
 (0)