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
/*
* Copyright (c) 2025-2026 Anton Kundenko <singaraiona@gmail.com>
* All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "linkop.h"
#include "idxop.h"
#include "ops/internal.h" /* col_propagate_str_pool */
#include "ops/ops.h" /* RAY_IS_PARTED */
#include "mem/cow.h"
#include "vec/vec.h"
#include "table/table.h"
#include "table/sym.h"
#include "lang/eval.h"
#include "lang/env.h"
#include <string.h>
/* --------------------------------------------------------------------------
* ray_link_attach
* -------------------------------------------------------------------------- */
ray_t* ray_link_attach(ray_t** vp, int64_t target_sym_id) {
if (!vp || !*vp || RAY_IS_ERR(*vp))
return ray_error("type", "link: null/error vector");
ray_t* v = *vp;
if (!ray_is_vec(v) || (v->type != RAY_I32 && v->type != RAY_I64))
return ray_error("type", "link: column must be RAY_I32 or RAY_I64 (got %d)",
(int)v->type);
if (v->attrs & RAY_ATTR_SLICE)
return ray_error("type", "link: cannot attach to a slice; materialize first");
if (target_sym_id < 0)
return ray_error("type", "link: invalid target sym ID");
/* Validate that target_sym_id resolves to a RAY_TABLE in the env. */
ray_t* target = ray_env_get(target_sym_id);
if (!target || target->type != RAY_TABLE)
return ray_error("name", "link: target sym does not name a table");
/* Reject parted dim tables — deref math (target_col[linkcol[i]]) is
* straight indexing, with no notion of which segment a global rowid
* lives in. Pointing a link at a parted target would silently
* misbehave at deref time. Better an explicit nyi here than a
* three-layers-deep wrong-answer bug. See guide-indexes.html and
* queries-links.html for the supported shape (parted fact -> regular
* non-parted dim). */
int64_t tcols = ray_table_ncols(target);
for (int64_t c = 0; c < tcols; c++) {
ray_t* tcol = ray_table_get_col_idx(target, c);
if (tcol && RAY_IS_PARTED(tcol->type))
return ray_error("nyi",
"link: target table has a parted column (%d); "
"link targets must be non-parted (in-memory or splayed) tables",
(int)c);
}
/* COW so we own the bytes we're about to mutate. */
v = ray_cow(v);
if (!v || RAY_IS_ERR(v)) return v;
*vp = v;
/* Nulls live as sentinels in the payload — bytes 0-15 of the union
* carry no per-element data, so we can write link_target into
* bytes 8-15 unconditionally. */
v->link_target = target_sym_id;
v->attrs |= RAY_ATTR_HAS_LINK;
/* If an accelerator index is also attached, the index's saved snapshot
* captured the pre-link bytes 8-15 (which were _idx_pad / NULL). Update
* the snapshot so a future index-drop restores the link too. */
if (v->attrs & RAY_ATTR_HAS_INDEX) {
ray_index_t* ix = ray_index_payload(v->index);
memcpy(&ix->saved_aux[8], &target_sym_id, 8);
}
return v;
}
/* --------------------------------------------------------------------------
* ray_link_detach
* -------------------------------------------------------------------------- */
ray_t* ray_link_detach(ray_t** vp) {
if (!vp || !*vp || RAY_IS_ERR(*vp)) return *vp;
ray_t* v = *vp;
if (!(v->attrs & RAY_ATTR_HAS_LINK)) return v;
v = ray_cow(v);
if (!v || RAY_IS_ERR(v)) { *vp = v; return v; }
*vp = v;
v->link_target = 0;
v->attrs &= (uint8_t)~RAY_ATTR_HAS_LINK;
if (v->attrs & RAY_ATTR_HAS_INDEX) {
ray_index_t* ix = ray_index_payload(v->index);
memset(&ix->saved_aux[8], 0, 8);
}
return v;
}
/* --------------------------------------------------------------------------
* ray_link_deref — produce target_col[link_col[i]] for each row i
*
* Result type matches the target column type. Length matches the link
* column. Null rows in the link become null in the result; null rows in
* the target also propagate. Returns NULL when target table or field
* column don't exist (caller treats as a probe miss).
* -------------------------------------------------------------------------- */
ray_t* ray_link_deref(ray_t* v, int64_t sym_id) {
if (!ray_link_has(v)) return NULL;
if (v->type != RAY_I32 && v->type != RAY_I64) return NULL;
/* Slice-through: a slice over a linked parent inherits the link.
* link_target lives on the parent; the slice's own bytes 8-15 are
* slice_offset, which would be garbage if we read it as a sym ID. */
int64_t target_sym = (v->attrs & RAY_ATTR_SLICE)
? v->slice_parent->link_target
: v->link_target;
ray_t* target_tab = ray_env_get(target_sym);
if (!target_tab || target_tab->type != RAY_TABLE) return NULL;
/* Reject parted targets at deref time, mirroring the attach-time guard
* in ray_link_attach. The attach-time check catches the obvious case
* (user calls (.col.link 'parted_dim ...)), but two paths bypass it:
* 1. Lazy rebind — attach saw a non-parted table; the sym was later
* rebound to a parted one (env lookup is at deref time).
* 2. .link sidecar reload — try_load_link_sidecar (col.c) writes
* link_target straight from the on-disk sym name without any
* env-state check.
* Without a deref-time guard, both produce a silent wrong-answer bug
* (target_col[linkcol[i]] indexes into RAY_PARTED data, which is a
* list of segment pointers — straight-byte indexing is meaningless). */
int64_t tcols = ray_table_ncols(target_tab);
for (int64_t c = 0; c < tcols; c++) {
ray_t* tcol = ray_table_get_col_idx(target_tab, c);
if (tcol && RAY_IS_PARTED(tcol->type))
return ray_error("nyi",
"link deref: target table has a parted column (%d); "
"links to parted dim tables are not supported in v1",
(int)c);
}
ray_t* target_col = ray_table_get_col(target_tab, sym_id);
if (!target_col) return NULL;
int64_t n = v->len;
int64_t target_n = target_col->len;
int8_t out_type = target_col->type;
/* Resolve through slices: SYM-width and (later) str_pool
* all live on the slice_parent's attrs/union, never on the slice
* itself. The slice contributes only its [slice_offset, len) view.
* Compute the canonical width and base-pointer once here so the
* gather loop stays correct for narrow-width sliced sym columns. */
ray_t* col_owner = (target_col->attrs & RAY_ATTR_SLICE)
? target_col->slice_parent : target_col;
int64_t col_off = (target_col->attrs & RAY_ATTR_SLICE)
? target_col->slice_offset : 0;
uint8_t target_width = col_owner->attrs & RAY_SYM_W_MASK;
uint8_t target_esz = (out_type == RAY_SYM)
? (uint8_t)(1u << target_width)
: ray_sym_elem_size(out_type, col_owner->attrs);
/* Allocate result. For RAY_SYM mirror the parent's width so the
* subsequent memcpy is byte-correct; otherwise the canonical size
* for the type. */
ray_t* result;
if (out_type == RAY_SYM) {
result = ray_sym_vec_new(target_width, n);
} else {
result = ray_vec_new(out_type, n);
}
if (!result || RAY_IS_ERR(result)) return result;
result->len = n;
/* The gather below copies SYM cell ids verbatim from the dim-table
* column — resolve over its domain (slice-aware via slice_parent). */
if (out_type == RAY_SYM) ray_sym_vec_adopt_domain(result, target_col);
uint8_t out_esz = ray_sym_elem_size(out_type, result->attrs);
if (out_esz > 0) memset(ray_data(result), 0, (size_t)n * out_esz);
/* By construction, out_esz == target_esz: SYM widths match,
* STR is always 16, numeric types match because out_type == target. */
const uint8_t* link_base = (const uint8_t*)ray_data(v);
uint8_t link_esz = ray_sym_elem_size(v->type, v->attrs);
char* out_base = (char*)ray_data(result);
/* Compute the source-data base by hand (not via ray_data on the
* slice) because ray_data_fn assumes ray_type_sizes[RAY_SYM] = 8
* (W64), which mis-offsets narrow-width sliced sym columns. */
const char* col_data_base = (const char*)ray_data(col_owner);
const char* tgt_base = col_data_base + (size_t)col_off * target_esz;
for (int64_t i = 0; i < n; i++) {
if (ray_vec_is_null(v, i)) {
ray_vec_set_null(result, i, true);
continue;
}
int64_t rid;
if (link_esz == 4) {
int32_t r;
memcpy(&r, link_base + i * 4, 4);
rid = (int64_t)r;
} else {
memcpy(&rid, link_base + i * 8, 8);
}
if (rid < 0 || rid >= target_n) {
ray_vec_set_null(result, i, true);
continue;
}
if (ray_vec_is_null(target_col, rid)) {
ray_vec_set_null(result, i, true);
continue;
}
if (target_esz > 0 && out_esz == target_esz) {
memcpy(out_base + i * out_esz,
tgt_base + rid * target_esz,
target_esz);
}
}
/* Fill correct-width sentinel into null payload slots so consumers
* reading raw payload honor the contract. */
switch (out_type) {
case RAY_F64: {
double* d = (double*)ray_data(result);
for (int64_t i = 0; i < n; i++)
if (ray_vec_is_null(result, i)) d[i] = NULL_F64;
break;
}
case RAY_I64: case RAY_TIMESTAMP: {
int64_t* d = (int64_t*)ray_data(result);
for (int64_t i = 0; i < n; i++)
if (ray_vec_is_null(result, i)) d[i] = NULL_I64;
break;
}
case RAY_I32: case RAY_DATE: case RAY_TIME: {
int32_t* d = (int32_t*)ray_data(result);
for (int64_t i = 0; i < n; i++)
if (ray_vec_is_null(result, i)) d[i] = NULL_I32;
break;
}
case RAY_I16: {
int16_t* d = (int16_t*)ray_data(result);
for (int64_t i = 0; i < n; i++)
if (ray_vec_is_null(result, i)) d[i] = NULL_I16;
break;
}
default: break;
}
/* Type-specific metadata propagation.
* RAY_STR: share the source pool so ray_str_t pool_offs are valid. */
if (out_type == RAY_STR) {
col_propagate_str_pool(result, target_col);
}
return result;
}
/* --------------------------------------------------------------------------
* Rayfall builtin entry points
* -------------------------------------------------------------------------- */
ray_t* ray_col_link_fn(ray_t* target_sym, ray_t* int_vec) {
if (!target_sym || target_sym->type != -RAY_SYM)
return ray_error("type", "(.col.link target v): target must be a sym");
if (!int_vec || RAY_IS_ERR(int_vec))
return int_vec ? int_vec : ray_error("type", "(.col.link target v): null v");
int64_t target_id = target_sym->i64;
ray_t* w = int_vec;
ray_retain(w);
ray_t* r = ray_link_attach(&w, target_id);
if (RAY_IS_ERR(r)) { ray_release(w); return r; }
return w;
}
ray_t* ray_col_unlink_fn(ray_t* v) {
if (!v || RAY_IS_ERR(v)) return v;
ray_t* w = v;
ray_retain(w);
ray_t* r = ray_link_detach(&w);
if (RAY_IS_ERR(r)) { ray_release(w); return r; }
return w;
}
ray_t* ray_col_link_p_fn(ray_t* v) {
return ray_bool(ray_link_has(v) ? 1 : 0);
}
ray_t* ray_col_target_fn(ray_t* v) {
if (!ray_link_has(v)) return RAY_NULL_OBJ;
/* Slice-aware: ray_link_target_id reads from slice_parent for slices,
* because v->link_target on a slice aliases slice_offset and would
* surface as a garbage sym ID. */
return ray_sym(ray_link_target_id(v));
}