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
/*
* 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.
*/
;
; /* store/enccol.h */
; /* store/enccol.h */
/* A bound column source: the object a scan reads ranges of. Binding
* retains the object (and a non-runtime SYM domain) and never decodes.
* The struct owns seg_off: never copy it by value (bind it in place, share
* it by pointer, release it once). */
typedef struct ray_col_src_t;
/* A view of rows [start, start + n): elements in the generation-0
* representation, nulls in-band. */
typedef struct ray_col_range_t;
/* Per-source scratch for generation-2 decodes: one fixed vector (1024
* elements of the widest width, 16 KB) and a growable STR pool, both from
* ray_alloc_raw. Serves one source in one scan task. The bytes it decodes
* are counted (ray_enc_counters_get, the column's decoded_bytes) when the
* scratch moves to another column or is freed, not per vector; the scratch
* holds a reference on that column meanwhile, so it may outlive its source.
* A RAW vector read in place (ray_enccol_raw_view) decodes nothing: an
* integer one (SYM included) still counts the bytes it delivers, so a key's
* read-once counts hold whatever its vectors' encodings; a float one
* counts none. */
typedef struct ray_col_scratch_t;
/* The fixed vector a scan task lends its scratch from its own stack, so
* a worker's first decode allocates nothing: a fresh worker heap maps and
* faults in a whole pool on its first allocation, a fixed cost per worker
* that generation 0, reading its columns in place, never pays. One per
* scratch, alive until the scratch is freed. */
typedef struct ray_col_fixed_t;
/* NULL or an owned error (ray_error_free). */
ray_t* ;
void ;
void ;
void ;
/* An initialised scratch decodes into buf (the caller's, outliving the
* scratch's use) instead of a fixed vector from the heap. */
void ;
/* Test hook: fixed vectors range reads took from the heap since process
* start (a lent scratch takes none). */
uint64_t ;
/* Range of [start, start + n), clipped at the source end, at a parted
* segment or slice boundary and (generation 2) at the next vector boundary;
* `out->n` says how many rows were returned (0 only at the source end or
* for n == 0). Generation 0 is zero copy; generation 2 decodes the vector
* into `scratch`. The view is valid until the next call with the same
* scratch or until the source is released. A loaded parted source has no
* missing segment (ray_read_parted gives a ragged partition's absent
* column a segment of nulls, #724); a hand-built wrapper's missing segment holds no rows,
* matching ray_parted_nrows, and the executor refuses a table whose parted
* columns' row counts differ, so a scan never reads one.
* For a parted source the SYM element width and SORTED are per range only
* (segments may differ): read them from the view's `attrs`, never from the
* source's. */
ray_t* ;
/* Rows [start, start + n) decoded straight into dst as `wide` (RAY_I64 /
* RAY_F64) elements, nulls as NULL_I64 / NULL_F64 (an expression
* register), when they are exactly one whole vector of a single-file
* generation-2 source that ray_enccol_decode_wide takes: *got = n. Else
* *got = 0 and nothing is decoded: read the rows with ray_col_range. NULL
* or an owned error. */
ray_t* ;
/* The vector of a generation-2 source (single file, or a parted source's
* encoded segment, neither with a published materialization) that starts
* at row `start`, decoded straight into dst at the generation-0 width,
* when it ends inside [start, start + n) and is not STR or GUID (SYM: of
* the width in `attrs`): *got = its rows (a RAW vector: with the RAW
* vectors after it inside the range, copied at once). Else *got = 0 and nothing is
* read: read the rows with ray_col_range or a cursor. dst may hold a
* partial vector on an error. NULL or an owned error. */
ray_t* ;
/* Test hook: ray_col_range_into's deliveries and the rows they held
* since process start. */
void ;
/* Test hook: vectors ray_col_range_wide decoded since process start
* (counted when their scratch is freed). */
uint64_t ;
/* Bind a table column without decoding; an absent column leaves out->obj
* NULL and returns NULL. */
ray_t* ;
/* The bound source's plain column (borrowed; valid while the source is
* bound): the object itself for generation 0, the cached materialization
* for generation 2. NULL or an owned error. */
ray_t* ;
/* Literal translation into the source's SYM domain, without decoding:
* the position of the bytes, -1 when absent. */
int64_t ;
/* STR dictionary code of a constant: >= 0 the code, -1 provably absent,
* -2 no dictionary to resolve against (generation 2 in P1: STR_DICT
* arrives in P6). Reads only the generation-0 column's DICT index. */
int64_t ;
/* ---- Scan side: cursors, the piece walk, the failure envelope ---- */
/* The contiguous-mode block bound: the pool's task grain (8192 rows). */
/* The source's object is a RAY_ENCCOL (generation 2, or a parted column
* with an encoded or synthetic segment). */
static inline bool
/* The plain column a scan can read in place, without decoding: a plain
* object (vector, slice, plain parted wrapper) itself, or a RAY_ENCCOL's
* published materialization; NULL when reading it would decode.
* Borrowed from the source; never decodes. */
ray_t* ;
/* The base pointer (row 0) a scan reads the source at in place: the ready
* plain vector's data; NULL when it must be read by range (not ready, or a
* parted wrapper, which has no single base). Never decodes. */
const void* ;
/* Generation-0 element width of the source (SYM: its persisted width). */
uint8_t ;
/* Chunk c's statistics when the source is a single-file generation-2
* column; false otherwise (plain, parted, slice). */
bool ;
/* Vector v's statistics (rows [v * RAY_ENC_VEC, + RAY_ENC_VEC), the
* vector's descriptor, nested inside the chunk's statistics) when the
* source is a single-file generation-2 column and the vector carries them:
* min / max are logical_T values as ray_enc_chunk_stat_t's, n_nulls the
* vector's null count. False otherwise: not such a source, v outside it,
* or statistics absent (every row null, or a type without them). */
bool ;
/* Vector v of a single-file generation-2 source (not synthetic, not
* materialized) as a view: ray_enccol_vview's 1 / 0 / -1. 0 for any
* other source (read it by range; a published materialization is read in
* place). */
int ;
/* The statistics of a parted source, chunk by chunk in row order: fn is
* called for rows [lo, hi) of every RAY_ENC_CHUNK-row chunk of every
* segment, with the chunk's directory statistics when the segment is a
* single-file generation-2 column, else NULL (a plain segment). False
* when the source is not parted, or with an owned *err (OOM). */
typedef void ;
bool ;
/* The segments of a parted source: *n of them in row order (borrowed
* RAY_ENCCOL or plain vectors, NULL for a missing one), segment i holding
* rows [off[i], off[i + 1]). False when the source is not parted, or with
* an owned *err (OOM). Publishes the segment offsets on first use. */
bool ;
/* Row `row` of a single-file or parted generation-2 integer source (not
* SYM) read without decoding its vector (ray_enccol_point_i64: FOR_BP,
* DICT over FOR_BP, CONST or RLE, without nulls; a parted row is its
* segment's): 1
* served into *out (the value widened to int64), 0 declined (read the row
* through a cursor, which decodes the vector; always for a
* plain or materialized source or segment), -1 an owned error in *err.
* For samplers: a sampled row costs no vector decode. A parted source
* publishes its segment offsets on first use (so the source is not const). */
int ;
/* ray_col_src_point_i64 of the ascending rows[0, n), all inside one
* vector (ray_col_src_vec_span), the vector's body read once
* (ray_enccol_points_i64): 1 every row served into out, 0 declined (read
* the vector through a cursor; out partly written), -1 an owned *err. */
int ;
/* ray_col_src_points_i64 for a gather (ray_enccol_gather_points): SYM ids
* and vectors with a null bitmap served too, nul[q] 1 for a bitmap null. */
int ;
/* The rows [*lo, *hi) of the vector holding row `row` (segment-local
* vectors for a parted source) when ray_col_src_point_i64 may serve it (a
* single-file encoded column or segment, not materialized): true. False
* for any other row, which a cursor reads. For a caller choosing between
* point reads and one decode by how many of a vector's rows it needs. */
bool ;
/* The vector holding row `row` as a run view: a single-file generation-2
* integer or SYM source, or a parted source's single-file encoded segment
* (segment-local vectors), neither materialized, whose descriptor says
* CONST or RLE (no other codec builds a view) and which ray_enccol_vview
* vouches for, without a bitmap of nulls (SYM nulls are in-band ids, run
* values like the others). *lo / *hi are set in every case to rows of
* the source holding row and read alike: the vector, or a plain,
* materialized or stand-in segment (the source) whole. 1: the view of the
* vector [*lo, *hi); 0 declined (read the rows through a cursor, which
* decodes the vector); -1 an owned *err (a side entry's oom). */
int ;
/* Open decision D2 (ruled per codec at the measured crossovers of a
* batched point read against one decode, re-measured in Task 10b): a
* vector's requested rows are point-read when there are at most
* RAY_ENC_VEC / 32 of them in a FOR_BP vector, RAY_ENC_VEC / 16 in an RLE,
* CONST or DICT one or any vector with a null bitmap (a RAW one without
* nulls is read in place); any other codec declines (ray_enccol_points_i64 decides; enccol.c records the
* measurements). RAY_COL_POINT_ROWS is the largest of them, the most
* rows a caller hands one call (RAY_ENC_VEC = RAY_MORSEL_ELEMS values).
* Structural ratios, not knobs. */
/* One sequential read-ahead hint for rows [lo, hi): plain mmapped
* columns hint their element bytes, generation 2 the chunk bytes from the
* directory, parted sources each touched segment; heap vectors nothing. */
void ;
/* Test hook: the spans ray_col_src_advise would hint (page-aligned start,
* bytes), the first `cap` stored; returns how many there are. */
int ;
/* One source in one task: P1's scratch plus the current view. */
typedef struct ray_col_cursor_t;
void ;
/* ray_col_cursor_init with its scratch lent buf (ray_col_scratch_lend). */
void ;
void ;
/* The view covering row r (0 <= r < len): the current one when it covers
* r, else a range read of [r, len) (generation 2: up to the end of r's
* vector; plain: to the source, segment or slice end). A RAY_ENCCOL whose
* materialization is published is read from that vector. NULL or an
* owned error; *out is valid until the next call or the cursor's free. */
ray_t* ;
/* One source's view of a piece: data points at the piece's first row. */
typedef struct ray_col_piece_t;
typedef void ;
/* Walk [lo, hi) of n sources. plain[i] is the base pointer of source i
* (its row 0) when it can be read in place, or NULL to read it through a
* cursor (generation 2, RAY_ENCCOL_PARTED, a plain parted wrapper); a NULL
* srcs[i] is skipped (COUNT inputs). Each call of fn covers a piece
* [b, e) with one ray_col_piece_t per source. Plain sources point at
* plain + b * esz; cursor sources point into their view, so a piece ends
* at the nearest view end. contiguous: one piece [lo, hi)
* (hi - lo <= RAY_COL_BLOCK_ROWS), every cursor F32/F64 source copied into
* a buffer of the call (any other cursor type is `nyi` in this mode).
* Cursors and buffers live for the call: allocate at its start, free at its
* end. NULL or an owned error (fn is not called again after an error). */
ray_t* ;
/* ray_col_walk's non-contiguous mode through the caller's cursors: cur[i]
* is source i's cursor (ray_col_cursor_init'd for every srcs[i] with a NULL
* plain[i]; other entries unused), kept by the caller across calls. A task
* walking consecutive blocks with one cursor set decodes the vector one
* block ends inside once, not once per block. NULL or an owned error. */
ray_t* ;
/* A dispatch's failure envelope (R-FAIL): a worker notes a range error
* keyed by its task's first row and stops reading. The answer is the
* lowest-keyed failure, independent of scheduling, so a task may return
* early only when ray_scan_fail_skip says a LOWER key already failed (a
* task below the lowest failure still runs: it may fail lower). After the
* join, every coordinator calls ray_scan_fail_finish, which also turns a
* cancelled dispatch (tasks the pool skipped note nothing) into `cancel`. */
typedef struct ray_scan_fail_t;
void ;
/* Consumes err; snapshots its code and this thread's message when key is
* the lowest so far. */
void ;
static inline bool
/* True only when a failure with a key lower than `key` is already noted:
* the task of `key` cannot change the answer and may skip its work. */
static inline bool
/* NULL, or a fresh error rebuilt on the calling thread. */
ray_t* ;
/* The coordinator's verdict after the join: the lowest noted failure; else
* `cancel` when the dispatch was cancelled (ray_interrupted(), or `pool`'s
* cancel flag; pool may be NULL); else NULL. A noted failure stands over a
* cancel: it is a real fault of the data, a cancel only an absence of work. */
ray_t* ;
/* RAY_COLRANGE_H */