1#include "duckdb/function/table/system_functions.hpp"
2#include "duckdb/storage/buffer_manager.hpp"
3
4namespace duckdb {
5
6struct DuckDBTemporaryFilesData : public GlobalTableFunctionState {
7 DuckDBTemporaryFilesData() : offset(0) {
8 }
9
10 vector<TemporaryFileInformation> entries;
11 idx_t offset;
12};
13
14static unique_ptr<FunctionData> DuckDBTemporaryFilesBind(ClientContext &context, TableFunctionBindInput &input,
15 vector<LogicalType> &return_types, vector<string> &names) {
16 names.emplace_back(args: "path");
17 return_types.emplace_back(args: LogicalType::VARCHAR);
18
19 names.emplace_back(args: "size");
20 return_types.emplace_back(args: LogicalType::BIGINT);
21
22 return nullptr;
23}
24
25unique_ptr<GlobalTableFunctionState> DuckDBTemporaryFilesInit(ClientContext &context, TableFunctionInitInput &input) {
26 auto result = make_uniq<DuckDBTemporaryFilesData>();
27
28 result->entries = BufferManager::GetBufferManager(context).GetTemporaryFiles();
29 return std::move(result);
30}
31
32void DuckDBTemporaryFilesFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
33 auto &data = data_p.global_state->Cast<DuckDBTemporaryFilesData>();
34 if (data.offset >= data.entries.size()) {
35 // finished returning values
36 return;
37 }
38 // start returning values
39 // either fill up the chunk or return all the remaining columns
40 idx_t count = 0;
41 while (data.offset < data.entries.size() && count < STANDARD_VECTOR_SIZE) {
42 auto &entry = data.entries[data.offset++];
43 // return values:
44 idx_t col = 0;
45 // database_name, VARCHAR
46 output.SetValue(col_idx: col++, index: count, val: entry.path);
47 // database_oid, BIGINT
48 output.SetValue(col_idx: col++, index: count, val: Value::BIGINT(value: entry.size));
49 count++;
50 }
51 output.SetCardinality(count);
52}
53
54void DuckDBTemporaryFilesFun::RegisterFunction(BuiltinFunctions &set) {
55 set.AddFunction(function: TableFunction("duckdb_temporary_files", {}, DuckDBTemporaryFilesFunction, DuckDBTemporaryFilesBind,
56 DuckDBTemporaryFilesInit));
57}
58
59} // namespace duckdb
60