lora-executor 0.20.0

Query-plan executor for LoraDB's Cypher implementation.
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
//! Sort helpers shared by buffered execution and the pull pipeline.
//!
//! [`SortBuffer`] evaluates each row's sort keys once, when the row
//! arrives, instead of twice per comparison. With a row bound (`ORDER BY
//! … LIMIT k`, the bound being `skip + limit`) it holds at most about
//! `2k` rows: when full it keeps the best `k` (a linear-time selection)
//! and remembers the worst of them, so any later row that does not sort
//! before it is dropped after one key comparison.
//!
//! Ordering is exactly the stable sort's: keys compare with
//! [`compare_values_for_sort`] (nulls last ascending, first descending,
//! mixed types by [`compare_values_total`]) and ties keep arrival order,
//! enforced by comparing arrival sequence numbers last.

use std::cmp::Ordering;

use lora_analyzer::ResolvedSortItem;
use lora_ast::SortDirection;
use lora_compiler::SortLimit;
use lora_store::GraphStorage;

use crate::eval::{eval_expr, EvalContext};
use crate::value::{LoraValue, Row};

use super::helpers::compare_values_total;

pub(crate) fn sort_rows_with_top_k<S: GraphStorage>(
    rows: &mut Vec<Row>,
    items: &[ResolvedSortItem],
    eval_ctx: &EvalContext<'_, S>,
    top_k: Option<usize>,
) {
    if top_k.is_none() && rows.len() < 2 {
        return;
    }
    let taken = std::mem::take(rows);
    *rows = SortBuffer::from_rows(items, top_k, taken, eval_ctx).finish();
}

/// The row bound a Sort may apply: the static `top_k` and, for `LIMIT
/// $n`, the parent LIMIT's expressions evaluated now (the way the LIMIT
/// itself evaluates them, so both agree on the count). `None` when every
/// row must be kept.
pub(crate) fn sort_row_bound(
    top_k: Option<usize>,
    limit: Option<&SortLimit>,
    eval_ctx: &EvalContext<'_, impl GraphStorage>,
) -> Option<usize> {
    let dynamic = limit.and_then(|bound| {
        let scratch = Row::new();
        let limit = eval_expr(&bound.limit, &scratch, eval_ctx).as_i64()?.max(0) as usize;
        let skip = bound
            .skip
            .as_ref()
            .and_then(|e| eval_expr(e, &scratch, eval_ctx).as_i64())
            .unwrap_or(0)
            .max(0) as usize;
        Some(skip.saturating_add(limit))
    });
    match (top_k, dynamic) {
        (Some(a), Some(b)) => Some(a.min(b)),
        (a, b) => a.or(b),
    }
}

/// Rows being sorted, with their keys evaluated once. See the module docs.
pub(crate) struct SortBuffer<'i> {
    items: &'i [ResolvedSortItem],
    /// Most rows to keep; `None` keeps all.
    bound: Option<usize>,
    rows: Vec<Row>,
    /// `keys[slot * items.len() + i]` is slot's key for `items[i]`.
    keys: Vec<LoraValue>,
    /// Arrival order of each slot's row, the final tie-break. Only kept
    /// under a bound; unbounded, a slot's index is its arrival order.
    seqs: Vec<u64>,
    /// Under a bound, once the buffer has been cut down: the keys of the
    /// worst row kept. A later row that does not sort strictly before it
    /// can never be in the result.
    cutoff: Option<Vec<LoraValue>>,
    next_seq: u64,
    scratch: Vec<LoraValue>,
}

impl<'i> SortBuffer<'i> {
    pub(crate) fn new(items: &'i [ResolvedSortItem], bound: Option<usize>) -> Self {
        Self {
            items,
            bound,
            rows: Vec::new(),
            keys: Vec::new(),
            seqs: Vec::new(),
            cutoff: None,
            next_seq: 0,
            scratch: Vec::with_capacity(items.len()),
        }
    }

    /// A buffer over rows already collected, keeping them in place.
    fn from_rows<S: GraphStorage>(
        items: &'i [ResolvedSortItem],
        bound: Option<usize>,
        rows: Vec<Row>,
        eval_ctx: &EvalContext<'_, S>,
    ) -> Self {
        let mut keys = Vec::with_capacity(rows.len() * items.len());
        for row in &rows {
            for item in items {
                keys.push(eval_expr(&item.expr, row, eval_ctx));
            }
        }
        let seqs = if bound.is_some() {
            (0..rows.len() as u64).collect()
        } else {
            Vec::new()
        };
        let next_seq = rows.len() as u64;
        Self {
            items,
            bound,
            rows,
            keys,
            seqs,
            cutoff: None,
            next_seq,
            scratch: Vec::new(),
        }
    }

    pub(crate) fn push<S: GraphStorage>(&mut self, row: Row, eval_ctx: &EvalContext<'_, S>) {
        let Some(bound) = self.bound else {
            for item in self.items {
                self.keys.push(eval_expr(&item.expr, &row, eval_ctx));
            }
            self.rows.push(row);
            return;
        };
        if bound == 0 {
            return;
        }
        let seq = self.next_seq;
        self.next_seq += 1;
        self.scratch.clear();
        for item in self.items {
            self.scratch.push(eval_expr(&item.expr, &row, eval_ctx));
        }
        // A later row loses every tie, so equal to the cutoff is out too.
        if let Some(cutoff) = &self.cutoff {
            if compare_keys(self.items, &self.scratch, cutoff) != Ordering::Less {
                return;
            }
        }
        self.keys.append(&mut self.scratch);
        self.rows.push(row);
        self.seqs.push(seq);
        // Cut back to `bound` once twice that many are held: amortized
        // linear, and memory stays O(bound).
        if self.rows.len() >= bound.saturating_mul(2).max(64) {
            self.cut_to(bound);
        }
    }

    /// Keep only the best `bound` rows (in no particular order) and
    /// remember the worst of them as the cutoff.
    fn cut_to(&mut self, bound: usize) {
        if self.rows.len() <= bound {
            return;
        }
        let mut order: Vec<usize> = (0..self.rows.len()).collect();
        order.select_nth_unstable_by(bound - 1, |&a, &b| self.cmp_slots(a, b));
        let mut keep = vec![false; self.rows.len()];
        for &slot in &order[..bound] {
            keep[slot] = true;
        }
        let n = self.items.len();
        let worst = order[bound - 1];
        self.cutoff = Some(self.keys[worst * n..(worst + 1) * n].to_vec());

        let mut slot = 0;
        self.rows.retain(|_| {
            slot += 1;
            keep[slot - 1]
        });
        let mut slot = 0;
        self.seqs.retain(|_| {
            slot += 1;
            keep[slot - 1]
        });
        let mut i = 0;
        self.keys.retain(|_| {
            i += 1;
            keep[(i - 1) / n]
        });
    }

    /// The kept rows, in order.
    pub(crate) fn finish(mut self) -> Vec<Row> {
        if let Some(bound) = self.bound {
            if bound == 0 {
                return Vec::new();
            }
            self.cut_to(bound);
        }
        let len = self.rows.len();
        if len < 2 {
            return self.rows;
        }
        let mut order: Vec<usize> = (0..len).collect();
        order.sort_unstable_by(|&a, &b| self.cmp_slots(a, b));
        // Already in order (common when rows arrive sorted): no moves.
        if order.iter().enumerate().all(|(i, &slot)| i == slot) {
            return self.rows;
        }
        let mut rows = self.rows;
        order
            .into_iter()
            .map(|slot| std::mem::take(&mut rows[slot]))
            .collect()
    }

    /// Row order of two slots: keys, then arrival.
    fn cmp_slots(&self, a: usize, b: usize) -> Ordering {
        let n = self.items.len();
        compare_keys(
            self.items,
            &self.keys[a * n..(a + 1) * n],
            &self.keys[b * n..(b + 1) * n],
        )
        .then_with(|| {
            if self.seqs.is_empty() {
                a.cmp(&b)
            } else {
                self.seqs[a].cmp(&self.seqs[b])
            }
        })
    }
}

fn compare_keys(items: &[ResolvedSortItem], a: &[LoraValue], b: &[LoraValue]) -> Ordering {
    for ((item, av), bv) in items.iter().zip(a).zip(b) {
        let ord = compare_values_for_sort(av, bv, matches!(item.direction, SortDirection::Asc));
        if ord != Ordering::Equal {
            return ord;
        }
    }
    Ordering::Equal
}

fn compare_values_for_sort(a: &LoraValue, b: &LoraValue, ascending: bool) -> Ordering {
    let ord = match (a, b) {
        (LoraValue::Null, LoraValue::Null) => Ordering::Equal,
        (LoraValue::Null, _) => Ordering::Greater,
        (_, LoraValue::Null) => Ordering::Less,
        _ => compare_values_total(a, b),
    };

    if ascending {
        ord
    } else {
        ord.reverse()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use lora_analyzer::symbols::VarId;
    use lora_analyzer::ResolvedExpr;
    use lora_store::InMemoryGraph;
    use std::collections::BTreeMap;

    fn item(var: u32, direction: SortDirection) -> ResolvedSortItem {
        ResolvedSortItem {
            expr: ResolvedExpr::Variable(VarId(var)),
            direction,
        }
    }

    fn row(a: LoraValue, b: i64) -> Row {
        let mut r = Row::new();
        r.insert(VarId(0), a);
        r.insert(VarId(1), LoraValue::Int(b));
        r
    }

    fn values() -> Vec<LoraValue> {
        vec![
            LoraValue::Int(3),
            LoraValue::Null,
            LoraValue::String("b".into()),
            LoraValue::Float(1.5),
            LoraValue::Int(3),
            LoraValue::Bool(true),
            LoraValue::String("a".into()),
            LoraValue::Null,
            LoraValue::Int(-7),
            LoraValue::Float(3.0),
            LoraValue::Int(1),
            LoraValue::String("b".into()),
        ]
    }

    /// The reference: a stable full sort comparing freshly evaluated keys.
    fn reference(rows: &[Row], items: &[ResolvedSortItem]) -> Vec<(LoraValue, LoraValue)> {
        let mut rows = rows.to_vec();
        rows.sort_by(|a, b| {
            let ka: Vec<LoraValue> = items
                .iter()
                .map(|i| {
                    a.get(match &i.expr {
                        ResolvedExpr::Variable(v) => *v,
                        _ => unreachable!(),
                    })
                    .cloned()
                    .unwrap()
                })
                .collect();
            let kb: Vec<LoraValue> = items
                .iter()
                .map(|i| {
                    b.get(match &i.expr {
                        ResolvedExpr::Variable(v) => *v,
                        _ => unreachable!(),
                    })
                    .cloned()
                    .unwrap()
                })
                .collect();
            compare_keys(items, &ka, &kb)
        });
        rows.iter().map(project).collect()
    }

    fn project(r: &Row) -> (LoraValue, LoraValue) {
        (
            r.get(VarId(0)).cloned().unwrap(),
            r.get(VarId(1)).cloned().unwrap(),
        )
    }

    #[test]
    fn bounded_sort_matches_stable_sort_prefix() {
        let graph = InMemoryGraph::new();
        let params = BTreeMap::new();
        let eval_ctx = EvalContext {
            storage: &graph,
            params: &params,
        };
        let rows: Vec<Row> = values()
            .into_iter()
            .enumerate()
            .map(|(i, v)| row(v, i as i64))
            .collect();
        for items in [
            vec![item(0, SortDirection::Asc)],
            vec![item(0, SortDirection::Desc)],
            vec![item(0, SortDirection::Asc), item(1, SortDirection::Desc)],
        ] {
            let expected = reference(&rows, &items);
            for k in 0..=rows.len() + 2 {
                let mut got = rows.clone();
                sort_rows_with_top_k(&mut got, &items, &eval_ctx, Some(k));
                let got: Vec<_> = got.iter().map(project).collect();
                assert_eq!(got, expected[..k.min(expected.len())], "k={k}");
            }
            let mut got = rows.clone();
            sort_rows_with_top_k(&mut got, &items, &eval_ctx, None);
            let got: Vec<_> = got.iter().map(project).collect();
            assert_eq!(got, expected);
        }
    }

    #[test]
    fn streamed_bound_cuts_match_stable_sort_prefix() {
        let graph = InMemoryGraph::new();
        let params = BTreeMap::new();
        let eval_ctx = EvalContext {
            storage: &graph,
            params: &params,
        };
        // Many ties and some nulls, in scrambled order.
        let rows: Vec<Row> = (0..700i64)
            .map(|i| {
                let j = (i * 7919) % 700;
                let a = match j % 11 {
                    0 => LoraValue::Null,
                    1 => LoraValue::Float((j % 5) as f64 + 0.5),
                    _ => LoraValue::Int(j % 17),
                };
                row(a, i)
            })
            .collect();
        for items in [
            vec![item(0, SortDirection::Asc)],
            vec![item(0, SortDirection::Desc)],
        ] {
            let expected = reference(&rows, &items);
            for k in [0, 1, 5, 31, 32, 33, 100, 350, 699, 700, 900] {
                let mut buffer = SortBuffer::new(&items, Some(k));
                for r in rows.iter().cloned() {
                    buffer.push(r, &eval_ctx);
                }
                let got: Vec<_> = buffer.finish().iter().map(project).collect();
                assert_eq!(got, expected[..k.min(expected.len())], "streamed k={k}");
                let mut got = rows.clone();
                sort_rows_with_top_k(&mut got, &items, &eval_ctx, Some(k));
                let got: Vec<_> = got.iter().map(project).collect();
                assert_eq!(got, expected[..k.min(expected.len())], "buffered k={k}");
            }
        }
    }
}