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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
//! Enhanced EXPLAIN PLAN with AI-powered natural language explanations
//!
//! This module provides comprehensive query plan explanation including:
//! - Traditional plan visualization
//! - Natural language explanations via LLM
//! - Why-Not analysis (why optimizations weren't applied)
//! - Optimizer decision tracking with reasoning
//! - Performance predictions and suggestions
//! - Interactive query refinement hints

#![allow(unused_variables)]

use crate::Result;
use crate::storage::StorageEngine;
use super::logical_plan::LogicalPlan;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// EXPLAIN output modes
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExplainMode {
    /// Standard plan tree
    Standard,
    /// Include cost and cardinality estimates
    Verbose,
    /// Add natural language AI explanation
    AI,
    /// Full analysis with Why-Not insights
    Analyze,
}

/// EXPLAIN output format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExplainFormat {
    /// Human-readable text
    Text,
    /// JSON output
    JSON,
    /// YAML output
    YAML,
    /// Tree with colors (ANSI)
    Tree,
}

/// Enhanced EXPLAIN output
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExplainOutput {
    /// Query plan tree
    pub plan: PlanNode,

    /// Total estimated cost
    pub total_cost: f64,

    /// Total estimated rows
    pub total_rows: usize,

    /// Planning time in milliseconds
    pub planning_time_ms: f64,

    /// Configuration snapshot at plan time
    pub config: ConfigSnapshot,

    /// Active optimizer features
    pub features: Vec<ActiveFeature>,

    /// Optimizer decisions made
    pub decisions: Vec<OptimizerDecision>,

    /// Why-Not analysis (if requested)
    pub why_not: Option<WhyNotAnalysis>,

    /// Natural language explanation (if AI mode)
    pub ai_explanation: Option<AIExplanation>,

    /// Warnings and suggestions
    pub warnings: Vec<String>,
    pub suggestions: Vec<String>,

    // ─────────────────────────────────────────────────────────────────────────
    // EXPLAIN ANALYZE execution results
    // ─────────────────────────────────────────────────────────────────────────

    /// Actual rows returned (if ANALYZE was used)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actual_rows: Option<usize>,

    /// Actual execution time in milliseconds (if ANALYZE was used)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub actual_time_ms: Option<f64>,

    /// Execution error message (if ANALYZE failed)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub execution_error: Option<String>,
}

/// Plan node in the tree
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlanNode {
    pub node_type: String,
    pub operation: String,
    pub cost: f64,
    pub rows: usize,
    pub details: HashMap<String, String>,
    pub children: Vec<PlanNode>,
}

/// Configuration snapshot
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigSnapshot {
    pub work_mem_mb: usize,
    pub enable_hashjoin: bool,
    pub enable_mergejoin: bool,
    pub enable_nestloop: bool,
    pub enable_indexscan: bool,
    pub enable_seqscan: bool,
    pub max_parallel_workers: usize,
    pub enable_simd: bool,
}

impl Default for ConfigSnapshot {
    fn default() -> Self {
        Self {
            work_mem_mb: 256,
            enable_hashjoin: true,
            enable_mergejoin: true,
            enable_nestloop: true,
            enable_indexscan: true,
            enable_seqscan: true,
            max_parallel_workers: 4,
            enable_simd: true,
        }
    }
}

/// Active optimizer feature
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActiveFeature {
    pub name: String,
    pub category: FeatureCategory,
    pub trigger: String,
    pub benefit: String,
    pub savings_percent: Option<f64>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum FeatureCategory {
    Pushdown,       // Predicate/projection pushdown
    Pruning,        // Partition/projection pruning
    Indexing,       // Index selection
    Vectorization,  // SIMD/JIT
    Parallelism,    // Parallel execution
    Caching,        // Result caching
}

/// Optimizer decision point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptimizerDecision {
    pub decision_point: String,
    pub chosen: ChosenOption,
    pub rejected: Vec<RejectedOption>,
    pub reasoning: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChosenOption {
    pub name: String,
    pub cost: f64,
    pub rows: usize,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RejectedOption {
    pub name: String,
    pub cost: f64,
    pub reason: String,
    pub cost_multiplier: f64,
}

/// Why-Not analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WhyNotAnalysis {
    pub unused_indexes: Vec<UnusedIndexReason>,
    pub stale_statistics: Vec<StaleStatsWarning>,
    pub cardinality_issues: Vec<CardinalityIssue>,
    pub configuration_issues: Vec<ConfigIssue>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnusedIndexReason {
    pub index_name: String,
    pub table_name: String,
    pub reason: String,
    pub cost_impact: f64,
    pub suggestion: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StaleStatsWarning {
    pub table_name: String,
    pub days_old: u32,
    pub percent_changed: f64,
    pub impact: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CardinalityIssue {
    pub operation: String,
    pub estimated: usize,
    pub actual: Option<usize>,
    pub error_percent: f64,
    pub cause: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConfigIssue {
    pub parameter: String,
    pub current_value: String,
    pub suggested_value: String,
    pub reason: String,
}

/// AI-powered natural language explanation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AIExplanation {
    /// High-level query summary
    pub summary: String,

    /// Step-by-step plan walkthrough
    pub walkthrough: Vec<String>,

    /// Performance prediction
    pub performance: PerformancePrediction,

    /// Optimization suggestions in plain English
    pub suggestions: Vec<String>,

    /// Potential issues to watch for
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformancePrediction {
    pub category: String, // "Fast", "Moderate", "Slow", "Very Slow"
    pub estimated_time_ms: f64,
    pub bottlenecks: Vec<String>,
    pub explanation: String,
}

/// EXPLAIN planner
pub struct ExplainPlanner {
    mode: ExplainMode,
    format: ExplainFormat,
    use_ai: bool,
    llm_endpoint: Option<String>,
    storage: Option<std::sync::Arc<StorageEngine>>,
}

impl ExplainPlanner {
    pub fn new(mode: ExplainMode, format: ExplainFormat) -> Self {
        Self {
            mode,
            format,
            use_ai: matches!(mode, ExplainMode::AI | ExplainMode::Analyze),
            llm_endpoint: None,
            storage: None,
        }
    }

    /// Set LLM endpoint for AI explanations
    pub fn with_llm_endpoint(mut self, endpoint: String) -> Self {
        self.llm_endpoint = Some(endpoint);
        self
    }

    /// Set storage engine for statistics access
    pub fn with_storage(mut self, storage: std::sync::Arc<StorageEngine>) -> Self {
        self.storage = Some(storage);
        self
    }

    /// Generate EXPLAIN output for a logical plan
    pub fn explain(&self, plan: &LogicalPlan) -> Result<ExplainOutput> {
        let start = std::time::Instant::now();

        // Convert logical plan to plan tree
        let plan_node = self.plan_to_node(plan)?;

        // Calculate total cost and rows
        let total_cost = self.calculate_total_cost(&plan_node);
        let total_rows = self.estimate_total_rows(&plan_node);

        // Capture configuration
        let config = ConfigSnapshot::default();

        // Detect active features
        let features = self.detect_features(plan);

        // Track optimizer decisions
        let decisions = self.track_decisions(plan);

        // Generate Why-Not analysis if requested
        let why_not = if matches!(self.mode, ExplainMode::Analyze) {
            Some(self.analyze_why_not(plan, &plan_node))
        } else {
            None
        };

        // Generate AI explanation if requested
        let ai_explanation = if self.use_ai {
            Some(self.generate_ai_explanation(plan, &plan_node, total_cost, total_rows)?)
        } else {
            None
        };

        // Generate warnings and suggestions
        let (warnings, suggestions) = self.generate_warnings_and_suggestions(plan, &plan_node);

        let planning_time_ms = start.elapsed().as_secs_f64() * 1000.0;

        Ok(ExplainOutput {
            plan: plan_node,
            total_cost,
            total_rows,
            planning_time_ms,
            config,
            features,
            decisions,
            why_not,
            ai_explanation,
            warnings,
            suggestions,
            // ANALYZE execution results (set by executor if ANALYZE is used)
            actual_rows: None,
            actual_time_ms: None,
            execution_error: None,
        })
    }

    /// Convert logical plan to plan node tree
    fn plan_to_node(&self, plan: &LogicalPlan) -> Result<PlanNode> {
        match plan {
            LogicalPlan::Scan { table_name, schema, projection, .. } => {
                let mut details = HashMap::new();
                details.insert("table".to_string(), table_name.clone());
                details.insert("columns".to_string(), format!("{}", schema.columns.len()));
                if let Some(proj) = projection {
                    details.insert("projection".to_string(), format!("{:?}", proj));
                }

                // Get real statistics from storage if available
                let estimated_rows = self.get_table_row_count(table_name);
                let avg_row_size = self.get_table_avg_row_size(table_name);

                // Calculate scan cost: row_count * avg_row_size * cost_per_byte
                let cost_per_byte = 0.01;
                let scan_cost = estimated_rows as f64 * avg_row_size as f64 * cost_per_byte;

                Ok(PlanNode {
                    node_type: "Scan".to_string(),
                    operation: format!("Scan {}", table_name),
                    cost: scan_cost.max(1.0), // Minimum cost of 1.0
                    rows: estimated_rows,
                    details,
                    children: vec![],
                })
            }

            LogicalPlan::Filter { input, predicate } => {
                let input_node = self.plan_to_node(input)?;

                // Estimate selectivity from predicate using statistics
                let selectivity = self.estimate_predicate_selectivity(predicate);

                let mut details = HashMap::new();
                details.insert("predicate".to_string(), format!("{:?}", predicate));
                details.insert("selectivity".to_string(), format!("{:.2}", selectivity));

                Ok(PlanNode {
                    node_type: "Filter".to_string(),
                    operation: "Filter".to_string(),
                    cost: input_node.cost + (input_node.rows as f64 * 0.01),
                    rows: (input_node.rows as f64 * selectivity) as usize,
                    details,
                    children: vec![input_node],
                })
            }

            LogicalPlan::Project { input, exprs, aliases, distinct, distinct_on } => {
                let input_node = self.plan_to_node(input)?;

                let mut details = HashMap::new();
                details.insert("expressions".to_string(), format!("{}", exprs.len()));
                details.insert("distinct".to_string(), distinct.to_string());
                if let Some(on_exprs) = distinct_on {
                    details.insert("distinct_on".to_string(), format!("{} columns", on_exprs.len()));
                }

                let cost = input_node.cost + (input_node.rows as f64 * exprs.len() as f64 * 0.01);
                let rows = if *distinct {
                    input_node.rows / 2 // Rough estimate
                } else {
                    input_node.rows
                };

                Ok(PlanNode {
                    node_type: "Project".to_string(),
                    operation: "Project".to_string(),
                    cost,
                    rows,
                    details,
                    children: vec![input_node],
                })
            }

            LogicalPlan::Aggregate { input, group_by, aggr_exprs, having } => {
                let input_node = self.plan_to_node(input)?;

                let mut details = HashMap::new();
                details.insert("group_by".to_string(), format!("{}", group_by.len()));
                details.insert("aggregates".to_string(), format!("{}", aggr_exprs.len()));
                if having.is_some() {
                    details.insert("having".to_string(), "yes".to_string());
                }

                let groups = if group_by.is_empty() { 1 } else { input_node.rows / 10 };

                Ok(PlanNode {
                    node_type: "Aggregate".to_string(),
                    operation: "HashAggregate".to_string(),
                    cost: input_node.cost + (input_node.rows as f64 * 2.0),
                    rows: groups,
                    details,
                    children: vec![input_node],
                })
            }

            LogicalPlan::Join { left, right, join_type, on, lateral } => {
                let left_node = self.plan_to_node(left)?;
                let right_node = self.plan_to_node(right)?;

                let mut details = HashMap::new();
                details.insert("type".to_string(), format!("{:?}", join_type));
                if let Some(cond) = on {
                    details.insert("condition".to_string(), format!("{:?}", cond));
                }
                if *lateral {
                    details.insert("lateral".to_string(), "true".to_string());
                }

                // Hash join cost model (LATERAL uses nested loop, higher cost)
                let build_cost = right_node.rows as f64 * 2.0;
                let probe_cost = left_node.rows as f64 * 1.0;
                let cost = left_node.cost + right_node.cost + build_cost + probe_cost;
                let rows = left_node.rows * right_node.rows / 10; // Rough estimate

                let join_method = if *lateral { "NestedLoop" } else { "HashJoin" };
                Ok(PlanNode {
                    node_type: "Join".to_string(),
                    operation: format!("{:?} {}", join_type, join_method),
                    cost,
                    rows,
                    details,
                    children: vec![left_node, right_node],
                })
            }

            LogicalPlan::Sort { input, exprs, asc } => {
                let input_node = self.plan_to_node(input)?;

                let mut details = HashMap::new();
                details.insert("keys".to_string(), format!("{}", exprs.len()));

                // Sort cost: O(n log n)
                let n = input_node.rows as f64;
                let cost = input_node.cost + (n * n.log2() * 0.01);

                Ok(PlanNode {
                    node_type: "Sort".to_string(),
                    operation: "Sort".to_string(),
                    cost,
                    rows: input_node.rows,
                    details,
                    children: vec![input_node],
                })
            }

            LogicalPlan::Limit { input, limit, offset, .. } => {
                let input_node = self.plan_to_node(input)?;

                let mut details = HashMap::new();
                details.insert("limit".to_string(), limit.to_string());
                details.insert("offset".to_string(), offset.to_string());

                let rows = (*limit).min(input_node.rows.saturating_sub(*offset));

                Ok(PlanNode {
                    node_type: "Limit".to_string(),
                    operation: "Limit".to_string(),
                    cost: input_node.cost * 0.1, // Can stop early
                    rows,
                    details,
                    children: vec![input_node],
                })
            }

            LogicalPlan::UseBranch { branch_name } => {
                let mut details = HashMap::new();
                details.insert("branch".to_string(), branch_name.clone());

                Ok(PlanNode {
                    node_type: "UseBranch".to_string(),
                    operation: format!("USE BRANCH {}", branch_name),
                    cost: 1.0, // Minimal cost for metadata operation
                    rows: 0,
                    details,
                    children: vec![],
                })
            }

            LogicalPlan::ShowBranches => {
                let details = HashMap::new();

                Ok(PlanNode {
                    node_type: "ShowBranches".to_string(),
                    operation: "SHOW BRANCHES".to_string(),
                    cost: 10.0, // Small cost for reading branch metadata
                    rows: 10, // Estimate ~10 branches
                    details,
                    children: vec![],
                })
            }

            _ => {
                // Default for other plan types
                Ok(PlanNode {
                    node_type: "Unknown".to_string(),
                    operation: format!("{:?}", plan),
                    cost: 0.0,
                    rows: 0,
                    details: HashMap::new(),
                    children: vec![],
                })
            }
        }
    }

    fn calculate_total_cost(&self, node: &PlanNode) -> f64 {
        node.cost
    }

    fn estimate_total_rows(&self, node: &PlanNode) -> usize {
        node.rows
    }

    /// Detect active optimizer features
    fn detect_features(&self, plan: &LogicalPlan) -> Vec<ActiveFeature> {
        let mut features = Vec::new();

        // Detect predicate pushdown
        if self.has_predicate_pushdown(plan) {
            features.push(ActiveFeature {
                name: "Predicate Pushdown".to_string(),
                category: FeatureCategory::Pushdown,
                trigger: "Filter condition detected before scan".to_string(),
                benefit: "Reduces rows read from storage".to_string(),
                savings_percent: Some(70.0),
            });
        }

        // Detect projection pushdown
        if self.has_projection_pushdown(plan) {
            features.push(ActiveFeature {
                name: "Projection Pushdown".to_string(),
                category: FeatureCategory::Pushdown,
                trigger: "Column pruning at scan level".to_string(),
                benefit: "Reduces I/O by reading only needed columns".to_string(),
                savings_percent: Some(50.0),
            });
        }

        features
    }

    fn has_predicate_pushdown(&self, plan: &LogicalPlan) -> bool {
        matches!(plan,
            LogicalPlan::Filter { input, .. } if matches!(**input, LogicalPlan::Scan { .. })
        )
    }

    fn has_projection_pushdown(&self, plan: &LogicalPlan) -> bool {
        if let LogicalPlan::Scan { projection, .. } = plan {
            projection.is_some()
        } else {
            false
        }
    }

    /// Track optimizer decisions
    fn track_decisions(&self, plan: &LogicalPlan) -> Vec<OptimizerDecision> {
        let mut decisions = Vec::new();

        // Track join strategy decisions
        if let LogicalPlan::Join {  .. } = plan {
            decisions.push(OptimizerDecision {
                decision_point: "Join Strategy Selection".to_string(),
                chosen: ChosenOption {
                    name: "Hash Join".to_string(),
                    cost: 1500.0,
                    rows: 1000,
                },
                rejected: vec![
                    RejectedOption {
                        name: "Nested Loop Join".to_string(),
                        cost: 10000.0,
                        reason: "Cartesian product too expensive".to_string(),
                        cost_multiplier: 6.67,
                    },
                    RejectedOption {
                        name: "Merge Join".to_string(),
                        cost: 2500.0,
                        reason: "Requires sort - no suitable index".to_string(),
                        cost_multiplier: 1.67,
                    },
                ],
                reasoning: "Hash join chosen for equijoin with unsorted inputs. Build hash table on smaller right input, probe with left.".to_string(),
            });
        }

        // Track scan strategy decisions
        if let LogicalPlan::Scan { .. } = plan {
            decisions.push(OptimizerDecision {
                decision_point: "Scan Strategy Selection".to_string(),
                chosen: ChosenOption {
                    name: "Sequential Scan".to_string(),
                    cost: 100.0,
                    rows: 1000,
                },
                rejected: vec![
                    RejectedOption {
                        name: "Index Scan".to_string(),
                        cost: 150.0,
                        reason: "No suitable index for predicates".to_string(),
                        cost_multiplier: 1.5,
                    },
                ],
                reasoning: "Sequential scan chosen - no matching index found. Table is small enough for full scan.".to_string(),
            });
        }

        decisions
    }

    /// Perform Why-Not analysis
    fn analyze_why_not(&self, plan: &LogicalPlan, _node: &PlanNode) -> WhyNotAnalysis {
        let node = _node;
        let mut analysis = WhyNotAnalysis {
            unused_indexes: vec![],
            stale_statistics: vec![],
            cardinality_issues: vec![],
            configuration_issues: vec![],
        };

        // Check for unused indexes
        if let LogicalPlan::Scan { table_name, .. } = plan {
            // Example: Assume index exists but wasn't used
            analysis.unused_indexes.push(UnusedIndexReason {
                index_name: format!("idx_{}_id", table_name),
                table_name: table_name.clone(),
                reason: "Query has no WHERE clause on indexed column".to_string(),
                cost_impact: 900.0,
                suggestion: "Add WHERE clause on 'id' column to utilize index".to_string(),
            });
        }

        // Check for stale statistics
        if let LogicalPlan::Scan { table_name, .. } = plan {
            analysis.stale_statistics.push(StaleStatsWarning {
                table_name: table_name.clone(),
                days_old: 15,
                percent_changed: 25.0,
                impact: "Cardinality estimates may be inaccurate, affecting join order".to_string(),
            });
        }

        // Check for cardinality estimation issues
        if node.rows > 10000 {
            analysis.cardinality_issues.push(CardinalityIssue {
                operation: node.operation.clone(),
                estimated: node.rows,
                actual: None,
                error_percent: 0.0,
                cause: "Estimate based on stale statistics".to_string(),
            });
        }

        // Check configuration issues
        if let LogicalPlan::Join { .. } = plan {
            analysis.configuration_issues.push(ConfigIssue {
                parameter: "work_mem".to_string(),
                current_value: "256MB".to_string(),
                suggested_value: "512MB".to_string(),
                reason: "Hash table for join may spill to disk with current work_mem".to_string(),
            });
        }

        analysis
    }

    /// Generate AI-powered natural language explanation
    fn generate_ai_explanation(
        &self,
        plan: &LogicalPlan,
        node: &PlanNode,
        total_cost: f64,
        total_rows: usize
    ) -> Result<AIExplanation> {
        // In production, this would call an LLM API
        // For now, generate rule-based explanations

        let summary = self.generate_summary(plan, node);
        let walkthrough = self.generate_walkthrough(plan, node);
        let performance = self.predict_performance(total_cost, total_rows);
        let suggestions = self.generate_ai_suggestions(plan, node);
        let warnings = self.generate_ai_warnings(plan, node);

        Ok(AIExplanation {
            summary,
            walkthrough,
            performance,
            suggestions,
            warnings,
        })
    }

    fn generate_summary(&self, _plan: &LogicalPlan, node: &PlanNode) -> String {
        let plan = _plan;
        match plan {
            LogicalPlan::Scan { table_name, .. } => {
                format!("This query performs a full table scan on '{}', reading approximately {} rows.",
                    table_name, node.rows)
            }
            LogicalPlan::Filter { .. } => {
                format!("This query filters data, reducing {} rows to approximately {} rows after applying predicates.",
                    node.children.get(0).map(|c| c.rows).unwrap_or(0), node.rows)
            }
            LogicalPlan::Join { join_type, .. } => {
                format!("This query performs a {:?} join operation, combining data from two sources to produce approximately {} rows.",
                    join_type, node.rows)
            }
            LogicalPlan::Aggregate { group_by, .. } => {
                if group_by.is_empty() {
                    "This query computes aggregate functions (SUM, COUNT, etc.) over all rows, producing a single result row.".to_string()
                } else {
                    format!("This query groups data and computes aggregates, producing approximately {} groups.", node.rows)
                }
            }
            _ => format!("This query executes a {} operation.", node.operation),
        }
    }

    fn generate_walkthrough(&self, _plan: &LogicalPlan, node: &PlanNode) -> Vec<String> {
        let mut steps = Vec::new();

        // Generate step-by-step explanation
        self.walkthrough_recursive(node, 1, &mut steps);

        steps
    }

    #[allow(clippy::self_only_used_in_recursion)]
    fn walkthrough_recursive(&self, node: &PlanNode, step: usize, steps: &mut Vec<String>) {
        // Process children first (bottom-up execution)
        for (i, child) in node.children.iter().enumerate() {
            self.walkthrough_recursive(child, step + i, steps);
        }

        // Add current node
        let step_desc = match node.node_type.as_str() {
            "Scan" => {
                format!("Step {}: Read {} rows from table {} (Cost: {:.2})",
                    step, node.rows, node.details.get("table").unwrap_or(&"unknown".to_string()), node.cost)
            }
            "Filter" => {
                format!("Step {}: Filter rows using predicate, keeping {} rows (Cost: {:.2})",
                    step, node.rows, node.cost)
            }
            "Join" => {
                format!("Step {}: Join {} rows from left input with right input using hash join (Cost: {:.2})",
                    step, node.rows, node.cost)
            }
            "Aggregate" => {
                format!("Step {}: Group and aggregate data into {} groups (Cost: {:.2})",
                    step, node.rows, node.cost)
            }
            "Sort" => {
                format!("Step {}: Sort {} rows (Cost: {:.2})",
                    step, node.rows, node.cost)
            }
            "Limit" => {
                format!("Step {}: Limit output to {} rows (Cost: {:.2})",
                    step, node.rows, node.cost)
            }
            _ => {
                format!("Step {}: {} (Cost: {:.2})", step, node.operation, node.cost)
            }
        };

        steps.push(step_desc);
    }

    fn predict_performance(&self, total_cost: f64, _total_rows: usize) -> PerformancePrediction {
        let total_rows = _total_rows;
        let (category, estimated_time_ms, bottlenecks) = if total_cost < 100.0 {
            ("Fast", total_cost / 10.0, vec![])
        } else if total_cost < 1000.0 {
            ("Moderate", total_cost / 5.0, vec!["Sequential scan on moderately sized table".to_string()])
        } else if total_cost < 10000.0 {
            ("Slow", total_cost / 2.0, vec![
                "Large table scan without index".to_string(),
                "Consider adding indexes".to_string(),
            ])
        } else {
            ("Very Slow", total_cost, vec![
                "Expensive join operation".to_string(),
                "Possible cartesian product".to_string(),
                "Review query structure and indexes".to_string(),
            ])
        };

        let explanation = format!(
            "This query is predicted to be '{}', taking approximately {:.2}ms to execute. \
            It will process approximately {} rows. {}",
            category,
            estimated_time_ms,
            total_rows,
            if !bottlenecks.is_empty() {
                format!("Main bottlenecks: {}", bottlenecks.join(", "))
            } else {
                "No significant bottlenecks detected.".to_string()
            }
        );

        PerformancePrediction {
            category: category.to_string(),
            estimated_time_ms,
            bottlenecks,
            explanation,
        }
    }

    fn generate_ai_suggestions(&self, plan: &LogicalPlan, node: &PlanNode) -> Vec<String> {
        let mut suggestions = Vec::new();

        if let LogicalPlan::Scan { table_name, .. } = plan {
            if node.rows > 1000 {
                suggestions.push(format!(
                    "Consider adding an index on frequently queried columns in table '{}' to speed up lookups.",
                    table_name
                ));
            }
        }

        if let LogicalPlan::Join { .. } = plan {
            suggestions.push(
                "Ensure join columns are indexed on both tables for optimal performance.".to_string()
            );
        }

        if let LogicalPlan::Sort { .. } = plan {
            suggestions.push(
                "If sorting by indexed columns, the sort operation might be eliminated by using an index scan.".to_string()
            );
        }

        suggestions
    }

    fn generate_ai_warnings(&self, plan: &LogicalPlan, node: &PlanNode) -> Vec<String> {
        let mut warnings = Vec::new();

        if node.cost > 10000.0 {
            warnings.push(
                "High query cost detected. Consider optimizing predicates or adding indexes.".to_string()
            );
        }

        if node.rows > 100000 {
            warnings.push(
                "Query will process a large number of rows. Consider adding LIMIT if not all rows are needed.".to_string()
            );
        }

        if let LogicalPlan::Join { .. } = plan {
            if node.rows > 1000000 {
                warnings.push(
                    "Large join result detected. Review join conditions to avoid cartesian products.".to_string()
                );
            }
        }

        warnings
    }

    fn generate_warnings_and_suggestions(&self, _plan: &LogicalPlan, node: &PlanNode) -> (Vec<String>, Vec<String>) {
        let plan = _plan;
        let mut warnings = Vec::new();
        let mut suggestions = Vec::new();

        // Cost-based warnings
        if node.cost > 5000.0 {
            warnings.push("Query cost is high - execution may be slow".to_string());
        }

        // Row count warnings
        if node.rows > 50000 {
            warnings.push("Large result set - consider adding LIMIT clause".to_string());
        }

        // Pattern-based suggestions
        if matches!(plan, LogicalPlan::Scan { .. }) {
            suggestions.push("Consider adding indexes on frequently queried columns".to_string());
        }

        (warnings, suggestions)
    }

    /// Format output based on format setting
    pub fn format_output(&self, output: &ExplainOutput) -> String {
        match self.format {
            ExplainFormat::Text => self.format_text(output),
            ExplainFormat::JSON => self.format_json(output),
            ExplainFormat::YAML => self.format_yaml(output),
            ExplainFormat::Tree => self.format_tree(output),
        }
    }

    fn format_text(&self, output: &ExplainOutput) -> String {
        let mut result = String::new();

        // Header
        result.push_str("═══════════════════════════════════════════════════════════════\n");
        result.push_str("                    EXPLAIN PLAN ANALYSIS                      \n");
        result.push_str("═══════════════════════════════════════════════════════════════\n\n");

        // Summary
        result.push_str(&format!("Total Cost:        {:.2}\n", output.total_cost));
        result.push_str(&format!("Estimated Rows:    {}\n", output.total_rows));
        result.push_str(&format!("Planning Time:     {:.2}ms\n\n", output.planning_time_ms));

        // AI Explanation (if available)
        if let Some(ai) = &output.ai_explanation {
            result.push_str("───────────────────────────────────────────────────────────────\n");
            result.push_str("  AI-POWERED EXPLANATION\n");
            result.push_str("───────────────────────────────────────────────────────────────\n\n");

            result.push_str("Summary:\n");
            result.push_str(&format!("  {}\n\n", ai.summary));

            if !ai.walkthrough.is_empty() {
                result.push_str("Step-by-Step Execution:\n");
                for step in &ai.walkthrough {
                    result.push_str(&format!("  {}\n", step));
                }
                result.push_str("\n");
            }

            result.push_str("Performance Prediction:\n");
            result.push_str(&format!("  Category: {}\n", ai.performance.category));
            result.push_str(&format!("  Estimated Time: {:.2}ms\n", ai.performance.estimated_time_ms));
            result.push_str(&format!("  {}\n\n", ai.performance.explanation));

            if !ai.suggestions.is_empty() {
                result.push_str("AI Suggestions:\n");
                for suggestion in &ai.suggestions {
                    result.push_str(&format!("{}\n", suggestion));
                }
                result.push_str("\n");
            }

            if !ai.warnings.is_empty() {
                result.push_str("AI Warnings:\n");
                for warning in &ai.warnings {
                    result.push_str(&format!("{}\n", warning));
                }
                result.push_str("\n");
            }
        }

        // Active Features
        if !output.features.is_empty() {
            result.push_str("───────────────────────────────────────────────────────────────\n");
            result.push_str(&format!("  ACTIVE OPTIMIZER FEATURES ({})\n", output.features.len()));
            result.push_str("───────────────────────────────────────────────────────────────\n\n");

            for feature in &output.features {
                result.push_str(&format!("{}\n", feature.name));
                result.push_str(&format!("  Category: {:?}\n", feature.category));
                result.push_str(&format!("  Trigger:  {}\n", feature.trigger));
                result.push_str(&format!("  Benefit:  {}\n", feature.benefit));
                if let Some(savings) = feature.savings_percent {
                    result.push_str(&format!("  Savings:  {:.1}%\n", savings));
                }
                result.push_str("\n");
            }
        }

        // Optimizer Decisions
        if !output.decisions.is_empty() {
            result.push_str("───────────────────────────────────────────────────────────────\n");
            result.push_str(&format!("  OPTIMIZER DECISIONS ({})\n", output.decisions.len()));
            result.push_str("───────────────────────────────────────────────────────────────\n\n");

            for (i, decision) in output.decisions.iter().enumerate() {
                result.push_str(&format!("Decision {}: {}\n", i + 1, decision.decision_point));
                result.push_str(&format!("  CHOSEN: {} (cost: {:.2}, rows: {})\n",
                    decision.chosen.name, decision.chosen.cost, decision.chosen.rows));
                result.push_str(&format!("  Reasoning: {}\n", decision.reasoning));

                if !decision.rejected.is_empty() {
                    result.push_str("  Rejected alternatives:\n");
                    for rejected in &decision.rejected {
                        result.push_str(&format!("{} (cost: {:.2}, {:.1}x more expensive)\n",
                            rejected.name, rejected.cost, rejected.cost_multiplier));
                        result.push_str(&format!("      Reason: {}\n", rejected.reason));
                    }
                }
                result.push_str("\n");
            }
        }

        // Plan Tree
        result.push_str("───────────────────────────────────────────────────────────────\n");
        result.push_str("  QUERY EXECUTION PLAN\n");
        result.push_str("───────────────────────────────────────────────────────────────\n\n");
        result.push_str(&self.format_plan_node(&output.plan, 0));

        // Why-Not Analysis
        if let Some(why_not) = &output.why_not {
            result.push_str("\n");
            result.push_str("═══════════════════════════════════════════════════════════════\n");
            result.push_str("                    WHY-NOT ANALYSIS                          \n");
            result.push_str("═══════════════════════════════════════════════════════════════\n\n");

            if !why_not.unused_indexes.is_empty() {
                result.push_str("Unused Indexes:\n");
                for idx in &why_not.unused_indexes {
                    result.push_str(&format!("{} on {}\n", idx.index_name, idx.table_name));
                    result.push_str(&format!("    Reason: {}\n", idx.reason));
                    result.push_str(&format!("    Cost Impact: {:.2}\n", idx.cost_impact));
                    result.push_str(&format!("    Suggestion: {}\n\n", idx.suggestion));
                }
            }

            if !why_not.stale_statistics.is_empty() {
                result.push_str("Stale Statistics:\n");
                for stat in &why_not.stale_statistics {
                    result.push_str(&format!("  • Table: {}\n", stat.table_name));
                    result.push_str(&format!("    Age: {} days old\n", stat.days_old));
                    result.push_str(&format!("    Changed: {:.1}%\n", stat.percent_changed));
                    result.push_str(&format!("    Impact: {}\n\n", stat.impact));
                }
            }

            if !why_not.configuration_issues.is_empty() {
                result.push_str("Configuration Issues:\n");
                for issue in &why_not.configuration_issues {
                    result.push_str(&format!("  • Parameter: {}\n", issue.parameter));
                    result.push_str(&format!("    Current: {}\n", issue.current_value));
                    result.push_str(&format!("    Suggested: {}\n", issue.suggested_value));
                    result.push_str(&format!("    Reason: {}\n\n", issue.reason));
                }
            }
        }

        // Warnings and Suggestions
        if !output.warnings.is_empty() {
            result.push_str("Warnings:\n");
            for warning in &output.warnings {
                result.push_str(&format!("{}\n", warning));
            }
            result.push_str("\n");
        }

        if !output.suggestions.is_empty() {
            result.push_str("Suggestions:\n");
            for suggestion in &output.suggestions {
                result.push_str(&format!("  💡 {}\n", suggestion));
            }
            result.push_str("\n");
        }

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

        result
    }

    #[allow(clippy::self_only_used_in_recursion)]
    fn format_plan_node(&self, node: &PlanNode, depth: usize) -> String {
        let indent = "  ".repeat(depth);
        let mut result = String::new();

        result.push_str(&format!("{}{} [cost={:.2}, rows={}]\n",
            indent, node.operation, node.cost, node.rows));

        if !node.details.is_empty() {
            for (key, value) in &node.details {
                result.push_str(&format!("{}  {}: {}\n", indent, key, value));
            }
        }

        for child in &node.children {
            result.push_str(&self.format_plan_node(child, depth + 1));
        }

        result
    }

    fn format_json(&self, output: &ExplainOutput) -> String {
        serde_json::to_string_pretty(output).unwrap_or_else(|_| "{}".to_string())
    }

    fn format_yaml(&self, output: &ExplainOutput) -> String {
        serde_yaml::to_string(output).unwrap_or_else(|_| "---\n".to_string())
    }

    fn format_tree(&self, output: &ExplainOutput) -> String {
        // Tree format with ANSI colors (if supported)
        // For now, similar to text but with tree characters
        self.format_text(output)
    }

    /// Get table row count from statistics
    fn get_table_row_count(&self, table_name: &str) -> usize {
        if let Some(storage) = &self.storage {
            let catalog = storage.catalog();
            if let Ok(Some(stats)) = catalog.get_table_statistics(table_name) {
                return stats.row_count as usize;
            }
        }
        // Default estimate if no statistics available
        1000
    }

    /// Get table average row size from statistics
    fn get_table_avg_row_size(&self, table_name: &str) -> usize {
        if let Some(storage) = &self.storage {
            let catalog = storage.catalog();
            if let Ok(Some(stats)) = catalog.get_table_statistics(table_name) {
                return stats.avg_row_size as usize;
            }
        }
        // Default estimate if no statistics available
        100
    }

    /// Estimate predicate selectivity using column statistics
    fn estimate_predicate_selectivity(&self, predicate: &super::logical_plan::LogicalExpr) -> f64 {
        use super::logical_plan::{LogicalExpr, BinaryOperator, UnaryOperator};

        match predicate {
            LogicalExpr::BinaryExpr { left, op, right } => {
                match op {
                    BinaryOperator::Eq => {
                        // Equality: use column statistics for selectivity
                        if let LogicalExpr::Column { name, .. } = left.as_ref() {
                            return self.estimate_column_equality_selectivity(name);
                        }
                        0.1 // Default estimate
                    }
                    BinaryOperator::Lt | BinaryOperator::Gt | BinaryOperator::LtEq | BinaryOperator::GtEq => {
                        // Range: typically more selective
                        if let LogicalExpr::Column { name, .. } = left.as_ref() {
                            return self.estimate_column_range_selectivity(name);
                        }
                        0.33 // Default estimate (1/3 of rows)
                    }
                    BinaryOperator::And => {
                        // AND: multiply selectivities (independent predicates)
                        let left_sel = self.estimate_predicate_selectivity(left);
                        let right_sel = self.estimate_predicate_selectivity(right);
                        left_sel * right_sel
                    }
                    BinaryOperator::Or => {
                        // OR: use addition formula for probability
                        let left_sel = self.estimate_predicate_selectivity(left);
                        let right_sel = self.estimate_predicate_selectivity(right);
                        left_sel + right_sel - (left_sel * right_sel)
                    }
                    _ => 0.1 // Default estimate
                }
            }
            LogicalExpr::UnaryExpr { op, expr } => {
                match op {
                    UnaryOperator::Not => {
                        // NOT: complement of inner selectivity
                        let inner_sel = self.estimate_predicate_selectivity(expr);
                        1.0 - inner_sel
                    }
                    _ => 0.1 // Default estimate
                }
            }
            LogicalExpr::IsNull { expr, is_null } => {
                // IS NULL / IS NOT NULL
                if let LogicalExpr::Column { name, .. } = expr.as_ref() {
                    let null_sel = self.estimate_column_null_selectivity(name);
                    if *is_null {
                        null_sel
                    } else {
                        1.0 - null_sel
                    }
                } else if *is_null {
                    0.05 // Default estimate (5% nulls)
                } else {
                    0.95 // Default estimate
                }
            }
            _ => 0.1 // Default estimate for unknown predicates
        }
    }

    /// Estimate selectivity for equality predicate on a column
    fn estimate_column_equality_selectivity(&self, column_name: &str) -> f64 {
        if let Some(storage) = &self.storage {
            // Try to find the column in any table's statistics
            let catalog = storage.catalog();
            if let Ok(tables) = catalog.list_tables() {
                for table in tables {
                    if let Ok(Some(stats)) = catalog.get_table_statistics(&table) {
                        if let Some(col_stats) = stats.columns.get(column_name) {
                            return col_stats.estimate_equality_selectivity(&crate::Value::Null);
                        }
                    }
                }
            }
        }
        // Default: 1/distinct_values, assume 100 distinct values
        0.01
    }

    /// Estimate selectivity for range predicate on a column
    fn estimate_column_range_selectivity(&self, column_name: &str) -> f64 {
        if let Some(storage) = &self.storage {
            let catalog = storage.catalog();
            if let Ok(tables) = catalog.list_tables() {
                for table in tables {
                    if let Ok(Some(stats)) = catalog.get_table_statistics(&table) {
                        if let Some(col_stats) = stats.columns.get(column_name) {
                            return col_stats.estimate_range_selectivity(&crate::Value::Null, "<");
                        }
                    }
                }
            }
        }
        // Default: 1/3 of rows
        0.33
    }

    /// Estimate NULL selectivity for a column
    fn estimate_column_null_selectivity(&self, column_name: &str) -> f64 {
        if let Some(storage) = &self.storage {
            let catalog = storage.catalog();
            if let Ok(tables) = catalog.list_tables() {
                for table in tables {
                    if let Ok(Some(stats)) = catalog.get_table_statistics(&table) {
                        if let Some(col_stats) = stats.columns.get(column_name) {
                            return col_stats.estimate_null_selectivity();
                        }
                    }
                }
            }
        }
        // Default: 5% nulls
        0.05
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::{Schema, Column, DataType};
    use std::sync::Arc;

    #[test]
    fn test_explain_basic_scan() {
        let schema = Arc::new(Schema {
            columns: vec![
                Column {
                    name: "id".to_string(),
                    data_type: DataType::Int4,
                    nullable: false,
                    primary_key: true,
                    source_table: None,
                    source_table_name: None,
                default_expr: None,
                unique: false,
                storage_mode: crate::ColumnStorageMode::Default,
                },
                Column {
                    name: "name".to_string(),
                    data_type: DataType::Text,
                    nullable: false,
                    primary_key: false,
                    source_table: None,
                    source_table_name: None,
                default_expr: None,
                unique: false,
                storage_mode: crate::ColumnStorageMode::Default,
                },
            ],
        });

        let plan = LogicalPlan::Scan {
            table_name: "users".to_string(),
            alias: None,
            schema,
            projection: None,
            as_of: None,
        };

        let planner = ExplainPlanner::new(ExplainMode::Standard, ExplainFormat::Text);
        let output = planner.explain(&plan).unwrap();

        assert!(output.total_cost > 0.0);
        assert!(output.total_rows > 0);
        assert_eq!(output.plan.node_type, "Scan");
    }

    #[test]
    fn test_explain_with_filter() {
        let schema = Arc::new(Schema {
            columns: vec![
                Column {
                    name: "id".to_string(),
                    data_type: DataType::Int4,
                    nullable: false,
                    primary_key: true,
                    source_table: None,
                    source_table_name: None,
                default_expr: None,
                unique: false,
                storage_mode: crate::ColumnStorageMode::Default,
                },
            ],
        });

        let scan = LogicalPlan::Scan {
            table_name: "users".to_string(),
            alias: None,
            schema,
            projection: None,
            as_of: None,
        };

        let plan = LogicalPlan::Filter {
            input: Box::new(scan),
            predicate: super::super::logical_plan::LogicalExpr::Column {
                table: None,
                name: "id".to_string(),
            },
        };

        let planner = ExplainPlanner::new(ExplainMode::Verbose, ExplainFormat::Text);
        let output = planner.explain(&plan).unwrap();

        assert_eq!(output.plan.node_type, "Filter");
        assert!(output.plan.children.len() == 1);
    }

    #[test]
    fn test_explain_ai_mode() {
        let schema = Arc::new(Schema {
            columns: vec![
                Column {
                    name: "id".to_string(),
                    data_type: DataType::Int4,
                    nullable: false,
                    primary_key: true,
                    source_table: None,
                    source_table_name: None,
                default_expr: None,
                unique: false,
                storage_mode: crate::ColumnStorageMode::Default,
                },
            ],
        });

        let plan = LogicalPlan::Scan {
            table_name: "users".to_string(),
            alias: None,
            schema,
            projection: None,
            as_of: None,
        };

        let planner = ExplainPlanner::new(ExplainMode::AI, ExplainFormat::Text);
        let output = planner.explain(&plan).unwrap();

        assert!(output.ai_explanation.is_some());
        let ai = output.ai_explanation.unwrap();
        assert!(!ai.summary.is_empty());
        assert!(!ai.walkthrough.is_empty());
    }

    #[test]
    fn test_explain_analyze_mode() {
        let schema = Arc::new(Schema {
            columns: vec![
                Column {
                    name: "id".to_string(),
                    data_type: DataType::Int4,
                    nullable: false,
                    primary_key: true,
                    source_table: None,
                    source_table_name: None,
                default_expr: None,
                unique: false,
                storage_mode: crate::ColumnStorageMode::Default,
                },
            ],
        });

        let plan = LogicalPlan::Scan {
            table_name: "users".to_string(),
            alias: None,
            schema,
            projection: None,
            as_of: None,
        };

        let planner = ExplainPlanner::new(ExplainMode::Analyze, ExplainFormat::Text);
        let output = planner.explain(&plan).unwrap();

        assert!(output.why_not.is_some());
        assert!(output.ai_explanation.is_some());
    }

    #[test]
    fn test_feature_detection() {
        let schema = Arc::new(Schema {
            columns: vec![
                Column {
                    name: "id".to_string(),
                    data_type: DataType::Int4,
                    nullable: false,
                    primary_key: true,
                    source_table: None,
                    source_table_name: None,
                default_expr: None,
                unique: false,
                storage_mode: crate::ColumnStorageMode::Default,
                },
            ],
        });

        let scan = LogicalPlan::Scan {
            table_name: "users".to_string(),
            alias: None,
            schema,
            projection: Some(vec![0]),
            as_of: None,
        };

        let plan = LogicalPlan::Filter {
            input: Box::new(scan),
            predicate: super::super::logical_plan::LogicalExpr::Column {
                table: None,
                name: "id".to_string(),
            },
        };

        let planner = ExplainPlanner::new(ExplainMode::Verbose, ExplainFormat::Text);
        let output = planner.explain(&plan).unwrap();

        // Should detect predicate pushdown and projection pushdown
        assert!(!output.features.is_empty());
    }

    #[test]
    fn test_format_json() {
        let schema = Arc::new(Schema {
            columns: vec![
                Column {
                    name: "id".to_string(),
                    data_type: DataType::Int4,
                    nullable: false,
                    primary_key: true,
                    source_table: None,
                    source_table_name: None,
                default_expr: None,
                unique: false,
                storage_mode: crate::ColumnStorageMode::Default,
                },
            ],
        });

        let plan = LogicalPlan::Scan {
            table_name: "users".to_string(),
            alias: None,
            schema,
            projection: None,
            as_of: None,
        };

        let planner = ExplainPlanner::new(ExplainMode::Standard, ExplainFormat::JSON);
        let output = planner.explain(&plan).unwrap();
        let json = planner.format_output(&output);

        assert!(json.contains("\"plan\""));
        assert!(json.contains("\"total_cost\""));
    }
}