14
14
namespace duckdb {
15
15
16
16
struct SystemDatabasesData : public TableFunctionData {
17
- vector<string> databases;
17
+ vector<Value> names;
18
+ vector<Value> engines;
19
+ vector<Value> data_paths;
20
+ vector<Value> metadata_paths;
21
+ vector<Value> uuids;
22
+ vector<Value> engine_full;
23
+ vector<Value> comments;
18
24
idx_t offset = 0 ;
19
25
};
20
26
21
27
struct SystemTablesData : public TableFunctionData {
22
28
vector<Value> databases;
23
- vector<Value> schemas;
24
29
vector<Value> names;
25
30
vector<Value> uuids;
26
31
vector<Value> engines;
27
32
vector<Value> is_temporary;
28
- vector<Value> column_names;
29
- vector<Value> column_types;
30
- vector<Value> create_table_query;
31
- vector<Value> comments;
33
+ vector<Value> data_paths;
34
+ vector<Value> metadata_paths;
35
+ vector<Value> metadata_modification_times;
36
+ vector<Value> metadata_versions;
37
+ vector<Value> create_table_queries;
32
38
idx_t offset = 0 ;
33
39
};
34
40
@@ -52,17 +58,19 @@ struct SystemFunctionsData : public TableFunctionData {
52
58
53
59
static void SystemDatabasesFunction (ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
54
60
auto &data = (SystemDatabasesData &)*data_p.bind_data ;
55
- if (data.offset >= data.databases .size ()) {
61
+ if (data.offset >= data.names .size ()) {
56
62
return ;
57
63
}
58
64
59
65
idx_t count = 0 ;
60
- while (data.offset < data.databases .size () && count < STANDARD_VECTOR_SIZE) {
61
- auto &db_name = data.databases [data.offset ];
62
-
63
- output.SetValue (0 , count, Value (db_name)); // name
64
- output.SetValue (1 , count, Value (" DuckDB" )); // engine
65
- output.SetValue (2 , count, Value (db_name == " memory" ? " memory" : " attached" )); // type
66
+ while (data.offset < data.names .size () && count < STANDARD_VECTOR_SIZE) {
67
+ output.SetValue (0 , count, data.names [data.offset ]); // name
68
+ output.SetValue (1 , count, data.engines [data.offset ]); // engine
69
+ output.SetValue (2 , count, data.data_paths [data.offset ]); // data_path
70
+ output.SetValue (3 , count, data.metadata_paths [data.offset ]); // metadata_path
71
+ output.SetValue (4 , count, data.uuids [data.offset ]); // uuid
72
+ output.SetValue (5 , count, data.engine_full [data.offset ]); // engine_full
73
+ output.SetValue (6 , count, data.comments [data.offset ]); // comment
66
74
67
75
count++;
68
76
data.offset ++;
@@ -79,16 +87,16 @@ static void SystemTablesFunction(ClientContext &context, TableFunctionInput &dat
79
87
80
88
idx_t count = 0 ;
81
89
while (data.offset < data.databases .size () && count < STANDARD_VECTOR_SIZE) {
82
- output.SetValue (0 , count, data.databases [data.offset ]); // database
83
- output.SetValue (1 , count, data.schemas [data.offset ]); // schema
84
- output.SetValue (2 , count, data.names [data.offset ]); // name
85
- output.SetValue (3 , count, data.uuids [data.offset ]); // uuid
86
- output.SetValue (4 , count, data.engines [data.offset ]); // engine
87
- output.SetValue (5 , count, data.is_temporary [data.offset ]); // is_temporary
88
- output.SetValue (6 , count, data.column_names [data.offset ]); // column_names
89
- output.SetValue (7 , count, data.column_types [data.offset ]); // column_types
90
- output.SetValue (8 , count, data.create_table_query [data.offset ]); // create_table_query
91
- output.SetValue (9 , count, data.comments [data.offset ]); // comment
90
+ output.SetValue (0 , count, data.databases [data.offset ]); // database
91
+ output.SetValue (1 , count, data.names [data.offset ]); // name
92
+ output.SetValue (2 , count, data.uuids [data.offset ]); // uuid
93
+ output.SetValue (3 , count, data.engines [data.offset ]); // engine
94
+ output.SetValue (4 , count, data.is_temporary [data.offset ]); // is_temporary
95
+ output.SetValue (5 , count, data.data_paths [data.offset ]); // data_path
96
+ output.SetValue (6 , count, data.metadata_paths [data.offset ]); // metadata_path
97
+ output.SetValue (7 , count, data.metadata_modification_times [data.offset ]); // metadata_modification_time
98
+ output.SetValue (8 , count, data.metadata_versions [data.offset ]); // metadata_version
99
+ output.SetValue (9 , count, data.create_table_queries [data.offset ]); // create_table_query
92
100
93
101
count++;
94
102
data.offset ++;
@@ -141,23 +149,40 @@ static void SystemColumnsFunction(ClientContext &context, TableFunctionInput &da
141
149
142
150
static unique_ptr<FunctionData> SystemDatabasesBind (ClientContext &context, TableFunctionBindInput &input,
143
151
vector<LogicalType> &return_types, vector<string> &names) {
152
+ // Define columns
144
153
names.emplace_back (" name" );
145
154
names.emplace_back (" engine" );
146
- names.emplace_back (" type" );
155
+ names.emplace_back (" data_path" );
156
+ names.emplace_back (" metadata_path" );
157
+ names.emplace_back (" uuid" );
158
+ names.emplace_back (" engine_full" );
159
+ names.emplace_back (" comment" );
147
160
148
161
return_types.emplace_back (LogicalType::VARCHAR);
149
162
return_types.emplace_back (LogicalType::VARCHAR);
150
163
return_types.emplace_back (LogicalType::VARCHAR);
164
+ return_types.emplace_back (LogicalType::VARCHAR);
165
+ return_types.emplace_back (LogicalType::UUID);
166
+ return_types.emplace_back (LogicalType::VARCHAR);
167
+ return_types.emplace_back (LogicalType::VARCHAR);
151
168
152
169
auto result = make_uniq<SystemDatabasesData>();
153
170
154
- // Get databases from catalog
171
+ // Get databases using duckdb_databases()
155
172
Connection con (Catalog::GetSystemCatalog (context).GetDatabase ());
156
- auto db_result = con.Query (" SELECT DISTINCT database FROM (SHOW ALL TABLES)" );
157
-
158
- if (!db_result->HasError ()) {
159
- for (auto &row : *db_result) {
160
- result->databases .push_back (row.GetValue <string>(0 ));
173
+ auto database_result = con.Query (" SELECT database_name, 'duckdb' as engine, path as data_path, '' as metadata_path, "
174
+ " database_name as uuid, 'DuckDB' as engine_full, comment "
175
+ " FROM duckdb_databases()" );
176
+
177
+ if (!database_result->HasError ()) {
178
+ for (auto &row : *database_result) {
179
+ result->names .push_back (row.GetValue <std::string>(0 )); // name
180
+ result->engines .push_back (row.GetValue <std::string>(1 )); // engine
181
+ result->data_paths .push_back (row.GetValue <std::string>(2 )); // data_path
182
+ result->metadata_paths .push_back (row.GetValue <std::string>(3 )); // metadata_path
183
+ result->uuids .push_back (Value::UUID (row.GetValue <std::string>(4 ))); // uuid
184
+ result->engine_full .push_back (row.GetValue <std::string>(5 )); // engine_full
185
+ result->comments .push_back (row.GetValue <std::string>(6 )); // comment
161
186
}
162
187
}
163
188
@@ -166,47 +191,51 @@ static unique_ptr<FunctionData> SystemDatabasesBind(ClientContext &context, Tabl
166
191
167
192
static unique_ptr<FunctionData> SystemTablesBind (ClientContext &context, TableFunctionBindInput &input,
168
193
vector<LogicalType> &return_types, vector<string> &names) {
169
- // Core columns
194
+ // Define columns
170
195
names.emplace_back (" database" );
171
- names.emplace_back (" schema" );
172
196
names.emplace_back (" name" );
173
197
names.emplace_back (" uuid" );
174
198
names.emplace_back (" engine" );
175
199
names.emplace_back (" is_temporary" );
176
- names.emplace_back (" column_names" );
177
- names.emplace_back (" column_types" );
200
+ names.emplace_back (" data_path" );
201
+ names.emplace_back (" metadata_path" );
202
+ names.emplace_back (" metadata_modification_time" );
203
+ names.emplace_back (" metadata_version" );
178
204
names.emplace_back (" create_table_query" );
179
- names.emplace_back (" comment" );
180
205
181
- return_types.emplace_back (LogicalType::VARCHAR);
182
206
return_types.emplace_back (LogicalType::VARCHAR);
183
207
return_types.emplace_back (LogicalType::VARCHAR);
184
208
return_types.emplace_back (LogicalType::UUID);
185
209
return_types.emplace_back (LogicalType::VARCHAR);
186
210
return_types.emplace_back (LogicalType::BOOLEAN);
187
- return_types.emplace_back (LogicalType::LIST (LogicalType::VARCHAR));
188
- return_types.emplace_back (LogicalType::LIST (LogicalType::VARCHAR));
189
211
return_types.emplace_back (LogicalType::VARCHAR);
190
212
return_types.emplace_back (LogicalType::VARCHAR);
213
+ return_types.emplace_back (LogicalType::TIMESTAMP);
214
+ return_types.emplace_back (LogicalType::INTEGER);
215
+ return_types.emplace_back (LogicalType::VARCHAR);
191
216
192
217
auto result = make_uniq<SystemTablesData>();
193
-
194
- // Get tables using SHOW ALL TABLES
218
+
219
+ // Get tables using duckdb_tables()
195
220
Connection con (Catalog::GetSystemCatalog (context).GetDatabase ());
196
- auto table_result = con.Query (" SHOW ALL TABLES" );
197
-
221
+ auto table_result = con.Query (" SELECT database_name, table_name, table_name as uuid, "
222
+ " 'BASE TABLE' as engine, false as is_temporary, '' as data_path, "
223
+ " '' as metadata_path, '1970-01-01 00:00:00' as metadata_modification_time, "
224
+ " 0 as metadata_version, sql as create_table_query "
225
+ " FROM duckdb_tables()" );
226
+
198
227
if (!table_result->HasError ()) {
199
228
for (auto &row : *table_result) {
200
- result->databases .push_back (row.GetValue <std::string>(0 )); // database
201
- result->schemas .push_back (row.GetValue <std::string>(1 )); // schema
202
- result->names .push_back (row.GetValue <std::string>(2 )); // name
229
+ result->databases .push_back (row.GetValue <std::string>(0 )); // database
230
+ result->names .push_back (row.GetValue <std::string>(1 )); // name
203
231
result->uuids .push_back (Value::UUID (row.GetValue <std::string>(2 ))); // uuid (placeholder using table name hash)
204
- result->engines .push_back (Value (row.GetValue <bool >(5 ) ? " TEMPORARY" : " BASE TABLE" )); // engine
205
- result->is_temporary .push_back (row.GetValue <bool >(5 )); // is_temporary
206
- result->column_names .push_back (row.GetValue <Value>(3 )); // column_names
207
- result->column_types .push_back (row.GetValue <Value>(4 )); // column_types
208
- result->create_table_query .push_back (Value (" " )); // create_table_query (placeholder)
209
- result->comments .push_back (Value (" " )); // comment (placeholder)
232
+ result->engines .push_back (row.GetValue <std::string>(3 )); // engine
233
+ result->is_temporary .push_back (row.GetValue <bool >(4 )); // is_temporary
234
+ result->data_paths .push_back (row.GetValue <std::string>(5 )); // data_path
235
+ result->metadata_paths .push_back (row.GetValue <std::string>(6 )); // metadata_path
236
+ result->metadata_modification_times .push_back (Value::TIMESTAMP (row.GetValue <timestamp_t >(7 ))); // metadata_modification_time
237
+ result->metadata_versions .push_back (row.GetValue <int32_t >(8 )); // metadata_version
238
+ result->create_table_queries .push_back (row.GetValue <std::string>(9 )); // create_table_query
210
239
}
211
240
}
212
241
@@ -344,6 +373,7 @@ DUCKDB_EXTENSION_API void chsql_system_init(duckdb::DatabaseInstance &db) {
344
373
DUCKDB_EXTENSION_API const char *chsql_system_version () {
345
374
return duckdb::DuckDB::LibraryVersion ();
346
375
}
376
+
347
377
}
348
378
349
379
#ifndef DUCKDB_EXTENSION_MAIN
0 commit comments