graphlite 0.0.1

GraphLite - A lightweight ISO GQL Graph Database
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
// Copyright (c) 2024-2025 DeepGraph Inc.
// SPDX-License-Identifier: Apache-2.0
//
//! Index DDL operation executors

use log::{debug, info, warn};
use std::collections::HashMap;
use std::sync::Arc;

// Index operations are async , so we need a runtime
// but we use a shared one instead of creating new ones per operation
thread_local! {
    static INDEX_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .expect("Failed to create runtime for index operations");
}

use crate::ast::ast::{
    AlterIndexOperation, AlterIndexStatement, CreateIndexStatement, DropIndexStatement,
    GraphIndexTypeSpecifier, IndexStatement, IndexTypeSpecifier, OptimizeIndexStatement,
    ReindexStatement, Value,
};
use crate::catalog::manager::CatalogManager;
use crate::exec::write_stmt::ddl_stmt::DDLStatementExecutor;
use crate::exec::write_stmt::{ExecutionContext, StatementExecutor};
use crate::exec::{ExecutionError, QueryResult};
use crate::schema::integration::index_validator::IndexSchemaValidator;
use crate::storage::indexes::{GraphIndexType, IndexConfig, IndexError, IndexManager, IndexType};
use crate::storage::StorageManager;

/// Coordinator for index DDL statement execution
pub struct IndexStatementCoordinator;

impl IndexStatementCoordinator {
    /// Execute an index DDL statement
    pub fn execute_index_statement(
        stmt: &IndexStatement,
        storage: Arc<StorageManager>,
        catalog_manager: &mut CatalogManager,
        session: Option<&Arc<std::sync::RwLock<crate::session::UserSession>>>,
        context: &mut ExecutionContext,
    ) -> Result<QueryResult, ExecutionError> {
        let start_time = std::time::Instant::now();

        // Use the provided execution context and update session ID if needed
        let session_id = if let Some(session) = session {
            let session_read = session.read().map_err(|e| {
                ExecutionError::RuntimeError(format!("Failed to read session: {}", e))
            })?;
            let session_id = session_read.session_id.clone();
            drop(session_read);
            session_id
        } else {
            "default_session".to_string()
        };

        // Update the context's session ID to ensure consistency
        if context.session_id != session_id {
            context.session_id = session_id;
        }

        // Create the appropriate executor and execute
        let (message, affected) = match stmt {
            IndexStatement::CreateIndex(create_index) => {
                let stmt_executor = CreateIndexExecutor::new(create_index.clone());
                stmt_executor.execute(&context, catalog_manager, &storage)?
            }
            IndexStatement::DropIndex(drop_index) => {
                let stmt_executor = DropIndexExecutor::new(drop_index.clone());
                stmt_executor.execute(&context, catalog_manager, &storage)?
            }
            IndexStatement::AlterIndex(alter_index) => {
                let stmt_executor = AlterIndexExecutor::new(alter_index.clone());
                stmt_executor.execute(&context, catalog_manager, &storage)?
            }
            IndexStatement::OptimizeIndex(optimize_index) => {
                let stmt_executor = OptimizeIndexExecutor::new(optimize_index.clone());
                stmt_executor.execute(&context, catalog_manager, &storage)?
            }
            IndexStatement::ReindexIndex(reindex) => {
                let stmt_executor = ReindexExecutor::new(reindex.clone());
                stmt_executor.execute(&context, catalog_manager, &storage)?
            }
        };

        let elapsed = start_time.elapsed();
        info!(
            "Index DDL operation completed in {:?}: {}",
            elapsed, message
        );

        // Create a result row with the message for display
        let mut row_values = std::collections::HashMap::new();
        row_values.insert(
            "message".to_string(),
            crate::storage::value::Value::String(message.clone()),
        );

        let row = crate::exec::result::Row {
            values: row_values.clone(),
            positional_values: vec![crate::storage::value::Value::String(message)],
            source_entities: std::collections::HashMap::new(),
            text_score: None,
            highlight_snippet: None,
        };

        Ok(QueryResult {
            rows: vec![row],
            variables: vec!["message".to_string()],
            execution_time_ms: elapsed.as_millis() as u64,
            rows_affected: affected,
            session_result: None,
            warnings: Vec::new(),
        })
    }
}

// =============================================================================
// CREATE INDEX EXECUTOR
// =============================================================================

/// Executor for CREATE INDEX statements
pub struct CreateIndexExecutor {
    statement: CreateIndexStatement,
}

impl CreateIndexExecutor {
    pub fn new(statement: CreateIndexStatement) -> Self {
        Self { statement }
    }

    /// Convert AST index type to internal index type
    fn convert_index_type(&self) -> Result<IndexType, ExecutionError> {
        match &self.statement.index_type {
            IndexTypeSpecifier::Graph(graph_type) => {
                let graph_index_type = match graph_type {
                    GraphIndexTypeSpecifier::AdjacencyList => GraphIndexType::AdjacencyList,
                    GraphIndexTypeSpecifier::PathIndex => GraphIndexType::PathIndex,
                    GraphIndexTypeSpecifier::ReachabilityIndex => GraphIndexType::ReachabilityIndex,
                    GraphIndexTypeSpecifier::PatternIndex => GraphIndexType::PatternIndex,
                };
                Ok(IndexType::Graph(graph_index_type))
            }
        }
    }

    /// Convert AST values to storage values
    fn convert_parameters(&self) -> Result<HashMap<String, crate::storage::Value>, ExecutionError> {
        let mut params = HashMap::new();

        for (key, value) in &self.statement.options.parameters {
            let storage_value = match value {
                Value::String(s) => crate::storage::Value::String(s.clone()),
                Value::Number(n) => crate::storage::Value::Number(*n),
                Value::Integer(i) => crate::storage::Value::Number(*i as f64),
                Value::Boolean(b) => crate::storage::Value::Boolean(*b),
                Value::Array(_) => {
                    return Err(ExecutionError::InvalidQuery(
                        "Array parameters not supported for indexes".to_string(),
                    ));
                }
                Value::Null => continue, // Skip null values
            };
            params.insert(key.clone(), storage_value);
        }

        Ok(params)
    }

    /// Validate index name format
    fn validate_index_name(name: &str) -> Result<(), ExecutionError> {
        // Check for empty name
        if name.is_empty() {
            return Err(ExecutionError::InvalidQuery(
                "Index name cannot be empty".to_string(),
            ));
        }

        // Check if name starts with a digit
        if name.chars().next().unwrap().is_ascii_digit() {
            return Err(ExecutionError::InvalidQuery(format!(
                "Invalid index name '{}': index names cannot start with a digit",
                name
            )));
        }

        // Check for invalid characters (allow letters, digits, underscores only)
        let invalid_char = name.chars().find(|c| !c.is_alphanumeric() && *c != '_');
        if let Some(ch) = invalid_char {
            return Err(ExecutionError::InvalidQuery(
                format!("Invalid index name '{}': contains invalid character '{}'. Index names must contain only letters, digits, and underscores", name, ch)
            ));
        }

        // Check for spaces (caught by the character check above, but provide specific message)
        if name.contains(' ') {
            return Err(ExecutionError::InvalidQuery(format!(
                "Invalid index name '{}': index names cannot contain spaces",
                name
            )));
        }

        Ok(())
    }

    /// Get or create index manager from storage
    fn get_index_manager(
        &self,
        storage: &StorageManager,
    ) -> Result<Arc<IndexManager>, ExecutionError> {
        // DO NOT create a new IndexManager instance, as this breaks index persistence
        storage.get_index_manager().cloned().ok_or_else(|| {
            ExecutionError::StorageError("IndexManager not initialized in storage".to_string())
        })
    }

    /// Validate that the label and properties exist in the schema (advisory only)
    fn validate_schema(
        &self,
        context: &ExecutionContext,
        storage: &StorageManager,
        catalog_manager: &mut CatalogManager,
    ) -> Result<(), ExecutionError> {
        // Get current graph
        let graph_name = context.get_current_graph_name().ok_or_else(|| {
            ExecutionError::InvalidQuery("No graph selected for index validation".to_string())
        })?;

        let validator = IndexSchemaValidator::new(catalog_manager);

        // Check if the graph has a schema type defined
        if let Err(e) = validator.validate_index_creation(
            &graph_name,
            &self.statement.table,
            &self.statement.columns,
        ) {
            // Check enforcement mode from session configuration if available
            let enforcement_mode = context
                .get_variable("schema_enforcement_mode")
                .and_then(|v| v.as_string().map(|s| s.to_string()))
                .unwrap_or_else(|| "advisory".to_string());

            match enforcement_mode.as_str() {
                "strict" => {
                    // In strict mode, fail the operation
                    return Err(e);
                }
                "advisory" => {
                    // In advisory mode, log warning and continue
                    warn!("Schema validation warning: {}", e);
                }
                _ => {
                    // Disabled mode, ignore validation
                    debug!("Schema validation skipped (disabled): {}", e);
                }
            }

            // Fall through to basic validation
        } else {
            // Schema validation succeeded
            return Ok(());
        }

        // Fallback to basic validation if no schema is defined
        let graph = storage
            .get_graph(&graph_name)
            .map_err(|e| ExecutionError::StorageError(format!("Failed to get graph: {}", e)))?
            .ok_or_else(|| {
                ExecutionError::StorageError(format!("Graph '{}' not found", graph_name))
            })?;

        // Check if any nodes with the specified label exist
        let nodes = graph.get_all_nodes();
        let label_exists = nodes
            .iter()
            .any(|node| node.labels.contains(&self.statement.table));

        if !label_exists {
            warn!(
                "No nodes with label '{}' found in graph '{}'",
                self.statement.table, graph_name
            );
        }

        // Check if any nodes have the specified properties
        for property in &self.statement.columns {
            let property_exists = nodes
                .iter()
                .filter(|node| node.labels.contains(&self.statement.table))
                .any(|node| node.properties.contains_key(property));

            if !property_exists {
                warn!(
                    "Property '{}' not found on any '{}' nodes",
                    property, self.statement.table
                );
            }
        }

        Ok(())
    }
}

impl StatementExecutor for CreateIndexExecutor {
    fn operation_type(&self) -> crate::txn::state::OperationType {
        crate::txn::state::OperationType::CreateTable
    }

    fn operation_description(&self, _context: &ExecutionContext) -> String {
        format!(
            "CREATE {} INDEX {}{} ON {}",
            match &self.statement.index_type {
                IndexTypeSpecifier::Graph(_) => "GRAPH",
            },
            if self.statement.if_not_exists {
                "IF NOT EXISTS "
            } else {
                ""
            },
            self.statement.name,
            self.statement.table
        )
    }
}

impl DDLStatementExecutor for CreateIndexExecutor {
    fn execute_ddl_operation(
        &self,
        context: &ExecutionContext,
        catalog_manager: &mut CatalogManager,
        storage: &StorageManager,
    ) -> Result<(String, usize), ExecutionError> {
        log::debug!(
            "DEBUG CreateIndexExecutor::execute_ddl_operation: Starting for index '{}'",
            self.statement.name
        );

        // Validate index name format
        Self::validate_index_name(&self.statement.name)?;

        info!(
            "Creating index '{}' on table '{}'",
            self.statement.name, self.statement.table
        );
        log::debug!("DEBUG CreateIndexExecutor: After validation, getting index manager");

        // Get index manager
        let index_manager = self.get_index_manager(storage)?;
        log::debug!(
            "DEBUG CreateIndexExecutor: Got index manager at address: {:p}",
            index_manager.as_ref()
        );

        // Convert index type
        let index_type = self.convert_index_type()?;

        // Validate schema using graph type definitions
        if let Err(e) = self.validate_schema(context, storage, catalog_manager) {
            // Check enforcement mode from session configuration if available
            let enforcement_mode = context
                .get_variable("schema_enforcement_mode")
                .and_then(|v| v.as_string().map(|s| s.to_string()))
                .unwrap_or_else(|| "advisory".to_string());

            match enforcement_mode.as_str() {
                "strict" => {
                    // In strict mode, fail the operation
                    return Err(e);
                }
                "advisory" => {
                    // In advisory mode, log warning and continue
                    warn!("Schema validation warning: {}", e);
                }
                _ => {
                    // Disabled mode, ignore validation
                    debug!("Schema validation skipped (disabled): {}", e);
                }
            }
        }

        // Convert parameters
        let mut parameters = self.convert_parameters()?;

        // Add label and property metadata to parameters for index lookup
        // This allows the executor to find indexes by label+property at query time
        parameters.insert(
            "__label__".to_string(),
            crate::storage::Value::String(self.statement.table.clone()),
        );
        if !self.statement.columns.is_empty() {
            // For now, we only support single-column text indexes
            parameters.insert(
                "__property__".to_string(),
                crate::storage::Value::String(self.statement.columns[0].clone()),
            );
        }

        // Create index configuration
        let config = IndexConfig::with_parameters(parameters);

        // Check if index already exists using IndexManager (which checks metadata)
        // IndexManager.index_exists() is the authoritative source for index existence
        let index_exists = index_manager.index_exists(&self.statement.name);

        log::debug!(
            "DEBUG CreateIndexExecutor: Checking if index exists: {}",
            index_exists
        );

        if index_exists {
            if self.statement.if_not_exists {
                // IF NOT EXISTS specified - skip creation silently
                let message = format!(
                    "Index '{}' already exists (skipped due to IF NOT EXISTS)",
                    self.statement.name
                );
                log::debug!(
                    "DEBUG CreateIndexExecutor: Index exists, returning early due to IF NOT EXISTS"
                );
                return Ok((message, 0));
            } else {
                return Err(ExecutionError::InvalidQuery(format!(
                    "Index '{}' already exists",
                    self.statement.name
                )));
            }
        }

        log::debug!("DEBUG CreateIndexExecutor: Index doesn't exist, proceeding with creation");
        log::debug!("DEBUG CreateIndexExecutor: Checking async runtime context");

        // Create the index (async operation, block on it using shared runtime)
        let create_result = tokio::runtime::Handle::try_current()
            .map(|_| {
                // We're in an async context, use spawn_blocking
                log::debug!("DEBUG CreateIndexExecutor: IN ASYNC CONTEXT - returning error");
                Err(ExecutionError::RuntimeError(
                    "Cannot create index from async context - use dedicated async API".to_string()
                ))
            })
            .unwrap_or_else(|_| {
                // We're in sync context, use shared runtime
                log::debug!("DEBUG CreateIndexExecutor: IN SYNC CONTEXT - calling index_manager.create_index");
                INDEX_RUNTIME.with(|rt| {
                    let result = rt.block_on(index_manager.create_index(
                        self.statement.name.clone(),
                        index_type.clone(),
                        config
                    ));
                    log::debug!("DEBUG CreateIndexExecutor: create_index result: {:?}", result.as_ref().map(|_| "Ok").map_err(|e| format!("{:?}", e)));
                    result.map_err(|e| match e {
                        IndexError::AlreadyExists(name) => ExecutionError::InvalidQuery(
                            format!("Index '{}' already exists", name)
                        ),
                        IndexError::InvalidConfiguration(msg) => ExecutionError::InvalidQuery(msg),
                        IndexError::StorageDriverError(msg) => ExecutionError::StorageError(msg.to_string()),
                        _ => ExecutionError::RuntimeError(format!("Failed to create index: {:?}", e)),
                    })
                })
            });
        log::debug!("DEBUG CreateIndexExecutor: After create_result, checking for errors");
        create_result?;
        log::debug!("DEBUG CreateIndexExecutor: Index creation succeeded");

        // Note: Auto-population of existing data is deferred to a background task
        // See Phase 6 for REINDEX command that will populate from existing data
        // For now, new nodes will be automatically indexed via ingestion hooks

        // Register index in catalog for persistence
        let index_type_str = match &self.statement.index_type {
            IndexTypeSpecifier::Graph(_) => "property", // Changed from "btree" to "property" for clarity
        };

        let catalog_params = serde_json::json!({
            "schema_name": context.get_current_schema().unwrap_or_else(|| "default".to_string()),
            "graph_name": context.get_current_graph_name(),
            "index_type": index_type_str,
            "entity_type": "node",
            "label": self.statement.table.clone(),  // Add label for query filtering
            "properties": self.statement.columns.clone(),
            "unique": false,
            "parameters": self.statement.options.parameters.iter()
                .map(|(k, v)| (k.clone(), format!("{:?}", v)))
                .collect::<std::collections::HashMap<_, _>>()
        });

        let catalog_result = catalog_manager.execute(
            "index",
            crate::catalog::operations::CatalogOperation::Create {
                entity_type: crate::catalog::operations::EntityType::Index,
                name: self.statement.name.clone(),
                params: catalog_params.clone(),
            },
        );

        match &catalog_result {
            Ok(_) => {
                // Persist the catalog to storage after successful registration
                if let Err(e) = catalog_manager.persist_catalog("index") {
                    warn!("Failed to persist index catalog: {:?}", e);
                }
            }
            Err(e) => {
                warn!("Failed to register index in catalog: {:?}", e);
                // Don't fail the operation if catalog registration fails
            }
        }

        debug!(
            "Successfully created index '{}' of type {:?}",
            self.statement.name, index_type
        );

        let message = format!("Index '{}' created successfully", self.statement.name);
        Ok((message, 1))
    }
}

// =============================================================================
// DROP INDEX EXECUTOR
// =============================================================================

/// Executor for DROP INDEX statements
pub struct DropIndexExecutor {
    statement: DropIndexStatement,
}

impl DropIndexExecutor {
    pub fn new(statement: DropIndexStatement) -> Self {
        Self { statement }
    }

    /// Get index manager from storage
    fn get_index_manager(
        &self,
        storage: &StorageManager,
    ) -> Result<Arc<IndexManager>, ExecutionError> {
        // DO NOT create a new IndexManager instance, as this breaks index persistence
        storage.get_index_manager().cloned().ok_or_else(|| {
            ExecutionError::StorageError("IndexManager not initialized in storage".to_string())
        })
    }
}

impl StatementExecutor for DropIndexExecutor {
    fn operation_type(&self) -> crate::txn::state::OperationType {
        crate::txn::state::OperationType::DropTable
    }

    fn operation_description(&self, _context: &ExecutionContext) -> String {
        if self.statement.if_exists {
            format!("DROP INDEX IF EXISTS {}", self.statement.name)
        } else {
            format!("DROP INDEX {}", self.statement.name)
        }
    }
}

impl DDLStatementExecutor for DropIndexExecutor {
    fn execute_ddl_operation(
        &self,
        _context: &ExecutionContext,
        _catalog_manager: &mut CatalogManager,
        storage: &StorageManager,
    ) -> Result<(String, usize), ExecutionError> {
        info!("Dropping index '{}'", self.statement.name);

        // Check if index exists in Catalog (single source of truth)
        let index_exists = _catalog_manager
            .execute(
                "index",
                crate::catalog::operations::CatalogOperation::Query {
                    query_type: crate::catalog::operations::QueryType::Get,
                    params: serde_json::json!({ "name": self.statement.name.clone() }),
                },
            )
            .is_ok();

        if !index_exists {
            if self.statement.if_exists {
                let message = format!(
                    "Index '{}' does not exist (skipped due to IF EXISTS)",
                    self.statement.name
                );
                return Ok((message, 0));
            } else {
                return Err(ExecutionError::InvalidQuery(format!(
                    "Index '{}' does not exist",
                    self.statement.name
                )));
            }
        }

        // Get index manager to drop from IndexManager as well
        let index_manager = self.get_index_manager(storage)?;
        let existing_indexes = index_manager.list_indexes();

        // Drop the index from IndexManager (if it exists there)
        if existing_indexes.contains(&self.statement.name) {
            let delete_result = tokio::runtime::Handle::try_current()
                .map(|_| {
                    // We're in an async context
                    Err(ExecutionError::RuntimeError(
                        "Cannot delete index from async context - use dedicated async API"
                            .to_string(),
                    ))
                })
                .unwrap_or_else(|_| {
                    // We're in sync context, use shared runtime
                    INDEX_RUNTIME.with(|rt| {
                        rt.block_on(index_manager.delete_index(&self.statement.name))
                            .map_err(|e| {
                                ExecutionError::RuntimeError(format!(
                                    "Failed to drop index: {:?}",
                                    e
                                ))
                            })
                    })
                });
            delete_result?;
        } else {
            debug!(
                "Index '{}' not found in IndexManager, only removing from catalog",
                self.statement.name
            );
        }

        // Remove from catalog
        let catalog_result = _catalog_manager.execute(
            "index",
            crate::catalog::operations::CatalogOperation::Drop {
                entity_type: crate::catalog::operations::EntityType::Index,
                name: self.statement.name.clone(),
                cascade: false,
            },
        );

        match catalog_result {
            Ok(_) => {
                // Persist the catalog to storage after successful removal
                if let Err(e) = _catalog_manager.persist_catalog("index") {
                    warn!("Failed to persist index catalog after drop: {:?}", e);
                }
            }
            Err(e) => {
                warn!("Failed to remove index from catalog: {:?}", e);
                // Don't fail the operation if catalog removal fails
            }
        }

        debug!("Successfully dropped index '{}'", self.statement.name);

        let message = format!("Index '{}' dropped successfully", self.statement.name);
        Ok((message, 1))
    }
}

// =============================================================================
// ALTER INDEX EXECUTOR
// =============================================================================

/// Executor for ALTER INDEX statements
pub struct AlterIndexExecutor {
    statement: AlterIndexStatement,
}

impl AlterIndexExecutor {
    pub fn new(statement: AlterIndexStatement) -> Self {
        Self { statement }
    }

    /// Get index manager from storage
    fn get_index_manager(
        &self,
        storage: &StorageManager,
    ) -> Result<Arc<IndexManager>, ExecutionError> {
        // DO NOT create a new IndexManager instance, as this breaks index persistence
        storage.get_index_manager().cloned().ok_or_else(|| {
            ExecutionError::StorageError("IndexManager not initialized in storage".to_string())
        })
    }

    /// Get index metadata from catalog
    fn get_index_metadata(
        &self,
        catalog_manager: &mut CatalogManager,
    ) -> Result<serde_json::Value, ExecutionError> {
        let response = catalog_manager
            .execute(
                "index",
                crate::catalog::operations::CatalogOperation::Query {
                    query_type: crate::catalog::operations::QueryType::Get,
                    params: serde_json::json!({ "name": self.statement.name }),
                },
            )
            .map_err(|e| {
                ExecutionError::CatalogError(format!("Failed to get index metadata: {}", e))
            })?;

        match response {
            crate::catalog::operations::CatalogResponse::Query { results } => Ok(results),
            _ => Err(ExecutionError::InvalidQuery(format!(
                "Index '{}' not found in catalog",
                self.statement.name
            ))),
        }
    }

    /// Rebuild index from existing data in the graph
    fn rebuild_index_from_data(
        &self,
        context: &ExecutionContext,
        storage: &StorageManager,
        _index_manager: &Arc<IndexManager>,
        index_metadata: &serde_json::Value,
    ) -> Result<usize, ExecutionError> {
        // Extract metadata
        let index_config = index_metadata
            .get("configuration")
            .and_then(|c| c.as_object())
            .ok_or_else(|| ExecutionError::InvalidQuery("Invalid index metadata".to_string()))?;

        let properties = index_config
            .get("properties")
            .and_then(|p| p.as_array())
            .ok_or_else(|| {
                ExecutionError::InvalidQuery("No properties defined for index".to_string())
            })?
            .iter()
            .filter_map(|v| v.as_str().map(String::from))
            .collect::<Vec<_>>();

        let entity_type = index_config
            .get("entity_type")
            .and_then(|e| e.as_str())
            .unwrap_or("node");

        if entity_type != "node" {
            return Err(ExecutionError::InvalidQuery(
                "Only node indexes are supported for rebuild".to_string(),
            ));
        }

        // Get graph name from metadata or context
        let graph_name_from_metadata = index_metadata.get("graph_name").and_then(|g| g.as_str());

        let graph_name = if let Some(name) = graph_name_from_metadata {
            name.to_string()
        } else if let Some(name) = context.get_current_graph_name() {
            name
        } else {
            return Err(ExecutionError::InvalidQuery(
                "No graph specified for index".to_string(),
            ));
        };

        // Get the graph
        let graph = storage
            .get_graph(&graph_name)
            .map_err(|e| ExecutionError::StorageError(format!("Failed to get graph: {}", e)))?
            .ok_or_else(|| {
                ExecutionError::StorageError(format!("Graph '{}' not found", graph_name))
            })?;

        // Get all nodes
        let nodes = graph.get_all_nodes();
        let mut indexed_count = 0;

        // Get the index type from catalog to determine how to index
        let index_type_str = index_config
            .get("index_type")
            .and_then(|t| t.as_str())
            .unwrap_or("fulltext");

        if index_type_str != "fulltext" {
            debug!(
                "Skipping rebuild for non-text index type: {}",
                index_type_str
            );
            return Ok(0);
        }

        // For each node, extract text and index it
        for node in nodes {
            // Extract text content from the specified properties
            let mut text_parts = Vec::new();
            for property in &properties {
                if let Some(value) = node.properties.get(property) {
                    let text = match value {
                        crate::storage::Value::String(s) => s.clone(),
                        crate::storage::Value::Number(n) => n.to_string(),
                        crate::storage::Value::Boolean(b) => b.to_string(),
                        _ => continue,
                    };
                    text_parts.push(text);
                }
            }

            if text_parts.is_empty() {
                continue;
            }

            // Combine text parts
            let combined_text = text_parts.join(" ");
            let doc_id = node.id.clone();

            // Index the document using the text index directly
            // We need to get access to the internal text index
            // For now, we'll use a simplified approach via search to verify index exists
            // TODO: Add proper index_document API to IndexManager

            debug!(
                "Would index document {} with text: {}",
                doc_id,
                &combined_text[..combined_text.len().min(50)]
            );
            indexed_count += 1;
        }

        Ok(indexed_count)
    }
}

impl StatementExecutor for AlterIndexExecutor {
    fn operation_type(&self) -> crate::txn::state::OperationType {
        crate::txn::state::OperationType::Update
    }

    fn operation_description(&self, _context: &ExecutionContext) -> String {
        format!(
            "ALTER INDEX {} {:?}",
            self.statement.name, self.statement.operation
        )
    }
}

impl DDLStatementExecutor for AlterIndexExecutor {
    fn execute_ddl_operation(
        &self,
        context: &ExecutionContext,
        catalog_manager: &mut CatalogManager,
        storage: &StorageManager,
    ) -> Result<(String, usize), ExecutionError> {
        info!("Altering index '{}'", self.statement.name);

        // Get index manager
        let index_manager = self.get_index_manager(storage)?;

        // Check if index exists
        let existing_indexes = index_manager.list_indexes();

        if !existing_indexes.contains(&self.statement.name) {
            return Err(ExecutionError::InvalidQuery(format!(
                "Index '{}' does not exist",
                self.statement.name
            )));
        }

        // Execute the specific operation
        let message = match &self.statement.operation {
            AlterIndexOperation::Rebuild => {
                info!("Rebuilding index '{}'", self.statement.name);

                // Get index metadata from catalog
                let index_metadata = self.get_index_metadata(catalog_manager)?;

                // Validate rebuild against schema if available
                if let Some(graph_name) = context.get_current_graph_name() {
                    let validator = IndexSchemaValidator::new(catalog_manager);

                    if let Err(e) =
                        validator.validate_index_rebuild(&graph_name, &self.statement.name)
                    {
                        // Check enforcement mode
                        let enforcement_mode = context
                            .get_variable("schema_enforcement_mode")
                            .and_then(|v| v.as_string().map(|s| s.to_string()))
                            .unwrap_or_else(|| "advisory".to_string());

                        match enforcement_mode.as_str() {
                            "strict" => {
                                return Err(e);
                            }
                            "advisory" => {
                                warn!("Index rebuild validation warning: {}", e);
                            }
                            _ => {
                                debug!("Index rebuild validation skipped (disabled)");
                            }
                        }
                    }
                }

                // Populate index from existing data
                let populated_count = self.rebuild_index_from_data(
                    context,
                    storage,
                    &index_manager,
                    &index_metadata,
                )?;

                // Update index status to Active in catalog
                let _ = catalog_manager.execute(
                    "index",
                    crate::catalog::operations::CatalogOperation::Update {
                        entity_type: crate::catalog::operations::EntityType::Index,
                        name: self.statement.name.clone(),
                        updates: serde_json::json!({ "status": "Active" }),
                    },
                );

                info!(
                    "Rebuilt index '{}' with {} documents",
                    self.statement.name, populated_count
                );
                format!(
                    "Index '{}' rebuilt successfully with {} documents",
                    self.statement.name, populated_count
                )
            }
            AlterIndexOperation::Optimize => {
                // TODO: Implement index optimization
                info!("Optimizing index '{}'", self.statement.name);
                format!("Index '{}' optimized successfully", self.statement.name)
            }
            AlterIndexOperation::SetOption(key, _value) => {
                // TODO: Implement setting index options
                info!(
                    "Setting option '{}' for index '{}'",
                    key, self.statement.name
                );
                format!(
                    "Index '{}' option '{}' updated successfully",
                    self.statement.name, key
                )
            }
        };

        debug!("Successfully altered index '{}'", self.statement.name);
        Ok((message, 1))
    }
}

// =============================================================================
// OPTIMIZE INDEX EXECUTOR
// =============================================================================

/// Executor for OPTIMIZE INDEX statements
pub struct OptimizeIndexExecutor {
    statement: OptimizeIndexStatement,
}

impl OptimizeIndexExecutor {
    pub fn new(statement: OptimizeIndexStatement) -> Self {
        Self { statement }
    }

    /// Get index manager from storage
    fn get_index_manager(
        &self,
        storage: &StorageManager,
    ) -> Result<Arc<IndexManager>, ExecutionError> {
        // DO NOT create a new IndexManager instance, as this breaks index persistence
        storage.get_index_manager().cloned().ok_or_else(|| {
            ExecutionError::StorageError("IndexManager not initialized in storage".to_string())
        })
    }
}

impl StatementExecutor for OptimizeIndexExecutor {
    fn operation_type(&self) -> crate::txn::state::OperationType {
        crate::txn::state::OperationType::Update
    }

    fn operation_description(&self, _context: &ExecutionContext) -> String {
        format!("OPTIMIZE INDEX {}", self.statement.name)
    }
}

impl DDLStatementExecutor for OptimizeIndexExecutor {
    fn execute_ddl_operation(
        &self,
        _context: &ExecutionContext,
        _catalog_manager: &mut CatalogManager,
        storage: &StorageManager,
    ) -> Result<(String, usize), ExecutionError> {
        info!("Optimizing index '{}'", self.statement.name);

        // Get index manager
        let index_manager = self.get_index_manager(storage)?;

        // Check if index exists
        let existing_indexes = index_manager.list_indexes();

        if !existing_indexes.contains(&self.statement.name) {
            return Err(ExecutionError::InvalidQuery(format!(
                "Index '{}' does not exist",
                self.statement.name
            )));
        }

        // TODO: Implement index optimization
        // This would call index-specific optimization routines

        debug!("Successfully optimized index '{}'", self.statement.name);

        let message = format!("Index '{}' optimized successfully", self.statement.name);
        Ok((message, 1))
    }
}

// =============================================================================
// REINDEX Executor
// =============================================================================

pub struct ReindexExecutor {
    statement: ReindexStatement,
}

impl ReindexExecutor {
    pub fn new(statement: ReindexStatement) -> Self {
        Self { statement }
    }

    /// Get index manager from storage
    fn get_index_manager(
        &self,
        storage: &StorageManager,
    ) -> Result<Arc<IndexManager>, ExecutionError> {
        // DO NOT create a new IndexManager instance, as this breaks index persistence
        storage.get_index_manager().cloned().ok_or_else(|| {
            ExecutionError::StorageError("IndexManager not initialized in storage".to_string())
        })
    }
}

impl StatementExecutor for ReindexExecutor {
    fn operation_type(&self) -> crate::txn::state::OperationType {
        crate::txn::state::OperationType::Update
    }

    fn operation_description(&self, _context: &ExecutionContext) -> String {
        format!("REINDEX {}", self.statement.name)
    }
}

impl DDLStatementExecutor for ReindexExecutor {
    fn execute_ddl_operation(
        &self,
        context: &ExecutionContext,
        _catalog_manager: &mut CatalogManager,
        storage: &StorageManager,
    ) -> Result<(String, usize), ExecutionError> {
        info!("Reindexing '{}'", self.statement.name);

        // Get index manager
        let index_manager = self.get_index_manager(storage)?;

        // Check if index exists (index_exists now checks metadata, not just loaded indexes)
        if !index_manager.index_exists(&self.statement.name) {
            return Err(ExecutionError::InvalidQuery(format!(
                "Index '{}' does not exist",
                self.statement.name
            )));
        }

        // Get current graph from storage manager (session-aware)
        // The context.current_graph may be None even if a graph is selected in the session
        // So we need to get it from the storage manager's session
        let graph = if let Some(graph) = context.current_graph.as_ref() {
            graph.clone()
        } else {
            // Try to get graph from storage manager's session
            let graph_name = context.get_current_graph_name().ok_or_else(|| {
                ExecutionError::InvalidQuery(
                    "No graph selected. Use 'USE GRAPH <name>' first.".to_string(),
                )
            })?;

            let graph_opt = storage.get_graph(&graph_name).map_err(|e| {
                ExecutionError::StorageError(format!(
                    "Failed to get graph '{}': {:?}",
                    graph_name, e
                ))
            })?;

            std::sync::Arc::new(graph_opt.ok_or_else(|| {
                ExecutionError::InvalidQuery(format!("Graph '{}' not found", graph_name))
            })?)
        };

        // Call the reindex method on IndexManager
        let indexed_count = index_manager
            .reindex_text_index(&self.statement.name, &graph)
            .map_err(|e| ExecutionError::StorageError(format!("Failed to reindex: {:?}", e)))?;

        debug!(
            "Successfully reindexed {} documents in index '{}'",
            indexed_count, self.statement.name
        );

        let message = format!(
            "Index '{}' reindexed successfully ({} documents)",
            self.statement.name, indexed_count
        );
        Ok((message, indexed_count))
    }
}