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 2 commits
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
12 changes: 12 additions & 0 deletions src/include/duckdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,18 @@ The resulting type should be destroyed with `duckdb_destroy_logical_type`.
DUCKDB_API duckdb_logical_type duckdb_create_struct_type(duckdb_logical_type *member_types, const char **member_names,
idx_t member_count);

/*!
Creates an ENUM type from the passed member name array.
The resulting type should be destroyed with `duckdb_destroy_logical_type`.

* enum_name: The name of the enum.
* member_names: The array of names that the enum should consist of.
* 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);

/*!
Creates a `duckdb_logical_type` of type decimal with the specified width and scale
The resulting type should be destroyed with `duckdb_destroy_logical_type`.
Expand Down
22 changes: 22 additions & 0 deletions src/main/capi/logical_types-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ 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) {
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++) {
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);
return reinterpret_cast<duckdb_logical_type>(mtype);
}

duckdb_logical_type duckdb_create_map_type(duckdb_logical_type key_type, duckdb_logical_type value_type) {
if (!key_type || !value_type) {
return nullptr;
Expand Down
16 changes: 16 additions & 0 deletions test/api/capi/test_capi_complex_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,19 @@ TEST_CASE("Test struct types creation C API", "[capi]") {
}
duckdb_destroy_logical_type(&logical_type);
}

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());
REQUIRE(duckdb_get_type_id(logical_type) == duckdb_type::DUCKDB_TYPE_ENUM);
REQUIRE(duckdb_enum_dictionary_size(logical_type) == 2);

for (idx_t i = 0; i < names.size(); i++) {
auto name = duckdb_enum_dictionary_value(logical_type, i);
string str_name(name);
duckdb_free(name);
REQUIRE(str_name == names[i]);
}
duckdb_destroy_logical_type(&logical_type);
}