linkrs-query 0.1.0

Query engine, parser, and executor for Linkrs
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
//! Typed required-property demand analyzer.

use crate::planning::plan::core::nodes::base::plan_node_traits::{
    MultipleInputNode, SingleInputNode,
};
use crate::planning::plan::PlanNodeEnum;
use linkrs_core::types::ContextualExpression;
use linkrs_core::Expression;

use super::map::RequiredPropertiesMap;
use super::ref_collector::CollectedRefs;
use super::requirement::PropertyRequirement;

/// Typed required-property demand analyzer.
#[derive(Debug, Clone, Default)]
pub struct RequiredPropertyAnalyzer;

impl RequiredPropertyAnalyzer {
    pub fn new() -> Self {
        Self
    }

    /// Analyze a plan tree and return the per-node requirement map.
    ///
    /// The traversal is top-down: requirements accumulated from consumers
    /// are merged with each node's own expression references and passed to
    /// the node's children. Graph sources record the requirement for their
    /// binding variable, tagged with the resolved tag / edge type when the
    /// binding is single-typed.
    pub fn analyze(&self, root: &PlanNodeEnum) -> RequiredPropertiesMap {
        let mut map = RequiredPropertiesMap::default();
        self.propagate(root, &[], &mut map);
        map
    }

    /// Propagate requirements down a plan subtree.
    fn propagate(
        &self,
        node: &PlanNodeEnum,
        incoming: &[PropertyRequirement],
        map: &mut RequiredPropertiesMap,
    ) {
        use PlanNodeEnum::*;

        // Leaf graph sources: resolve the binding variable, tag the
        // requirements that match it, and stop.
        match node {
            GetVertices(n) => {
                self.record_leaf(
                    map,
                    node.id(),
                    incoming,
                    binding_var(n.output_var(), n.src_vids()),
                    single_tag(n.tag_props()),
                );
                return;
            }
            GetNeighbors(n) => {
                self.record_leaf(
                    map,
                    node.id(),
                    incoming,
                    binding_var(n.output_var(), n.src_vids()),
                    single_tag(n.tag_props()),
                );
                return;
            }
            ScanVertices(n) => {
                self.record_leaf(
                    map,
                    node.id(),
                    incoming,
                    n.col_names().first().map(String::as_str),
                    n.tag().map(|t| t.to_string()),
                );
                return;
            }
            ScanEdges(n) => {
                self.record_leaf(
                    map,
                    node.id(),
                    incoming,
                    n.col_names().first().map(String::as_str),
                    n.edge_type().map(|e| e.to_string()),
                );
                return;
            }
            GetEdges(n) => {
                self.record_leaf(
                    map,
                    node.id(),
                    incoming,
                    Some(n.src()),
                    Some(n.edge_type().to_string()),
                );
                return;
            }
            AppendVertices(n) => {
                self.record_leaf(
                    map,
                    node.id(),
                    incoming,
                    n.input_var(),
                    Some(n.vertex_tag().to_string()),
                );
                return;
            }
            IndexScan(_) | Start(_) | Argument(_) => {
                map.record(node.id(), incoming);
                return;
            }
            _ => {}
        }

        // Intermediate nodes: merge the node's own expression references
        // into the requirements passed down. The node's incoming
        // requirements are recorded as-is (what its consumers demand).
        map.record(node.id(), incoming);
        let mut passed: Vec<PropertyRequirement> = Vec::new();
        for req in incoming {
            passed.push(req.clone());
        }

        match node {
            Project(n) => {
                for col in n.columns() {
                    if let Some(meta) = col.expression.expression() {
                        merge_collected(meta.inner(), &mut passed);
                    }
                }
                // A bare-variable column passes the incoming requirements
                // for that variable through to the input unchanged.
                for col in n.columns() {
                    if let Some(meta) = col.expression.expression() {
                        if let Expression::Variable(var) = meta.inner() {
                            if let Some(req) = incoming.iter().find(|r| r.alias == *var) {
                                merge_requirement(&mut passed, req);
                            }
                        }
                    }
                }
                self.propagate(n.input(), &passed, map);
            }
            Filter(n) => {
                merge_expr(n.condition(), &mut passed);
                self.propagate(n.input(), &passed, map);
            }
            Sort(n) => self.propagate_single(n.input(), &passed, map),
            Limit(n) => self.propagate_single(n.input(), &passed, map),
            TopN(n) => self.propagate_single(n.input(), &passed, map),
            Sample(n) => self.propagate_single(n.input(), &passed, map),
            Dedup(n) => self.propagate_single(n.input(), &passed, map),
            Aggregate(n) => self.propagate_single(n.input(), &passed, map),
            Window(n) => self.propagate_single(n.input(), &passed, map),
            Unwind(n) => self.propagate_single(n.input(), &passed, map),
            Materialize(n) => self.propagate_single(n.input(), &passed, map),
            DataCollect(n) => self.propagate_single(n.input(), &passed, map),

            // Binary join nodes: the join key expressions may reference
            // properties; both inputs receive the full requirement set and
            // each leaf keeps only the entries matching its binding.
            InnerJoin(n) => {
                Self::merge_join_keys(n.hash_keys(), n.probe_keys(), &mut passed);
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }
            LeftJoin(n) => {
                Self::merge_join_keys(n.hash_keys(), n.probe_keys(), &mut passed);
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }
            FullOuterJoin(n) => {
                Self::merge_join_keys(n.hash_keys(), n.probe_keys(), &mut passed);
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }
            RightJoin(n) => {
                Self::merge_join_keys(n.hash_keys(), n.probe_keys(), &mut passed);
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }
            SemiJoin(n) => {
                Self::merge_join_keys(n.hash_keys(), n.probe_keys(), &mut passed);
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }
            CrossJoin(n) => {
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }

            // Set operations: every input provides the same columns.
            Union(n) => {
                for dep in n.dependencies() {
                    self.propagate(dep, &passed, map);
                }
            }
            Minus(n) => {
                self.propagate(n.input(), &passed, map);
                self.propagate(n.minus_input(), &passed, map);
            }
            Intersect(n) => {
                self.propagate(n.input(), &passed, map);
                self.propagate(n.intersect_input(), &passed, map);
            }

            // Traversal nodes pass requirements through to their inputs.
            Expand(n) => {
                for dep in n.inputs() {
                    self.propagate(dep, &passed, map);
                }
            }
            ExpandAll(n) => {
                for dep in n.inputs() {
                    self.propagate(dep, &passed, map);
                }
            }
            Traverse(n) => {
                for dep in n.dependencies() {
                    self.propagate(dep, &passed, map);
                }
            }
            BiExpand(n) => {
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }
            BiTraverse(n) => {
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }

            // Pattern apply: the left input is the main pipeline; the right
            // input is the subquery pattern (correlated columns are part of
            // the subquery's own expressions).
            PatternApply(n) => {
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &[], map);
            }
            CorrelatedApply(n) => {
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &[], map);
            }
            RollUpApply(n) => {
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &[], map);
            }

            // Control flow nodes: propagate into every branch.
            Loop(n) => {
                if let Some(body) = n.body() {
                    self.propagate(body, &passed, map);
                }
            }
            Select(n) => {
                if let Some(branch) = n.if_branch() {
                    self.propagate(branch, &passed, map);
                }
                if let Some(branch) = n.else_branch() {
                    self.propagate(branch, &passed, map);
                }
            }

            // Algorithm nodes: pass requirements into their inputs.
            ShortestPath(n) => {
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }
            MultiShortestPath(n) => {
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }
            BFSShortest(n) => {
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }
            AllPaths(n) => {
                self.propagate(n.left_input(), &passed, map);
                self.propagate(n.right_input(), &passed, map);
            }

            // Data processing nodes.
            Assign(n) => {
                for dep in n.dependencies() {
                    self.propagate(dep, &passed, map);
                }
            }

            // Remaining nodes have no property-consuming expressions and no
            // graph source children; requirements stop here.
            _ => {}
        }
    }

    fn propagate_single(
        &self,
        input: &PlanNodeEnum,
        passed: &[PropertyRequirement],
        map: &mut RequiredPropertiesMap,
    ) {
        self.propagate(input, passed, map);
    }

    /// Merge the property references of join hash/probe keys into the
    /// requirement list so keyed columns are never pruned away.
    fn merge_join_keys(
        hash_keys: &[ContextualExpression],
        probe_keys: &[ContextualExpression],
        passed: &mut Vec<PropertyRequirement>,
    ) {
        for key in hash_keys.iter().chain(probe_keys) {
            merge_expr(key, passed);
        }
    }

    /// Record the requirements demanded of a leaf graph source, tagged with
    /// the binding's resolved tag / edge type.
    fn record_leaf(
        &self,
        map: &mut RequiredPropertiesMap,
        node_id: i64,
        incoming: &[PropertyRequirement],
        binding_var: Option<&str>,
        tag_name: Option<String>,
    ) {
        let Some(var) = binding_var else {
            return;
        };
        let mut resolved: Vec<PropertyRequirement> = Vec::new();
        for req in incoming {
            if req.alias == var {
                let mut tagged = req.clone();
                tagged.tag_name = tag_name.clone();
                resolved.push(tagged);
            } else if let Some(tag) = tag_name.as_deref() {
                // `TagProperty{tag, prop}` / `EdgeProperty{edge, prop}` references
                // are collected under the tag/edge name; remap them onto the
                // binding variable when the names match so scans can narrow.
                // `full_value` stays sticky: an opaque use still blocks pruning.
                if req.alias == tag {
                    let mut remapped = req.clone();
                    remapped.alias = var.to_string();
                    remapped.tag_name = tag_name.clone();
                    resolved.push(remapped);
                }
            }
        }
        if !resolved.is_empty() {
            map.record(node_id, &resolved);
        }
    }
}

/// The binding variable of a graph source, preferring the planner-assigned
/// output variable and falling back to a single non-constant `src_vids`.
///
/// Shared with the projection pushdown rules that narrow graph operator
/// `projected_properties`.
pub fn binding_var<'a>(output_var: Option<&'a str>, src_vids: &'a str) -> Option<&'a str> {
    if let Some(var) = output_var {
        if !var.is_empty() {
            return Some(var);
        }
    }
    let trimmed = src_vids.trim();
    if trimmed.is_empty() || trimmed.split(',').count() != 1 {
        return None;
    }
    // Constant vertex ids are numeric literals, not binding variables.
    if trimmed.parse::<i64>().is_ok() {
        return None;
    }
    Some(trimmed)
}

/// The single tag of a `TagProp` list, or `None` for multi-tag bindings.
fn single_tag(tag_props: &[crate::planning::plan::core::common::TagProp]) -> Option<String> {
    let mut tags = tag_props
        .iter()
        .map(|tp| tp.tag.clone())
        .collect::<Vec<_>>();
    tags.sort();
    tags.dedup();
    if tags.len() == 1 {
        tags.pop()
    } else {
        None
    }
}

/// Merge the collected references of `expr` into the requirement list,
/// tracking which variables need their full value.
fn merge_collected(expr: &Expression, passed: &mut Vec<PropertyRequirement>) {
    let collected = CollectedRefs::visit(expr);
    for (var, prop) in collected.props {
        merge_property(passed, &var, &prop);
    }
    for var in collected.full_vars {
        // A full-value variable suppresses pruning: the requirement flag is
        // sticky, so later property references cannot re-open the binding.
        merge_property(passed, &var, "");
    }
}

/// Merge the property references of a context expression into the
/// requirement list.
fn merge_expr(expr: &ContextualExpression, passed: &mut Vec<PropertyRequirement>) {
    let Some(meta) = expr.expression() else {
        return;
    };
    merge_collected(meta.inner(), passed);
}

/// Add (or merge into) the requirement for `var` and property `prop`.
///
/// An empty `prop` marks the variable as full-value (non-narrowable).
fn merge_property(passed: &mut Vec<PropertyRequirement>, var: &str, prop: &str) {
    if let Some(req) = passed.iter_mut().find(|r| r.alias == var) {
        if prop.is_empty() {
            req.full_value = true;
        } else {
            req.prop_names.insert(prop.to_string());
        }
    } else {
        let mut req = PropertyRequirement::new(var);
        if prop.is_empty() {
            req.full_value = true;
        } else {
            req.prop_names.insert(prop.to_string());
        }
        passed.push(req);
    }
}

/// Merge a whole requirement into the list.
fn merge_requirement(passed: &mut Vec<PropertyRequirement>, req: &PropertyRequirement) {
    match passed.iter_mut().find(|r| r.alias == req.alias) {
        Some(existing) => existing.merge(req),
        None => passed.push(req.clone()),
    }
}