Skip to content

Commit 47672f3

Browse files
committed
extend functions
1 parent 22c7794 commit 47672f3

File tree

1 file changed

+81
-51
lines changed

1 file changed

+81
-51
lines changed

src/chsql_system_extension.cpp

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,27 @@
1414
namespace duckdb {
1515

1616
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;
1824
idx_t offset = 0;
1925
};
2026

2127
struct SystemTablesData : public TableFunctionData {
2228
vector<Value> databases;
23-
vector<Value> schemas;
2429
vector<Value> names;
2530
vector<Value> uuids;
2631
vector<Value> engines;
2732
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;
3238
idx_t offset = 0;
3339
};
3440

@@ -52,17 +58,19 @@ struct SystemFunctionsData : public TableFunctionData {
5258

5359
static void SystemDatabasesFunction(ClientContext &context, TableFunctionInput &data_p, DataChunk &output) {
5460
auto &data = (SystemDatabasesData &)*data_p.bind_data;
55-
if (data.offset >= data.databases.size()) {
61+
if (data.offset >= data.names.size()) {
5662
return;
5763
}
5864

5965
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
6674

6775
count++;
6876
data.offset++;
@@ -79,16 +87,16 @@ static void SystemTablesFunction(ClientContext &context, TableFunctionInput &dat
7987

8088
idx_t count = 0;
8189
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
92100

93101
count++;
94102
data.offset++;
@@ -141,23 +149,40 @@ static void SystemColumnsFunction(ClientContext &context, TableFunctionInput &da
141149

142150
static unique_ptr<FunctionData> SystemDatabasesBind(ClientContext &context, TableFunctionBindInput &input,
143151
vector<LogicalType> &return_types, vector<string> &names) {
152+
// Define columns
144153
names.emplace_back("name");
145154
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");
147160

148161
return_types.emplace_back(LogicalType::VARCHAR);
149162
return_types.emplace_back(LogicalType::VARCHAR);
150163
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);
151168

152169
auto result = make_uniq<SystemDatabasesData>();
153170

154-
// Get databases from catalog
171+
// Get databases using duckdb_databases()
155172
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
161186
}
162187
}
163188

@@ -166,47 +191,51 @@ static unique_ptr<FunctionData> SystemDatabasesBind(ClientContext &context, Tabl
166191

167192
static unique_ptr<FunctionData> SystemTablesBind(ClientContext &context, TableFunctionBindInput &input,
168193
vector<LogicalType> &return_types, vector<string> &names) {
169-
// Core columns
194+
// Define columns
170195
names.emplace_back("database");
171-
names.emplace_back("schema");
172196
names.emplace_back("name");
173197
names.emplace_back("uuid");
174198
names.emplace_back("engine");
175199
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");
178204
names.emplace_back("create_table_query");
179-
names.emplace_back("comment");
180205

181-
return_types.emplace_back(LogicalType::VARCHAR);
182206
return_types.emplace_back(LogicalType::VARCHAR);
183207
return_types.emplace_back(LogicalType::VARCHAR);
184208
return_types.emplace_back(LogicalType::UUID);
185209
return_types.emplace_back(LogicalType::VARCHAR);
186210
return_types.emplace_back(LogicalType::BOOLEAN);
187-
return_types.emplace_back(LogicalType::LIST(LogicalType::VARCHAR));
188-
return_types.emplace_back(LogicalType::LIST(LogicalType::VARCHAR));
189211
return_types.emplace_back(LogicalType::VARCHAR);
190212
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);
191216

192217
auto result = make_uniq<SystemTablesData>();
193-
194-
// Get tables using SHOW ALL TABLES
218+
219+
// Get tables using duckdb_tables()
195220
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+
198227
if (!table_result->HasError()) {
199228
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
203231
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
210239
}
211240
}
212241

@@ -344,6 +373,7 @@ DUCKDB_EXTENSION_API void chsql_system_init(duckdb::DatabaseInstance &db) {
344373
DUCKDB_EXTENSION_API const char *chsql_system_version() {
345374
return duckdb::DuckDB::LibraryVersion();
346375
}
376+
347377
}
348378

349379
#ifndef DUCKDB_EXTENSION_MAIN

0 commit comments

Comments
 (0)