Skip to content

Add aggregate function support to the C API #13229

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

Merged
merged 15 commits into from
Aug 1, 2024

Conversation

Mytherin
Copy link
Collaborator

This PR adds initial support for aggregate functions to the C API. This allows aggregate functions to be defined and registered using only the C API, which in turn will enable these functions to be added using other APIs that build on top of the C API (e.g. Rust or Go).

These are the added callbacks that allow for creating and registering a function:

duckdb_aggregate_function duckdb_create_aggregate_function();
void duckdb_destroy_aggregate_function(duckdb_aggregate_function *aggregate_function);
void duckdb_aggregate_function_set_name(duckdb_aggregate_function aggregate_function, const char *name);
void duckdb_aggregate_function_add_parameter(duckdb_aggregate_function aggregate_function,
                                                        duckdb_logical_type type);
void duckdb_aggregate_function_set_return_type(duckdb_aggregate_function aggregate_function,
                                                          duckdb_logical_type type);
void duckdb_aggregate_function_set_functions(duckdb_aggregate_function aggregate_function,
                                                        duckdb_aggregate_state_size state_size,
                                                        duckdb_aggregate_init_t state_init,
                                                        duckdb_aggregate_update_t update,
                                                        duckdb_aggregate_combine_t combine,
                                                        duckdb_aggregate_finalize_t finalize);
void duckdb_aggregate_function_set_destructor(duckdb_aggregate_function aggregate_function,
                                                         duckdb_aggregate_destroy_t destroy);
duckdb_state duckdb_register_aggregate_function(duckdb_connection con,
                                                           duckdb_aggregate_function aggregate_function);
void duckdb_aggregate_function_set_special_handling(duckdb_aggregate_function aggregate_function);
void duckdb_aggregate_function_set_extra_info(duckdb_aggregate_function aggregate_function, void *extra_info,
                                                         duckdb_delete_callback_t destroy);
void *duckdb_aggregate_function_get_extra_info(duckdb_function_info info);
void duckdb_aggregate_function_set_error(duckdb_function_info info, const char *error);

The C API aggregate functions themselves need to implement five different callbacks, plus one optional callback (for destroying any allocated state):

//! Returns the aggregate state size
typedef idx_t (*duckdb_aggregate_state_size)(duckdb_function_info info);
//! Initialize the aggregate state
typedef void (*duckdb_aggregate_init_t)(duckdb_function_info info, duckdb_aggregate_state state);
//! Destroy aggregate state (optional)
typedef void (*duckdb_aggregate_destroy_t)(duckdb_aggregate_state *states, idx_t count);
//! Update a set of aggregate states with new values
typedef void (*duckdb_aggregate_update_t)(duckdb_function_info info, duckdb_data_chunk input,
                                          duckdb_aggregate_state *states);
//! Combine aggregate states
typedef void (*duckdb_aggregate_combine_t)(duckdb_function_info info, duckdb_aggregate_state *source,
                                           duckdb_aggregate_state *target, idx_t count);
//! Finalize aggregate states into a result vector
typedef void (*duckdb_aggregate_finalize_t)(duckdb_function_info info, duckdb_aggregate_state *source,
                                            duckdb_vector result, idx_t count, idx_t offset);

@duckdb-draftbot duckdb-draftbot marked this pull request as draft July 31, 2024 13:57
@Mytherin Mytherin marked this pull request as ready for review July 31, 2024 13:59
@duckdb-draftbot duckdb-draftbot marked this pull request as draft July 31, 2024 14:30
@Mytherin Mytherin marked this pull request as ready for review July 31, 2024 14:37
@Mytherin Mytherin merged commit 5f2db1a into duckdb:main Aug 1, 2024
40 checks passed
github-actions bot pushed a commit to duckdb/duckdb-r that referenced this pull request Aug 2, 2024
Merge pull request duckdb/duckdb#13229 from Mytherin/capiaggregate
@Mytherin Mytherin deleted the capiaggregate branch August 4, 2024 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant