reifydb-sdk 0.9.3

SDK for building ReifyDB operators, procedures, transforms and more
Documentation
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 ReifyDB

use std::{ptr, ptr::null_mut, slice::from_raw_parts};

use reifydb_codec::{key::encoded::EncodedKey, row::bytes::EncodedBytes};
use reifydb_core::{
	key::operator::state::{GroupId, GroupStateKey, KeyspaceId},
	state::timer::TimerKind,
};
use reifydb_flow::operator::state::reclaim::ReclaimOutcome;
use reifydb_value::{
	count::Count,
	util::cowvec::CowVec,
	value::{datetime::DateTime, row_number::RowNumber},
};
use tracing::{Span, instrument};

use crate::{
	common::extern_c::wire::{
		buffer::ExternCBuffer,
		key_ref::ExternCKeyRef,
		status::{EXTERN_C_END_OF_ITERATION, EXTERN_C_NOT_FOUND, EXTERN_C_OK},
	},
	error::{Result, SdkError},
	flow::operator::{
		context::GuestBound,
		extern_c::{
			binding::context::ExternCContext,
			wire::{
				iterators::ExternCStateIterator,
				state::{ExternCGroupId, ExternCStateEntry, ExternCStateSlice},
			},
		},
	},
};

const _: () = assert!(
	size_of::<ExternCGroupId>() == GroupId::WIDTH,
	"the wire group id must stay exactly as wide as GroupId, or the pair array stride reads across elements"
);

fn wire_group(group: GroupId) -> ExternCGroupId {
	ExternCGroupId {
		bytes: *group.as_bytes(),
	}
}

#[instrument(name = "flow::operator::state::extern_c:get", level = "trace", skip(ctx), fields(
	operator_id = ctx.operator_id().0,
	key_len = key.as_bytes().len(),
	found
))]
pub(crate) fn get(ctx: &ExternCContext, key: &EncodedKey) -> Result<Option<EncodedBytes>> {
	let key_bytes = key.as_bytes();
	let mut output = ExternCBuffer {
		ptr: null_mut(),
		len: 0,
		cap: 0,
	};

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; key_bytes outlives the callback, which only reads it. On EXTERN_C_OK the host
	// writes a buffer of output.len initialised bytes, copied out before memory.free releases it with the length
	// it was allocated at.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.get)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			key_bytes.as_ptr(),
			key_bytes.len(),
			&mut output,
		);

		if result == EXTERN_C_OK {
			if output.ptr.is_null() || output.len == 0 {
				Span::current().record("found", false);
				Ok(None)
			} else {
				let value_bytes = from_raw_parts(output.ptr, output.len).to_vec();

				((*ctx.ctx).callbacks.memory.free)(output.ptr as *mut u8, output.len);
				Span::current().record("found", true);
				Ok(Some(EncodedBytes(CowVec::new(value_bytes))))
			}
		} else if result == EXTERN_C_NOT_FOUND {
			Span::current().record("found", false);
			Ok(None)
		} else {
			Err(SdkError::Other(format!("host_state_get failed with code {}", result)))
		}
	}
}

#[instrument(name = "flow::operator::state::extern_c:set", level = "trace", skip(ctx, value), fields(
	operator_id = ctx.operator_id().0,
	key_len = key.as_bytes().len(),
	value_len = value.as_ref().len()
))]
pub(crate) fn set(ctx: &mut ExternCContext, key: &EncodedKey, value: &EncodedBytes) -> Result<()> {
	let key_bytes = key.as_bytes();
	let value_bytes = value.as_ref();

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; key_bytes and value_bytes borrow guest allocations that outlive the callback,
	// which only reads them.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.set)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			key_bytes.as_ptr(),
			key_bytes.len(),
			value_bytes.as_ptr(),
			value_bytes.len(),
		);

		if result == EXTERN_C_OK {
			Ok(())
		} else {
			Err(SdkError::Other(format!("host_state_set failed with code {}", result)))
		}
	}
}

#[instrument(name = "flow::operator::extern_c::binding::state::remove", level = "trace", skip(ctx), fields(
	operator_id = ctx.operator_id().0,
	key_len = key.as_bytes().len()
))]
pub(crate) fn remove(ctx: &mut ExternCContext, key: &EncodedKey) -> Result<()> {
	let key_bytes = key.as_bytes();

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; key_bytes outlives the callback, which only reads it.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.remove)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			key_bytes.as_ptr(),
			key_bytes.len(),
		);

		if result == EXTERN_C_OK {
			Ok(())
		} else {
			Err(SdkError::Other(format!("host_state_remove failed with code {}", result)))
		}
	}
}

#[instrument(name = "flow::operator::state::extern_c:get_many", level = "debug", skip(ctx, keys), fields(
	operator_id = ctx.operator_id().0,
	key_count = keys.len(),
	result_count
))]
pub(crate) fn get_many(ctx: &ExternCContext, keys: &[EncodedKey]) -> Result<Vec<(EncodedKey, EncodedBytes)>> {
	if keys.is_empty() {
		Span::current().record("result_count", 0);
		return Ok(Vec::new());
	}

	let key_refs: Vec<ExternCKeyRef> = keys
		.iter()
		.map(|key| {
			let bytes = key.as_bytes();
			ExternCKeyRef {
				ptr: bytes.as_ptr(),
				len: bytes.len(),
			}
		})
		.collect();

	let mut iterator: *mut ExternCStateIterator = null_mut();

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; key_refs borrows keys that outlive the callback. The handle the host opens is
	// passed once to collect_iterator_results, discharging its precondition that the handle is fresh and freed
	// exactly there.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.get_many)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			key_refs.as_ptr(),
			key_refs.len(),
			&mut iterator,
		);

		if result != EXTERN_C_OK {
			return Err(SdkError::Other(format!("host_state_get_many failed with code {}", result)));
		}

		collect_iterator_results(ctx, iterator)
	}
}

#[instrument(name = "flow::operator::state::extern_c:prefix", level = "debug", skip(ctx), fields(
	operator_id = ctx.operator_id().0,
	prefix_len = prefix.as_bytes().len(),
	result_count
))]
pub(crate) fn prefix(
	ctx: &ExternCContext,
	prefix: &EncodedKey,
	limit: usize,
) -> Result<Vec<(EncodedKey, EncodedBytes)>> {
	let prefix_bytes = prefix.as_bytes();
	let mut iterator: *mut ExternCStateIterator = null_mut();

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; prefix_bytes outlives the callback. The handle the host opens is passed once to
	// collect_iterator_results, discharging its precondition that the handle is fresh and freed exactly there.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.prefix)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			prefix_bytes.as_ptr(),
			prefix_bytes.len(),
			limit,
			&mut iterator,
		);

		if result != EXTERN_C_OK {
			return Err(SdkError::Other(format!("host_state_prefix failed with code {}", result)));
		}

		collect_iterator_results(ctx, iterator)
	}
}

const BOUND_UNBOUNDED: u8 = 0;
const BOUND_INCLUDED: u8 = 1;
const BOUND_EXCLUDED: u8 = 2;

fn wire_bound(bound: GuestBound<'_>) -> (*const u8, usize, u8) {
	match bound {
		GuestBound::Unbounded => (null_mut(), 0, BOUND_UNBOUNDED),
		GuestBound::Included(suffix) => (suffix.as_ptr(), suffix.len(), BOUND_INCLUDED),
		GuestBound::Excluded(suffix) => (suffix.as_ptr(), suffix.len(), BOUND_EXCLUDED),
	}
}

#[instrument(name = "flow::operator::extern_c::binding::state::range", level = "debug", skip(ctx), fields(
	operator_id = ctx.operator_id().0,
	result_count
))]
pub(crate) fn range(
	ctx: &ExternCContext,
	group: GroupId,
	keyspace: KeyspaceId,
	start: GuestBound<'_>,
	end: GuestBound<'_>,
	limit: usize,
) -> Result<Vec<(EncodedKey, EncodedBytes)>> {
	let mut iterator: *mut ExternCStateIterator = null_mut();

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; each bound pointer is null with length 0 or borrows a suffix that outlives the
	// callback. The handle the host opens is passed once to collect_iterator_results, which owns and frees it.
	unsafe {
		let (start_ptr, start_len, start_bound_type) = wire_bound(start);
		let (end_ptr, end_len, end_bound_type) = wire_bound(end);

		let result = ((*ctx.ctx).callbacks.state.range)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			wire_group(group),
			keyspace.0,
			start_ptr,
			start_len,
			start_bound_type,
			end_ptr,
			end_len,
			end_bound_type,
			limit,
			&mut iterator,
		);

		if result != EXTERN_C_OK {
			return Err(SdkError::Other(format!("host_state_range failed with code {}", result)));
		}

		collect_iterator_results(ctx, iterator)
	}
}

/// # Safety
///
/// `ctx.ctx` must point to a live `ExternCContextRaw` whose state callbacks are valid,
/// and `iterator` must be null or an open handle from that context's state
/// scan/range callback that has not yet been freed. This takes ownership of the
/// handle and frees it before returning, so the caller must not free it again.
unsafe fn collect_iterator_results(
	ctx: &ExternCContext,
	iterator: *mut ExternCStateIterator,
) -> Result<Vec<(EncodedKey, EncodedBytes)>> {
	if iterator.is_null() {
		Span::current().record("result_count", 0);
		return Ok(Vec::new());
	}

	const ITERATOR_BATCH_CAP: usize = 256;
	let empty = ExternCStateSlice {
		ptr: ptr::null(),
		len: 0,
	};
	let mut batch = [ExternCStateEntry {
		key: empty,
		value: empty,
	}; ITERATOR_BATCH_CAP];
	let mut results = Vec::new();

	loop {
		let mut out_len = 0usize;
		// SAFETY: iterator was checked non-null above and is still the open handle this call owns; batch is a
		// live array of ITERATOR_BATCH_CAP entries and out_len a local slot, so the host writes at most
		// ITERATOR_BATCH_CAP entries in bounds.
		let next_result = unsafe {
			((*ctx.ctx).callbacks.state.iterator_next)(
				iterator,
				batch.as_mut_ptr(),
				ITERATOR_BATCH_CAP,
				&mut out_len,
			)
		};

		if next_result != EXTERN_C_OK && next_result != EXTERN_C_END_OF_ITERATION {
			// SAFETY: iterator is the open handle this call owns and has not been freed yet; the return
			// immediately after means it is freed exactly once.
			unsafe { ((*ctx.ctx).callbacks.state.iterator_free)(iterator) };
			return Err(SdkError::Other(format!(
				"host_state_iterator_next failed with code {}",
				next_result
			)));
		}

		for entry in batch.iter().take(out_len) {
			if entry.key.ptr.is_null() || entry.key.len == 0 {
				continue;
			}
			// SAFETY: the host fills out_len entries whose key ptr/len describe initialised bytes owned
			// by the iterator and valid until the next iterator_next or iterator_free, both of which
			// happen after this copy. Null and zero-length keys are skipped above.
			let key_bytes = unsafe { from_raw_parts(entry.key.ptr, entry.key.len) }.to_vec();
			let value = if !entry.value.ptr.is_null() && entry.value.len > 0 {
				// SAFETY: same iterator-owned lifetime contract as the key slice.
				let value_bytes = unsafe { from_raw_parts(entry.value.ptr, entry.value.len) }.to_vec();
				EncodedBytes(CowVec::new(value_bytes))
			} else {
				EncodedBytes(CowVec::new(Vec::new()))
			};
			results.push((EncodedKey::new(key_bytes), value));
		}

		if next_result == EXTERN_C_END_OF_ITERATION {
			break;
		}
	}

	// SAFETY: iterator is the open handle this call owns; the loop only breaks on the path that has not freed it,
	// so this frees it exactly once.
	unsafe { ((*ctx.ctx).callbacks.state.iterator_free)(iterator) };
	Span::current().record("result_count", results.len());
	Ok(results)
}

#[instrument(name = "flow::operator::extern_c::binding::state::clear", level = "trace", skip(ctx), fields(
	operator_id = ctx.operator_id().0
))]
pub(crate) fn clear(ctx: &mut ExternCContext) -> Result<()> {
	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; no guest pointer crosses the boundary here.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.clear)((*ctx.ctx).operator_id, ctx.ctx);

		if result == EXTERN_C_OK {
			Ok(())
		} else {
			Err(SdkError::Other(format!("host_state_clear failed with code {}", result)))
		}
	}
}

fn key_refs(keys: &[EncodedKey]) -> Vec<ExternCKeyRef> {
	keys.iter()
		.map(|key| {
			let bytes = key.as_bytes();
			ExternCKeyRef {
				ptr: bytes.as_ptr(),
				len: bytes.len(),
			}
		})
		.collect()
}

pub(crate) fn get_or_create_row_numbers_for_pairs(
	ctx: &mut ExternCContext,
	pairs: &[(GroupId, EncodedKey)],
) -> Result<Vec<(RowNumber, bool)>> {
	if pairs.is_empty() {
		return Ok(Vec::new());
	}
	let group_ids: Vec<ExternCGroupId> = pairs.iter().map(|(group, _)| wire_group(*group)).collect();
	let refs: Vec<ExternCKeyRef> = pairs
		.iter()
		.map(|(_, key)| {
			let bytes = key.as_bytes();
			ExternCKeyRef {
				ptr: bytes.as_ptr(),
				len: bytes.len(),
			}
		})
		.collect();
	let mut row_numbers = vec![0u64; pairs.len()];
	let mut is_new = vec![0u8; pairs.len()];

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; group_ids and refs borrow pairs for the duration of the call, and row_numbers
	// and is_new are live, initialised arrays of exactly pairs.len() slots each for the host to fill.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.get_or_create_row_numbers_for_pairs)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			group_ids.as_ptr(),
			refs.as_ptr(),
			refs.len(),
			row_numbers.as_mut_ptr(),
			is_new.as_mut_ptr(),
		);
		if result != EXTERN_C_OK {
			return Err(SdkError::Other(format!(
				"host_get_or_create_row_numbers_for_pairs failed with code {}",
				result
			)));
		}
	}

	Ok(row_numbers.into_iter().zip(is_new).map(|(rn, new)| (RowNumber(rn), new != 0)).collect())
}

pub(crate) fn arm_timer(ctx: &mut ExternCContext, due: DateTime, kind: TimerKind, key: &EncodedKey) -> Result<()> {
	let bytes = key.as_bytes();

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; bytes outlives the callback, which only reads it.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.arm_timer)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			due.to_bits(),
			kind as u8,
			bytes.as_ptr(),
			bytes.len(),
		);
		if result != EXTERN_C_OK {
			return Err(SdkError::Other(format!("host_arm_timer failed with code {}", result)));
		}
	}

	Ok(())
}

pub(crate) fn reclaim_group_identity(ctx: &mut ExternCContext, group: GroupId, limit: usize) -> Result<ReclaimOutcome> {
	let mut removed = 0usize;
	let mut more = 0u8;

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null, and the host keeps the ExternCContextRaw alive
	// and aligned for the whole guest call; removed and more are local stack slots.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.reclaim_group_identity)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			wire_group(group),
			limit,
			&mut removed,
			&mut more,
		);
		if result != EXTERN_C_OK {
			return Err(SdkError::Other(format!(
				"host_reclaim_group_identity failed with code {}",
				result
			)));
		}
	}

	Ok(ReclaimOutcome {
		removed: Count::new(removed as u64),
		more: more != 0,
	})
}

pub(crate) fn reclaim_group_identity_keys(
	ctx: &mut ExternCContext,
	group: GroupId,
	keys: &[GroupStateKey],
) -> Result<ReclaimOutcome> {
	let key_refs: Vec<ExternCKeyRef> = keys
		.iter()
		.map(|key| {
			let bytes = key.as_bytes();
			ExternCKeyRef {
				ptr: bytes.as_ptr(),
				len: bytes.len(),
			}
		})
		.collect();

	let mut removed = 0usize;
	let mut more = 0u8;

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null, and the host keeps the ExternCContextRaw alive
	// and aligned for the whole guest call; key_refs borrows keys that outlive the callback, and removed and
	// more are local stack slots.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.reclaim_group_identity_keys)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			wire_group(group),
			key_refs.as_ptr(),
			key_refs.len(),
			&mut removed,
			&mut more,
		);
		if result != EXTERN_C_OK {
			return Err(SdkError::Other(format!(
				"host_reclaim_group_identity_keys failed with code {}",
				result
			)));
		}
	}

	Ok(ReclaimOutcome {
		removed: Count::new(removed as u64),
		more: more != 0,
	})
}

pub(crate) fn flow_watermark(ctx: &mut ExternCContext) -> Result<Option<DateTime>> {
	let mut bits = 0u64;
	let mut present = 0u8;

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null, and the host keeps the ExternCContextRaw alive
	// and aligned for the whole guest call; bits and present are local stack slots.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.flow_watermark)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			&mut bits,
			&mut present,
		);
		if result != EXTERN_C_OK {
			return Err(SdkError::Other(format!("host_flow_watermark failed with code {}", result)));
		}
	}

	Ok((present != 0).then(|| DateTime::from_bits(bits)))
}

pub(crate) fn disarm_timer(ctx: &mut ExternCContext, due: DateTime, kind: TimerKind, key: &EncodedKey) -> Result<()> {
	let bytes = key.as_bytes();

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; bytes outlives the callback, which only reads it.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.disarm_timer)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			due.to_bits(),
			kind as u8,
			bytes.as_ptr(),
			bytes.len(),
		);
		if result != EXTERN_C_OK {
			return Err(SdkError::Other(format!("host_disarm_timer failed with code {}", result)));
		}
	}

	Ok(())
}

pub(crate) fn get_or_create_row_numbers(
	ctx: &mut ExternCContext,
	group: GroupId,
	keys: &[EncodedKey],
) -> Result<Vec<(RowNumber, bool)>> {
	if keys.is_empty() {
		return Ok(Vec::new());
	}
	let key_refs = key_refs(keys);
	let mut row_numbers = vec![0u64; keys.len()];
	let mut is_new = vec![0u8; keys.len()];

	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; key_refs borrows keys for the duration of the call, and row_numbers and is_new are
	// live, initialised arrays of exactly keys.len() slots each for the host to fill.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.get_or_create_row_numbers)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			wire_group(group),
			key_refs.as_ptr(),
			key_refs.len(),
			row_numbers.as_mut_ptr(),
			is_new.as_mut_ptr(),
		);
		if result != EXTERN_C_OK {
			return Err(SdkError::Other(format!(
				"host_get_or_create_row_numbers failed with code {}",
				result
			)));
		}
	}

	Ok(row_numbers.into_iter().zip(is_new).map(|(rn, new)| (RowNumber(rn), new != 0)).collect())
}

pub(crate) fn remove_row_number(ctx: &mut ExternCContext, group: GroupId, key: &EncodedKey) -> Result<()> {
	let key_bytes = key.as_bytes();
	// SAFETY: ExternCContext::new asserts ctx.ctx is non-null and the host keeps the ExternCContextRaw valid
	// for the whole guest call; key_bytes outlives the callback, which only reads it.
	unsafe {
		let result = ((*ctx.ctx).callbacks.state.remove_row_number)(
			(*ctx.ctx).operator_id,
			ctx.ctx,
			wire_group(group),
			key_bytes.as_ptr(),
			key_bytes.len(),
		);
		if result == EXTERN_C_OK {
			Ok(())
		} else {
			Err(SdkError::Other(format!("host_remove_row_number failed with code {}", result)))
		}
	}
}