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

Adding progress bar display func #951

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
50 changes: 50 additions & 0 deletions src/connection.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "rapi.hpp"
#include "r_progress_bar_display.hpp"
#include "duckdb/main/client_context.hpp"

using namespace duckdb;

Expand All @@ -7,6 +9,50 @@ void duckdb::ConnDeleter(ConnWrapper *conn) {
delete conn;
}

unique_ptr<ProgressBarDisplay> RProgressBarDisplay::Create() {
return make_uniq<RProgressBarDisplay>();
}

void RProgressBarDisplay::Initialize() {
auto progress_display = Rf_GetOption(RStrings::get().progress_display_sym, R_BaseEnv);
if (Rf_isFunction(progress_display)) {
progress_callback = progress_display;
}
D_ASSERT(progress_callback != R_NilValue);
}

RProgressBarDisplay::RProgressBarDisplay() : ProgressBarDisplay() {
// Empty
}

void RProgressBarDisplay::Update(double percentage) {
if (progress_callback == R_NilValue) {
Initialize();
}
if (progress_callback != R_NilValue) {
try {
cpp11::sexp call = Rf_lang2(progress_callback, Rf_ScalarReal(percentage));
cpp11::safe[Rf_eval](call, R_BaseEnv);
} catch (std::exception &e) {
// Ignore progress bar error
}
}
}

void RProgressBarDisplay::Finish() {
Update(100);
}

static void SetDefaultConfigArguments(ClientContext &context) {
auto &config = ClientConfig::GetConfig(context);
// Set the function used to create the display for the progress bar
config.display_create_func = RProgressBarDisplay::Create;
auto progress_display = Rf_GetOption(RStrings::get().progress_display_sym, R_BaseEnv);
if (Rf_isFunction(progress_display)) {
config.enable_progress_bar = true;
}
}

[[cpp11::register]] duckdb::conn_eptr_t rapi_connect(duckdb::db_eptr_t dual) {
if (!dual || !dual.get()) {
cpp11::stop("rapi_connect: Invalid database reference");
Expand All @@ -20,6 +66,10 @@ void duckdb::ConnDeleter(ConnWrapper *conn) {
conn_wrapper->conn = make_uniq<Connection>(*db->db);
conn_wrapper->db.swap(db);

// Set progress display config
auto &client_context = *conn_wrapper->conn->context;
SetDefaultConfigArguments(client_context);

// The connection now holds a reference to the database.
// This reference is released when the connection is closed.
// From the R side, the database pointer will remain valid
Expand Down
28 changes: 28 additions & 0 deletions src/include/r_progress_bar_display.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "rapi.hpp"
#include "duckdb/common/progress_bar/progress_bar_display.hpp"
#include "duckdb/common/helper.hpp"

namespace duckdb {

class RProgressBarDisplay : public ProgressBarDisplay {
public:
RProgressBarDisplay();
virtual ~RProgressBarDisplay() {
}

static unique_ptr<ProgressBarDisplay> Create();

public:
void Update(double percentage) override;
void Finish() override;

private:
void Initialize();

private:
SEXP progress_callback = R_NilValue;
};

} // namespace duckdb
1 change: 1 addition & 0 deletions src/include/rapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ struct RStrings {
SEXP ImportRecordBatchReader_sym;
SEXP materialize_callback_sym;
SEXP materialize_message_sym;
SEXP progress_display_sym;
SEXP duckdb_row_names_sym;
SEXP duckdb_vector_sym;

Expand Down
1 change: 1 addition & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ RStrings::RStrings() {
Table__from_record_batches_sym = Rf_install("Table__from_record_batches");
materialize_message_sym = Rf_install("duckdb.materialize_message");
materialize_callback_sym = Rf_install("duckdb.materialize_callback");
progress_display_sym = Rf_install("duckdb.progress_display");
duckdb_row_names_sym = Rf_install("duckdb_row_names");
duckdb_vector_sym = Rf_install("duckdb_vector");
}
Expand Down