Skip to content

Add duckdb_enum_values C API function to get enum dictionary strings #11704

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

Closed
wants to merge 4 commits into from

Conversation

karlseguin
Copy link
Contributor

This adds a duckdb_enum_values function to the C-API. It populates the passed-in char** with the enum string values, preserving the insertion order. This allows a driver to accept a string value and map it to the internal integer representation. I plan on using in my Zig driver to allow string values to be appended to enum columns (using the existing duckdb_append_data_chunk).

Change API to return a duckdb_state and use an out parameter for set length.
@duckdb-draftbot duckdb-draftbot marked this pull request as draft April 18, 2024 09:32
@karlseguin karlseguin marked this pull request as ready for review April 18, 2024 10:27
@duckdb-draftbot duckdb-draftbot marked this pull request as draft April 18, 2024 13:04
@karlseguin karlseguin marked this pull request as ready for review April 18, 2024 14:00
@Tishj
Copy link
Contributor

Tishj commented Apr 21, 2024

D:\a\duckdb\duckdb\src\main\capi\logical_types-c.cpp(216,45): error C2065: 'string_t': undeclared identifier [D:\a\duckdb\duckdb\src\main\capi\duckdb_main_capi.vcxproj]
D:\a\duckdb\duckdb\src\main\capi\logical_types-c.cpp(216,37): error C2672: 'duckdb::FlatVector::GetData': no matching overloaded function found [D:\a\duckdb\duckdb\src\main\capi\duckdb_main_capi.vcxproj]
  D:\a\duckdb\duckdb\src\include\duckdb\common\types\vector.hpp(329,19):
  could be 'T *duckdb::FlatVector::GetData(duckdb::Vector &)'
  	D:\a\duckdb\duckdb\src\main\capi\logical_types-c.cpp(216,45):
  	'duckdb::FlatVector::GetData': invalid template argument for 'T', type expected
  D:\a\duckdb\duckdb\src\include\duckdb\common\types\vector.hpp(325,25):
  or       'const T *duckdb::FlatVector::GetData(const duckdb::Vector &)'
  	D:\a\duckdb\duckdb\src\main\capi\logical_types-c.cpp(216,45):
  	'duckdb::FlatVector::GetData': invalid template argument for 'T', type expected
  
D:\a\duckdb\duckdb\src\main\capi\logical_types-c.cpp(221,16): error C3536: 'strings': cannot be used before it is initialized [D:\a\duckdb\duckdb\src\main\capi\duckdb_main_capi.vcxproj]
D:\a\duckdb\duckdb\src\main\capi\logical_types-c.cpp(221,25): error C2109: subscript requires array or pointer type [D:\a\duckdb\duckdb\src\main\capi\duckdb_main_capi.vcxproj]
D:\a\duckdb\duckdb\src\main\capi\logical_types-c.cpp(222,47): error C3536: 'value': cannot be used before it is initialized [D:\a\duckdb\duckdb\src\main\capi\duckdb_main_capi.vcxproj]

seems to missing an include for duckdb/common/types/string_type.hpp

@duckdb-draftbot duckdb-draftbot marked this pull request as draft April 22, 2024 03:31
@karlseguin
Copy link
Contributor Author

I don't understand the build. I was able to run the tests and build it. But I changed string_t to duckdb::string_t which duckdb_create_enum_type in the hope that this is what was needed.

@karlseguin karlseguin marked this pull request as ready for review April 22, 2024 05:00
@Mytherin
Copy link
Collaborator

Thanks for the PR! Can't the same behavior be accomplished by using duckdb_enum_dictionary_size and duckdb_enum_dictionary_value?

In general we want to keep the C API lean and not add functions unnecessarily. It is not meant to be nice to use stand-alone but rather meant as a building block for other APIs, so if the functionality is available elsewhere adding extra functions is not desirable.

@Mytherin Mytherin added the Draft label Apr 23, 2024
@Giorgi
Copy link
Contributor

Giorgi commented Apr 23, 2024

@Mytherin What about adding a function that takes an enum member name and returns its numeric value? Similar to duckdb_enum_dictionary_value but reversed. Sure, it can be done with duckdb_enum_dictionary_size and duckdb_enum_dictionary_value but I think it's useful to have a dedicated function instead of calling duckdb_enum_dictionary_value in a loop.

@Mytherin
Copy link
Collaborator

Sure, that makes sense

@karlseguin
Copy link
Contributor Author

You know...I didn't want to couple the size of the dictionary to the underlying enum value. I figured the two might not always be tightly coupled: what if you were allowed to drop an enum value, then you'd have a hole. But I realize that might implementation has the same baked-in coupling by depending on the index of the array...So sorry.

@karlseguin karlseguin closed this Apr 24, 2024
@Mytherin
Copy link
Collaborator

No need to apologize.

The logical type contains the type after catalog resolution, so a drop/create of the enum value would not affect it as we are not going back to the catalog between these steps. As such that scenario will work correctly in both cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants