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

Add SQL scalar and table macros and tests #84

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
28 changes: 28 additions & 0 deletions src/quack_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "duckdb/function/scalar_function.hpp"
#include "duckdb/main/extension_util.hpp"
#include <duckdb/parser/parsed_data/create_scalar_function_info.hpp>
#include "duckdb/catalog/default/default_functions.hpp"
#include "duckdb/catalog/default/default_table_functions.hpp"

// OpenSSL linked through vcpkg
#include <openssl/opensslv.h>
Expand All @@ -33,7 +35,22 @@ inline void QuackOpenSSLVersionScalarFun(DataChunk &args, ExpressionState &state
});
}

// clang-format off
// SQL scalar macros
static DefaultMacro quack_macros[] = {
{DEFAULT_SCHEMA, "default_to_times_two", {"x", nullptr}, {{"multiplier", "2"}, {nullptr, nullptr}}, R"( x * multiplier )"},
{nullptr, nullptr, {nullptr}, {{nullptr, nullptr}}, nullptr}
};

// SQL table macros
static const DefaultTableMacro quack_table_macros[] = {
{DEFAULT_SCHEMA, "default_to_times_two_table", {"x", nullptr}, {{"multiplier", "2"}, {nullptr, nullptr}}, R"( SELECT x * multiplier as output_column; )"},
{nullptr, nullptr, {nullptr}, {{nullptr, nullptr}}, nullptr}
};
// clang-format on

static void LoadInternal(DatabaseInstance &instance) {

// Register a scalar function
auto quack_scalar_function = ScalarFunction("quack", {LogicalType::VARCHAR}, LogicalType::VARCHAR, QuackScalarFun);
ExtensionUtil::RegisterFunction(instance, quack_scalar_function);
Expand All @@ -42,6 +59,17 @@ static void LoadInternal(DatabaseInstance &instance) {
auto quack_openssl_version_scalar_function = ScalarFunction("quack_openssl_version", {LogicalType::VARCHAR},
LogicalType::VARCHAR, QuackOpenSSLVersionScalarFun);
ExtensionUtil::RegisterFunction(instance, quack_openssl_version_scalar_function);

// Register SQL scalar macros
for (idx_t index = 0; quack_macros[index].name != nullptr; index++) {
auto info = DefaultFunctionGenerator::CreateInternalMacroInfo(quack_macros[index]);
ExtensionUtil::RegisterFunction(instance, *info);
}
// Register SQL table macros
for (idx_t index = 0; quack_table_macros[index].name != nullptr; index++) {
auto table_info = DefaultTableFunctionGenerator::CreateTableMacroInfo(quack_table_macros[index]);
ExtensionUtil::RegisterFunction(instance, *table_info);
}
}

void QuackExtension::Load(DuckDB &db) {
Expand Down
15 changes: 15 additions & 0 deletions test/sql/quack.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,27 @@ Catalog Error: Scalar Function with name quack does not exist!
require quack

# Confirm the extension works

# Test scalar C++ function
query I
SELECT quack('Sam');
----
Quack Sam 🐥

# Test scalar C++ function using vcpkg and openssl
query I
SELECT quack_openssl_version('Michael') ILIKE 'Quack Michael, my linked OpenSSL version is OpenSSL%';
----
true

# Test scalar macro overriding the default
query I
SELECT default_to_times_two(4, multiplier:=3);
----
12

# Test table macro overriding the default
query I
SELECT * FROM default_to_times_two_table(5, multiplier:=3);
----
15
Loading