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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! PhysicalPlan → PlanDescription conversion for EXPLAIN.
//!
//! Step 2.2: The EXPLAIN statement now uses the arena PhysicalPlan as its
//! data source instead of the old ExecutionPlan / PlanNodeEnum tree.
//! This function walks the PhysicalPlan's operator arena and fragment DAG
//! to produce a PlanDescription compatible with the existing formatters.

use std::collections::{HashMap, HashSet};

use crate::executor::streaming::plan::types::{
    FragmentGraph, FragmentId, PhysicalOperatorId, PhysicalPlan,
};
use crate::planning::plan::explain::{Pair, PlanDescription, PlanNodeDescription};

/// Flat bypass columns (`{var}.{prop}`) carried by a rowless expand hop, in
/// layout order (edge properties first, then destination properties).
fn expand_bypass_display(
    col_names: &[String],
    edge_props: &Option<Vec<String>>,
    dst_props: &Option<Vec<String>>,
) -> Vec<String> {
    let Some(edge_var) = col_names.get(1) else {
        return Vec::new();
    };
    let Some(dst_var) = col_names.get(2) else {
        return Vec::new();
    };
    let mut out = Vec::new();
    if let Some(props) = edge_props {
        let mut sorted = props.clone();
        sorted.sort();
        sorted.dedup();
        out.extend(sorted.iter().map(|prop| format!("{edge_var}.{prop}")));
    }
    if let Some(props) = dst_props {
        let mut sorted = props.clone();
        sorted.sort();
        sorted.dedup();
        out.extend(sorted.iter().map(|prop| format!("{dst_var}.{prop}")));
    }
    out
}

/// Build a [`PlanDescription`] from an arena [`PhysicalPlan`].
///
/// Walks the fragment DAG in topological order (producers before consumers)
/// and creates one [`PlanNodeDescription`] per physical operator.
pub fn physical_plan_to_plan_description(plan: &PhysicalPlan) -> PlanDescription {
    let mut desc = PlanDescription::new();
    desc.requested_workers = 1;
    desc.parallel_fallback_reason = plan.parallel_fallback_reason.clone();
    desc.cbo_notes = plan.cbo_notes.clone();
    desc.statistics_summary = plan.statistics_summary.clone();
    if let Some(spec) = plan.partition_spec() {
        let version = spec
            .layout_version()
            .map(|v| format!(", layout_version={v}"))
            .unwrap_or_default();
        desc.partition_spec_description = Some(match spec.strategy() {
            crate::planning::plan::PartitionStrategy::Range => {
                let ranges = spec
                    .ranges()
                    .iter()
                    .map(|r| format!("[{}..{})", r.start, r.end))
                    .collect::<Vec<_>>()
                    .join(", ");
                format!(
                    "{} partitioned into {} ranges [{}]{}",
                    spec.source(),
                    spec.partition_count(),
                    ranges,
                    version
                )
            }
            crate::planning::plan::PartitionStrategy::Hash { key } => {
                let key_desc = if key.is_empty() {
                    String::new()
                } else {
                    format!(" by key '{key}'")
                };
                format!(
                    "{} hash-partitioned into {} buckets{}{}",
                    spec.source(),
                    spec.partition_count(),
                    key_desc,
                    version
                )
            }
            crate::planning::plan::PartitionStrategy::RoundRobin => {
                format!(
                    "{} round-robin partitioned into {} buckets{}",
                    spec.source(),
                    spec.partition_count(),
                    version
                )
            }
        });
    }

    // Build a reverse dependency map: operator_id → list of operator ids that depend on it.
    // Also track which fragment each operator belongs to.
    let topo = topological_operator_order(&plan.fragments, plan);

    let mut operator_to_fragment: HashMap<PhysicalOperatorId, FragmentId> = HashMap::new();
    for frag in plan.fragments.fragments() {
        for op_id in &frag.operators {
            operator_to_fragment.insert(*op_id, frag.id);
        }
    }

    // parent_of[b] = a means "a depends on b" → b is a child of a in the output DAG.
    // We build this by following the fragment graph.
    let mut parent_of: HashMap<PhysicalOperatorId, Vec<PhysicalOperatorId>> = HashMap::new();

    // Within each fragment, operators form a pipeline (listed leaf→root).
    for frag in plan.fragments.fragments() {
        for (i, op_id) in frag.operators.iter().enumerate() {
            if i > 0 {
                // op[i] depends on op[i-1] within the same fragment
                parent_of
                    .entry(frag.operators[i - 1])
                    .or_default()
                    .push(*op_id);
            }
        }
        // The first operator in a fragment depends on the root operators of input fragments.
        if let Some(first_op) = frag.operators.first() {
            for input_fid in &frag.inputs {
                if let Some(input_frag) = plan.fragments.get(*input_fid) {
                    parent_of
                        .entry(input_frag.root_operator)
                        .or_default()
                        .push(*first_op);
                }
            }
        }
    }

    // Collect all operator IDs in topological order (producers first).
    let mut desc_ids: HashSet<PhysicalOperatorId> = HashSet::new();
    let mut ordered_ops: Vec<PhysicalOperatorId> = Vec::new();
    for &fid in &topo {
        if let Some(frag) = plan.fragments.get(fid) {
            for op_id in &frag.operators {
                if desc_ids.insert(*op_id) {
                    ordered_ops.push(*op_id);
                }
            }
        }
    }

    for &op_id in &ordered_ops {
        let op_spec = match plan.operator(op_id) {
            Some(s) => s,
            None => continue,
        };

        let mut pnd = PlanNodeDescription::new(op_spec.explain_name, op_id.0 as i64);

        // Extract description from properties
        let mut pairs = Vec::new();
        if let Some(est) = op_spec.estimated_cardinality {
            pairs.push(Pair::new("est_rows", est.to_string()));
        }
        if let Some(reason) = &op_spec.choice_reason {
            pairs.push(Pair::new("reason", reason.clone()));
        }
        // Constant folding applied on the source logical node.
        if op_spec.has_folded_expressions {
            pairs.push(Pair::new("folded", "true"));
        }

        let props = &op_spec.properties;
        if let crate::executor::streaming::plan::properties::Ordering::Sorted(orders) =
            &props.ordering
        {
            let order_str = orders
                .iter()
                .map(|o| {
                    format!(
                        "slot#{} {}",
                        o.slot,
                        if o.ascending { "ASC" } else { "DESC" }
                    )
                })
                .collect::<Vec<_>>()
                .join(", ");
            if !order_str.is_empty() {
                pairs.push(Pair::new("ordering", order_str));
            }
        }

        if let crate::executor::streaming::plan::properties::Distribution::HashPartitioned(keys) =
            &props.distribution
        {
            if !keys.is_empty() {
                pairs.push(Pair::new(
                    "hash_slots",
                    keys.iter()
                        .map(|s| format!("slot#{}", s))
                        .collect::<Vec<_>>()
                        .join(", "),
                ));
            }
        }

        if props.pipeline_kind
            == crate::executor::streaming::plan::properties::PipelineKind::Blocking
        {
            pairs.push(Pair::new("blocking", "true"));
        }

        // PatternApply subquery keys: hash_keys evaluate against the outer
        // (left) layout, probe_keys against the subquery (right) layout.
        if let crate::executor::streaming::plan::types::OperatorKindSpec::Apply(
            crate::executor::streaming::operators::spec::ApplySpec::PatternApply {
                hash_keys,
                probe_keys,
                anti,
            },
        ) = &op_spec.spec
        {
            if !hash_keys.is_empty() {
                pairs.push(Pair::new(
                    "hash_keys",
                    hash_keys
                        .iter()
                        .map(linkrs_core::types::expr::Expression::to_expression_string)
                        .collect::<Vec<_>>()
                        .join(", "),
                ));
            }
            if !probe_keys.is_empty() {
                pairs.push(Pair::new(
                    "probe_keys",
                    probe_keys
                        .iter()
                        .map(linkrs_core::types::expr::Expression::to_expression_string)
                        .collect::<Vec<_>>()
                        .join(", "),
                ));
            }
            if *anti {
                pairs.push(Pair::new("anti", "true"));
            }
        }

        // CorrelatedApply: the right subtree is re-executed per outer row.
        if let crate::executor::streaming::plan::types::OperatorKindSpec::Apply(
            crate::executor::streaming::operators::spec::ApplySpec::CorrelatedApply {
                anti, ..
            },
        ) = &op_spec.spec
        {
            if *anti {
                pairs.push(Pair::new("anti", "true"));
            }
        }

        // SemiJoin (Mark-Join): surface the residual join condition and the
        // anti (NOT EXISTS) semantics.
        if let crate::executor::streaming::plan::types::OperatorKindSpec::Join(
            crate::executor::streaming::operators::spec::JoinSpec::SemiJoin {
                join_condition,
                anti,
            },
        ) = &op_spec.spec
        {
            if let Some(condition) = join_condition {
                pairs.push(Pair::new(
                    "join_condition",
                    condition.to_expression_string(),
                ));
            }
            if *anti {
                pairs.push(Pair::new("anti", "true"));
            }
        }

        // Expression-level subqueries: Filter/Project/Assign hosts
        // surface the number of compiled subqueries as `subquery: N`.
        if let crate::executor::streaming::plan::types::OperatorKindSpec::Unary(unary_spec) =
            &op_spec.spec
        {
            let count = unary_spec.subquery_runners().len();
            if count > 0 {
                pairs.push(Pair::new("subquery", count.to_string()));
            }
        }

        if let crate::executor::streaming::plan::properties::MemoryPolicy::Spillable { threshold } =
            &props.memory_policy
        {
            pairs.push(Pair::new("spill_threshold", format!("{}", threshold)));
        }

        // Annotated ExpandAll hops surface their de-materialization mode.
        // The columnar marker reflects the planner closed-loop claim; the
        // executor re-verifies it against storage schemas per chunk and falls
        // back to the row path on mismatch.
        if let crate::executor::streaming::plan::types::OperatorKindSpec::Graph(
            crate::executor::streaming::operators::spec::GraphSpec::ExpandAll {
                count_only,
                emit_raw_ids,
                closed_loop,
                skip_rows,
                col_names,
                edge_required_props,
                dst_required_props,
                ..
            },
        ) = &op_spec.spec
        {
            if *count_only {
                pairs.push(crate::planning::plan::explain::Pair::new(
                    "mode",
                    "count_only",
                ));
                // A count-only expand emits one partial-count row per input
                // chunk: its estimate counts chunks, not edges.
                pairs.push(crate::planning::plan::explain::Pair::new("unit", "chunks"));
            } else if *emit_raw_ids {
                pairs.push(crate::planning::plan::explain::Pair::new("mode", "id_only"));
            } else if *closed_loop {
                pairs.push(crate::planning::plan::explain::Pair::new(
                    "mode", "columnar",
                ));
                if *skip_rows {
                    pairs.push(crate::planning::plan::explain::Pair::new("rows", "skipped"));
                    let bypass =
                        expand_bypass_display(col_names, edge_required_props, dst_required_props);
                    if !bypass.is_empty() {
                        pairs.push(crate::planning::plan::explain::Pair::new(
                            "bypass",
                            bypass.join(","),
                        ));
                    }
                }
            }
        }

        // Try to extract output column names from SourceSpec
        if let crate::executor::streaming::plan::types::OperatorKindSpec::Source(src_spec) =
            &op_spec.spec
        {
            let col_names: Vec<String> = match src_spec {
                crate::executor::streaming::operators::spec::SourceSpec::ScanVertices {
                    col_names,
                    ..
                }
                | crate::executor::streaming::operators::spec::SourceSpec::StandaloneValues {
                    col_names,
                    ..
                } => col_names.clone(),
                crate::executor::streaming::operators::spec::SourceSpec::StorageScanVertices {
                    col_names,
                    identity_only,
                    skip_rows,
                    ..
                } => {
                    if *identity_only {
                        pairs.push(crate::planning::plan::explain::Pair::new(
                            "mode", "identity",
                        ));
                    }
                    if *skip_rows {
                        pairs.push(crate::planning::plan::explain::Pair::new("rows", "skipped"));
                    }
                    col_names.clone()
                }
                crate::executor::streaming::operators::spec::SourceSpec::ScanEdges {
                    col_names,
                    ..
                } => col_names.clone(),
                crate::executor::streaming::operators::spec::SourceSpec::StorageScanEdges {
                    col_names,
                    identity_only,
                    ..
                } => {
                    if *identity_only {
                        pairs.push(crate::planning::plan::explain::Pair::new(
                            "mode", "identity",
                        ));
                    }
                    col_names.clone()
                }
                _ => vec![],
            };
            if !col_names.is_empty() {
                pnd.output_var = col_names.join(", ");
            }
            // Surface the projected property list for storage scans and
            // graph-operator sources so the property-pruning optimizations
            // (EnrichScanSlotsWithFilterProps, typed GetVertices/GetNeighbors
            // pushdown) are observable in EXPLAIN output.
            let projected: Vec<String> = match src_spec {
                crate::executor::streaming::operators::spec::SourceSpec::StorageScanVertices {
                    projected_properties,
                    ..
                }
                | crate::executor::streaming::operators::spec::SourceSpec::StorageScanEdges {
                    projected_properties,
                    ..
                }
                | crate::executor::streaming::operators::spec::SourceSpec::GetVertices {
                    projected_properties,
                    ..
                }
                | crate::executor::streaming::operators::spec::SourceSpec::GetEdges {
                    projected_properties,
                    ..
                }
                | crate::executor::streaming::operators::spec::SourceSpec::GetNeighbors {
                    projected_properties,
                    ..
                } => projected_properties.clone(),
                _ => vec![],
            };
            if !projected.is_empty() {
                pairs.push(crate::planning::plan::explain::Pair::new(
                    "projected",
                    projected.join(","),
                ));
            }
        }

        if !pairs.is_empty() {
            pnd.description = Some(pairs);
        }

        // Dependencies
        if let Some(deps) = parent_of.get(&op_id) {
            let dep_ids: Vec<i64> = deps.iter().map(|d| d.0 as i64).collect();
            pnd.set_dependencies(dep_ids);
        }

        desc.add_node_desc(pnd);
    }

    desc
}

/// Return fragment IDs in topological order (sources first, root last).
fn topological_operator_order(fragments: &FragmentGraph, _plan: &PhysicalPlan) -> Vec<FragmentId> {
    let root = fragments.root();
    let mut order = Vec::new();
    let mut visited = HashSet::new();
    let mut stack = vec![root];

    while let Some(fid) = stack.pop() {
        if !visited.insert(fid) {
            continue;
        }
        if let Some(frag) = fragments.get(fid) {
            for &input in &frag.inputs {
                stack.push(input);
            }
            order.push(fid);
        }
    }

    order.reverse();
    order
}