Commit f64fdab
authored
sdk: typed prerun/postrun (#551)
* sdk: typed prerun/postrun hooks
Introduce typed C++ wrappers for the per-statement lifecycle hooks
(prerun and postrun). Extension authors no longer touch raw ABI
structs to write these hooks; only the typed signatures are accepted.
Why
---
Extension authors writing prerun/postrun previously had to:
void my_prerun(vef_context_t *, vef_prerun_args_t *args,
vef_prerun_result_t *result) {
if (args->arg_count == 0) {
result->type = VEF_RESULT_ERROR;
snprintf(result->error_msg, VEF_MAX_ERROR_LEN, "...");
return;
}
result->result_buffer_size = N;
result->user_data = new MyState{};
}
Two problems: raw ABI in the user-facing API (against the SDK's
direction; see #548) and bespoke error-reporting plumbing.
After this PR:
void my_prerun(vsql::PrerunArgs args, vsql::PrerunResult out) {
if (args.size() == 0) {
out.error("at least one argument required");
return;
}
out.request_buffer_size(N);
out.set_user_data(new MyState{});
}
void my_postrun(vsql::PostrunArgs args) {
args.delete_state<MyState>();
}
void my_vdf(MyState &state, IntResult out) { ... }
State lifetime stays explicit in this PR: prerun stashes the pointer,
postrun frees it. A follow-up will add auto-cleanup via .state<T>()
(implementable without ABI changes; see TODO in pre_post_run.h).
Where to start reviewing
------------------------
Read the layers in this order:
1. villagesql/sdk/include/villagesql/vsql/pre_post_run.h
New public types: PrerunArgs, PrerunArgType, PrerunResult,
PostrunArgs. This is what extension authors see.
2. villagesql/examples/vsql-simple/src/extension.cc
The ba_call_index demo: set_user_data in prerun, mutable State&
in the VDF, delete_state in postrun.
3. mysql-test/suite/villagesql/extension/t/extension_simple_type_usage.test
What it looks like at the SQL surface.
4. villagesql/sdk/include/villagesql/extension.h
Refreshed user-facing docs for the typed hook shape.
The remaining file is SDK plumbing:
5. villagesql/sdk/include/villagesql/vsql/func_builder.h
- .prerun<>() / .postrun<>() are typed-only; static_assert
rejects raw vef_prerun_func_t / vef_postrun_func_t.
- FuncBuilder gained a HasPrerun template flag; .prerun<>() flips
it true, and build() static_asserts that void(State&,...) and
void(void*,...) signatures require it. Registering a state-style
VDF without a prerun is a compile error, not a runtime null deref.
- WrapperTypedState / WrapperVoidState dispatch on the first
param of the VDF: State& / const State& / void* selects how
user_data is forwarded.
- typed_prerun_thunk / typed_postrun_thunk adapt the user's
typed signature to the raw ABI shape the server invokes.
Not in this PR
--------------
- A `.state<T>()` builder that records State at the template level
and routes typed signatures of the form `void(T&, ...)`. The SDK
would install both prerun and postrun thunks to manage the typed
state's lifetime via the existing user_data slot — no ABI side
channel needed. Will land as a follow-up.
- Typed VarArgs/AnyArg wrappers for varargs VDFs (the other raw-ABI
place in extension code, only reached via PR #254).
- vef_postrun_result_t is empty in the ABI today, so there is no
PostrunResult wrapper. Will add when the struct grows.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
* demo: state_writeback test showing void* user_data writeback gap
* fix previous VDF
* vef: convert ba_concat_all_prerun to typed PrerunArgs/PrerunResult
---------1 parent 3bd0964 commit f64fdab
10 files changed
Lines changed: 637 additions & 66 deletions
File tree
- mysql-test/suite/villagesql
- extension
- r
- t
- std_data
- villagesql
- examples/vsql-simple/src
- sdk/include/villagesql
- detail
- vsql
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
108 | 108 | | |
109 | 109 | | |
110 | 110 | | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
111 | 117 | | |
112 | 118 | | |
113 | 119 | | |
| |||
Lines changed: 25 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
82 | 85 | | |
83 | 86 | | |
84 | 87 | | |
| |||
Lines changed: 38 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
Lines changed: 83 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
112 | 112 | | |
113 | 113 | | |
114 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
115 | 137 | | |
116 | 138 | | |
117 | 139 | | |
| |||
136 | 158 | | |
137 | 159 | | |
138 | 160 | | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
145 | 164 | | |
146 | 165 | | |
147 | | - | |
148 | | - | |
149 | | - | |
150 | | - | |
151 | | - | |
152 | | - | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
153 | 171 | | |
154 | 172 | | |
155 | 173 | | |
156 | | - | |
| 174 | + | |
157 | 175 | | |
158 | 176 | | |
159 | 177 | | |
| |||
205 | 223 | | |
206 | 224 | | |
207 | 225 | | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
208 | 232 | | |
209 | 233 | | |
210 | 234 | | |
| |||
0 commit comments