Skip to main content

reddb_engine/query/
logical_plan.rs

1//! Canonical logical plan data shared with native planning and browser execution.
2use std::collections::BTreeMap;
3
4/// Canonical logical plan used for explain/introspection during the planner transition.
5#[derive(Debug, Clone, Default)]
6pub struct CanonicalLogicalPlan {
7    pub root: CanonicalLogicalNode,
8}
9
10/// Canonical logical operator node.
11#[derive(Debug, Clone, Default)]
12pub struct CanonicalLogicalNode {
13    pub operator: String,
14    pub source: Option<String>,
15    pub details: BTreeMap<String, String>,
16    pub estimated_rows: f64,
17    pub estimated_selectivity: f64,
18    pub estimated_confidence: f64,
19    pub operator_cost: f64,
20    pub children: Vec<CanonicalLogicalNode>,
21}
22
23/// Cardinality estimate for a query result
24#[derive(Debug, Clone, Default)]
25pub struct CardinalityEstimate {
26    /// Estimated row/record count
27    pub rows: f64,
28    /// Selectivity factor (0.0 - 1.0)
29    pub selectivity: f64,
30    /// Confidence in the estimate (0.0 - 1.0)
31    pub confidence: f64,
32}
33
34impl CardinalityEstimate {
35    /// Create a new cardinality estimate
36    pub fn new(rows: f64, selectivity: f64) -> Self {
37        Self {
38            rows,
39            selectivity,
40            confidence: 1.0,
41        }
42    }
43
44    /// Full table scan estimate
45    pub fn full_scan(table_size: f64) -> Self {
46        Self {
47            rows: table_size,
48            selectivity: 1.0,
49            confidence: 1.0,
50        }
51    }
52
53    /// Apply a filter to reduce cardinality
54    pub fn with_filter(mut self, filter_selectivity: f64) -> Self {
55        self.rows *= filter_selectivity;
56        self.selectivity *= filter_selectivity;
57        self.confidence *= 0.9; // Reduce confidence with each estimate
58        self
59    }
60}
61
62#[derive(Debug, Clone)]
63pub struct AccessPathDecision {
64    pub path: &'static str,
65    pub index_hint: Option<String>,
66    pub reason: String,
67    pub warning: Option<String>,
68}