rayforce-sys 1.4.0

Raw FFI bindings to the RayforceDB v2 core (librayforce)
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
/*
 *   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 "store/enceval.h"
#include <math.h>
#include <string.h>

static inline int cmp_one(int64_t x, uint16_t op, int64_t c) {
    switch (op) {
    case OP_EQ: return x == c; case OP_NE: return x != c;
    case OP_LT: return x < c;  case OP_LE: return x <= c;
    case OP_GT: return x > c;  case OP_GE: return x >= c;
    default:    return 0;
    }
}
/* the predicate comparator on doubles: -0.0 == +0.0; a NaN never reaches
 * here (it is the null, in the bitmap) */
static inline int cmp_one_f(double x, uint16_t op, double c) {
    switch (op) {
    case OP_EQ: return x == c; case OP_NE: return x != c;
    case OP_LT: return x < c;  case OP_LE: return x <= c;
    case OP_GT: return x > c;  case OP_GE: return x >= c;
    default:    return 0;
    }
}
static inline uint64_t lane(const ray_enc_lanes_t* l, int T, uint32_t i) {
    switch (T) { case 8: return l->b[i]; case 16: return l->h[i]; case 32: return l->s[i]; default: return l->q[i]; }
}
static inline bool is_float(int8_t t) { return t == RAY_F32 || t == RAY_F64; }
static inline bool is_cmp_op(uint16_t op) { return op >= OP_EQ && op <= OP_GE; }

void ray_enc_mask_nulls(const ray_enc_vview_t* v, uint8_t nr, uint8_t* bits) {
    if (!v->nullmap) return;
    for (int i = 0; i < v->n; i++)
        if ((v->nullmap[i >> 3] >> (i & 7)) & 1) bits[i] = nr;
}

int ray_enc_const_decision(const ray_enc_vview_t* v, uint16_t op, int64_t c, uint8_t nr) {
    if (v->enc != RAY_ENC_CONST || is_float(v->type) || !is_cmp_op(op)) return -1;
    if (v->type == RAY_SYM && op != OP_EQ && op != OP_NE) return -1;   /* never order ids (#738) */
    if (v->nullmap && v->n_nulls == v->n) return nr;          /* all null: every row is null_result */
    int r = cmp_one(ray_enc_logical(v->cbits, v->k), op, c);
    if (!v->nullmap || r == nr) return r;
    return -1;
}

int ray_enc_const_decision_f64(const ray_enc_vview_t* v, uint16_t op, double c, uint8_t nr) {
    if (v->enc != RAY_ENC_CONST || !is_float(v->type) || !is_cmp_op(op)) return -1;
    if (isnan(c)) return -1;                                  /* a NaN constant is the null constant: decline */
    if (v->nullmap && v->n_nulls == v->n) return nr;
    int r = cmp_one_f(ray_enc_fval(v->cbits, v->type == RAY_F64 ? 64 : 32), op, c);
    if (!v->nullmap || r == nr) return r;
    return -1;
}

/* FOR_BP: x = logical(base) + u with 0 <= u <= umax and no wrap in logical
 * space (the view checked it on the actual residuals), so x op c <=> u op t
 * with t = c - logical(base), computed in 128 bits; t outside [0, umax]
 * decides every non-null row alike.  The residuals come from the lane
 * kernels at width T and compare as unsigned T-bit values. */
#define EVAL_RES(UT) do { const UT* u_ = (const UT*)res; const UT t_ = (UT)tt;               \
    switch (op) {                                                                             \
    case OP_EQ: for (int i = 0; i < v->n; i++) bits[i] = u_[i] == t_; break;                  \
    case OP_NE: for (int i = 0; i < v->n; i++) bits[i] = u_[i] != t_; break;                  \
    case OP_LT: for (int i = 0; i < v->n; i++) bits[i] = u_[i] <  t_; break;                  \
    case OP_LE: for (int i = 0; i < v->n; i++) bits[i] = u_[i] <= t_; break;                  \
    case OP_GT: for (int i = 0; i < v->n; i++) bits[i] = u_[i] >  t_; break;                  \
    case OP_GE: for (int i = 0; i < v->n; i++) bits[i] = u_[i] >= t_; break;                  \
    default:    memset(bits, 0, (size_t)v->n); break; } } while (0)

static void eval_for(const ray_enc_vview_t* v, uint16_t op, int64_t c, uint8_t* bits) {
    const uint64_t umax = v->width >= 64 ? ~0ull : ((1ull << v->width) - 1);
    /* one formula for every kind: the only unsigned 64-bit kind is SYM W64,
     * whose ids (and so its base) are below 2^32, where logical(base) is
     * the base itself; a negative c then lands in t < 0, which is right
     * for EQ / NE, the only operators SYM reaches */
    const __int128 t = (__int128)c - (__int128)ray_enc_logical(v->base, v->k);
    if (t < 0 || t > (__int128)umax) {
        memset(bits, cmp_one(t < 0 ? 1 : -1, op, 0), (size_t)v->n);   /* x > c, or x < c, for every row */
        return;
    }
    const uint64_t tt = (uint64_t)t;
    _Alignas(64) ray_enc_lanes_t lanes;                       /* the residuals, on the stack */
    const void* res = &lanes;
    ray_enccol_unpack_resid(v, &lanes);
    switch (v->k.T) {
    case 8:  EVAL_RES(uint8_t);  break;
    case 16: EVAL_RES(uint16_t); break;
    case 32: EVAL_RES(uint32_t); break;
    default: EVAL_RES(uint64_t); break;
    }
}
#undef EVAL_RES

/* DICT: VALUE_DICT is strictly ascending in native order (the view admits
 * signed integer kinds only); the passing codes form one interval [lo, hi)
 * or its complement (NE). */
static uint32_t lower_bound(const ray_enc_vview_t* v, int64_t c) {
    uint32_t lo = 0, hi = v->dict_n;
    while (lo < hi) { uint32_t mid = lo + (hi - lo) / 2; if (ray_enc_logical(v->dict[mid], v->k) < c) lo = mid + 1; else hi = mid; }
    return lo;
}
static uint32_t upper_bound(const ray_enc_vview_t* v, int64_t c) {
    uint32_t lo = 0, hi = v->dict_n;
    while (lo < hi) { uint32_t mid = lo + (hi - lo) / 2; if (ray_enc_logical(v->dict[mid], v->k) <= c) lo = mid + 1; else hi = mid; }
    return lo;
}
static void eval_dict(const ray_enc_vview_t* v, uint16_t op, int64_t c, uint8_t* bits) {
    const uint32_t lb = lower_bound(v, c), ub = upper_bound(v, c);
    uint32_t lo = 0, hi = 0; uint8_t neg = 0;
    switch (op) {
    case OP_EQ: lo = lb; hi = ub; break;
    case OP_NE: lo = lb; hi = ub; neg = 1; break;
    case OP_LT: lo = 0;  hi = lb; break;
    case OP_LE: lo = 0;  hi = ub; break;
    case OP_GT: lo = ub; hi = v->dict_n; break;
    case OP_GE: lo = lb; hi = v->dict_n; break;
    default: break;
    }
    const uint32_t span = hi - lo;
    for (int i = 0; i < v->n; i++)
        bits[i] = (uint8_t)((v->codes[i] - lo < span) ^ neg);   /* unsigned: one compare */
}

/* x in set: the set sorted ascending, distinct */
static bool in_set(const int64_t* set, int nset, int64_t x) {
    int lo = 0, hi = nset;
    while (lo < hi) { int mid = lo + (hi - lo) / 2; if (set[mid] < x) lo = mid + 1; else hi = mid; }
    return lo < nset && set[lo] == x;
}
/* RLE: each run's value decides its rows (CONST takes one memset), under
 * a comparison or a membership. */
typedef struct { bool member; uint16_t op; int64_t c; const int64_t* set; int nset; } run_pred_t;
static inline int run_pred(const run_pred_t* p, int64_t x) {
    return p->member ? in_set(p->set, p->nset, x) : cmp_one(x, p->op, p->c);
}
/* ray_enc_logical, inlined for the per-run loop */
static inline int64_t logical_of(uint64_t bits, ray_enc_kind_t k) {
    if (k.T == 64) return (int64_t)bits;
    const uint64_t m = (1ull << k.T) - 1;
    bits &= m;
    return k.is_signed && (bits >> (k.T - 1)) & 1 ? (int64_t)(bits | ~m) : (int64_t)bits;
}
/* bits[i, e) = b in 8-byte stores (runs are often a few rows: a memset
 * call per run costs more than the run).  A store may reach past e, never
 * past n: the next run, which starts at e, overwrites those bytes. */
static inline void fill_run(uint8_t* bits, uint32_t i, uint32_t e, uint32_t n, uint8_t b) {
    const uint64_t pat = 0x0101010101010101ull * b;
    for (; i + 8 <= e; i += 8) memcpy(bits + i, &pat, 8);
    if (i < e) {
        if (i + 8 <= n) memcpy(bits + i, &pat, 8);
        else for (; i < e; i++) bits[i] = b;
    }
}
static void eval_runs(const ray_enc_vview_t* v, const run_pred_t* p, uint8_t* bits) {
    uint32_t i = 0;
    for (uint32_t r = 0; r < v->n_runs; r++) {
        const uint8_t b = (uint8_t)run_pred(p, logical_of(lane(&v->run_vals, v->k.T, r), v->k));
        const uint32_t e = v->run_ends.h[r];
        fill_run(bits, i, e, (uint32_t)v->n, b);
        i = e;
    }
}

/* Some run of v holds the logical value c: its T-bit pattern compared
 * against the run values, one pass (no per-run fill), for equality's
 * all-fail case.  A value outside the kind's range is held by none. */
#define RUNS_HOLD(UT, F) do { const UT x_ = (UT)pat; bool hit_ = false;               \
        for (uint32_t r = 0; r < v->n_runs; r++) hit_ |= v->run_vals.F[r] == x_;      \
        return hit_; } while (0)
static bool runs_hold(const ray_enc_vview_t* v, int64_t c) {
    const int T = v->k.T;
    if (T < 64) {
        const int64_t lo = v->k.is_signed ? -((int64_t)1 << (T - 1)) : 0;
        const int64_t hi = v->k.is_signed ? ((int64_t)1 << (T - 1)) - 1 : (int64_t)((1ull << T) - 1);
        if (c < lo || c > hi) return false;
    } else if (!v->k.is_signed && c < 0) {
        return false;
    }
    const uint64_t pat = T == 64 ? (uint64_t)c : (uint64_t)c & ((1ull << T) - 1);
    switch (T) {
    case 8:  RUNS_HOLD(uint8_t, b);
    case 16: RUNS_HOLD(uint16_t, h);
    case 32: RUNS_HOLD(uint32_t, s);
    default: RUNS_HOLD(uint64_t, q);
    }
}
#undef RUNS_HOLD

/* eval_runs of EQ / NE for a constant some run holds (runs_hold, so it
 * lies inside the kind's range): its T-bit pattern once, then one compare
 * per run at the lane width (no per-run widening or operator switch). */
#define RUNS_EQ(UT, F) do { const UT x_ = (UT)pat;                                        \
        for (uint32_t r = 0; r < v->n_runs; r++) {                                        \
            const uint32_t e_ = v->run_ends.h[r];                                         \
            fill_run(bits, i, e_, (uint32_t)v->n, (uint8_t)((v->run_vals.F[r] == x_) ^ ne)); \
            i = e_;                                                                       \
        } } while (0)
static void eval_runs_eq(const ray_enc_vview_t* v, int64_t c, uint8_t ne, uint8_t* bits) {
    const int T = v->k.T;
    const uint64_t pat = T == 64 ? (uint64_t)c : (uint64_t)c & ((1ull << T) - 1);
    uint32_t i = 0;
    switch (T) {
    case 8:  RUNS_EQ(uint8_t, b);  break;
    case 16: RUNS_EQ(uint16_t, h); break;
    case 32: RUNS_EQ(uint32_t, s); break;
    default: RUNS_EQ(uint64_t, q); break;
    }
}
#undef RUNS_EQ

bool ray_enc_eval_cmp_f64(const ray_enc_vview_t* v, uint16_t op, double c, uint8_t nr, uint8_t* bits) {
    if (!is_float(v->type) || !is_cmp_op(op) || isnan(c)) return false;
    const int T = v->k.T;
    switch (v->enc) {
    case RAY_ENC_CONST:
        memset(bits, cmp_one_f(ray_enc_fval(v->cbits, T), op, c), (size_t)v->n);
        break;
    case RAY_ENC_RLE: {
        uint32_t i = 0;
        for (uint32_t r = 0; r < v->n_runs; r++) {
            const uint32_t e = v->run_ends.h[r];
            fill_run(bits, i, e, (uint32_t)v->n, (uint8_t)cmp_one_f(ray_enc_fval(lane(&v->run_vals, T, r), T), op, c));
            i = e;
        }
        break;
    }
    case RAY_ENC_DICT: {
        /* the entries ascend in dictionary order, numerically nondecreasing
         * (the order only puts -0.0 before +0.0), so the entries equal to c
         * under the predicate comparator are one interval [lb, ub): both
         * zeros for a zero c */
        uint32_t lo = 0, hi = v->dict_n;
        while (lo < hi) { uint32_t mid = lo + (hi - lo) / 2; if (ray_enc_fval(v->dict[mid], T) < c) lo = mid + 1; else hi = mid; }
        const uint32_t lb = lo;
        hi = v->dict_n;
        while (lo < hi) { uint32_t mid = lo + (hi - lo) / 2; if (ray_enc_fval(v->dict[mid], T) <= c) lo = mid + 1; else hi = mid; }
        const uint32_t ub = lo;
        uint32_t a = 0, b = 0; uint8_t neg = 0;
        switch (op) {
        case OP_EQ: a = lb; b = ub; break;
        case OP_NE: a = lb; b = ub; neg = 1; break;
        case OP_LT: a = 0;  b = lb; break;
        case OP_LE: a = 0;  b = ub; break;
        case OP_GT: a = ub; b = v->dict_n; break;
        default:    a = lb; b = v->dict_n; break;          /* OP_GE */
        }
        const uint32_t span = b - a;
        for (int i = 0; i < v->n; i++)
            bits[i] = (uint8_t)((v->codes[i] - a < span) ^ neg);   /* unsigned: one compare */
        break;
    }
    default:
        return false;
    }
    /* a null row holds a real value (the fill rule) or, wholly null, any
     * bits: the mask decides it */
    ray_enc_mask_nulls(v, nr, bits);
    return true;
}

bool ray_enc_eval_cmp(const ray_enc_vview_t* v, uint16_t op, int64_t c, uint8_t nr, uint8_t* bits) {
    if (is_float(v->type)) return false;                              /* floats: ray_enc_eval_cmp_f64 */
    if (!is_cmp_op(op)) return false;
    if (v->type == RAY_SYM && op != OP_EQ && op != OP_NE) return false;   /* never order ids (#738) */
    switch (v->enc) {
    case RAY_ENC_CONST:
        memset(bits, cmp_one(ray_enc_logical(v->cbits, v->k), op, c), (size_t)v->n);
        break;
    case RAY_ENC_FOR_BP: eval_for(v, op, c, bits); break;
    case RAY_ENC_RLE:
        if ((op == OP_EQ || op == OP_NE) && !runs_hold(v, c)) memset(bits, op == OP_NE, (size_t)v->n);
        else if (op == OP_EQ || op == OP_NE) eval_runs_eq(v, c, op == OP_NE, bits);
        else eval_runs(v, &(run_pred_t){ false, op, c, NULL, 0 }, bits);
        break;
    case RAY_ENC_DICT:   eval_dict(v, op, c, bits); break;
    default: return false;
    }
    ray_enc_mask_nulls(v, nr, bits);
    return true;
}

bool ray_enc_eval_cmp_runs(const ray_enc_vview_t* v, uint16_t op, int64_t c, uint8_t* pass) {
    if (v->enc != RAY_ENC_RLE || v->nullmap || is_float(v->type) || !is_cmp_op(op)) return false;
    if (v->type == RAY_SYM && op != OP_EQ && op != OP_NE) return false;   /* never order ids (#738) */
    if ((op == OP_EQ || op == OP_NE) && !runs_hold(v, c)) {
        memset(pass, op == OP_NE, v->n_runs);
        return true;
    }
    if (op == OP_EQ || op == OP_NE) {
        const int T = v->k.T;
        const uint64_t pat = T == 64 ? (uint64_t)c : (uint64_t)c & ((1ull << T) - 1);
        const uint8_t ne = op == OP_NE;
#define RUNS_PASS(UT, F) do { const UT x_ = (UT)pat;                                          \
            for (uint32_t r = 0; r < v->n_runs; r++) pass[r] = (uint8_t)((v->run_vals.F[r] == x_) ^ ne); \
        } while (0)
        switch (T) {
        case 8:  RUNS_PASS(uint8_t, b);  break;
        case 16: RUNS_PASS(uint16_t, h); break;
        case 32: RUNS_PASS(uint32_t, s); break;
        default: RUNS_PASS(uint64_t, q); break;
        }
#undef RUNS_PASS
        return true;
    }
    for (uint32_t r = 0; r < v->n_runs; r++)
        pass[r] = (uint8_t)cmp_one(logical_of(lane(&v->run_vals, v->k.T, r), v->k), op, c);
    return true;
}

bool ray_enc_eval_cmp_runs_f64(const ray_enc_vview_t* v, uint16_t op, double c, uint8_t* pass) {
    if (v->enc != RAY_ENC_RLE || v->nullmap || !is_float(v->type) || !is_cmp_op(op) || isnan(c)) return false;
    const int T = v->k.T;
    for (uint32_t r = 0; r < v->n_runs; r++)
        pass[r] = (uint8_t)cmp_one_f(ray_enc_fval(lane(&v->run_vals, T, r), T), op, c);
    return true;
}

bool ray_enc_eval_in(const ray_enc_vview_t* v, const int64_t* set, int nset, uint8_t nr, uint8_t* bits) {
    if (is_float(v->type)) return false;
    if (nset < 0) nset = 0;
    switch (v->enc) {
    case RAY_ENC_CONST:
        memset(bits, in_set(set, nset, ray_enc_logical(v->cbits, v->k)), (size_t)v->n);
        break;
    case RAY_ENC_RLE: eval_runs(v, &(run_pred_t){ true, 0, 0, set, nset }, bits); break;
    case RAY_ENC_DICT: {
        /* the codes of the set's members: a bitmap over dict_n <= 2^16 codes (8 KB) */
        uint64_t pass[(1u << 16) / 64];
        memset(pass, 0, ((size_t)v->dict_n + 63) / 64 * 8);
        for (int s = 0; s < nset; s++) {
            const uint32_t lb = lower_bound(v, set[s]);
            if (lb < v->dict_n && ray_enc_logical(v->dict[lb], v->k) == set[s]) pass[lb >> 6] |= 1ull << (lb & 63);
        }
        for (int i = 0; i < v->n; i++) bits[i] = (uint8_t)((pass[v->codes[i] >> 6] >> (v->codes[i] & 63)) & 1);
        break;
    }
    case RAY_ENC_FOR_BP: {
        /* x = logical(base) + u with no wrap (the view's guarantee), so the
         * wrapping 64-bit sum is the logical value exactly */
        const uint64_t lb = (uint64_t)ray_enc_logical(v->base, v->k);
        _Alignas(64) ray_enc_lanes_t lanes;                   /* the residuals, on the stack */
        ray_enccol_unpack_resid(v, &lanes);
        for (int i = 0; i < v->n; i++)
            bits[i] = (uint8_t)in_set(set, nset, (int64_t)(lb + lane(&lanes, v->k.T, (uint32_t)i)));
        break;
    }
    default: return false;
    }
    ray_enc_mask_nulls(v, nr, bits);
    return true;
}

/* CONST: n_nonnull * logical(value) (0 when every row is null); FOR_BP:
 * n_nonnull * logical(base) + the non-null residuals (no wrap, the view's
 * guarantee; the writer fills null slots with the base, and the residual
 * sum skips them all the same); RLE without nulls: logical(value) * run
 * length per run. */
bool ray_enc_vec_sum(const ray_enc_vview_t* v, int64_t* hi, uint64_t* lo, int64_t* nn) {
    switch (v->type) {
    case RAY_I16: case RAY_I32: case RAY_I64: case RAY_TIME: break;
    case RAY_BOOL: case RAY_U8: if (v->n_nulls) return false; break;
    default: return false;
    }
    __int128 s = 0;
    const int64_t n_nonnull = (int64_t)v->n - v->n_nulls;
    switch (v->enc) {
    case RAY_ENC_CONST:
        if (n_nonnull) s = (__int128)n_nonnull * ray_enc_logical(v->cbits, v->k);
        break;
    case RAY_ENC_FOR_BP: {
        _Alignas(64) ray_enc_lanes_t u;                      /* the residuals, on the stack */
        ray_enccol_unpack_resid(v, &u);
        const uint8_t* nm = v->n_nulls ? v->nullmap : NULL;
        __int128 r = 0;
        if (v->k.T == 64) {
            if (nm) { for (int i = 0; i < v->n; i++) if (!((nm[i >> 3] >> (i & 7)) & 1)) r += u.q[i]; }
            else    { for (int i = 0; i < v->n; i++) r += u.q[i]; }
        } else {
            /* at most 1024 residuals below 2^32: the 64-bit total cannot wrap */
            uint64_t r64 = 0;
            for (int i = 0; i < v->n; i++) {
                uint64_t x = lane(&u, v->k.T, (uint32_t)i);
                if (nm) x &= (uint64_t)((nm[i >> 3] >> (i & 7)) & 1) - 1;   /* a null row adds 0 */
                r64 += x;
            }
            r = r64;
        }
        s = (__int128)n_nonnull * ray_enc_logical(v->base, v->k) + r;
        break;
    }
    case RAY_ENC_RLE: {
        if (v->n_nulls) return false;                        /* nullable RLE decodes */
        uint32_t prev = 0;
        for (uint32_t r = 0; r < v->n_runs; r++) {
            s += (__int128)ray_enc_logical(lane(&v->run_vals, v->k.T, r), v->k) * (int64_t)(v->run_ends.h[r] - prev);
            prev = v->run_ends.h[r];
        }
        break;
    }
    default: return false;
    }
    *hi = (int64_t)(s >> 64);
    *lo = (uint64_t)s;
    *nn = n_nonnull;
    return true;
}

/* Counts (the count fast path): the evaluators' rules, summed instead of
 * written out.  A view with a bitmap declines (its nulls would need the
 * mask); SYM ids count == / != only, never ordered (#738). */
/* FOR_BP: eval_for's threshold t = c - logical(base) over the residuals
 * (no wrap, the view's guarantee); t outside [0, umax] decides every row.
 * The residuals are compared as the lane kernels unpack them, never
 * stored (ray_enccol_count_resid: measured 12-16% faster than unpacking
 * into a lane buffer and counting it). */
static int64_t count_for(const ray_enc_vview_t* v, uint16_t op, int64_t c) {
    const uint64_t umax = v->width >= 64 ? ~0ull : ((1ull << v->width) - 1);
    const __int128 t = (__int128)c - (__int128)ray_enc_logical(v->base, v->k);
    if (t < 0 || t > (__int128)umax) return cmp_one(t < 0 ? 1 : -1, op, 0) ? v->n : 0;
    return ray_enccol_count_resid(v, op, (uint64_t)t);
}

bool ray_enc_count_cmp(const ray_enc_vview_t* v, uint16_t op, int64_t c, int64_t* count) {
    if (v->nullmap || is_float(v->type) || !is_cmp_op(op)) return false;
    if (v->type == RAY_SYM && op != OP_EQ && op != OP_NE) return false;   /* never order ids (#738) */
    int64_t n = 0;
    switch (v->enc) {
    case RAY_ENC_CONST:
        n = cmp_one(ray_enc_logical(v->cbits, v->k), op, c) ? v->n : 0;
        break;
    case RAY_ENC_RLE: {
        uint32_t prev = 0;
        for (uint32_t r = 0; r < v->n_runs; r++) {
            const uint32_t e = v->run_ends.h[r];
            if (cmp_one(logical_of(lane(&v->run_vals, v->k.T, r), v->k), op, c)) n += e - prev;
            prev = e;
        }
        break;
    }
    case RAY_ENC_FOR_BP: n = count_for(v, op, c); break;
    case RAY_ENC_DICT: {
        const uint32_t lb = lower_bound(v, c), ub = upper_bound(v, c);
        uint32_t lo = 0, hi = 0;
        switch (op) {
        case OP_EQ: case OP_NE: lo = lb; hi = ub; break;
        case OP_LT: hi = lb; break;
        case OP_LE: hi = ub; break;
        case OP_GT: lo = ub; hi = v->dict_n; break;
        default:    lo = lb; hi = v->dict_n; break;        /* OP_GE */
        }
        const uint32_t span = hi - lo;
        uint32_t a = 0;
        for (int i = 0; i < v->n; i++) a += v->codes[i] - lo < span;   /* unsigned: one compare */
        n = op == OP_NE ? v->n - (int64_t)a : (int64_t)a;
        break;
    }
    default: return false;
    }
    *count = n;
    return true;
}

bool ray_enc_runs(const ray_enc_vview_t* v, void* ctx, ray_enc_run_fn fn) {
    if ((v->enc != RAY_ENC_CONST && v->enc != RAY_ENC_RLE) || is_float(v->type)) return false;
    uint32_t prev = 0;
    for (uint32_t r = 0; r < v->n_runs; r++) {
        const uint32_t e = v->run_ends.h[r];
        fn(ctx, logical_of(lane(&v->run_vals, v->k.T, r), v->k), (int32_t)prev, (int32_t)e);
        prev = e;
    }
    return true;
}

void ray_enc_view_range(const ray_enc_vview_t* v, int64_t* mn, int64_t* mx) {
    int64_t a = INT64_MAX, b = INT64_MIN;
    /* only run values are a value range; any other view yields the empty range */
    for (uint32_t q = 0; (v->enc == RAY_ENC_CONST || v->enc == RAY_ENC_RLE) && q < v->n_runs; q++) {
        const int64_t x = logical_of(lane(&v->run_vals, v->k.T, q), v->k);
        if (x < a) a = x;
        if (x > b) b = x;
    }
    *mn = a; *mx = b;
}