Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAPI: Make it possible to create enum types #8788

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Remove "enum_name"
  • Loading branch information
Mytherin authored Nov 8, 2023
commit d717196dd99b31be742fd62e79ca4035e9c40861
3 changes: 1 addition & 2 deletions src/include/duckdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -1393,8 +1393,7 @@ The resulting type should be destroyed with `duckdb_destroy_logical_type`.
* member_count: The number of elements that were specified in the array.
* returns: The logical type.
*/
DUCKDB_API duckdb_logical_type duckdb_create_enum_type(const char *enum_name, const char **member_names,
idx_t member_count);
DUCKDB_API duckdb_logical_type duckdb_create_enum_type(const char **member_names, idx_t member_count);

/*!
Creates a `duckdb_logical_type` of type decimal with the specified width and scale
Expand Down
15 changes: 6 additions & 9 deletions src/main/capi/logical_types-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,22 @@ duckdb_logical_type duckdb_create_struct_type(duckdb_logical_type *member_types_
return reinterpret_cast<duckdb_logical_type>(mtype);
}

duckdb_logical_type duckdb_create_enum_type(const char *enum_name, const char **member_names, idx_t member_count) {
if (!enum_name || !member_names) {
duckdb_logical_type duckdb_create_enum_type(const char **member_names, idx_t member_count) {
if (!member_names) {
return nullptr;
}
for (idx_t i = 0; i < member_count; i++) {
if (!member_names[i]) {
return nullptr;
}
}

duckdb::Vector enum_vector(duckdb::LogicalType::VARCHAR, member_count);
auto enum_vector_ptr = duckdb::FlatVector::GetData<duckdb::string_t>(enum_vector);

for (idx_t i = 0; i < member_count; i++) {
if (!member_names[i]) {
return nullptr;
}
enum_vector_ptr[i] = duckdb::StringVector::AddStringOrBlob(enum_vector, member_names[i]);
}

duckdb::LogicalType *mtype = new duckdb::LogicalType;
*mtype = duckdb::LogicalType::ENUM(enum_name, enum_vector, member_count);
*mtype = duckdb::LogicalType::ENUM(enum_vector, member_count);
return reinterpret_cast<duckdb_logical_type>(mtype);
}

Expand Down
4 changes: 3 additions & 1 deletion test/api/capi/test_capi_complex_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ TEST_CASE("Test struct types creation C API", "[capi]") {
TEST_CASE("Test enum types creation C API", "[capi]") {
duckdb::vector<const char *> names = {"a", "b"};

auto logical_type = duckdb_create_enum_type("name", names.data(), names.size());
auto logical_type = duckdb_create_enum_type(names.data(), names.size());
REQUIRE(duckdb_get_type_id(logical_type) == duckdb_type::DUCKDB_TYPE_ENUM);
REQUIRE(duckdb_enum_dictionary_size(logical_type) == 2);

Expand All @@ -215,4 +215,6 @@ TEST_CASE("Test enum types creation C API", "[capi]") {
REQUIRE(str_name == names[i]);
}
duckdb_destroy_logical_type(&logical_type);

REQUIRE(duckdb_create_enum_type(nullptr, 0) == nullptr);
}
Loading