Skip to content

Commit 97da74c

Browse files
committed
fix: add function docs
1 parent c8ef67a commit 97da74c

File tree

4 files changed

+50
-51
lines changed

4 files changed

+50
-51
lines changed

duckdb

Submodule duckdb updated 7022 files

src/include/tsid_extension.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace duckdb {
66

77
class TsidExtension : public Extension {
88
public:
9-
void Load(DuckDB &db) override;
9+
void Load(ExtensionLoader &loader) override;
1010
std::string Name() override;
11-
std::string Version() const override;
11+
std::string Version() const override;
1212
};
1313

1414
} // namespace duckdb

src/tsid_extension.cpp

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,18 @@
44
#include "duckdb/common/exception.hpp"
55
#include "duckdb/common/string_util.hpp"
66
#include "duckdb/function/scalar_function.hpp"
7-
#include "duckdb/main/extension_util.hpp"
7+
#include "duckdb/main/extension/extension_loader.hpp"
88
#include "duckdb/parser/parsed_data/create_scalar_function_info.hpp"
99
#include "duckdb/common/exception/conversion_exception.hpp"
1010
#include "uutid.hpp"
1111

1212
namespace duckdb {
1313

14-
// Generate new TSID (with input parameter - ignored)
14+
// Generate new TSID
1515
static void TsidScalarFun(DataChunk &args, ExpressionState &state, Vector &result) {
1616
auto result_data = FlatVector::GetData<string_t>(result);
1717
auto &validity = FlatVector::Validity(result);
18-
19-
for (idx_t i = 0; i < args.size(); i++) {
20-
auto id = UUTID::new_id();
21-
result_data[i] = StringVector::AddString(result, id.to_string());
22-
validity.Set(i, true);
23-
}
24-
}
2518

26-
// Generate new TSID (no input parameter)
27-
static void TsidScalarFunNoArgs(DataChunk &args, ExpressionState &state, Vector &result) {
28-
auto result_data = FlatVector::GetData<string_t>(result);
29-
auto &validity = FlatVector::Validity(result);
30-
3119
for (idx_t i = 0; i < args.size(); i++) {
3220
auto id = UUTID::new_id();
3321
result_data[i] = StringVector::AddString(result, id.to_string());
@@ -52,32 +40,48 @@ static void TsidToTimestampScalarFun(DataChunk &args, ExpressionState &state, Ve
5240
});
5341
}
5442

55-
static void LoadInternal(DatabaseInstance &instance) {
56-
// Register tsid() functions
57-
ScalarFunctionSet set("tsid");
58-
59-
// Add variant with text parameter
60-
ScalarFunction tsid_fun({LogicalType::VARCHAR}, LogicalType::VARCHAR, TsidScalarFun);
43+
static void LoadInternal(ExtensionLoader &loader) {
44+
// Register tsid() function
45+
ScalarFunction tsid_fun({}, LogicalType::VARCHAR, TsidScalarFun);
6146
tsid_fun.stability = FunctionStability::VOLATILE;
62-
set.AddFunction(tsid_fun);
63-
64-
// Add variant without parameters
65-
ScalarFunction tsid_fun_no_args({}, LogicalType::VARCHAR, TsidScalarFunNoArgs);
66-
tsid_fun_no_args.stability = FunctionStability::VOLATILE;
67-
set.AddFunction(tsid_fun_no_args);
68-
69-
ExtensionUtil::RegisterFunction(instance, set);
47+
48+
ScalarFunctionSet tsid_set("tsid");
49+
tsid_set.AddFunction(tsid_fun);
50+
51+
CreateScalarFunctionInfo tsid_info(tsid_set);
52+
FunctionDescription tsid_desc;
53+
tsid_desc.description = "Generates a new Time-Sorted Unique Identifier (TSID). "
54+
"TSIDs are chronologically sortable 128-bit unique identifiers "
55+
"that embed a timestamp, making them ideal for distributed systems "
56+
"and time-series data.";
57+
tsid_desc.examples = {"tsid()"};
58+
tsid_desc.categories = {"uuid"};
59+
tsid_info.descriptions.push_back(std::move(tsid_desc));
60+
61+
loader.RegisterFunction(std::move(tsid_info));
7062

7163
// Register tsid_to_timestamp() function
72-
auto tsid_to_timestamp_function = ScalarFunction(
73-
"tsid_to_timestamp", {LogicalType::VARCHAR}, LogicalType::TIMESTAMP,
74-
TsidToTimestampScalarFun
75-
);
76-
ExtensionUtil::RegisterFunction(instance, tsid_to_timestamp_function);
64+
ScalarFunction tsid_to_ts_fun("tsid_to_timestamp", {LogicalType::VARCHAR}, LogicalType::TIMESTAMP,
65+
TsidToTimestampScalarFun);
66+
67+
ScalarFunctionSet tsid_to_ts_set("tsid_to_timestamp");
68+
tsid_to_ts_set.AddFunction(tsid_to_ts_fun);
69+
70+
CreateScalarFunctionInfo tsid_to_ts_info(tsid_to_ts_set);
71+
FunctionDescription tsid_to_ts_desc;
72+
tsid_to_ts_desc.parameter_names = {"tsid"};
73+
tsid_to_ts_desc.parameter_types = {LogicalType::VARCHAR};
74+
tsid_to_ts_desc.description = "Extracts the embedded timestamp from a TSID. "
75+
"Returns the timestamp that was recorded when the TSID was generated.";
76+
tsid_to_ts_desc.examples = {"tsid_to_timestamp('0193b9c8d23d7192bc1cc82b43e6e8f3')"};
77+
tsid_to_ts_desc.categories = {"uuid"};
78+
tsid_to_ts_info.descriptions.push_back(std::move(tsid_to_ts_desc));
79+
80+
loader.RegisterFunction(std::move(tsid_to_ts_info));
7781
}
7882

79-
void TsidExtension::Load(DuckDB &db) {
80-
LoadInternal(*db.instance);
83+
void TsidExtension::Load(ExtensionLoader &loader) {
84+
LoadInternal(loader);
8185
}
8286

8387
std::string TsidExtension::Name() {
@@ -96,13 +100,8 @@ std::string TsidExtension::Version() const {
96100

97101
extern "C" {
98102

99-
DUCKDB_EXTENSION_API void tsid_init(duckdb::DatabaseInstance &db) {
100-
duckdb::DuckDB db_wrapper(db);
101-
db_wrapper.LoadExtension<duckdb::TsidExtension>();
102-
}
103-
104-
DUCKDB_EXTENSION_API const char *tsid_version() {
105-
return duckdb::DuckDB::LibraryVersion();
103+
DUCKDB_CPP_EXTENSION_ENTRY(tsid, loader) {
104+
duckdb::LoadInternal(loader);
106105
}
107106

108107
}

test/sql/tsid.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Before we load the extension, this will fail
66
statement error
7-
SELECT tsid('Sam');
7+
SELECT tsid();
88
----
99
Catalog Error: Scalar Function with name tsid does not exist!
1010

@@ -14,19 +14,19 @@ require tsid
1414
# Test TSID generation
1515
# Verify it generates a valid hex string of correct length (32 chars)
1616
query I
17-
SELECT length(tsid('any')) = 32;
17+
SELECT length(tsid()) = 32;
1818
----
1919
true
2020

2121
# Verify hex format (all characters are valid hex)
2222
query I
23-
SELECT tsid('any') ~ '^[0-9a-f]{32}$';
23+
SELECT tsid() ~ '^[0-9a-f]{32}$';
2424
----
2525
true
2626

2727
# Test timestamp extraction
2828
query I
29-
SELECT tsid_to_timestamp(tsid('any')) IS NOT NULL;
29+
SELECT tsid_to_timestamp(tsid()) IS NOT NULL;
3030
----
3131
true
3232

@@ -38,13 +38,13 @@ Invalid TSID format
3838

3939
# Verify timestamp is recent
4040
query I
41-
SELECT tsid_to_timestamp(tsid('any')) >= '2024-01-01';
41+
SELECT tsid_to_timestamp(tsid()) >= '2024-01-01';
4242
----
4343
true
4444

4545
# Test basic bulk operation
4646
query I
4747
SELECT COUNT(*)
48-
FROM (SELECT tsid('any') FROM range(10)) t;
48+
FROM (SELECT tsid() FROM range(10)) t;
4949
----
5050
10

0 commit comments

Comments
 (0)