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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! Image traversal direction optimizer module
//!
//! Select the optimal traversal direction (forward or backward) based on edge statistics.
//!
//! ## Optimization Strategies
//!
//! Choose the direction with a smaller degree value in order to reduce the intermediate results.
//! Consider the impact of super nodes.
//! Support for cost-based direction selection
//!
//! ## Usage Examples
//!
//! ```rust
//! use linkrs_query::optimizer::cost::CostCalculator;
//! use linkrs_query::optimizer::cost_based::TraversalDirectionOptimizer;
//! use linkrs_query::optimizer::stats::StatisticsManager;
//! use std::sync::Arc;
//!
//! let stats_manager = Arc::new(StatisticsManager::new());
//! let cost_calculator = Arc::new(CostCalculator::new(stats_manager));
//! let optimizer = TraversalDirectionOptimizer::new(cost_calculator);
//!
//! // let decision = optimizer.optimize_direction("KNOWS", None);
//! ```

use std::sync::Arc;

use crate::optimizer::cost::config::DEFAULT_DEGREE_FALLBACK;
use crate::optimizer::cost::CostCalculator;
use crate::optimizer::stats::EdgeTypeStatistics;
use linkrs_core::types::EdgeDirection;

/// Decision on the direction of traversal
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TraversalDirection {
    /// Forward traversal (outward direction)
    /// Traverse from the source vertex to the target vertex.
    Forward,
    /// Reverse traversal (inward direction)
    /// Traverse from the target vertex to the source vertex.
    Backward,
    /// Bidirectional traversal
    /// Consider both directions at the same time.
    Bidirectional,
}

impl TraversalDirection {
    /// Obtain the name of the direction
    pub fn name(&self) -> &'static str {
        match self {
            TraversalDirection::Forward => "Forward",
            TraversalDirection::Backward => "Backward",
            TraversalDirection::Bidirectional => "Bidirectional",
        }
    }

    /// Convert to EdgeDirection
    pub fn to_edge_direction(&self) -> EdgeDirection {
        match self {
            TraversalDirection::Forward => EdgeDirection::Out,
            TraversalDirection::Backward => EdgeDirection::In,
            TraversalDirection::Bidirectional => EdgeDirection::Both,
        }
    }

    /// Conversion from EdgeDirection
    pub fn from_edge_direction(direction: &EdgeDirection) -> Self {
        match direction {
            EdgeDirection::Out => TraversalDirection::Forward,
            EdgeDirection::In => TraversalDirection::Backward,
            EdgeDirection::Both => TraversalDirection::Bidirectional,
        }
    }
}

/// Reasons for the choice of direction
#[derive(Debug, Clone)]
pub enum DirectionSelectionReason {
    /// Outdegree is less than indegree.
    OutDegreeLower { out_degree: f64, in_degree: f64 },
    /// The in-degree is less than the out-degree.
    InDegreeLower { in_degree: f64, out_degree: f64 },
    /// The degrees are equal or very similar.
    DegreesEqual { out_degree: f64, in_degree: f64 },
    /// Selection based on cost comparison
    CostBased {
        forward_cost: f64,
        backward_cost: f64,
    },
    /// Avoid using super nodes.
    AvoidSuperNode {
        super_node_direction: TraversalDirection,
        threshold: f64,
    },
    /// Statistical information is not available; the default orientation will be used.
    StatsUnavailable,
    /// Explicitly specifying the direction
    ExplicitDirection,
}

/// Decision on the traversal direction
#[derive(Debug, Clone)]
pub struct TraversalDirectionDecision {
    /// The chosen direction
    pub direction: TraversalDirection,
    /// Estimated number of output lines
    pub estimated_output_rows: u64,
    /// Estimated cost
    pub estimated_cost: f64,
    /// Reason for the choice
    pub reason: DirectionSelectionReason,
    /// Average degree (selected direction)
    pub avg_degree: f64,
    /// Does it involve super nodes?
    pub involves_super_node: bool,
}

/// Traversal Direction Optimizer
///
/// Selecting the optimal traversal direction based on edge-based statistical information
#[derive(Debug)]
pub struct TraversalDirectionOptimizer {
    cost_calculator: Arc<CostCalculator>,
    /// Space name used to look up statistics.
    space: String,
    /// Super Node Threshold (A node is considered a super node if its degree exceeds this value.)
    super_node_threshold: f64,
    /// Threshold for degree difference: Differences smaller than this value are considered equal.
    degree_equality_threshold: f64,
}

/// Direction optimization context
#[derive(Debug, Clone)]
pub struct DirectionContext {
    /// Edge type
    pub edge_type: String,
    /// Number of starting nodes
    pub start_nodes: u64,
    /// The explicitly specified direction (if any)
    pub explicit_direction: Option<TraversalDirection>,
    /// Is bidirectional traversal allowed?
    pub allow_bidirectional: bool,
    /// Number of iterations
    pub steps: u32,
}

impl TraversalDirectionOptimizer {
    /// Create a new optimizer for optimizing traversal directions.
    pub fn new(cost_calculator: Arc<CostCalculator>) -> Self {
        // Single super-node source: CostModelConfig::super_node_threshold.
        let thresholds = cost_calculator.config().strategy_thresholds;
        Self {
            cost_calculator: Arc::clone(&cost_calculator),
            space: String::new(),
            super_node_threshold: cost_calculator.config().super_node_threshold as f64,
            degree_equality_threshold: thresholds.bidirectional_savings_threshold,
        }
    }

    /// Set the space name used to look up statistics.
    pub fn with_space(mut self, space: &str) -> Self {
        self.space = space.to_string();
        self
    }

    /// Setting the threshold for super nodes
    pub fn with_super_node_threshold(mut self, threshold: f64) -> Self {
        self.super_node_threshold = threshold;
        self
    }

    /// Set a threshold for equal degrees
    pub fn with_equality_threshold(mut self, threshold: f64) -> Self {
        self.degree_equality_threshold = threshold;
        self
    }

    /// Optimize the direction of the traversal
    ///
    /// # Parameters
    /// “Direction optimization context”
    ///
    /// # Return
    /// Directional decision-making outcome
    pub fn optimize_direction(&self, context: &DirectionContext) -> TraversalDirectionDecision {
        // If a specific direction is explicitly specified, that direction should be given priority.
        if let Some(explicit) = context.explicit_direction {
            return self.create_explicit_decision(context, explicit);
        }

        // Obtain edge statistics information
        let stats = self
            .cost_calculator
            .statistics_manager()
            .get_edge_stats(&self.space, &context.edge_type);

        match stats {
            Some(edge_stats) => self.optimize_with_stats(context, &edge_stats),
            None => self.create_default_decision(context),
        }
    }

    /// Optimization directions based on statistical information
    fn optimize_with_stats(
        &self,
        context: &DirectionContext,
        stats: &EdgeTypeStatistics,
    ) -> TraversalDirectionDecision {
        let out_degree = stats.avg_out_degree;
        let in_degree = stats.avg_in_degree;

        // Check whether super nodes are involved.
        let forward_is_super = out_degree > self.super_node_threshold;
        let backward_is_super = in_degree > self.super_node_threshold;

        // If both directions involve super nodes, choose the one with the smaller degree (i.e., the one with fewer connections).
        if forward_is_super && backward_is_super {
            let direction = if out_degree <= in_degree {
                TraversalDirection::Forward
            } else {
                TraversalDirection::Backward
            };

            let avg_degree = if out_degree <= in_degree {
                out_degree
            } else {
                in_degree
            };

            return TraversalDirectionDecision {
                direction,
                estimated_output_rows: (context.start_nodes as f64 * avg_degree) as u64,
                estimated_cost: self.calculate_cost(context, true),
                reason: DirectionSelectionReason::AvoidSuperNode {
                    super_node_direction: if out_degree > in_degree {
                        TraversalDirection::Forward
                    } else {
                        TraversalDirection::Backward
                    },
                    threshold: self.super_node_threshold,
                },
                avg_degree,
                involves_super_node: true,
            };
        }

        // If there is only one direction in which the node is a super node, avoid that direction.
        if forward_is_super {
            return TraversalDirectionDecision {
                direction: TraversalDirection::Backward,
                estimated_output_rows: (context.start_nodes as f64 * in_degree) as u64,
                estimated_cost: self.calculate_cost(context, false),
                reason: DirectionSelectionReason::AvoidSuperNode {
                    super_node_direction: TraversalDirection::Forward,
                    threshold: self.super_node_threshold,
                },
                avg_degree: in_degree,
                involves_super_node: true,
            };
        }

        if backward_is_super {
            return TraversalDirectionDecision {
                direction: TraversalDirection::Forward,
                estimated_output_rows: (context.start_nodes as f64 * out_degree) as u64,
                estimated_cost: self.calculate_cost(context, false),
                reason: DirectionSelectionReason::AvoidSuperNode {
                    super_node_direction: TraversalDirection::Backward,
                    threshold: self.super_node_threshold,
                },
                avg_degree: out_degree,
                involves_super_node: true,
            };
        }

        // Choosing the direction based on the comparison of degrees
        let degree_diff = (out_degree - in_degree).abs();
        let degree_ratio = degree_diff / ((out_degree + in_degree) / 2.0);

        if degree_ratio < self.degree_equality_threshold {
            // The degrees are close; the choice is made based on cost calculations.
            self.select_by_cost(context, out_degree, in_degree)
        } else if out_degree < in_degree {
            // The degree of deviation is small; therefore, the positive option should be chosen.
            TraversalDirectionDecision {
                direction: TraversalDirection::Forward,
                estimated_output_rows: (context.start_nodes as f64 * out_degree) as u64,
                estimated_cost: self.calculate_cost(context, false),
                reason: DirectionSelectionReason::OutDegreeLower {
                    out_degree,
                    in_degree,
                },
                avg_degree: out_degree,
                involves_super_node: false,
            }
        } else {
            TraversalDirectionDecision {
                direction: TraversalDirection::Backward,
                estimated_output_rows: (context.start_nodes as f64 * in_degree) as u64,
                estimated_cost: self.calculate_cost(context, false),
                reason: DirectionSelectionReason::InDegreeLower {
                    in_degree,
                    out_degree,
                },
                avg_degree: in_degree,
                involves_super_node: false,
            }
        }
    }

    /// Choosing the direction based on cost
    fn select_by_cost(
        &self,
        context: &DirectionContext,
        out_degree: f64,
        in_degree: f64,
    ) -> TraversalDirectionDecision {
        // Calculate costs with respective degrees
        let forward_cost = self.calculate_cost_with_degree(context, out_degree);
        let backward_cost = self.calculate_cost_with_degree(context, in_degree);

        let (direction, avg_degree) = if forward_cost <= backward_cost {
            (TraversalDirection::Forward, out_degree)
        } else {
            (TraversalDirection::Backward, in_degree)
        };

        let involves_super_node =
            out_degree > self.super_node_threshold || in_degree > self.super_node_threshold;

        TraversalDirectionDecision {
            direction,
            estimated_output_rows: (context.start_nodes as f64 * avg_degree) as u64,
            estimated_cost: forward_cost.min(backward_cost),
            reason: DirectionSelectionReason::CostBased {
                forward_cost,
                backward_cost,
            },
            avg_degree,
            involves_super_node,
        }
    }

    /// Calculating the cost of traversal with a specific degree
    fn calculate_cost_with_degree(&self, context: &DirectionContext, degree: f64) -> f64 {
        let base_cost = self.cost_calculator.calculate_expand_cost(
            &self.space,
            context.start_nodes,
            Some(&context.edge_type),
        );

        // Apply penalty for high-degree (super node) scenarios
        let degree_factor = if degree > self.super_node_threshold {
            self.cost_calculator.config().super_node_penalty
        } else {
            // Gradual increase in cost based on degree ratio
            1.0 + (degree / self.super_node_threshold).ln_1p()
        };

        base_cost * degree_factor
    }

    /// Calculating the cost of traversal
    fn calculate_cost(&self, context: &DirectionContext, is_super: bool) -> f64 {
        let base_cost = self.cost_calculator.calculate_expand_cost(
            &self.space,
            context.start_nodes,
            Some(&context.edge_type),
        );

        if is_super {
            base_cost * self.cost_calculator.config().super_node_penalty
        } else {
            base_cost
        }
    }

    /// Making decisions that involve a clear direction
    fn create_explicit_decision(
        &self,
        context: &DirectionContext,
        direction: TraversalDirection,
    ) -> TraversalDirectionDecision {
        // Try to obtain statistical information.
        let avg_degree = self
            .cost_calculator
            .statistics_manager()
            .get_edge_stats(&self.space, &context.edge_type)
            .map(|s| match direction {
                TraversalDirection::Forward => s.avg_out_degree,
                TraversalDirection::Backward => s.avg_in_degree,
                TraversalDirection::Bidirectional => (s.avg_out_degree + s.avg_in_degree) / 2.0,
            })
            .unwrap_or(DEFAULT_DEGREE_FALLBACK);

        let is_super = avg_degree > self.super_node_threshold;

        TraversalDirectionDecision {
            direction,
            estimated_output_rows: (context.start_nodes as f64 * avg_degree) as u64,
            estimated_cost: self.calculate_cost(context, is_super),
            reason: DirectionSelectionReason::ExplicitDirection,
            avg_degree,
            involves_super_node: is_super,
        }
    }

    /// Create a default decision (statistical information not available).
    fn create_default_decision(&self, context: &DirectionContext) -> TraversalDirectionDecision {
        let default_degree = DEFAULT_DEGREE_FALLBACK;

        TraversalDirectionDecision {
            direction: TraversalDirection::Forward, // Default forward direction
            estimated_output_rows: (context.start_nodes as f64 * default_degree) as u64,
            estimated_cost: self.calculate_cost(context, false),
            reason: DirectionSelectionReason::StatsUnavailable,
            avg_degree: default_degree,
            involves_super_node: false,
        }
    }

    /// Quick direction selection (simplified version, for decision-making caching)
    pub fn select_direction_quick(&self, edge_type: &str) -> TraversalDirection {
        let stats = self
            .cost_calculator
            .statistics_manager()
            .get_edge_stats(&self.space, edge_type);

        match stats {
            Some(s) => {
                if s.avg_out_degree > self.super_node_threshold
                    && s.avg_in_degree <= self.super_node_threshold
                {
                    TraversalDirection::Backward
                } else if s.avg_in_degree > self.super_node_threshold
                    && s.avg_out_degree <= self.super_node_threshold
                    || s.avg_out_degree <= s.avg_in_degree
                {
                    TraversalDirection::Forward
                } else {
                    TraversalDirection::Backward
                }
            }
            None => TraversalDirection::Forward,
        }
    }

    /// Obtaining the degree information of the edges
    pub fn get_degree_info(&self, edge_type: &str) -> Option<DegreeInfo> {
        self.cost_calculator
            .statistics_manager()
            .get_edge_stats(&self.space, edge_type)
            .map(|s| DegreeInfo {
                avg_out_degree: s.avg_out_degree,
                avg_in_degree: s.avg_in_degree,
                max_out_degree: s.max_out_degree,
                max_in_degree: s.max_in_degree,
                is_out_super: s.avg_out_degree > self.super_node_threshold,
                is_in_super: s.avg_in_degree > self.super_node_threshold,
            })
    }
}

/// Degree information
#[derive(Debug, Clone)]
pub struct DegreeInfo {
    /// Average attendance
    pub avg_out_degree: f64,
    /// Average Indegree
    pub avg_in_degree: f64,
    /// Maximum Outdegree
    pub max_out_degree: u64,
    /// Maximum In-degree
    pub max_in_degree: u64,
    /// Is the outbound direction a super node?
    pub is_out_super: bool,
    /// Is the inbound direction a super node?
    pub is_in_super: bool,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::optimizer::stats::StatisticsManager;

    fn create_test_optimizer() -> TraversalDirectionOptimizer {
        let stats_manager = Arc::new(StatisticsManager::new());
        let cost_calculator = Arc::new(CostCalculator::new(stats_manager));
        TraversalDirectionOptimizer::new(cost_calculator).with_super_node_threshold(100.0)
    }

    #[test]
    fn test_explicit_direction() {
        let optimizer = create_test_optimizer();
        let context = DirectionContext {
            edge_type: "KNOWS".to_string(),
            start_nodes: 100,
            explicit_direction: Some(TraversalDirection::Backward),
            allow_bidirectional: false,
            steps: 1,
        };

        let decision = optimizer.optimize_direction(&context);
        assert_eq!(decision.direction, TraversalDirection::Backward);
        matches!(decision.reason, DirectionSelectionReason::ExplicitDirection);
    }

    #[test]
    fn test_default_direction_when_no_stats() {
        let optimizer = create_test_optimizer();
        let context = DirectionContext {
            edge_type: "UNKNOWN".to_string(),
            start_nodes: 100,
            explicit_direction: None,
            allow_bidirectional: false,
            steps: 1,
        };

        let decision = optimizer.optimize_direction(&context);
        assert_eq!(decision.direction, TraversalDirection::Forward);
        matches!(decision.reason, DirectionSelectionReason::StatsUnavailable);
    }

    #[test]
    fn test_traversal_direction_name() {
        assert_eq!(TraversalDirection::Forward.name(), "Forward");
        assert_eq!(TraversalDirection::Backward.name(), "Backward");
        assert_eq!(TraversalDirection::Bidirectional.name(), "Bidirectional");
    }

    #[test]
    fn test_traversal_direction_conversion() {
        assert_eq!(
            TraversalDirection::from_edge_direction(&EdgeDirection::Out),
            TraversalDirection::Forward
        );
        assert_eq!(
            TraversalDirection::from_edge_direction(&EdgeDirection::In),
            TraversalDirection::Backward
        );
        assert_eq!(
            TraversalDirection::from_edge_direction(&EdgeDirection::Both),
            TraversalDirection::Bidirectional
        );

        assert_eq!(
            TraversalDirection::Forward.to_edge_direction(),
            EdgeDirection::Out
        );
        assert_eq!(
            TraversalDirection::Backward.to_edge_direction(),
            EdgeDirection::In
        );
        assert_eq!(
            TraversalDirection::Bidirectional.to_edge_direction(),
            EdgeDirection::Both
        );
    }

    #[test]
    fn test_quick_selection_no_stats() {
        let optimizer = create_test_optimizer();
        let direction = optimizer.select_direction_quick("UNKNOWN");
        assert_eq!(direction, TraversalDirection::Forward);
    }

    #[test]
    fn test_degree_info() {
        let optimizer = create_test_optimizer();
        // The unknown edge type should return None.
        let info = optimizer.get_degree_info("UNKNOWN");
        assert!(info.is_none());
    }
}