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
/*
* 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.
*/
/*
* ray_rowsel — morsel-local row-filter selection.
*
* Replacement for the bitmap (RAY_SEL) form of g->selection used by
* OP_FILTER on table inputs. Stores the surviving rows of a filter
* as morsel-local uint16 indices instead of a per-row bitmap, so the
* downstream group / sort / agg hot loops iterate only the live rows
* with no per-row bitmap test.
*
* Layout — single ray_alloc block, contiguous payload at ray_data():
*
* ray_rowsel_t meta (24 bytes; at ray_data(block))
* uint8_t seg_flags[] (n_segs, padded to 8-byte boundary)
* uint32_t seg_offsets[] (n_segs + 1, prefix sum into idx[])
* uint16_t idx[] (total_pass entries; only MIX
* segments contribute)
*
* Per-segment flag values are the same NONE / ALL / MIX constants the
* existing RAY_SEL bitmap uses (src/ops/ops.h):
* - NONE: no rows in this morsel pass — consumer skips wholesale.
* - ALL: every row in this morsel passes — seg_offsets[seg+1]
* equals seg_offsets[seg], no indices stored, consumer
* iterates [seg_start, seg_end) densely.
* - MIX: partial pass — idx[seg_offsets[seg] .. seg_offsets[seg+1])
* holds the morsel-local positions (0..1023) of passing
* rows in segment order.
*
* Lifetime: single-owner. Producer (ray_rowsel_from_pred / refine)
* returns a fresh ray_t* with rc=1. Consumer calls ray_rowsel_release
* to free. No COW semantics — selection data is never shared and
* never serialized.
*
* The block is allocated via ray_alloc and uses no specific type tag
* (zeroed by ray_alloc); nothing in the runtime dispatches on it.
* The accessors below are the only valid way to read its contents.
*
* Note: this is unrelated to the existing RAY_SEL type tag used by
* src/ops/join.c and src/ops/traverse.c as a generic key-bit set.
* Those continue to use ray_sel_* unchanged.
*/
/* RAY_MORSEL_ELEMS must fit in uint16_t for morsel-local indices. */
;
/* Inline header at ray_data(block). Pointer fields are NOT stored
* here — they are reconstructed from this header's n_segs / total_pass
* via the accessor inlines below. The payload arrays live immediately
* after this struct in the same allocation. */
typedef struct ray_rowsel_t;
/* Round n up to a multiple of 8 so the next array starts aligned. */
static inline size_t
static inline ray_rowsel_t*
static inline uint8_t*
static inline uint32_t*
static inline uint16_t*
/* Compute the total bytes needed for the inline payload.
* `idx_count` is the number of uint16_t entries the idx[] array
* needs to hold — this is the sum of popcounts over MIX segments
* only, NOT the total passing-row count. ALL segments contribute
* zero to idx[]. */
static inline size_t
/* Allocate a rowsel block.
*
* `nrows` — source row count this selection covers.
* `total_pass` — number of passing rows (ALL + MIX). Stored in
* meta; consumers read it for sizing decisions.
* `idx_count` — number of uint16_t slots the idx[] array needs.
* Equal to the sum of popcounts over segments
* tagged MIX in the final layout. ALL and NONE
* segments contribute zero.
*
* Header fields are populated; arrays are uninitialized. Caller
* fills seg_flags, seg_offsets, and idx, then hands the block off
* (g->selection, etc.) or releases via ray_rowsel_release.
* Returns NULL on OOM. */
ray_t* ;
/* Release a rowsel block. Equivalent to ray_release / ray_free of
* the underlying allocation — exposed under its own name for clarity
* at call sites. */
void ;
/* Build a rowsel from a RAY_BOOL predicate vector.
*
* pred must be a flat RAY_BOOL vec (byte-per-row). Returns:
* - NULL if all rows pass (the all-pass convention is "no
* selection", same as g->selection == NULL).
* - A fresh rowsel block (rc=1) otherwise, including the
* none-pass case (zero-length idx, all flags NONE).
*
* The build runs in two parallel passes when nrows is large enough
* to benefit (>= RAY_PARALLEL_THRESHOLD): pass 1 computes per-segment
* popcount + flag, an inline prefix sum fills seg_offsets, pass 2
* writes the morsel-local indices into the global idx[] (each worker
* writes its own non-overlapping slice). Smaller pred vecs run the
* same logic single-threaded. */
ray_t* ;
/* Flatten a rowsel into a dense int64 array of global row indices,
* sorted ascending. Length of the array is `meta->total_pass`.
*
* Returned block is a ray_t* byte buffer whose ray_data() points to
* an `int64_t[total_pass]`. Consumer gets a raw pointer via
* ray_data() and releases the block when done via ray_release.
* Returns NULL on OOM.
*
* Used by exec_group and similar consumers that can't cheaply walk
* the morsel-local rowsel inline (yet) — they dispatch workers over
* [0, total_pass) using the flattened indices directly. */
ray_t* ;
/* Refine an existing rowsel by AND-ing it with a fresh predicate vec.
*
* Used by chained OP_FILTER on a table input that already has a
* g->selection. Walks `existing`'s surviving rows, tests pred at each,
* emits a new rowsel containing only the positions that pass both.
* Returns NULL if the result is all-pass (impossible here unless
* existing was already all-pass), or a fresh block otherwise.
*
* Does not consume `existing` — caller is responsible for releasing
* the old selection after replacing it. */
ray_t* ;
/* RAY_ROWSEL_H */