Skip to content

Commit 803e921

Browse files
sdk: make 'vsql' top-level namespace
This provides a clearer delineation between the new and old APIs, and should make errors harder to introduce. Issue #429
1 parent 639a1ea commit 803e921

20 files changed

Lines changed: 178 additions & 201 deletions

File tree

mysql-test/suite/villagesql/std_data/aggregate_vdf.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void vdf_max_clear(vef_context_t *, vef_vdf_args_t *args) {
125125
void vdf_max_accumulate(vef_context_t *ctx, vef_vdf_args_t *args,
126126
vef_vdf_result_t *) {
127127
auto *state = static_cast<MaxState *>(args->user_data);
128-
vef_invalue_t val = villagesql::func_builder::get_invalue(ctx, args, 0);
128+
vef_invalue_t val = vsql::func_builder::get_invalue(ctx, args, 0);
129129
if (!val.is_null) {
130130
if (!state->has_value || val.int_value > state->max_val) {
131131
state->max_val = val.int_value;

villagesql/examples/vsql-complex/src/complex.cc

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ size_t fnv1a_hash(const unsigned char *data, size_t len) {
104104

105105
// COMPLEX encode: "(real,imag)" -> 16 bytes (with canonicalization of -0.0)
106106
// STRING -> COMPLEX
107-
bool complex_from_string(std::string_view from,
108-
villagesql::Span<unsigned char> buf, size_t *length) {
107+
bool complex_from_string(std::string_view from, vsql::Span<unsigned char> buf,
108+
size_t *length) {
109109
if (buf.size() < kComplexSize) return true;
110110
Complex cx;
111111
std::string from_str(from);
@@ -121,8 +121,8 @@ bool complex_from_string(std::string_view from,
121121
// COMPLEX2 encode: "(real,imag)" -> 16 bytes (without canonicalization,
122122
// preserves -0.0 in binary form)
123123
// STRING -> COMPLEX2
124-
bool complex2_from_string(std::string_view from,
125-
villagesql::Span<unsigned char> buf, size_t *length) {
124+
bool complex2_from_string(std::string_view from, vsql::Span<unsigned char> buf,
125+
size_t *length) {
126126
if (buf.size() < kComplexSize) return true;
127127
Complex cx;
128128
std::string from_str(from);
@@ -138,8 +138,8 @@ bool complex2_from_string(std::string_view from,
138138

139139
// Decode: 16 bytes -> "(real,imag)" string
140140
// COMPLEX -> STRING
141-
bool complex_to_string(villagesql::Span<const unsigned char> data,
142-
villagesql::Span<char> out, size_t *out_len) {
141+
bool complex_to_string(vsql::Span<const unsigned char> data,
142+
vsql::Span<char> out, size_t *out_len) {
143143
if (data.size() != kComplexSize) return true;
144144
Complex cx = load_complex(data.data());
145145
int written = snprintf(out.data(), out.size(), "(%g,%g)", cx.re, cx.im);
@@ -149,8 +149,8 @@ bool complex_to_string(villagesql::Span<const unsigned char> data,
149149
}
150150

151151
// Compare: (COMPLEX, COMPLEX) -> INT for ORDER BY, indexes
152-
int complex_compare(villagesql::Span<const unsigned char> a,
153-
villagesql::Span<const unsigned char> b) {
152+
int complex_compare(vsql::Span<const unsigned char> a,
153+
vsql::Span<const unsigned char> b) {
154154
if (a.size() != kComplexSize || b.size() != kComplexSize) return 0;
155155
Complex lhs = load_complex(a.data());
156156
Complex rhs = load_complex(b.data());
@@ -168,7 +168,7 @@ int complex_compare(villagesql::Span<const unsigned char> a,
168168
// Canonicalizes -0 to +0 before hashing so that -0.0 and +0.0 hash to the
169169
// same bucket. This allows COMPLEX2 to preserve -0 in storage while still
170170
// working correctly with hash joins and EXCEPT operations.
171-
size_t complex2_hash(villagesql::Span<const unsigned char> data) {
171+
size_t complex2_hash(vsql::Span<const unsigned char> data) {
172172
if (data.size() != kComplexSize) return 0;
173173
Complex cx = load_complex(data.data());
174174
cx.canonicalize();

villagesql/examples/vsql-keyring-reader/src/extension.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void keyring_read(StringArg data_id, StringArg auth_id, StringResult out) {
4747
}
4848

4949
std::string value;
50-
vef_keyring_result_t kr = villagesql::keyring::read(
50+
vef_keyring_result_t kr = vsql::keyring::read(
5151
data_id.value(), auth_id.is_null() ? "" : auth_id.value(), value);
5252
if (kr == VEF_KEYRING_UNAVAILABLE) {
5353
out.error("No keyring component is installed");
@@ -74,7 +74,7 @@ void keyring_store(StringArg data_id, StringArg auth_id, StringArg value,
7474
return;
7575
}
7676

77-
vef_keyring_result_t kr = villagesql::keyring::write(
77+
vef_keyring_result_t kr = vsql::keyring::write(
7878
data_id.value(), auth_id.is_null() ? "" : auth_id.value(), value.value());
7979
if (kr == VEF_KEYRING_UNAVAILABLE) {
8080
out.error("No keyring component is installed");

villagesql/examples/vsql-tvector/src/tvector.cc

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ bool tvector_int_to_params(int64_t value,
136136

137137
// Validate type parameters and compute storage characteristics.
138138
bool tvector_resolve_params(const std::map<std::string, std::string> &params,
139-
villagesql::ResolvedTypeParams *result,
140-
char *error_msg) {
139+
vsql::ResolvedTypeParams *result, char *error_msg) {
141140
auto it = params.find("dimension");
142141
if (it == params.end()) {
143142
snprintf(error_msg, VEF_MAX_ERROR_LEN,
@@ -178,7 +177,7 @@ bool tvector_resolve_params(const std::map<std::string, std::string> &params,
178177
// STRING -> TVECTOR
179178
// Dimension and element type are read from type parameters.
180179
bool tvector_from_string(const TVectorParams &p, std::string_view from,
181-
villagesql::Span<unsigned char> buf, size_t *length) {
180+
vsql::Span<unsigned char> buf, size_t *length) {
182181
// Computed values could be cached in the TVectorParams
183182
size_t byte_size = static_cast<size_t>(p.dimension) * p.bytes_per_elem;
184183
if (buf.size() < byte_size) return true;
@@ -229,8 +228,8 @@ bool tvector_from_string(const TVectorParams &p, std::string_view from,
229228
// TVECTOR -> STRING
230229
// Dimension and element type are read from type parameters.
231230
bool tvector_to_string(const TVectorParams &p,
232-
villagesql::Span<const unsigned char> data,
233-
villagesql::Span<char> out, size_t *out_len) {
231+
vsql::Span<const unsigned char> data,
232+
vsql::Span<char> out, size_t *out_len) {
234233
const size_t bpe = p.bytes_per_elem;
235234
if (data.size() != static_cast<size_t>(p.dimension) * bpe) return true;
236235

@@ -268,9 +267,8 @@ bool tvector_to_string(const TVectorParams &p,
268267
// TODO(villagesql-performance): we can also consider having templated versions
269268
// of these functions instead of using branches, then selecting the version to
270269
// use with one branch.
271-
int tvector_compare(const TVectorParams &p,
272-
villagesql::Span<const unsigned char> a,
273-
villagesql::Span<const unsigned char> b) {
270+
int tvector_compare(const TVectorParams &p, vsql::Span<const unsigned char> a,
271+
vsql::Span<const unsigned char> b) {
274272
for (int64_t i = 0; i < p.dimension; i++) {
275273
if (p.bytes_per_elem == 8) {
276274
double v1 = load_double(a.data() + i * p.bytes_per_elem);
@@ -301,9 +299,9 @@ std::string tvector_default(const TVectorParams &p, char * /*error_msg*/) {
301299

302300
// Dot product: (TVECTOR, TVECTOR) -> REAL
303301
// Returns the sum of element-wise products of two vectors of the same type.
304-
void tvector_dot_product(villagesql::CustomArgWith<TVectorParams> a,
305-
villagesql::CustomArgWith<TVectorParams> b,
306-
villagesql::RealResult out) {
302+
void tvector_dot_product(vsql::CustomArgWith<TVectorParams> a,
303+
vsql::CustomArgWith<TVectorParams> b,
304+
vsql::RealResult out) {
307305
if (a.is_null() || b.is_null()) {
308306
out.set_null();
309307
return;
@@ -331,9 +329,9 @@ void tvector_dot_product(villagesql::CustomArgWith<TVectorParams> a,
331329

332330
// Element-wise add: (TVECTOR, TVECTOR) -> TVECTOR
333331
// Vectors must have the same dimension and element type.
334-
void tvector_add(villagesql::CustomArgWith<TVectorParams> a,
335-
villagesql::CustomArgWith<TVectorParams> b,
336-
villagesql::CustomResultWith<TVectorParams> out) {
332+
void tvector_add(vsql::CustomArgWith<TVectorParams> a,
333+
vsql::CustomArgWith<TVectorParams> b,
334+
vsql::CustomResultWith<TVectorParams> out) {
337335
if (a.is_null() || b.is_null()) {
338336
out.set_null();
339337
return;
@@ -365,9 +363,8 @@ void tvector_add(villagesql::CustomArgWith<TVectorParams> a,
365363
}
366364

367365
// Scalar multiply: (TVECTOR, REAL) -> TVECTOR
368-
void tvector_scale(villagesql::CustomArgWith<TVectorParams> a,
369-
villagesql::RealArg scalar,
370-
villagesql::CustomResultWith<TVectorParams> out) {
366+
void tvector_scale(vsql::CustomArgWith<TVectorParams> a, vsql::RealArg scalar,
367+
vsql::CustomResultWith<TVectorParams> out) {
371368
if (a.is_null() || scalar.is_null()) {
372369
out.set_null();
373370
return;

villagesql/sdk/include/villagesql/detail/vef_register.h

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <villagesql/abi/types.h>
2929
#include <villagesql/sdk_version.h>
3030

31-
namespace villagesql {
31+
namespace vsql {
3232

3333
// Forward-declare the vsql globals so that name lookup succeeds inside the
3434
// `if constexpr (Ext::kHasVsqlGlobals)` block even when only the base
@@ -43,20 +43,48 @@ extern vef_read_keyring_fn g_read_keyring;
4343
extern vef_write_keyring_fn g_write_keyring;
4444
} // namespace keyring
4545

46-
// Forward-declare materialize_func_desc so the helpers below can reference it
47-
// without requiring func_builder.h to be included first.
46+
// Define materialize_func_desc here so it is available to both old
47+
// (villagesql::func_builder) and new (vsql::func_builder) API users without
48+
// requiring an additional include. villagesql/func_builder.h re-exports this
49+
// as villagesql::func_builder::materialize_func_desc via a using-declaration,
50+
// so ADL on old-API types and the explicit using in vef_fill_func_ptrs both
51+
// resolve to the same entity — eliminating overload ambiguity.
4852
namespace func_builder {
4953
template <typename FuncData, size_t Index>
50-
vef_func_desc_t *materialize_func_desc(const FuncData &func_data);
54+
__attribute__((visibility("hidden"))) vef_func_desc_t *materialize_func_desc(
55+
const FuncData &func_data) {
56+
static vef_signature_t signature;
57+
static vef_func_desc_t desc;
58+
59+
signature.param_count = static_cast<unsigned int>(func_data.num_params());
60+
signature.params = func_data.num_params() > 0 ? func_data.params() : nullptr;
61+
signature.return_type = func_data.return_type();
62+
63+
desc.protocol = VEF_PROTOCOL_2;
64+
desc.name = func_data.name();
65+
desc.signature = &signature;
66+
desc.vdf = func_data.vdf();
67+
desc.prerun = func_data.prerun();
68+
desc.postrun = func_data.postrun();
69+
desc.buffer_size = func_data.buffer_size();
70+
desc.deterministic = func_data.deterministic();
71+
desc.clear = func_data.clear();
72+
desc.accumulate = func_data.accumulate();
73+
74+
return &desc;
75+
}
5176
} // namespace func_builder
5277

78+
} // namespace vsql
79+
80+
namespace villagesql {
5381
namespace detail {
5482

5583
// Fills arr[I] with the materialized vef_func_desc_t* for each function.
5684
template <typename Ext, size_t... Is>
5785
void vef_fill_func_ptrs(vef_func_desc_t **arr, const Ext &e,
5886
std::index_sequence<Is...>) {
59-
using villagesql::func_builder::materialize_func_desc;
87+
using vsql::func_builder::materialize_func_desc;
6088
((arr[Is] = materialize_func_desc<decltype(e.template func_at<Is>()), Is>(
6189
e.template func_at<Is>())),
6290
...);
@@ -157,10 +185,10 @@ vef_registration_t *vef_register_impl(vef_registration_t &reg,
157185
// vsql headers in scope.
158186
if constexpr (Ext::kHasVsqlGlobals) {
159187
if (arg->protocol >= VEF_PROTOCOL_2) {
160-
villagesql::sys_var::g_get_variable = arg->get_variable;
161-
villagesql::sys_var::g_set_variable = arg->set_variable;
162-
villagesql::keyring::g_read_keyring = arg->read_keyring;
163-
villagesql::keyring::g_write_keyring = arg->write_keyring;
188+
vsql::sys_var::g_get_variable = arg->get_variable;
189+
vsql::sys_var::g_set_variable = arg->set_variable;
190+
vsql::keyring::g_read_keyring = arg->read_keyring;
191+
vsql::keyring::g_write_keyring = arg->write_keyring;
164192
}
165193
}
166194

villagesql/sdk/include/villagesql/extension_builder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <utility>
3131

3232
#include <villagesql/detail/vef_register.h>
33+
#include <villagesql/func_builder.h>
3334
#include <villagesql/type_builder.h>
3435

3536
namespace villagesql {

villagesql/sdk/include/villagesql/func_builder.h

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@
3131

3232
#include <villagesql/abi/types.h>
3333

34+
// Forward-declare materialize_func_desc in vsql::func_builder. The definition
35+
// lives in vef_register.h. The using-declaration below makes
36+
// villagesql::func_builder::materialize_func_desc an alias to the same entity,
37+
// so ADL on old-API types and the explicit using in vef_fill_func_ptrs both
38+
// resolve to one function — eliminating overload ambiguity.
39+
namespace vsql {
40+
namespace func_builder {
41+
template <typename FuncData, size_t Index>
42+
vef_func_desc_t *materialize_func_desc(const FuncData &func_data);
43+
} // namespace func_builder
44+
} // namespace vsql
45+
3446
namespace villagesql {
3547
namespace func_builder {
3648

@@ -267,29 +279,7 @@ struct StaticFuncDesc {
267279
constexpr void init_name() const {}
268280
};
269281

270-
template <typename FuncData, size_t Index>
271-
__attribute__((visibility("hidden"))) vef_func_desc_t *materialize_func_desc(
272-
const FuncData &func_data) {
273-
static vef_signature_t signature;
274-
static vef_func_desc_t desc;
275-
276-
signature.param_count = static_cast<unsigned int>(func_data.num_params());
277-
signature.params = func_data.num_params() > 0 ? func_data.params() : nullptr;
278-
signature.return_type = func_data.return_type();
279-
280-
desc.protocol = VEF_PROTOCOL_2;
281-
desc.name = func_data.name();
282-
desc.signature = &signature;
283-
desc.vdf = func_data.vdf();
284-
desc.prerun = func_data.prerun();
285-
desc.postrun = func_data.postrun();
286-
desc.buffer_size = func_data.buffer_size();
287-
desc.deterministic = func_data.deterministic();
288-
desc.clear = func_data.clear();
289-
desc.accumulate = func_data.accumulate();
290-
291-
return &desc;
292-
}
282+
using vsql::func_builder::materialize_func_desc;
293283

294284
// =============================================================================
295285
// FuncBuilder (V1 — raw vef_vdf_func_t only)

villagesql/sdk/include/villagesql/preview/ping.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct preview_ping {
6363
static constexpr auto bind(Inner builder) {
6464
using Cap = ping::Capability;
6565
return builder.required_capability(
66-
{Cap::kName, &::villagesql::vsql::cap_receive<Cap, &cap>,
66+
{Cap::kName, &::vsql::cap_receive<Cap, &cap>,
6767
::villagesql::detail::abi_type_hash<decltype(cap.abi_)>()});
6868
}
6969
};

villagesql/sdk/include/villagesql/vsql.h

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
//
3434
// // 1. Implement your type operations
3535
// bool complex_from_string(std::string_view s,
36-
// villagesql::Span<unsigned char> buf, size_t* len);
37-
// bool complex_to_string(villagesql::Span<const unsigned char> data,
38-
// villagesql::Span<char> out, size_t* out_len);
39-
// int complex_compare(villagesql::Span<const unsigned char> a,
40-
// villagesql::Span<const unsigned char> b);
36+
// vsql::Span<unsigned char> buf, size_t* len);
37+
// bool complex_to_string(vsql::Span<const unsigned char> data,
38+
// vsql::Span<char> out, size_t* out_len);
39+
// int complex_compare(vsql::Span<const unsigned char> a,
40+
// vsql::Span<const unsigned char> b);
4141
//
4242
// // 2. Define the type as a constexpr object
4343
// static constexpr const char kComplexTypeName[] = "COMPLEX";
@@ -90,49 +90,27 @@
9090

9191
namespace vsql {
9292

93-
// Re-export make_extension from villagesql::vsql (extended version)
94-
using villagesql::vsql::make_extension;
95-
96-
// Re-export make_func and type-operation entry points from the typed builder
97-
using villagesql::func_builder::make_func;
98-
using villagesql::func_builder::make_int_to_params;
99-
using villagesql::func_builder::make_intrinsic_default;
100-
using villagesql::func_builder::make_resolve_params;
101-
using villagesql::func_builder::make_type_compare;
102-
using villagesql::func_builder::make_type_decode;
103-
using villagesql::func_builder::make_type_encode;
104-
using villagesql::func_builder::make_type_hash;
105-
106-
// Re-export sys_var and keyring namespaces
107-
namespace sys_var = villagesql::sys_var;
108-
namespace keyring = villagesql::keyring;
109-
using villagesql::status_var_builder::make_status_var_double;
110-
using villagesql::status_var_builder::make_status_var_int;
111-
using villagesql::sys_var_builder::make_sys_var_bool;
112-
using villagesql::sys_var_builder::make_sys_var_double;
113-
using villagesql::sys_var_builder::make_sys_var_int;
114-
using villagesql::sys_var_builder::make_sys_var_str;
115-
116-
// Re-export typed argument/result wrappers
117-
using villagesql::CustomArg;
118-
using villagesql::CustomArgWith;
119-
using villagesql::CustomResult;
120-
using villagesql::CustomResultWith;
121-
using villagesql::IntArg;
122-
using villagesql::IntResult;
123-
using villagesql::RealArg;
124-
using villagesql::RealResult;
125-
using villagesql::Span;
126-
using villagesql::StringArg;
127-
using villagesql::StringResult;
128-
129-
// Re-export built-in type name constants
130-
using villagesql::func_builder::INT;
131-
using villagesql::func_builder::REAL;
132-
using villagesql::func_builder::STRING;
133-
134-
// Re-export sys_var change wrapper
135-
using villagesql::sys_var_builder::SysVarChange;
93+
// Re-export from func_builder sub-namespace into the flat vsql:: namespace
94+
using func_builder::INT;
95+
using func_builder::make_func;
96+
using func_builder::make_int_to_params;
97+
using func_builder::make_intrinsic_default;
98+
using func_builder::make_resolve_params;
99+
using func_builder::make_type_compare;
100+
using func_builder::make_type_decode;
101+
using func_builder::make_type_encode;
102+
using func_builder::make_type_hash;
103+
using func_builder::REAL;
104+
using func_builder::STRING;
105+
106+
// Re-export from sys_var_builder and status_var_builder sub-namespaces
107+
using status_var_builder::make_status_var_double;
108+
using status_var_builder::make_status_var_int;
109+
using sys_var_builder::make_sys_var_bool;
110+
using sys_var_builder::make_sys_var_double;
111+
using sys_var_builder::make_sys_var_int;
112+
using sys_var_builder::make_sys_var_str;
113+
using sys_var_builder::SysVarChange;
136114

137115
} // namespace vsql
138116

0 commit comments

Comments
 (0)