|
11 | 11 | #include "duckdb/common/unordered_set.hpp" |
12 | 12 | #include "duckdb/common/vector.hpp" |
13 | 13 | #include "duckdb/execution/expression_executor.hpp" |
| 14 | +#include "duckdb/function/scalar_function.hpp" |
14 | 15 | #include "duckdb/function/table_function.hpp" |
15 | 16 | #include "duckdb/parallel/task_executor.hpp" |
16 | 17 | #include "duckdb/parallel/task_scheduler.hpp" |
@@ -380,4 +381,75 @@ TableFunction ConditionCacheInfoFunction() { |
380 | 381 | return func; |
381 | 382 | } |
382 | 383 |
|
| 384 | +// ------- condition_cache_stats() ------- |
| 385 | +// Returns global cache statistics: total memory, hit count, access count. |
| 386 | + |
| 387 | +namespace { |
| 388 | + |
| 389 | +struct ConditionCacheStatsState : public GlobalTableFunctionState { |
| 390 | + bool done = false; |
| 391 | +}; |
| 392 | + |
| 393 | +unique_ptr<FunctionData> ConditionCacheStatsBind(ClientContext &context, TableFunctionBindInput &input, |
| 394 | + vector<LogicalType> &return_types, vector<string> &names) { |
| 395 | + names.emplace_back("total_memory_bytes"); |
| 396 | + return_types.emplace_back(LogicalType {LogicalTypeId::UBIGINT}); |
| 397 | + names.emplace_back("hit_count"); |
| 398 | + return_types.emplace_back(LogicalType {LogicalTypeId::UBIGINT}); |
| 399 | + names.emplace_back("access_count"); |
| 400 | + return_types.emplace_back(LogicalType {LogicalTypeId::UBIGINT}); |
| 401 | + return nullptr; |
| 402 | +} |
| 403 | + |
| 404 | +unique_ptr<GlobalTableFunctionState> ConditionCacheStatsInit(ClientContext &context, TableFunctionInitInput &input) { |
| 405 | + return make_uniq<ConditionCacheStatsState>(); |
| 406 | +} |
| 407 | + |
| 408 | +void ConditionCacheStatsExecute(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) { |
| 409 | + auto &gstate = data_p.global_state->Cast<ConditionCacheStatsState>(); |
| 410 | + if (gstate.done) { |
| 411 | + return; |
| 412 | + } |
| 413 | + gstate.done = true; |
| 414 | + |
| 415 | + auto store = ConditionCacheStore::GetOrCreate(context); |
| 416 | + auto stats = store->GetStats(context); |
| 417 | + |
| 418 | + output.SetCardinality(1); |
| 419 | + output.data[0].SetValue(0, Value::UBIGINT(stats.total_memory_bytes)); |
| 420 | + output.data[1].SetValue(0, Value::UBIGINT(stats.hit_count)); |
| 421 | + output.data[2].SetValue(0, Value::UBIGINT(stats.access_count)); |
| 422 | +} |
| 423 | + |
| 424 | +} // namespace |
| 425 | + |
| 426 | +TableFunction ConditionCacheStatsFunction() { |
| 427 | + TableFunction func("condition_cache_stats", {}, ConditionCacheStatsExecute, ConditionCacheStatsBind, |
| 428 | + ConditionCacheStatsInit); |
| 429 | + return func; |
| 430 | +} |
| 431 | + |
| 432 | +// ------- condition_cache_reset_stats() ------- |
| 433 | +// Scalar function: resets hit_count and access_count, returns true. |
| 434 | + |
| 435 | +namespace { |
| 436 | + |
| 437 | +unique_ptr<FunctionData> ConditionCacheResetStatsBind(ClientContext &context, ScalarFunction &bound_function, |
| 438 | + vector<unique_ptr<Expression>> &arguments) { |
| 439 | + auto store = ConditionCacheStore::GetOrCreate(context); |
| 440 | + store->ResetStats(); |
| 441 | + return nullptr; |
| 442 | +} |
| 443 | + |
| 444 | +void ConditionCacheResetStatsFn(DataChunk &args, ExpressionState &state, Vector &result) { |
| 445 | + result.Reference(Value::BOOLEAN(true)); |
| 446 | +} |
| 447 | + |
| 448 | +} // namespace |
| 449 | + |
| 450 | +ScalarFunction ConditionCacheResetStatsFunction() { |
| 451 | + return ScalarFunction("condition_cache_reset_stats", {}, LogicalType {LogicalTypeId::BOOLEAN}, |
| 452 | + ConditionCacheResetStatsFn, ConditionCacheResetStatsBind); |
| 453 | +} |
| 454 | + |
383 | 455 | } // namespace duckdb |
0 commit comments