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
1212namespace duckdb {
1313
14- // Generate new TSID (with input parameter - ignored)
14+ // Generate new TSID
1515static 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
8387std::string TsidExtension::Name () {
@@ -96,13 +100,8 @@ std::string TsidExtension::Version() const {
96100
97101extern " 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}
0 commit comments