heliosdb-nano 3.23.2

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! Query Performance Regression Detection
//!
//! Stores execution plan history and detects performance regressions.
//! Features:
//! - Execution plan history storage
//! - Plan comparison across executions
//! - Regression detection (better/worse)
//! - Performance degradation alerts
//! - Optimizer decision tracking over time

use crate::Result;
use super::explain::{ExplainOutput, PlanNode};
use super::realtime_explain::ExecutionStats;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::SystemTime;

/// Historical execution record
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionRecord {
    /// Unique query fingerprint (normalized SQL)
    pub query_fingerprint: String,

    /// Execution timestamp
    pub timestamp: SystemTime,

    /// Query plan used
    pub plan: PlanNode,

    /// Estimated cost
    pub estimated_cost: f64,

    /// Actual execution time
    pub actual_time_ms: f64,

    /// Rows processed
    pub rows_processed: usize,

    /// Execution statistics
    pub stats: Option<ExecutionStats>,

    /// Optimizer version/configuration
    pub optimizer_version: String,
}

/// Plan comparison result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlanComparison {
    /// Query fingerprint
    pub query_fingerprint: String,

    /// Previous execution
    pub previous: ExecutionSummary,

    /// Current execution
    pub current: ExecutionSummary,

    /// Change type
    pub change_type: ChangeType,

    /// Performance delta
    pub performance_delta: PerformanceDelta,

    /// Plan differences
    pub plan_differences: Vec<String>,

    /// Recommendations
    pub recommendations: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionSummary {
    pub timestamp: SystemTime,
    pub estimated_cost: f64,
    pub actual_time_ms: f64,
    pub rows_processed: usize,
    pub plan_hash: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ChangeType {
    Improvement,   // Performance got better
    Regression,    // Performance got worse
    PlanChange,    // Plan changed but similar performance
    NoChange,      // Same plan, similar performance
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceDelta {
    /// Time change in milliseconds
    pub time_delta_ms: f64,

    /// Time change percentage
    pub time_delta_percent: f64,

    /// Cost change
    pub cost_delta: f64,

    /// Rows change
    pub rows_delta: i64,

    /// Severity (0-100, higher = more severe)
    pub severity: f64,
}

/// Regression detector
pub struct RegressionDetector {
    history: HashMap<String, Vec<ExecutionRecord>>,
    max_history_per_query: usize,
    regression_threshold_percent: f64,
}

impl RegressionDetector {
    pub fn new() -> Self {
        Self {
            history: HashMap::new(),
            max_history_per_query: 100,
            regression_threshold_percent: 20.0, // Alert if >20% slower
        }
    }

    pub fn with_threshold(mut self, threshold_percent: f64) -> Self {
        self.regression_threshold_percent = threshold_percent;
        self
    }

    /// Record an execution
    pub fn record_execution(
        &mut self,
        query_fingerprint: String,
        plan: PlanNode,
        estimated_cost: f64,
        actual_time_ms: f64,
        rows_processed: usize,
        stats: Option<ExecutionStats>,
    ) {
        let record = ExecutionRecord {
            query_fingerprint: query_fingerprint.clone(),
            timestamp: SystemTime::now(),
            plan,
            estimated_cost,
            actual_time_ms,
            rows_processed,
            stats,
            optimizer_version: "v1.0".to_string(),
        };

        let history = self.history.entry(query_fingerprint).or_insert_with(Vec::new);
        history.push(record);

        // Keep only recent history
        if history.len() > self.max_history_per_query {
            history.remove(0);
        }
    }

    /// Compare current execution with historical baseline
    pub fn compare_with_history(
        &self,
        query_fingerprint: &str,
        current_plan: &PlanNode,
        current_time_ms: f64,
        current_rows: usize,
    ) -> Option<PlanComparison> {
        let history = self.history.get(query_fingerprint)?;
        if history.is_empty() {
            return None;
        }

        // Calculate baseline from recent executions
        let baseline = self.calculate_baseline(history)?;

        let previous = ExecutionSummary {
            timestamp: baseline.timestamp,
            estimated_cost: baseline.estimated_cost,
            actual_time_ms: baseline.actual_time_ms,
            rows_processed: baseline.rows_processed,
            plan_hash: self.hash_plan(&baseline.plan),
        };

        let current = ExecutionSummary {
            timestamp: SystemTime::now(),
            estimated_cost: current_plan.cost,
            actual_time_ms: current_time_ms,
            rows_processed: current_rows,
            plan_hash: self.hash_plan(current_plan),
        };

        let performance_delta = self.calculate_delta(&baseline, current_time_ms, current_rows);
        let change_type = self.classify_change(&previous, &current, &performance_delta);
        let plan_differences = self.find_plan_differences(&baseline.plan, current_plan);
        let recommendations = self.generate_recommendations(&change_type, &performance_delta, &plan_differences);

        Some(PlanComparison {
            query_fingerprint: query_fingerprint.to_string(),
            previous,
            current,
            change_type,
            performance_delta,
            plan_differences,
            recommendations,
        })
    }

    /// Calculate baseline from historical executions
    /// Returns None if history is empty
    fn calculate_baseline(&self, history: &[ExecutionRecord]) -> Option<ExecutionRecord> {
        if history.is_empty() {
            return None;
        }

        // Use median of recent executions
        let recent_count = 10.min(history.len());
        let recent = &history[history.len().saturating_sub(recent_count)..];

        let mut times: Vec<f64> = recent.iter().map(|r| r.actual_time_ms).collect();
        times.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let median_time = times.get(times.len() / 2).copied().unwrap_or(0.0);

        // Find record closest to median, with fallback to first record
        recent.iter()
            .min_by_key(|r| ((r.actual_time_ms - median_time).abs() * 1000.0) as i64)
            .or_else(|| recent.first())
            .cloned()
    }

    /// Calculate performance delta
    fn calculate_delta(
        &self,
        baseline: &ExecutionRecord,
        current_time_ms: f64,
        current_rows: usize,
    ) -> PerformanceDelta {
        let time_delta_ms = current_time_ms - baseline.actual_time_ms;
        let time_delta_percent = if baseline.actual_time_ms > 0.0 {
            (time_delta_ms / baseline.actual_time_ms) * 100.0
        } else {
            0.0
        };

        let cost_delta = current_time_ms - baseline.estimated_cost;
        let rows_delta = current_rows as i64 - baseline.rows_processed as i64;

        // Severity based on time delta percentage
        let severity = if time_delta_percent.abs() < 10.0 {
            10.0
        } else if time_delta_percent.abs() < 20.0 {
            30.0
        } else if time_delta_percent.abs() < 50.0 {
            60.0
        } else {
            90.0
        };

        PerformanceDelta {
            time_delta_ms,
            time_delta_percent,
            cost_delta,
            rows_delta,
            severity,
        }
    }

    /// Classify type of change
    fn classify_change(
        &self,
        previous: &ExecutionSummary,
        current: &ExecutionSummary,
        delta: &PerformanceDelta,
    ) -> ChangeType {
        let plan_changed = previous.plan_hash != current.plan_hash;

        if delta.time_delta_percent.abs() < 5.0 {
            if plan_changed {
                ChangeType::PlanChange
            } else {
                ChangeType::NoChange
            }
        } else if delta.time_delta_percent > self.regression_threshold_percent {
            ChangeType::Regression
        } else if delta.time_delta_percent < -10.0 {
            ChangeType::Improvement
        } else if plan_changed {
            ChangeType::PlanChange
        } else {
            ChangeType::NoChange
        }
    }

    /// Find differences between plans
    fn find_plan_differences(&self, previous: &PlanNode, current: &PlanNode) -> Vec<String> {
        let mut differences = Vec::new();

        // Compare node types
        if previous.node_type != current.node_type {
            differences.push(format!(
                "Node type changed: {}{}",
                previous.node_type, current.node_type
            ));
        }

        // Compare operations
        if previous.operation != current.operation {
            differences.push(format!(
                "Operation changed: {}{}",
                previous.operation, current.operation
            ));
        }

        // Compare costs
        let cost_change_percent = ((current.cost - previous.cost) / previous.cost.max(1.0)) * 100.0;
        if cost_change_percent.abs() > 20.0 {
            differences.push(format!(
                "Cost changed by {:.1}%: {:.2}{:.2}",
                cost_change_percent, previous.cost, current.cost
            ));
        }

        // Compare row estimates
        let rows_change_percent = ((current.rows as f64 - previous.rows as f64) / previous.rows.max(1) as f64) * 100.0;
        if rows_change_percent.abs() > 20.0 {
            differences.push(format!(
                "Row estimate changed by {:.1}%: {}{}",
                rows_change_percent, previous.rows, current.rows
            ));
        }

        // Compare children count
        if previous.children.len() != current.children.len() {
            differences.push(format!(
                "Number of child nodes changed: {}{}",
                previous.children.len(), current.children.len()
            ));
        }

        // Recursively check children
        for (i, (prev_child, curr_child)) in previous.children.iter().zip(current.children.iter()).enumerate() {
            let child_diffs = self.find_plan_differences(prev_child, curr_child);
            for diff in child_diffs {
                differences.push(format!("Child {}: {}", i, diff));
            }
        }

        differences
    }

    /// Generate recommendations based on changes
    fn generate_recommendations(
        &self,
        change_type: &ChangeType,
        delta: &PerformanceDelta,
        differences: &[String],
    ) -> Vec<String> {
        let mut recommendations = Vec::new();

        match change_type {
            ChangeType::Regression => {
                recommendations.push(format!(
                    "⚠️  PERFORMANCE REGRESSION DETECTED: Query is {:.1}% slower ({:.2}ms increase)",
                    delta.time_delta_percent, delta.time_delta_ms
                ));

                if !differences.is_empty() {
                    recommendations.push("Query plan has changed. Possible causes:".to_string());
                    for diff in differences.iter().take(3) {
                        recommendations.push(format!("{}", diff));
                    }
                }

                recommendations.push("Recommended actions:".to_string());
                recommendations.push("  1. Update table statistics (ANALYZE)".to_string());
                recommendations.push("  2. Review recent schema changes".to_string());
                recommendations.push("  3. Check for missing or dropped indexes".to_string());
                recommendations.push("  4. Consider query rewrite".to_string());
            }

            ChangeType::Improvement => {
                recommendations.push(format!(
                    "✓ Performance improved: Query is {:.1}% faster ({:.2}ms reduction)",
                    -delta.time_delta_percent, -delta.time_delta_ms
                ));

                if !differences.is_empty() {
                    recommendations.push("Query plan improvements:".to_string());
                    for diff in differences.iter().take(3) {
                        recommendations.push(format!("{}", diff));
                    }
                }
            }

            ChangeType::PlanChange => {
                recommendations.push("Query plan changed but performance is similar".to_string());
                recommendations.push("Monitor future executions for stability".to_string());
            }

            ChangeType::NoChange => {
                recommendations.push("Query performance is stable".to_string());
            }
        }

        recommendations
    }

    /// Hash a plan for comparison
    fn hash_plan(&self, plan: &PlanNode) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        plan.node_type.hash(&mut hasher);
        plan.operation.hash(&mut hasher);
        plan.children.len().hash(&mut hasher);

        format!("{:x}", hasher.finish())
    }

    /// Get execution history for a query
    pub fn get_history(&self, query_fingerprint: &str) -> Option<&Vec<ExecutionRecord>> {
        self.history.get(query_fingerprint)
    }

    /// Get trend analysis for a query
    pub fn analyze_trend(&self, query_fingerprint: &str) -> Option<TrendAnalysis> {
        let history = self.history.get(query_fingerprint)?;
        if history.len() < 3 {
            return None;
        }

        let times: Vec<f64> = history.iter().map(|r| r.actual_time_ms).collect();

        // Calculate linear regression
        let n = times.len() as f64;
        let x_mean = (n - 1.0) / 2.0;
        let y_mean = times.iter().sum::<f64>() / n;

        let mut numerator = 0.0;
        let mut denominator = 0.0;

        for (i, &time) in times.iter().enumerate() {
            let x = i as f64;
            numerator += (x - x_mean) * (time - y_mean);
            denominator += (x - x_mean).powi(2);
        }

        let slope = if denominator != 0.0 {
            numerator / denominator
        } else {
            0.0
        };

        let trend = if slope > 0.5 {
            Trend::Degrading
        } else if slope < -0.5 {
            Trend::Improving
        } else {
            Trend::Stable
        };

        Some(TrendAnalysis {
            query_fingerprint: query_fingerprint.to_string(),
            executions_analyzed: history.len(),
            trend,
            slope,
            average_time_ms: y_mean,
            min_time_ms: times.iter().cloned().fold(f64::INFINITY, f64::min),
            max_time_ms: times.iter().cloned().fold(f64::NEG_INFINITY, f64::max),
        })
    }

    /// Format comparison report
    pub fn format_comparison_report(&self, comparison: &PlanComparison) -> String {
        let mut report = String::new();

        report.push_str("═══════════════════════════════════════════════════════════════\n");
        report.push_str("           QUERY PERFORMANCE COMPARISON REPORT                \n");
        report.push_str("═══════════════════════════════════════════════════════════════\n\n");

        report.push_str(&format!("Query: {}\n", comparison.query_fingerprint));
        report.push_str(&format!("Change Type: {:?}\n", comparison.change_type));
        report.push_str(&format!("Severity: {:.1}/100\n\n", comparison.performance_delta.severity));

        report.push_str("───────────────────────────────────────────────────────────────\n");
        report.push_str("  PERFORMANCE METRICS\n");
        report.push_str("───────────────────────────────────────────────────────────────\n\n");

        report.push_str(&format!("Execution Time:\n"));
        report.push_str(&format!("  Previous: {:.2}ms\n", comparison.previous.actual_time_ms));
        report.push_str(&format!("  Current:  {:.2}ms\n", comparison.current.actual_time_ms));
        report.push_str(&format!("  Delta:    {:+.2}ms ({:+.1}%)\n\n",
            comparison.performance_delta.time_delta_ms,
            comparison.performance_delta.time_delta_percent));

        report.push_str(&format!("Rows Processed:\n"));
        report.push_str(&format!("  Previous: {}\n", comparison.previous.rows_processed));
        report.push_str(&format!("  Current:  {}\n", comparison.current.rows_processed));
        report.push_str(&format!("  Delta:    {:+}\n\n", comparison.performance_delta.rows_delta));

        if !comparison.plan_differences.is_empty() {
            report.push_str("───────────────────────────────────────────────────────────────\n");
            report.push_str("  PLAN DIFFERENCES\n");
            report.push_str("───────────────────────────────────────────────────────────────\n\n");

            for diff in &comparison.plan_differences {
                report.push_str(&format!("{}\n", diff));
            }
            report.push_str("\n");
        }

        if !comparison.recommendations.is_empty() {
            report.push_str("───────────────────────────────────────────────────────────────\n");
            report.push_str("  RECOMMENDATIONS\n");
            report.push_str("───────────────────────────────────────────────────────────────\n\n");

            for rec in &comparison.recommendations {
                report.push_str(&format!("{}\n", rec));
            }
            report.push_str("\n");
        }

        report.push_str("═══════════════════════════════════════════════════════════════\n");

        report
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TrendAnalysis {
    pub query_fingerprint: String,
    pub executions_analyzed: usize,
    pub trend: Trend,
    pub slope: f64,
    pub average_time_ms: f64,
    pub min_time_ms: f64,
    pub max_time_ms: f64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Trend {
    Improving,
    Stable,
    Degrading,
}

impl Default for RegressionDetector {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    fn create_test_plan(cost: f64, rows: usize) -> PlanNode {
        PlanNode {
            node_type: "Scan".to_string(),
            operation: "Seq Scan".to_string(),
            cost,
            rows,
            details: HashMap::new(),
            children: vec![],
        }
    }

    #[test]
    fn test_regression_detector_creation() {
        let detector = RegressionDetector::new();
        assert_eq!(detector.history.len(), 0);
    }

    #[test]
    fn test_record_execution() {
        let mut detector = RegressionDetector::new();
        let plan = create_test_plan(100.0, 1000);

        detector.record_execution(
            "SELECT * FROM users".to_string(),
            plan,
            100.0,
            95.0,
            1000,
            None,
        );

        assert_eq!(detector.history.len(), 1);
    }

    #[test]
    fn test_detect_regression() {
        let mut detector = RegressionDetector::new();
        let fingerprint = "SELECT * FROM users WHERE age > 25".to_string();

        // Record baseline
        for _ in 0..5 {
            detector.record_execution(
                fingerprint.clone(),
                create_test_plan(100.0, 1000),
                100.0,
                95.0,
                1000,
                None,
            );
        }

        // Record regression
        let comparison = detector.compare_with_history(
            &fingerprint,
            &create_test_plan(100.0, 1000),
            200.0, // 2x slower
            1000,
        );

        assert!(comparison.is_some());
        let comp = comparison.unwrap();
        assert_eq!(comp.change_type, ChangeType::Regression);
    }

    #[test]
    fn test_detect_improvement() {
        let mut detector = RegressionDetector::new();
        let fingerprint = "SELECT * FROM orders".to_string();

        // Record baseline
        for _ in 0..5 {
            detector.record_execution(
                fingerprint.clone(),
                create_test_plan(100.0, 1000),
                100.0,
                100.0,
                1000,
                None,
            );
        }

        // Record improvement
        let comparison = detector.compare_with_history(
            &fingerprint,
            &create_test_plan(50.0, 1000),
            45.0, // Much faster
            1000,
        );

        assert!(comparison.is_some());
        let comp = comparison.unwrap();
        assert_eq!(comp.change_type, ChangeType::Improvement);
    }

    #[test]
    fn test_plan_difference_detection() {
        let detector = RegressionDetector::new();

        let plan1 = create_test_plan(100.0, 1000);
        let plan2 = create_test_plan(150.0, 1500);

        let differences = detector.find_plan_differences(&plan1, &plan2);

        assert!(!differences.is_empty());
    }

    #[test]
    fn test_trend_analysis() {
        let mut detector = RegressionDetector::new();
        let fingerprint = "SELECT COUNT(*) FROM sales".to_string();

        // Record degrading trend
        for i in 0..10 {
            detector.record_execution(
                fingerprint.clone(),
                create_test_plan(100.0, 1000),
                100.0,
                50.0 + (i as f64 * 5.0), // Getting slower
                1000,
                None,
            );
        }

        let trend = detector.analyze_trend(&fingerprint);
        assert!(trend.is_some());

        let trend = trend.unwrap();
        assert_eq!(trend.trend, Trend::Degrading);
    }
}