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

Generalized application-defined scalar functions #1250

Merged
merged 14 commits into from
Nov 24, 2023

Conversation

trueqbit
Copy link
Collaborator

@trueqbit trueqbit commented Nov 22, 2023

Another use of the recently established method for creating aliases and column pointers, this time for creating scalar functions from existing free-standing functions, lambdas and classic function objects.

So instead of going through the process of constantly creating your own user-defined function object ("boiler plate" coding), it's easy to turn existing functions (e.g. from libraries) into scalar functions like this:

// example for a freestanding function from a library
constexpr auto clamp_int_f = "clamp_int"_scalar.quote(std::clamp<int>);
// example for a stateless lambda
constexpr auto is_fatal_error_f = "IS_FATAL_ERROR"_scalar.quote([](unsigned long errcode) {
    return errcode != 0;
});
// example for a function object instance
constexpr auto equal_to_int_f = "equal_to"_scalar.quote(std::equal_to<int>{});
// example for a function object
constexpr auto equal_to_int_2_f = "equal_to"_scalar.quote<std::equal_to<int>>();

storage.create_scalar_function<clamp_int_f>();
storage.create_scalar_function<is_fatal_error_f>();
storage.create_scalar_function<equal_to_int_f>();
storage.create_scalar_function<equal_to_int_2_f>();

auto rows = storage.select(clamp_int_f(0, 1, 1));
auto rows = storage.select(is_fatal_error_f(1));
auto rows = storage.select(equal_to_int_f(1, 1));
auto rows = storage.select(equal_to_int_2_f(1, 1));

A nice advantage is that a stateless function object does not have to be constantly recreated, saving a few levels in the call stack.

@trueqbit trueqbit requested a review from fnc12 November 23, 2023 13:45
dev/functional/char_array_template.h Outdated Show resolved Hide resolved
@trueqbit trueqbit merged commit ddfae57 into fnc12:dev Nov 24, 2023
1 check passed
@trueqbit trueqbit deleted the feature/generalized_udf branch December 24, 2023 13:20
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.

2 participants