Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
samansmink committed Jan 13, 2025
1 parent 020eb46 commit e5ee3e1
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 74 deletions.
11 changes: 6 additions & 5 deletions src/delta_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,13 @@ static void LoadInternal(DatabaseInstance &instance) {
"delta scan during explain analyze queries.",
LogicalType::BOOLEAN, Value(true));

config.AddExtensionOption("delta_kernel_logging",
"Forwards the internal logging of the Delta Kernel to the duckdb logger. Warning: this may impact "
"performance even with DuckDB logging disabled.",
LogicalType::BOOLEAN, Value(false), LoggerCallback::DuckDBSettingCallBack);
config.AddExtensionOption(
"delta_kernel_logging",
"Forwards the internal logging of the Delta Kernel to the duckdb logger. Warning: this may impact "
"performance even with DuckDB logging disabled.",
LogicalType::BOOLEAN, Value(false), LoggerCallback::DuckDBSettingCallBack);

LoggerCallback::Initialize(instance);
LoggerCallback::Initialize(instance);
}

void DeltaExtension::Load(DuckDB &db) {
Expand Down
103 changes: 52 additions & 51 deletions src/delta_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,69 +636,70 @@ uintptr_t PredicateVisitor::VisitFilter(const string &col_name, const TableFilte
}

void LoggerCallback::Initialize(DatabaseInstance &db_p) {
auto &instance = GetInstance();
unique_lock<mutex> lck(instance.lock);
instance.db = db_p.shared_from_this();
auto &instance = GetInstance();
unique_lock<mutex> lck(instance.lock);
instance.db = db_p.shared_from_this();
}

void LoggerCallback::CallbackEvent(ffi::Event event) {
auto &instance = GetInstance();
auto db_locked = instance.db.lock();
if (db_locked) {
auto transformed_log_level = GetDuckDBLogLevel(event.level);
string constructed_log_message;
Logger::Log("delta.Kernel", *db_locked, transformed_log_level, [&]() {
auto log_type = KernelUtils::FromDeltaString(event.target);
auto message = KernelUtils::FromDeltaString(event.message);
auto file = KernelUtils::FromDeltaString(event.file);
if (!file.empty()) {
constructed_log_message = StringUtil::Format("[%s] %s@%u : %s ", log_type, file, event.line, message);
} else {
constructed_log_message = message;
}

return constructed_log_message.c_str();
});
}
auto &instance = GetInstance();
auto db_locked = instance.db.lock();
if (db_locked) {
auto transformed_log_level = GetDuckDBLogLevel(event.level);
string constructed_log_message;
Logger::Log("delta.Kernel", *db_locked, transformed_log_level, [&]() {
auto log_type = KernelUtils::FromDeltaString(event.target);
auto message = KernelUtils::FromDeltaString(event.message);
auto file = KernelUtils::FromDeltaString(event.file);
if (!file.empty()) {
constructed_log_message = StringUtil::Format("[%s] %s@%u : %s ", log_type, file, event.line, message);
} else {
constructed_log_message = message;
}

return constructed_log_message.c_str();
});
}
}

LogLevel LoggerCallback::GetDuckDBLogLevel(ffi::Level level) {
switch (level) {
case ffi::Level::TRACE:
return LogLevel::LOG_TRACE;
case ffi::Level::DEBUGGING:
return LogLevel::LOG_DEBUG;
case ffi::Level::INFO:
return LogLevel::LOG_INFO;
case ffi::Level::WARN:
return LogLevel::LOG_WARN;
case ffi::Level::ERROR:
return LogLevel::LOG_ERROR;
}
switch (level) {
case ffi::Level::TRACE:
return LogLevel::LOG_TRACE;
case ffi::Level::DEBUGGING:
return LogLevel::LOG_DEBUG;
case ffi::Level::INFO:
return LogLevel::LOG_INFO;
case ffi::Level::WARN:
return LogLevel::LOG_WARN;
case ffi::Level::ERROR:
return LogLevel::LOG_ERROR;
}
}

LoggerCallback &LoggerCallback::GetInstance() {
static LoggerCallback instance;
return instance;
static LoggerCallback instance;
return instance;
}

void LoggerCallback::DuckDBSettingCallBack(ClientContext &context, SetScope scope, Value &parameter) {
Value current_setting;
auto res = context.TryGetCurrentSetting("delta_kernel_logging", current_setting);

if (res.GetScope() == SettingScope::INVALID) {
throw InternalException("Failed to find setting 'delta_kernel_logging'");
}

if (current_setting.GetValue<bool>() && !parameter.GetValue<bool>()) {
throw InvalidInputException("Can not disable 'delta_kernel_logging' after enabling it. You can disable DuckDB "
"logging with SET enable_logging=false, but there will still be some performance overhead from 'delta_kernel_logging'"
"that can only be mitigated by restarting DuckDB");
}

if (!current_setting.GetValue<bool>() && parameter.GetValue<bool>()) {
ffi::enable_event_tracing(LoggerCallback::CallbackEvent, ffi::Level::TRACE);
}
Value current_setting;
auto res = context.TryGetCurrentSetting("delta_kernel_logging", current_setting);

if (res.GetScope() == SettingScope::INVALID) {
throw InternalException("Failed to find setting 'delta_kernel_logging'");
}

if (current_setting.GetValue<bool>() && !parameter.GetValue<bool>()) {
throw InvalidInputException("Can not disable 'delta_kernel_logging' after enabling it. You can disable DuckDB "
"logging with SET enable_logging=false, but there will still be some performance "
"overhead from 'delta_kernel_logging'"
"that can only be mitigated by restarting DuckDB");
}

if (!current_setting.GetValue<bool>() && parameter.GetValue<bool>()) {
ffi::enable_event_tracing(LoggerCallback::CallbackEvent, ffi::Level::TRACE);
}
}

}; // namespace duckdb
23 changes: 11 additions & 12 deletions src/include/delta_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,23 +340,22 @@ class PredicateVisitor : public ffi::EnginePredicate {
// Singleton class to forward logs to DuckDB
class LoggerCallback {
public:
//! The Callback for the DuckDB setting to hook up Delta Kernel Logging to the DuckDB logger
static void DuckDBSettingCallBack(ClientContext &context, SetScope scope, Value &parameter);

//! The Callback for the DuckDB setting to hook up Delta Kernel Logging to the DuckDB logger
static void DuckDBSettingCallBack(ClientContext &context, SetScope scope, Value &parameter);
//! Singleton GetInstance
static LoggerCallback &GetInstance();
static void Initialize(DatabaseInstance &db);
static void CallbackEvent(ffi::Event log_line);

//! Singleton GetInstance
static LoggerCallback& GetInstance();
static void Initialize(DatabaseInstance &db);
static void CallbackEvent(ffi::Event log_line);


static LogLevel GetDuckDBLogLevel(ffi::Level);
static LogLevel GetDuckDBLogLevel(ffi::Level);

protected:
LoggerCallback() {}
LoggerCallback() {
}

mutex lock;
weak_ptr<DatabaseInstance> db;
mutex lock;
weak_ptr<DatabaseInstance> db;
};

} // namespace duckdb
12 changes: 6 additions & 6 deletions src/storage/delta_catalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ bool DeltaCatalog::UseCachedSnapshot() {

optional_idx DeltaCatalog::GetCatalogVersion(ClientContext &context) {
auto &delta_transaction = DeltaTransaction::Get(context, *this);
idx_t version = DConstants::INVALID_INDEX;
idx_t version = DConstants::INVALID_INDEX;

// Option 1: snapshot is cached table-wide
auto cached_snapshot = main_schema->GetCachedTable();
if (cached_snapshot) {
version = cached_snapshot->snapshot->GetVersion();
version = cached_snapshot->snapshot->GetVersion();
}

// Option 2: snapshot is cached in transaction
if (delta_transaction.table_entry) {
version = delta_transaction.table_entry->snapshot->GetVersion();
version = delta_transaction.table_entry->snapshot->GetVersion();
}

if (version != DConstants::INVALID_INDEX) {
return version;
}
if (version != DConstants::INVALID_INDEX) {
return version;
}

return optional_idx::Invalid();
}
Expand Down

0 comments on commit e5ee3e1

Please sign in to comment.