akar-main 0.1.12

Akar - pure Rust embedded graph database for AI agent memory
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
use akar_common::error::ProcessorError;
use akar_common::types::Value;
use akar_common::vector::DataChunk;
use akar_parser::ast::Expression;
use akar_processor::processor::{StandaloneCallFn, StandaloneCallHandler, StandaloneCallRegistry};
use std::sync::Arc;

use crate::connection::utils::{ast_constant_to_value, rows_to_datachunk, value_to_csv_string};
use crate::database::Database;

/// Callback for executing an arbitrary Cypher query string.
pub type QueryFn = Arc<dyn Fn(&str) -> Result<crate::query_result::QueryResult, String> + Send + Sync>;

pub struct DbStandaloneCallHandler {
    database: Arc<Database>,
    registry: StandaloneCallRegistry,
}

impl DbStandaloneCallHandler {
    pub fn new(database: Arc<Database>) -> Self {
        Self::with_query_executor(database, None)
    }

    pub fn with_query_executor(database: Arc<Database>, query_fn: Option<QueryFn>) -> Self {
        let mut registry = StandaloneCallRegistry::new();
        registry.register(Arc::new(ShowTablesHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(TableInfoHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(ShowFunctionsHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(ShowIndexesHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(ShowSequencesHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(ShowMacrosHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(ShowConnectionHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(DbVersionHandler));
        registry.register(Arc::new(CatalogVersionHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(CurrentSettingHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(StatsInfoHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(StorageInfoHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(ShowAttachedDatabasesHandler));
        registry.register(Arc::new(BmInfoHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(FileInfoHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(FreeSpaceInfoHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(DiskSizeInfoHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(StorageVersionHandler));
        registry.register(Arc::new(ShowLoadedExtensionsHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(ShowOfficialExtensionsHandler));
        registry.register(Arc::new(ClearWarningsHandler));
        registry.register(Arc::new(ShowWarningsHandler));
        registry.register(Arc::new(ShowProjectedGraphsHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(ProjectedGraphInfoHandler {
            database: database.clone(),
        }));
        registry.register(Arc::new(DropProjectedGraphHandler {
            database: database.clone(),
        }));
        if let Some(qf) = query_fn {
            registry.register(Arc::new(ExportCsvHandler { query_fn: qf.clone() }));
            registry.register(Arc::new(ExportParquetHandler { query_fn: qf }));
        }
        Self { database, registry }
    }
}

fn eval_ast_expr_to_value(expr: &Expression) -> Value {
    match expr {
        Expression::Constant(c) => ast_constant_to_value(c),
        _ => Value::Null,
    }
}

fn extract_arg_string(args: &[Expression], idx: usize) -> Result<String, String> {
    if idx >= args.len() {
        return Err(format!("Missing argument at index {} ({} provided)", idx, args.len()));
    }
    match &args[idx] {
        Expression::Constant(c) => match c {
            akar_parser::ast::Constant::String(s) => Ok(s.clone()),
            _ => Err(format!("Argument {} expected a string literal, got {:?}", idx, c)),
        },
        other => Err(format!(
            "Argument {} expected a constant expression, got: {:?}",
            idx, other
        )),
    }
}

impl StandaloneCallHandler for DbStandaloneCallHandler {
    fn execute_call(&self, name: &str, args: &[Expression]) -> Result<Vec<DataChunk>, ProcessorError> {
        if let Some(handler) = self.registry.get(name) {
            let result_rows = handler.execute(args)?;
            return Self::format_result(result_rows);
        }

        let args_vals: Vec<Value> = args.iter().map(eval_ast_expr_to_value).collect();
        let graph = akar_processor::processor::CatalogGraphSource::new(Some(&self.database.table_catalog()));
        let registry = self
            .database
            .function_registry
            .lock()
            .map_err(|e| format!("Lock poisoned: {e}"))?;
        match registry.execute_table_function(name, &args_vals, Some(&graph)) {
            Ok(rows) => Self::format_result(rows),
            Err(original_err) => {
                let known_calls = [
                    "show_tables",
                    "table_info",
                    "show_functions",
                    "show_indexes",
                    "show_sequences",
                    "show_macros",
                    "show_connection",
                    "db_version",
                    "catalog_version",
                    "current_setting",
                    "stats_info",
                    "storage_info",
                    "show_attached_databases",
                    "bm_info",
                    "file_info",
                    "free_space_info",
                    "disk_size_info",
                    "storage_version",
                    "show_loaded_extensions",
                    "show_official_extensions",
                    "clear_warnings",
                    "show_warnings",
                    "show_projected_graphs",
                    "projected_graph_info",
                    "drop_projected_graph",
                    "export_csv",
                    "export_parquet",
                ];
                let lower = name.to_lowercase();
                let suggestion = known_calls
                    .iter()
                    .find(|k| k.contains(&lower) || lower.contains(**k))
                    .map(|k| format!(" Did you mean CALL {}()?", k))
                    .unwrap_or_default();
                Err(ProcessorError::Execution(format!(
                    "CALL '{}' failed: {}.{}",
                    name, original_err, suggestion
                )))
            }
        }
    }
}

impl DbStandaloneCallHandler {
    fn format_result(result_rows: Vec<Vec<Value>>) -> Result<Vec<DataChunk>, ProcessorError> {
        if result_rows.is_empty() {
            Ok(vec![])
        } else {
            let num_cols = result_rows[0].len();
            let col_names_strings = (0..num_cols).map(|i| format!("col_{}", i)).collect::<Vec<_>>();
            let col_names = col_names_strings.iter().map(|s| s.as_str()).collect::<Vec<_>>();
            Ok(vec![rows_to_datachunk(result_rows, &col_names)?])
        }
    }
}

struct ShowTablesHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for ShowTablesHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let catalog = self
            .database
            .catalog
            .lock()
            .map_err(|e| format!("Lock poisoned: {e}"))?;
        let entries: Vec<Vec<Value>> = catalog
            .all_entries()
            .map(|e| {
                let kind = match e {
                    akar_catalog::CatalogEntry::NodeTable(_) => "NODE",
                    akar_catalog::CatalogEntry::RelTable(_) => "REL",
                    akar_catalog::CatalogEntry::Sequence(_) => "SEQUENCE",
                    akar_catalog::CatalogEntry::Macro(_) => "MACRO",
                    akar_catalog::CatalogEntry::VectorIndex(_) => "VECTOR_INDEX",
                    akar_catalog::CatalogEntry::Foreign(_) => "FOREIGN",
                };
                vec![Value::String(e.name().to_string()), Value::String(kind.to_string())]
            })
            .collect();
        Ok(entries)
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_tables", "show tables", "list_tables", "list tables", "tables"]
    }
}

struct TableInfoHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for TableInfoHandler {
    fn execute(&self, args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let table_name = extract_arg_string(args, 0)?;
        let cat = self
            .database
            .catalog
            .lock()
            .map_err(|e| format!("Lock poisoned: {e}"))?;
        let entry = cat
            .get_entry_by_name(&table_name)
            .ok_or_else(|| format!("Table '{table_name}' not found"))?;
        let columns = entry.columns();
        let rows: Vec<Vec<Value>> = columns
            .iter()
            .map(|col| {
                vec![
                    Value::String(table_name.clone()),
                    Value::String(col.name.clone()),
                    Value::String(format!("{:?}", col.logical_type)),
                    Value::String(if col.is_primary_key { "NO" } else { "YES" }.into()),
                ]
            })
            .collect();
        Ok(rows)
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["table_info"]
    }
}

struct ShowFunctionsHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for ShowFunctionsHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let registry = self
            .database
            .function_registry
            .lock()
            .map_err(|e| format!("Lock poisoned: {e}"))?;
        let funcs = registry.list_all();
        Ok(funcs
            .into_iter()
            .map(|(name, kind)| vec![Value::String(name), Value::String(kind)])
            .collect())
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_functions"]
    }
}

struct ShowIndexesHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for ShowIndexesHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let cat = self
            .database
            .catalog
            .lock()
            .map_err(|e| format!("Lock poisoned: {e}"))?;
        let indexes = cat.indexes();
        Ok(indexes
            .into_iter()
            .map(|(name, table, kind, col)| {
                vec![
                    Value::String(name),
                    Value::String(table),
                    Value::String(kind),
                    Value::String(col),
                ]
            })
            .collect())
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_indexes"]
    }
}

struct ShowSequencesHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for ShowSequencesHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let cat = self
            .database
            .catalog
            .lock()
            .map_err(|e| format!("Lock poisoned: {e}"))?;
        let seqs = cat.sequences();
        Ok(seqs
            .into_iter()
            .map(|s| vec![Value::String(s.name.clone()), Value::Int64(s.curr_val())])
            .collect())
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_sequences"]
    }
}

struct ShowMacrosHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for ShowMacrosHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let cat = self
            .database
            .catalog
            .lock()
            .map_err(|e| format!("Lock poisoned: {e}"))?;
        let macros = cat.macros();
        Ok(macros
            .into_iter()
            .map(|m| {
                vec![
                    Value::String(m.name.clone()),
                    Value::String(
                        m.default_args
                            .iter()
                            .map(|(k, v)| format!("{k}={v}"))
                            .collect::<Vec<_>>()
                            .join(", "),
                    ),
                ]
            })
            .collect())
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_macros"]
    }
}

struct ShowConnectionHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for ShowConnectionHandler {
    fn execute(&self, args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let table_name = extract_arg_string(args, 0)?;
        let cat = self
            .database
            .catalog
            .lock()
            .map_err(|e| format!("Lock poisoned: {e}"))?;
        let info = cat
            .connection_info(&table_name)
            .ok_or_else(|| format!("Table '{table_name}' not found"))?;
        Ok(vec![info])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_connection"]
    }
}

struct DbVersionHandler;

impl StandaloneCallFn for DbVersionHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let version = env!("CARGO_PKG_VERSION");
        Ok(vec![vec![Value::String(version.to_string())]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["db_version"]
    }
}

struct CatalogVersionHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for CatalogVersionHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let cat = self
            .database
            .catalog
            .lock()
            .map_err(|e| format!("Lock poisoned: {e}"))?;
        let ver = cat.version();
        Ok(vec![vec![Value::Int64(ver as i64)]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["catalog_version"]
    }
}

struct CurrentSettingHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for CurrentSettingHandler {
    fn execute(&self, args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let key = extract_arg_string(args, 0).unwrap_or_else(|_| String::new());
        let (k, v) = match key.to_lowercase().as_str() {
            "spill_threshold" => ("spill_threshold", self.database.effective_spill_threshold().to_string()),
            "checkpoint_threshold" => (
                "checkpoint_threshold",
                self.database.config.checkpoint_threshold.to_string(),
            ),
            "buffer_pool_size" => ("buffer_pool_size", self.database.config.buffer_pool_size.to_string()),
            "max_num_threads" => ("max_num_threads", self.database.config.max_num_threads.to_string()),
            "concurrent_writes" => (
                "concurrent_writes",
                // Read the live runtime toggle (SET concurrent_writes) rather
                // than the static config so current_setting stays in sync (P52.50).
                self.database.transaction_manager.allow_concurrent_writes().to_string(),
            ),
            "read_only" => ("read_only", self.database.config.read_only.to_string()),
            _ => (key.as_str(), "UNKNOWN".to_string()),
        };
        Ok(vec![vec![Value::String(k.to_string()), Value::String(v)]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["current_setting"]
    }
}

struct StatsInfoHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for StatsInfoHandler {
    fn execute(&self, args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let table_name = extract_arg_string(args, 0)?;
        let (row_count, storage_size) = {
            let cat = self
                .database
                .catalog
                .lock()
                .map_err(|e| format!("Lock poisoned: {e}"))?;
            let table_id = cat
                .get_table_id(&table_name)
                .ok_or_else(|| format!("Table '{table_name}' not found"))?;
            let stats = self
                .database
                .stats_store
                .lock()
                .map_err(|e| format!("Lock poisoned: {e}"))?;
            stats.table_stats_by_id(table_id)
        };
        Ok(vec![vec![
            Value::String(table_name),
            Value::Int64(row_count as i64),
            Value::String(crate::connection::utils::format_storage_size(storage_size)),
        ]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["stats_info"]
    }
}

struct StorageInfoHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for StorageInfoHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let sm = &self.database.storage_manager;
        let info = sm.storage_info();
        Ok(vec![vec![
            Value::String(info.db_path),
            Value::Int64(info.page_size as i64),
            Value::Int64(info.total_pages as i64),
            Value::Int64(info.free_pages as i64),
        ]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["storage_info"]
    }
}

struct ShowAttachedDatabasesHandler;

impl StandaloneCallFn for ShowAttachedDatabasesHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        Ok(vec![vec![
            Value::String("main".to_string()),
            Value::String("local".to_string()),
        ]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_attached_databases"]
    }
}

struct BmInfoHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for BmInfoHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let bm = &self.database.storage_manager;
        let info = bm.buffer_info();
        Ok(vec![vec![
            Value::String("buffer_pool".to_string()),
            Value::Int64(info.total_memory as i64),
            Value::Int64(info.used_memory as i64),
            Value::Int64(info.num_pinned as i64),
        ]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["bm_info"]
    }
}

struct FileInfoHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for FileInfoHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let sm = &self.database.storage_manager;
        let info = sm.file_info();
        Ok(vec![vec![
            Value::Int64(info.total_file_size as i64),
            Value::Int64(info.num_data_pages as i64),
            Value::Int64(info.wal_size as i64),
        ]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["file_info"]
    }
}

struct FreeSpaceInfoHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for FreeSpaceInfoHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let sm = &self.database.storage_manager;
        let info = sm.fsm_info();
        Ok(vec![vec![
            Value::Int64(info.total_free_pages as i64),
            Value::Int64(info.num_entries as i64),
        ]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["free_space_info"]
    }
}

struct DiskSizeInfoHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for DiskSizeInfoHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let sm = &self.database.storage_manager;
        let info = sm.file_info();
        Ok(vec![vec![
            Value::Int64(info.total_file_size as i64),
            Value::Int64(info.num_data_pages as i64),
            Value::Int64(info.wal_size as i64),
        ]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["disk_size_info"]
    }
}

struct StorageVersionHandler;

impl StandaloneCallFn for StorageVersionHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        Ok(vec![vec![Value::String(
            akar_storage::version_info::STORAGE_VERSION.to_string(),
        )]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["storage_version"]
    }
}

struct ShowLoadedExtensionsHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for ShowLoadedExtensionsHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let reg = self
            .database
            .extension_registry
            .lock()
            .map_err(|e| format!("Lock poisoned: {e}"))?;
        let names: Vec<Vec<Value>> = reg.names().iter().map(|n| vec![Value::String(n.clone())]).collect();
        Ok(names)
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_loaded_extensions"]
    }
}

struct ShowOfficialExtensionsHandler;

impl StandaloneCallFn for ShowOfficialExtensionsHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        Ok(vec![
            vec![Value::String("json".into()), Value::String("JSON functions".into())],
            vec![Value::String("fts".into()), Value::String("Full-Text Search".into())],
            vec![
                Value::String("vector".into()),
                Value::String("Vector similarity search".into()),
            ],
            vec![
                Value::String("httpfs".into()),
                Value::String("HTTP/S3 file access".into()),
            ],
            vec![
                Value::String("duckdb".into()),
                Value::String("DuckDB integration".into()),
            ],
            vec![
                Value::String("sqlite".into()),
                Value::String("SQLite integration".into()),
            ],
            vec![
                Value::String("postgres".into()),
                Value::String("PostgreSQL integration".into()),
            ],
            vec![
                Value::String("delta".into()),
                Value::String("Delta Lake integration".into()),
            ],
            vec![
                Value::String("iceberg".into()),
                Value::String("Apache Iceberg integration".into()),
            ],
            vec![
                Value::String("azure".into()),
                Value::String("Azure Blob Storage".into()),
            ],
            vec![
                Value::String("unity_catalog".into()),
                Value::String("Unity Catalog integration".into()),
            ],
            vec![Value::String("neo4j".into()), Value::String("Neo4j integration".into())],
            vec![Value::String("llm".into()), Value::String("LLM integration".into())],
            vec![Value::String("algo".into()), Value::String("Graph algorithms".into())],
        ])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_official_extensions"]
    }
}

struct ClearWarningsHandler;

impl StandaloneCallFn for ClearWarningsHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        Ok(vec![vec![Value::String("Warnings cleared".into())]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["clear_warnings"]
    }
}

struct ShowWarningsHandler;

impl StandaloneCallFn for ShowWarningsHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        Ok(vec![])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_warnings"]
    }
}

// ==================== Export CSV / Parquet handlers ====================
// Wraps COPY TO as CALL functions: export_csv / export_parquet
// Usage:  CALL export_csv('file.csv', 'MATCH (n) RETURN n');
//         CALL export_parquet('file.parquet', 'MATCH (n) RETURN n');

struct ExportCsvHandler {
    query_fn: QueryFn,
}

impl StandaloneCallFn for ExportCsvHandler {
    fn execute(&self, args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let file_path = extract_arg_string(args, 0)?;
        let query_string = extract_arg_string(args, 1)?;
        let result = (self.query_fn)(&query_string)?;

        let path = std::path::Path::new(&file_path);
        let mut w = csv::WriterBuilder::new()
            .has_headers(true)
            .from_path(path)
            .map_err(|e| format!("Cannot create CSV file '{}': {}", file_path, e))?;

        if let Some(first_chunk) = result.chunks.first() {
            let header: Vec<String> = if first_chunk.field_names.is_empty() {
                (0..first_chunk.num_fields()).map(|i| format!("column_{}", i)).collect()
            } else {
                first_chunk.field_names.clone()
            };
            if !header.is_empty() {
                w.write_record(&header).map_err(|e| format!("CSV write error: {e}"))?;
            }
        }
        for chunk in &result.chunks {
            for row in 0..chunk.size {
                let row_values: Vec<String> = (0..chunk.fields.len())
                    .map(|col_idx| {
                        chunk
                            .get_value(col_idx, row)
                            .map(|v| value_to_csv_string(&v))
                            .unwrap_or_default()
                    })
                    .collect();
                w.write_record(&row_values)
                    .map_err(|e| format!("CSV write error: {e}"))?;
            }
        }
        w.flush().map_err(|e| format!("CSV flush error: {e}"))?;
        Ok(vec![vec![Value::String(format!(
            "Exported {} rows to '{}'",
            result.num_rows, file_path
        ))]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["export_csv"]
    }
}

struct ExportParquetHandler {
    query_fn: QueryFn,
}

impl StandaloneCallFn for ExportParquetHandler {
    fn execute(&self, args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let file_path = extract_arg_string(args, 0)?;
        let query_string = extract_arg_string(args, 1)?;
        let result = (self.query_fn)(&query_string)?;

        #[cfg(feature = "parquet-export")]
        {
            crate::connection::ddl::write_parquet_to_file(&file_path, &result)
                .map_err(|e| format!("Parquet export error: {e}"))?;
            Ok(vec![vec![Value::String(format!(
                "Exported {} rows to '{}'",
                result.num_rows, file_path
            ))]])
        }
        #[cfg(not(feature = "parquet-export"))]
        {
            let _ = file_path;
            let _ = query_string;
            let _ = result;
            Err("Parquet export requires 'parquet-export' feature. \
                 Build with: cargo build --features parquet-export"
                .into())
        }
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["export_parquet"]
    }
}

// ==================== Projected Graph handlers ====================

struct ShowProjectedGraphsHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for ShowProjectedGraphsHandler {
    fn execute(&self, _args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let cat = self.database.catalog.lock().map_err(|e| format!("Lock error: {e}"))?;
        let graphs = cat.projected_graph_entries();
        let rows: Vec<Vec<Value>> = graphs
            .into_iter()
            .map(|g| vec![Value::String(g.name.clone()), Value::String(g.entry_type.clone())])
            .collect();
        Ok(rows)
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["show_projected_graphs"]
    }
}

struct ProjectedGraphInfoHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for ProjectedGraphInfoHandler {
    fn execute(&self, args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let graph_name = extract_arg_string(args, 0)?;
        let cat = self.database.catalog.lock().map_err(|e| format!("Lock error: {e}"))?;
        let info = cat
            .get_projected_graph(&graph_name)
            .ok_or_else(|| format!("Projected graph '{}' not found", graph_name))?;
        match info.entry_type.as_str() {
            "NATIVE" => {
                // NATIVE projected graph: return name, type marker
                Ok(vec![vec![
                    Value::String(info.name.clone()),
                    Value::String("NATIVE".into()),
                    Value::String("Node/rel tables defined at creation".into()),
                ]])
            }
            "CYPHER" => {
                let query = info.cypher_query.clone().unwrap_or_default();
                Ok(vec![vec![
                    Value::String(info.name.clone()),
                    Value::String("CYPHER".into()),
                    Value::String(query),
                ]])
            }
            other => Err(ProcessorError::Execution(format!(
                "Unknown projected graph type: {other}"
            ))),
        }
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["projected_graph_info"]
    }
}

struct DropProjectedGraphHandler {
    database: Arc<Database>,
}

impl StandaloneCallFn for DropProjectedGraphHandler {
    fn execute(&self, args: &[Expression]) -> Result<Vec<Vec<Value>>, ProcessorError> {
        let graph_name = extract_arg_string(args, 0)?;
        let mut cat = self.database.catalog.lock().map_err(|e| format!("Lock error: {e}"))?;
        cat.drop_projected_graph(&graph_name)
            .map_err(|e| ProcessorError::Execution(format!("{e}")))?;
        Ok(vec![vec![Value::String(format!(
            "Projected graph '{}' dropped",
            graph_name
        ))]])
    }

    fn aliases(&self) -> Vec<&'static str> {
        vec!["drop_projected_graph"]
    }
}