Skip to content

Commit

Permalink
Move CPP functions wrapper to dedicated file (#452)
Browse files Browse the repository at this point in the history
Move CPP function wrapper to dedicated file.
Next, will do the same for the PG wrapper.
  • Loading branch information
Y-- authored Dec 2, 2024
1 parent 3cb6110 commit af3d278
Show file tree
Hide file tree
Showing 19 changed files with 114 additions and 53 deletions.
2 changes: 2 additions & 0 deletions include/pgduckdb/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "pgduckdb/pgduckdb_process_lock.hpp"

#include "pgduckdb/utility/cpp_only_file.hpp" // Must be last include.

extern "C" {
bool errstart(int elevel, const char *domain);
void errfinish(const char *filename, int lineno, const char *funcname);
Expand Down
9 changes: 9 additions & 0 deletions include/pgduckdb/pg/error_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

extern "C" {
struct ErrorData;
}

namespace pgduckdb::pg {
const char* GetErrorDataMessage(ErrorData* error_data);
}
2 changes: 2 additions & 0 deletions include/pgduckdb/pgduckdb_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "pgduckdb/pg/declarations.hpp"

#include "pgduckdb/utility/cpp_only_file.hpp" // Must be last include.

namespace pgduckdb {

class PostgresScanGlobalState;
Expand Down
56 changes: 20 additions & 36 deletions include/pgduckdb/pgduckdb_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,26 @@
#include "duckdb/common/exception.hpp"
#include "duckdb/common/error_data.hpp"
#include "pgduckdb/pgduckdb_duckdb.hpp"
#include "pgduckdb/pg/error_data.hpp"
#include "pgduckdb/logger.hpp"

#include <setjmp.h>

#include "pgduckdb/utility/cpp_only_file.hpp" // Must be last include.

extern "C" {
#include "postgres.h"
// Note: these forward-declarations could live in a header under the `pg/` folder.
// But since they are (hopefully) only used in this file, we keep them here.
struct ErrorContextCallback;
struct MemoryContextData;

typedef struct MemoryContextData *MemoryContext;

extern sigjmp_buf *PG_exception_stack;
extern MemoryContext CurrentMemoryContext;
extern ErrorContextCallback *error_context_stack;
extern ErrorData * CopyErrorData();
extern void FlushErrorState();
}

namespace pgduckdb {
Expand Down Expand Up @@ -44,52 +61,19 @@ __PostgresFunctionGuard__(const char *func_name, FuncArgs... args) {
return func(std::forward<FuncArgs>(args)...);
} else {
g.RestoreStacks();
MemoryContextSwitchTo(ctx);
CurrentMemoryContext = ctx;
edata = CopyErrorData();
FlushErrorState();
}
} // PG_END_TRY();

auto message = duckdb::StringUtil::Format("(PGDuckDB/%s) %s", func_name, edata->message);
auto message = duckdb::StringUtil::Format("(PGDuckDB/%s) %s", func_name, pg::GetErrorDataMessage(edata));
throw duckdb::Exception(duckdb::ExceptionType::EXECUTOR, message);
}

#define PostgresFunctionGuard(FUNC, ...) \
pgduckdb::__PostgresFunctionGuard__<decltype(&FUNC), &FUNC>(__func__, __VA_ARGS__)

template <typename Func, Func func, typename... FuncArgs>
typename std::invoke_result<Func, FuncArgs...>::type
__CPPFunctionGuard__(const char *func_name, FuncArgs... args) {
const char *error_message = nullptr;
try {
return func(args...);
} catch (duckdb::Exception &ex) {
duckdb::ErrorData edata(ex.what());
error_message = pstrdup(edata.Message().c_str());
} catch (std::exception &ex) {
const auto msg = ex.what();
if (msg[0] == '{') {
duckdb::ErrorData edata(ex.what());
error_message = pstrdup(edata.Message().c_str());
} else {
error_message = pstrdup(ex.what());
}
}

elog(ERROR, "(PGDuckDB/%s) %s", func_name, error_message);
}

#define InvokeCPPFunc(FUNC, ...) pgduckdb::__CPPFunctionGuard__<decltype(&FUNC), &FUNC>(__FUNCTION__, __VA_ARGS__)

// Wrappers

#define DECLARE_PG_FUNCTION(func_name) \
PG_FUNCTION_INFO_V1(func_name); \
Datum func_name##_cpp(PG_FUNCTION_ARGS); \
Datum func_name(PG_FUNCTION_ARGS) { \
return InvokeCPPFunc(func_name##_cpp, fcinfo); \
} \
Datum func_name##_cpp(PG_FUNCTION_ARGS __attribute__((unused)))

duckdb::unique_ptr<duckdb::QueryResult> DuckDBQueryOrThrow(duckdb::ClientContext &context, const std::string &query);

Expand Down
43 changes: 43 additions & 0 deletions include/pgduckdb/utility/cpp_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

extern "C" {
#include "postgres.h"
}

namespace pgduckdb {

template <typename Func, Func func, typename... FuncArgs>
typename std::invoke_result<Func, FuncArgs...>::type
__CPPFunctionGuard__(const char *func_name, FuncArgs... args) {
const char *error_message = nullptr;
try {
return func(args...);
} catch (duckdb::Exception &ex) {
duckdb::ErrorData edata(ex.what());
error_message = pstrdup(edata.Message().c_str());
} catch (std::exception &ex) {
const auto msg = ex.what();
if (msg[0] == '{') {
duckdb::ErrorData edata(ex.what());
error_message = pstrdup(edata.Message().c_str());
} else {
error_message = pstrdup(ex.what());
}
}

elog(ERROR, "(PGDuckDB/%s) %s", func_name, error_message);
}

}

#define InvokeCPPFunc(FUNC, ...) pgduckdb::__CPPFunctionGuard__<decltype(&FUNC), &FUNC>(__FUNCTION__, __VA_ARGS__)

// Wrappers

#define DECLARE_PG_FUNCTION(func_name) \
PG_FUNCTION_INFO_V1(func_name); \
Datum func_name##_cpp(PG_FUNCTION_ARGS); \
Datum func_name(PG_FUNCTION_ARGS) { \
return InvokeCPPFunc(func_name##_cpp, fcinfo); \
} \
Datum func_name##_cpp(PG_FUNCTION_ARGS __attribute__((unused)))
11 changes: 11 additions & 0 deletions src/pg/error_data.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "pgduckdb/pg/error_data.hpp"

extern "C" {
#include "postgres.h"
}

namespace pgduckdb::pg {
const char* GetErrorDataMessage(ErrorData* error_data) {
return error_data->message;
}
} // namespace pgduckdb
3 changes: 2 additions & 1 deletion src/pgduckdb_background_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "duckdb/storage/table_storage_info.hpp"
#include "duckdb/main/attached_database.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"
#include "pgduckdb/utility/cpp_wrapper.hpp"
#include <string>
#include <unordered_map>

Expand Down Expand Up @@ -50,7 +52,6 @@ extern "C" {
#include "pgduckdb/pgduckdb_duckdb.hpp"
#include "pgduckdb/pgduckdb_background_worker.hpp"
#include "pgduckdb/pgduckdb_metadata_cache.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

static bool is_background_worker = false;
static std::unordered_map<std::string, std::string> last_known_motherduck_catalog_versions;
Expand Down
3 changes: 2 additions & 1 deletion src/pgduckdb_ddl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <regex>

#include "pgduckdb/pgduckdb_planner.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

extern "C" {
#include "postgres.h"
Expand All @@ -27,10 +28,10 @@ extern "C" {
#include "pgduckdb/pgduckdb_ruleutils.h"
}

#include "pgduckdb/utility/cpp_wrapper.hpp"
#include "pgduckdb/pgduckdb_duckdb.hpp"
#include "pgduckdb/pgduckdb_background_worker.hpp"
#include "pgduckdb/pgduckdb_metadata_cache.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"
#include "pgduckdb/utility/copy.hpp"
#include "pgduckdb/vendor/pg_list.hpp"
#include <inttypes.h>
Expand Down
5 changes: 3 additions & 2 deletions src/pgduckdb_detoast.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include "duckdb.hpp"

#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

extern "C" {
#include "postgres.h"
#include "pg_config.h"
Expand All @@ -20,9 +23,7 @@ extern "C" {
}

#include "pgduckdb/pgduckdb_process_lock.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_detoast.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

/*
* Following functions are direct logic found in postgres code but for duckdb execution they are needed to be thread
Expand Down
2 changes: 1 addition & 1 deletion src/pgduckdb_duckdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/scan/postgres_seq_scan.hpp"
#include "pgduckdb/pg/transactions.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

extern "C" {
#include "postgres.h"
Expand All @@ -23,7 +24,6 @@ extern "C" {
#include "pgduckdb/pgduckdb_options.hpp"
#include "pgduckdb/pgduckdb_xact.hpp"
#include "pgduckdb/pgduckdb_metadata_cache.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

#include <sys/stat.h>
#include <unistd.h>
Expand Down
2 changes: 1 addition & 1 deletion src/pgduckdb_filter.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "duckdb.hpp"
#include "duckdb/planner/filter/constant_filter.hpp"
#include "pgduckdb/pgduckdb_types.hpp"

extern "C" {
#include "postgres.h"
Expand All @@ -14,7 +15,6 @@ extern "C" {

#include "pgduckdb/pgduckdb_filter.hpp"
#include "pgduckdb/pgduckdb_detoast.hpp"
#include "pgduckdb/pgduckdb_types.hpp"

namespace pgduckdb {

Expand Down
3 changes: 2 additions & 1 deletion src/pgduckdb_hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "pgduckdb/pgduckdb_planner.hpp"
#include "pgduckdb/pg/transactions.hpp"
#include "pgduckdb/pgduckdb_xact.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

extern "C" {
#include "postgres.h"
Expand All @@ -29,8 +30,8 @@ extern "C" {
#include "pgduckdb/utility/copy.hpp"
#include "pgduckdb/vendor/pg_explain.hpp"
#include "pgduckdb/vendor/pg_list.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"
#include "pgduckdb/pgduckdb_node.hpp"
#include "pgduckdb/utility/cpp_wrapper.hpp"

static planner_hook_type prev_planner_hook = NULL;
static ExecutorStart_hook_type prev_executor_start_hook = NULL;
Expand Down
4 changes: 2 additions & 2 deletions src/pgduckdb_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "duckdb/common/exception.hpp"

#include "pgduckdb/pgduckdb_planner.hpp"
#include "pgduckdb/pgduckdb_types.hpp"

extern "C" {
#include "postgres.h"
Expand All @@ -14,9 +15,8 @@ extern "C" {
}

#include "pgduckdb/pgduckdb_node.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_duckdb.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"
#include "pgduckdb/utility/cpp_wrapper.hpp"

/* global variables */
CustomScanMethods duckdb_scan_scan_methods;
Expand Down
5 changes: 4 additions & 1 deletion src/pgduckdb_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include <filesystem>
#include <fstream>

#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

extern "C" {
#include "postgres.h"
#include "miscadmin.h"
Expand All @@ -24,8 +27,8 @@ extern "C" {
#include "pgduckdb/pgduckdb_options.hpp"
#include "pgduckdb/pgduckdb_duckdb.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_xact.hpp"
#include "pgduckdb/utility/cpp_wrapper.hpp"

namespace pgduckdb {

Expand Down
4 changes: 2 additions & 2 deletions src/pgduckdb_planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "pgduckdb/catalog/pgduckdb_transaction.hpp"
#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/pgduckdb_types.hpp"

extern "C" {
#include "postgres.h"
Expand All @@ -24,8 +25,7 @@ extern "C" {

#include "pgduckdb/pgduckdb_duckdb.hpp"
#include "pgduckdb/pgduckdb_node.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"
#include "pgduckdb/utility/cpp_wrapper.hpp"

bool duckdb_explain_analyze = false;

Expand Down
3 changes: 2 additions & 1 deletion src/pgduckdb_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include "duckdb/common/extra_type_info.hpp"
#include "duckdb/common/types/uuid.hpp"

#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/types/decimal.hpp"

extern "C" {
#include "postgres.h"
Expand All @@ -26,7 +28,6 @@ extern "C" {
#include "pgduckdb/types/decimal.hpp"
#include "pgduckdb/pgduckdb_filter.hpp"
#include "pgduckdb/pgduckdb_detoast.hpp"
#include "pgduckdb/pgduckdb_types.hpp"

namespace pgduckdb {

Expand Down
2 changes: 2 additions & 0 deletions src/pgduckdb_xact.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "duckdb/common/exception.hpp"
#include "pgduckdb/pgduckdb_duckdb.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

#include "pgduckdb/pg/transactions.hpp"
#include "pgduckdb/utility/cpp_wrapper.hpp"

namespace pgduckdb {

Expand Down
4 changes: 2 additions & 2 deletions src/scan/heap_reader.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "duckdb.hpp"

#include "pgduckdb/scan/heap_reader.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

extern "C" {
#include "postgres.h"
Expand All @@ -12,8 +14,6 @@ extern "C" {
}

#include "pgduckdb/pgduckdb_process_lock.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

#include <optional>

Expand Down
4 changes: 2 additions & 2 deletions src/scan/postgres_scan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "duckdb/common/enums/expression_type.hpp"

#include "pgduckdb/scan/postgres_scan.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

extern "C" {
#include "postgres.h"
Expand All @@ -28,8 +30,6 @@ extern "C" {
}

#include "pgduckdb/pgduckdb_process_lock.hpp"
#include "pgduckdb/pgduckdb_types.hpp"
#include "pgduckdb/pgduckdb_utils.hpp"

namespace pgduckdb {

Expand Down

0 comments on commit af3d278

Please sign in to comment.