oxirs-arq 0.2.4

Jena-style SPARQL algebra with extension points and query optimization
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
//! Query Hints System for SPARQL Query Optimization
//!
//! This module provides a comprehensive query hints system that allows users to guide
//! the query optimizer with specific optimization directives. Similar to SQL query hints
//! in PostgreSQL, MySQL, and Oracle.
//!
//! # Features
//!
//! - **Join Hints**: Specify join algorithms (HASH_JOIN, MERGE_JOIN, NESTED_LOOP)
//! - **Index Hints**: Force or avoid specific indexes
//! - **Cardinality Hints**: Override cardinality estimates
//! - **Parallelism Hints**: Control parallel execution
//! - **Materialization Hints**: Control intermediate result materialization
//! - **Timeout Hints**: Set query-specific timeouts
//! - **Memory Hints**: Control memory allocation for query processing
//!
//! # Usage
//!
//! ```sparql
//! # Hint comments are embedded in SPARQL queries
//! # /*+ HASH_JOIN(?s, ?o) PARALLEL(4) TIMEOUT(30s) */
//! SELECT ?s ?p ?o WHERE { ?s ?p ?o }
//! ```
//!
//! # Example
//!
//! ```rust
//! use oxirs_arq::query_hints::{QueryHints, JoinHint, HintParser};
//!
//! let query = r#"
//!     # /*+ HASH_JOIN(?person, ?name) CARDINALITY(?person, 1000) */
//!     SELECT ?person ?name WHERE {
//!         ?person <http://xmlns.com/foaf/0.1/name> ?name .
//!     }
//! "#;
//!
//! let hints = HintParser::parse(query)?;
//! println!("Parsed hints: {:?}", hints);
//! # Ok::<(), anyhow::Error>(())
//! ```

use crate::algebra::Variable;
use anyhow::{anyhow, Result};
use regex::Regex;
// Metrics imports available for future use
#[allow(unused_imports)]
use scirs2_core::metrics::{Counter, Timer};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, OnceLock};
use std::time::Duration;

/// Query hints for optimizer guidance
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct QueryHints {
    /// Join algorithm hints
    pub join_hints: Vec<JoinHint>,
    /// Index usage hints
    pub index_hints: Vec<IndexHint>,
    /// Cardinality override hints
    pub cardinality_hints: Vec<CardinalityHint>,
    /// Parallelism configuration hints
    pub parallelism_hints: Option<ParallelismHint>,
    /// Materialization strategy hints
    pub materialization_hints: Vec<MaterializationHint>,
    /// Query timeout hint
    pub timeout_hint: Option<Duration>,
    /// Memory limit hint
    pub memory_hint: Option<MemoryHint>,
    /// Cache control hints
    pub cache_hints: Option<CacheHint>,
    /// Ordering hints for join execution
    pub join_order_hint: Option<JoinOrderHint>,
    /// Filter pushdown hints
    pub filter_hints: Vec<FilterHint>,
    /// Custom optimizer directives
    pub custom_directives: HashMap<String, String>,
}

impl QueryHints {
    /// Create new empty hints
    pub fn new() -> Self {
        Self::default()
    }

    /// Create hints builder
    pub fn builder() -> QueryHintsBuilder {
        QueryHintsBuilder::new()
    }

    /// Check if any hints are specified
    pub fn is_empty(&self) -> bool {
        self.join_hints.is_empty()
            && self.index_hints.is_empty()
            && self.cardinality_hints.is_empty()
            && self.parallelism_hints.is_none()
            && self.materialization_hints.is_empty()
            && self.timeout_hint.is_none()
            && self.memory_hint.is_none()
            && self.cache_hints.is_none()
            && self.join_order_hint.is_none()
            && self.filter_hints.is_empty()
            && self.custom_directives.is_empty()
    }

    /// Get hint count
    pub fn hint_count(&self) -> usize {
        let mut count = self.join_hints.len()
            + self.index_hints.len()
            + self.cardinality_hints.len()
            + self.materialization_hints.len()
            + self.filter_hints.len()
            + self.custom_directives.len();

        if self.parallelism_hints.is_some() {
            count += 1;
        }
        if self.timeout_hint.is_some() {
            count += 1;
        }
        if self.memory_hint.is_some() {
            count += 1;
        }
        if self.cache_hints.is_some() {
            count += 1;
        }
        if self.join_order_hint.is_some() {
            count += 1;
        }
        count
    }

    /// Merge hints from another QueryHints instance (other takes precedence)
    pub fn merge(&mut self, other: QueryHints) {
        self.join_hints.extend(other.join_hints);
        self.index_hints.extend(other.index_hints);
        self.cardinality_hints.extend(other.cardinality_hints);
        self.materialization_hints
            .extend(other.materialization_hints);
        self.filter_hints.extend(other.filter_hints);
        self.custom_directives.extend(other.custom_directives);

        if other.parallelism_hints.is_some() {
            self.parallelism_hints = other.parallelism_hints;
        }
        if other.timeout_hint.is_some() {
            self.timeout_hint = other.timeout_hint;
        }
        if other.memory_hint.is_some() {
            self.memory_hint = other.memory_hint;
        }
        if other.cache_hints.is_some() {
            self.cache_hints = other.cache_hints;
        }
        if other.join_order_hint.is_some() {
            self.join_order_hint = other.join_order_hint;
        }
    }

    /// Get join hint for specific variables
    pub fn get_join_hint(&self, vars: &[Variable]) -> Option<&JoinHint> {
        self.join_hints.iter().find(|hint| {
            vars.iter()
                .all(|v| hint.variables.iter().any(|hv| hv == v.name()))
        })
    }

    /// Get cardinality hint for a variable
    pub fn get_cardinality_hint(&self, var: &Variable) -> Option<u64> {
        self.cardinality_hints
            .iter()
            .find(|hint| hint.variable == var.name())
            .map(|hint| hint.cardinality)
    }

    /// Get index hint for a pattern
    pub fn get_index_hint(&self, pattern_id: &str) -> Option<&IndexHint> {
        self.index_hints
            .iter()
            .find(|hint| hint.pattern_id == pattern_id)
    }
}

/// Builder for QueryHints
#[derive(Debug, Default)]
pub struct QueryHintsBuilder {
    hints: QueryHints,
}

impl QueryHintsBuilder {
    /// Create new builder
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a join hint
    pub fn with_join_hint(mut self, hint: JoinHint) -> Self {
        self.hints.join_hints.push(hint);
        self
    }

    /// Add hash join hint for variables
    pub fn hash_join(self, variables: Vec<&str>) -> Self {
        self.with_join_hint(JoinHint::new(
            variables.into_iter().map(String::from).collect(),
            JoinAlgorithmHint::HashJoin,
        ))
    }

    /// Add merge join hint for variables
    pub fn merge_join(self, variables: Vec<&str>) -> Self {
        self.with_join_hint(JoinHint::new(
            variables.into_iter().map(String::from).collect(),
            JoinAlgorithmHint::MergeJoin,
        ))
    }

    /// Add nested loop join hint for variables
    pub fn nested_loop_join(self, variables: Vec<&str>) -> Self {
        self.with_join_hint(JoinHint::new(
            variables.into_iter().map(String::from).collect(),
            JoinAlgorithmHint::NestedLoop,
        ))
    }

    /// Add an index hint
    pub fn with_index_hint(mut self, hint: IndexHint) -> Self {
        self.hints.index_hints.push(hint);
        self
    }

    /// Force use of a specific index
    pub fn use_index(self, pattern_id: &str, index_name: &str) -> Self {
        self.with_index_hint(IndexHint::use_index(pattern_id, index_name))
    }

    /// Ignore a specific index
    pub fn ignore_index(self, pattern_id: &str, index_name: &str) -> Self {
        self.with_index_hint(IndexHint::ignore_index(pattern_id, index_name))
    }

    /// Add a cardinality hint
    pub fn with_cardinality_hint(mut self, hint: CardinalityHint) -> Self {
        self.hints.cardinality_hints.push(hint);
        self
    }

    /// Override cardinality for a variable
    pub fn cardinality(self, variable: &str, cardinality: u64) -> Self {
        self.with_cardinality_hint(CardinalityHint::new(variable, cardinality))
    }

    /// Set parallelism hint
    pub fn with_parallelism(mut self, hint: ParallelismHint) -> Self {
        self.hints.parallelism_hints = Some(hint);
        self
    }

    /// Enable parallel execution with specified threads
    pub fn parallel(self, threads: usize) -> Self {
        self.with_parallelism(ParallelismHint::new(threads))
    }

    /// Disable parallel execution
    pub fn no_parallel(self) -> Self {
        self.with_parallelism(ParallelismHint::disabled())
    }

    /// Add materialization hint
    pub fn with_materialization_hint(mut self, hint: MaterializationHint) -> Self {
        self.hints.materialization_hints.push(hint);
        self
    }

    /// Set query timeout
    pub fn timeout(mut self, duration: Duration) -> Self {
        self.hints.timeout_hint = Some(duration);
        self
    }

    /// Set timeout in seconds
    pub fn timeout_secs(self, secs: u64) -> Self {
        self.timeout(Duration::from_secs(secs))
    }

    /// Set memory hint
    pub fn with_memory_hint(mut self, hint: MemoryHint) -> Self {
        self.hints.memory_hint = Some(hint);
        self
    }

    /// Set memory limit in bytes
    pub fn memory_limit(self, bytes: usize) -> Self {
        self.with_memory_hint(MemoryHint {
            max_memory: bytes,
            prefer_streaming: false,
            spill_to_disk: true,
        })
    }

    /// Prefer streaming execution
    pub fn prefer_streaming(self) -> Self {
        self.with_memory_hint(MemoryHint {
            max_memory: usize::MAX,
            prefer_streaming: true,
            spill_to_disk: false,
        })
    }

    /// Set cache hints
    pub fn with_cache_hint(mut self, hint: CacheHint) -> Self {
        self.hints.cache_hints = Some(hint);
        self
    }

    /// Bypass query cache
    pub fn no_cache(self) -> Self {
        self.with_cache_hint(CacheHint {
            use_cache: false,
            update_cache: false,
            cache_ttl: None,
        })
    }

    /// Set join order hint
    pub fn with_join_order(mut self, hint: JoinOrderHint) -> Self {
        self.hints.join_order_hint = Some(hint);
        self
    }

    /// Force specific join order
    pub fn fixed_join_order(self, order: Vec<&str>) -> Self {
        self.with_join_order(JoinOrderHint {
            strategy: JoinOrderStrategy::Fixed,
            order: order.into_iter().map(String::from).collect(),
        })
    }

    /// Add filter hint
    pub fn with_filter_hint(mut self, hint: FilterHint) -> Self {
        self.hints.filter_hints.push(hint);
        self
    }

    /// Add custom directive
    pub fn directive(mut self, key: &str, value: &str) -> Self {
        self.hints
            .custom_directives
            .insert(key.to_string(), value.to_string());
        self
    }

    /// Build the QueryHints
    pub fn build(self) -> QueryHints {
        self.hints
    }
}

/// Join algorithm hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JoinHint {
    /// Variables involved in the join
    pub variables: Vec<String>,
    /// Preferred join algorithm
    pub algorithm: JoinAlgorithmHint,
    /// Optional build side for hash join
    pub build_side: Option<JoinBuildSide>,
    /// Priority (higher = more important)
    pub priority: u8,
}

impl JoinHint {
    /// Create new join hint
    pub fn new(variables: Vec<String>, algorithm: JoinAlgorithmHint) -> Self {
        Self {
            variables,
            algorithm,
            build_side: None,
            priority: 1,
        }
    }

    /// Set build side for hash join
    pub fn with_build_side(mut self, side: JoinBuildSide) -> Self {
        self.build_side = Some(side);
        self
    }

    /// Set priority
    pub fn with_priority(mut self, priority: u8) -> Self {
        self.priority = priority;
        self
    }
}

/// Supported join algorithms for hints
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum JoinAlgorithmHint {
    /// Hash join (good for large datasets)
    HashJoin,
    /// Sort-merge join (good for pre-sorted data)
    MergeJoin,
    /// Nested loop join (good for small datasets or selective filters)
    NestedLoop,
    /// Index-based join
    IndexJoin,
    /// Let optimizer decide
    Auto,
}

impl std::fmt::Display for JoinAlgorithmHint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            JoinAlgorithmHint::HashJoin => write!(f, "HASH_JOIN"),
            JoinAlgorithmHint::MergeJoin => write!(f, "MERGE_JOIN"),
            JoinAlgorithmHint::NestedLoop => write!(f, "NESTED_LOOP"),
            JoinAlgorithmHint::IndexJoin => write!(f, "INDEX_JOIN"),
            JoinAlgorithmHint::Auto => write!(f, "AUTO"),
        }
    }
}

/// Build side for hash join
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum JoinBuildSide {
    /// Use left side as build side
    Left,
    /// Use right side as build side
    Right,
    /// Let optimizer choose
    Auto,
}

/// Index usage hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexHint {
    /// Pattern or variable this hint applies to
    pub pattern_id: String,
    /// Index directive
    pub directive: IndexDirective,
    /// Optional specific index names
    pub index_names: Vec<String>,
}

impl IndexHint {
    /// Create hint to use a specific index
    pub fn use_index(pattern_id: &str, index_name: &str) -> Self {
        Self {
            pattern_id: pattern_id.to_string(),
            directive: IndexDirective::Use,
            index_names: vec![index_name.to_string()],
        }
    }

    /// Create hint to ignore a specific index
    pub fn ignore_index(pattern_id: &str, index_name: &str) -> Self {
        Self {
            pattern_id: pattern_id.to_string(),
            directive: IndexDirective::Ignore,
            index_names: vec![index_name.to_string()],
        }
    }

    /// Create hint to force index scan
    pub fn force_index(pattern_id: &str) -> Self {
        Self {
            pattern_id: pattern_id.to_string(),
            directive: IndexDirective::Force,
            index_names: Vec::new(),
        }
    }

    /// Create hint to prevent index usage
    pub fn no_index(pattern_id: &str) -> Self {
        Self {
            pattern_id: pattern_id.to_string(),
            directive: IndexDirective::NoIndex,
            index_names: Vec::new(),
        }
    }
}

/// Index directive types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum IndexDirective {
    /// Prefer using specified indexes
    Use,
    /// Ignore specified indexes
    Ignore,
    /// Force index usage (any available)
    Force,
    /// Disable index usage (full scan)
    NoIndex,
}

/// Cardinality override hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CardinalityHint {
    /// Variable name
    pub variable: String,
    /// Override cardinality value
    pub cardinality: u64,
    /// Confidence level (0.0 - 1.0)
    pub confidence: f64,
}

impl CardinalityHint {
    /// Create new cardinality hint
    pub fn new(variable: &str, cardinality: u64) -> Self {
        Self {
            variable: variable.to_string(),
            cardinality,
            confidence: 1.0,
        }
    }

    /// Set confidence level
    pub fn with_confidence(mut self, confidence: f64) -> Self {
        self.confidence = confidence.clamp(0.0, 1.0);
        self
    }
}

/// Parallelism configuration hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParallelismHint {
    /// Enable parallel execution
    pub enabled: bool,
    /// Number of threads (None = auto)
    pub threads: Option<usize>,
    /// Minimum batch size for parallel execution
    pub min_batch_size: usize,
    /// Enable work stealing
    pub work_stealing: bool,
}

impl ParallelismHint {
    /// Create new parallelism hint with specified threads
    pub fn new(threads: usize) -> Self {
        Self {
            enabled: true,
            threads: Some(threads),
            min_batch_size: 1000,
            work_stealing: true,
        }
    }

    /// Create disabled parallelism hint
    pub fn disabled() -> Self {
        Self {
            enabled: false,
            threads: None,
            min_batch_size: 0,
            work_stealing: false,
        }
    }

    /// Create auto-parallelism hint
    pub fn auto() -> Self {
        Self {
            enabled: true,
            threads: None,
            min_batch_size: 1000,
            work_stealing: true,
        }
    }
}

/// Materialization strategy hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MaterializationHint {
    /// Pattern or subquery to materialize
    pub target: String,
    /// Materialization strategy
    pub strategy: MaterializationStrategy,
}

impl MaterializationHint {
    /// Create hint to materialize a pattern
    pub fn materialize(target: &str) -> Self {
        Self {
            target: target.to_string(),
            strategy: MaterializationStrategy::Eager,
        }
    }

    /// Create hint for lazy evaluation
    pub fn lazy(target: &str) -> Self {
        Self {
            target: target.to_string(),
            strategy: MaterializationStrategy::Lazy,
        }
    }

    /// Create hint for streaming execution
    pub fn streaming(target: &str) -> Self {
        Self {
            target: target.to_string(),
            strategy: MaterializationStrategy::Streaming,
        }
    }
}

/// Materialization strategies
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MaterializationStrategy {
    /// Materialize immediately
    Eager,
    /// Lazy evaluation (materialize on demand)
    Lazy,
    /// Streaming (no materialization, pipeline)
    Streaming,
    /// Partial materialization with threshold
    Partial,
}

/// Memory configuration hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemoryHint {
    /// Maximum memory to use (bytes)
    pub max_memory: usize,
    /// Prefer streaming execution for memory efficiency
    pub prefer_streaming: bool,
    /// Allow spilling to disk
    pub spill_to_disk: bool,
}

/// Cache control hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheHint {
    /// Use cached results if available
    pub use_cache: bool,
    /// Update cache with new results
    pub update_cache: bool,
    /// Cache TTL override
    pub cache_ttl: Option<Duration>,
}

/// Join order hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JoinOrderHint {
    /// Join ordering strategy
    pub strategy: JoinOrderStrategy,
    /// Explicit join order (pattern/variable names)
    pub order: Vec<String>,
}

/// Join ordering strategies
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum JoinOrderStrategy {
    /// Use optimizer's choice
    Auto,
    /// Fixed order as specified
    Fixed,
    /// Process in left-to-right order as written
    LeftToRight,
    /// Start with smallest estimates
    SmallestFirst,
    /// Start with most selective
    MostSelectiveFirst,
}

/// Filter pushdown hint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FilterHint {
    /// Filter expression identifier
    pub filter_id: String,
    /// Pushdown directive
    pub directive: FilterPushdownDirective,
    /// Target pattern for pushdown
    pub target_pattern: Option<String>,
}

/// Filter pushdown directives
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FilterPushdownDirective {
    /// Push filter down (default behavior)
    Push,
    /// Keep filter at current level
    NoPush,
    /// Push to specific pattern
    PushTo,
}

/// Parser for query hints embedded in SPARQL comments
pub struct HintParser {
    /// Statistics counter
    hints_parsed: Arc<AtomicU64>,
    /// Parsing timer
    parse_timer: Arc<AtomicU64>,
}

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

impl HintParser {
    /// Create new hint parser
    pub fn new() -> Self {
        Self {
            hints_parsed: Arc::new(AtomicU64::new(0)),
            parse_timer: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Parse hints from a SPARQL query string
    pub fn parse(query: &str) -> Result<QueryHints> {
        let parser = Self::new();
        parser.parse_query(query)
    }

    /// Parse hints from query
    pub fn parse_query(&self, query: &str) -> Result<QueryHints> {
        let start = std::time::Instant::now();
        let mut hints = QueryHints::new();

        // First, find all line-comment style hints: # /*+ ... */
        // and collect their positions to avoid double-matching
        let line_hint_pattern = line_hint_regex();
        let mut line_hint_positions: std::collections::HashSet<usize> =
            std::collections::HashSet::new();

        for cap in line_hint_pattern.captures_iter(query) {
            if let Some(hint_text) = cap.get(1) {
                let parsed = self.parse_hint_block(hint_text.as_str())?;
                hints.merge(parsed);
                // Record the position of the /*+ start to avoid double-matching
                if let Some(m) = cap.get(0) {
                    // The regular hint /*+ starts somewhere within this match
                    // Find where /*+ starts in the original query
                    let match_start = m.start();
                    let match_str = m.as_str();
                    if let Some(inner_pos) = match_str.find("/*+") {
                        line_hint_positions.insert(match_start + inner_pos);
                    }
                }
            }
        }

        // Look for regular hint comments: /*+ HINT1 HINT2 ... */
        // Skip any that are already matched as line comments
        let hint_pattern = regex();

        for cap in hint_pattern.captures_iter(query) {
            if let Some(m) = cap.get(0) {
                // Skip if this was already matched as a line comment
                if line_hint_positions.contains(&m.start()) {
                    continue;
                }
            }
            if let Some(hint_text) = cap.get(1) {
                let parsed = self.parse_hint_block(hint_text.as_str())?;
                hints.merge(parsed);
            }
        }

        self.hints_parsed
            .fetch_add(hints.hint_count() as u64, Ordering::Relaxed);
        self.parse_timer
            .fetch_add(start.elapsed().as_nanos() as u64, Ordering::Relaxed);

        Ok(hints)
    }

    /// Parse a hint block (the text inside /*+ ... */)
    fn parse_hint_block(&self, text: &str) -> Result<QueryHints> {
        let mut hints = QueryHints::new();

        // Split by whitespace, keeping parenthesized groups together
        let tokens = self.tokenize_hints(text);
        let mut i = 0;

        while i < tokens.len() {
            let token = &tokens[i];
            let hint_upper = token.to_uppercase();

            match hint_upper.as_str() {
                "HASH_JOIN" => {
                    if i + 1 < tokens.len() {
                        let vars = self.parse_variable_list(&tokens[i + 1])?;
                        hints
                            .join_hints
                            .push(JoinHint::new(vars, JoinAlgorithmHint::HashJoin));
                        i += 1;
                    }
                }
                "MERGE_JOIN" => {
                    if i + 1 < tokens.len() {
                        let vars = self.parse_variable_list(&tokens[i + 1])?;
                        hints
                            .join_hints
                            .push(JoinHint::new(vars, JoinAlgorithmHint::MergeJoin));
                        i += 1;
                    }
                }
                "NESTED_LOOP" | "NL_JOIN" => {
                    if i + 1 < tokens.len() {
                        let vars = self.parse_variable_list(&tokens[i + 1])?;
                        hints
                            .join_hints
                            .push(JoinHint::new(vars, JoinAlgorithmHint::NestedLoop));
                        i += 1;
                    }
                }
                "INDEX_JOIN" => {
                    if i + 1 < tokens.len() {
                        let vars = self.parse_variable_list(&tokens[i + 1])?;
                        hints
                            .join_hints
                            .push(JoinHint::new(vars, JoinAlgorithmHint::IndexJoin));
                        i += 1;
                    }
                }
                "CARDINALITY" => {
                    if i + 1 < tokens.len() {
                        let (var, card) = self.parse_cardinality_hint(&tokens[i + 1])?;
                        hints
                            .cardinality_hints
                            .push(CardinalityHint::new(&var, card));
                        i += 1;
                    }
                }
                "PARALLEL" => {
                    if i + 1 < tokens.len() {
                        let threads = self.parse_single_value(&tokens[i + 1])?;
                        hints.parallelism_hints = Some(ParallelismHint::new(threads as usize));
                        i += 1;
                    } else {
                        hints.parallelism_hints = Some(ParallelismHint::auto());
                    }
                }
                "NO_PARALLEL" | "NOPARALLEL" => {
                    hints.parallelism_hints = Some(ParallelismHint::disabled());
                }
                "TIMEOUT" => {
                    if i + 1 < tokens.len() {
                        let timeout = self.parse_duration(&tokens[i + 1])?;
                        hints.timeout_hint = Some(timeout);
                        i += 1;
                    }
                }
                "MEMORY_LIMIT" | "MAX_MEMORY" => {
                    if i + 1 < tokens.len() {
                        let bytes = self.parse_memory_size(&tokens[i + 1])?;
                        hints.memory_hint = Some(MemoryHint {
                            max_memory: bytes,
                            prefer_streaming: false,
                            spill_to_disk: true,
                        });
                        i += 1;
                    }
                }
                "STREAMING" => {
                    hints.memory_hint = Some(MemoryHint {
                        max_memory: usize::MAX,
                        prefer_streaming: true,
                        spill_to_disk: false,
                    });
                }
                "NO_CACHE" | "NOCACHE" => {
                    hints.cache_hints = Some(CacheHint {
                        use_cache: false,
                        update_cache: false,
                        cache_ttl: None,
                    });
                }
                "CACHE" => {
                    hints.cache_hints = Some(CacheHint {
                        use_cache: true,
                        update_cache: true,
                        cache_ttl: None,
                    });
                }
                "USE_INDEX" | "FORCE_INDEX" => {
                    if i + 1 < tokens.len() {
                        let (pattern, index) = self.parse_index_hint(&tokens[i + 1])?;
                        hints
                            .index_hints
                            .push(IndexHint::use_index(&pattern, &index));
                        i += 1;
                    }
                }
                "IGNORE_INDEX" | "NO_INDEX" => {
                    if i + 1 < tokens.len() {
                        let (pattern, index) = self.parse_index_hint(&tokens[i + 1])?;
                        hints
                            .index_hints
                            .push(IndexHint::ignore_index(&pattern, &index));
                        i += 1;
                    }
                }
                "ORDERED" | "FIXED_ORDER" => {
                    hints.join_order_hint = Some(JoinOrderHint {
                        strategy: JoinOrderStrategy::LeftToRight,
                        order: Vec::new(),
                    });
                }
                "LEADING" => {
                    if i + 1 < tokens.len() {
                        let order = self.parse_variable_list(&tokens[i + 1])?;
                        hints.join_order_hint = Some(JoinOrderHint {
                            strategy: JoinOrderStrategy::Fixed,
                            order,
                        });
                        i += 1;
                    }
                }
                "MATERIALIZE" => {
                    if i + 1 < tokens.len() {
                        let target = self.parse_single_string(&tokens[i + 1])?;
                        hints
                            .materialization_hints
                            .push(MaterializationHint::materialize(&target));
                        i += 1;
                    }
                }
                _ => {
                    // Unknown hint - store as custom directive
                    if i + 1 < tokens.len() && tokens[i + 1].starts_with('(') {
                        hints.custom_directives.insert(
                            hint_upper,
                            tokens[i + 1]
                                .trim_matches(|c| c == '(' || c == ')')
                                .to_string(),
                        );
                        i += 1;
                    }
                }
            }
            i += 1;
        }

        Ok(hints)
    }

    /// Tokenize hint text preserving parenthesized groups
    /// Splits hint names from their arguments: HASH_JOIN(?s, ?o) -> ["HASH_JOIN", "(?s, ?o)"]
    fn tokenize_hints(&self, text: &str) -> Vec<String> {
        let mut tokens = Vec::new();
        let mut current = String::new();
        let mut paren_depth = 0;

        for ch in text.chars() {
            match ch {
                '(' if paren_depth == 0 => {
                    // Starting a parenthesized group
                    // If there's a current token (hint name), push it first
                    if !current.is_empty() {
                        tokens.push(current.clone());
                        current.clear();
                    }
                    paren_depth += 1;
                    current.push(ch);
                }
                '(' => {
                    // Nested parens
                    paren_depth += 1;
                    current.push(ch);
                }
                ')' => {
                    paren_depth -= 1;
                    current.push(ch);
                    if paren_depth == 0 && !current.is_empty() {
                        // End of parenthesized group, push it as a token
                        tokens.push(current.clone());
                        current.clear();
                    }
                }
                ' ' | '\t' | '\n' | '\r' if paren_depth == 0 => {
                    if !current.is_empty() {
                        tokens.push(current.clone());
                        current.clear();
                    }
                }
                _ => {
                    current.push(ch);
                }
            }
        }

        if !current.is_empty() {
            tokens.push(current);
        }

        tokens
    }

    /// Parse variable list from parenthesized string: (?var1, ?var2)
    fn parse_variable_list(&self, text: &str) -> Result<Vec<String>> {
        let inner = text.trim_matches(|c| c == '(' || c == ')');
        let vars: Vec<String> = inner
            .split(',')
            .map(|s| s.trim().trim_start_matches('?').to_string())
            .filter(|s| !s.is_empty())
            .collect();

        if vars.is_empty() {
            return Err(anyhow!("Empty variable list"));
        }
        Ok(vars)
    }

    /// Parse cardinality hint: (?var, 1000)
    fn parse_cardinality_hint(&self, text: &str) -> Result<(String, u64)> {
        let inner = text.trim_matches(|c| c == '(' || c == ')');
        let parts: Vec<&str> = inner.split(',').map(|s| s.trim()).collect();

        if parts.len() != 2 {
            return Err(anyhow!(
                "Invalid cardinality hint format: expected (?var, number)"
            ));
        }

        let var = parts[0].trim_start_matches('?').to_string();
        let card: u64 = parts[1]
            .parse()
            .map_err(|_| anyhow!("Invalid cardinality value: {}", parts[1]))?;

        Ok((var, card))
    }

    /// Parse single numeric value: (4)
    fn parse_single_value(&self, text: &str) -> Result<u64> {
        let inner = text.trim_matches(|c| c == '(' || c == ')');
        inner
            .parse()
            .map_err(|_| anyhow!("Invalid numeric value: {}", inner))
    }

    /// Parse single string value: (name)
    fn parse_single_string(&self, text: &str) -> Result<String> {
        let inner = text.trim_matches(|c| c == '(' || c == ')');
        Ok(inner.trim().to_string())
    }

    /// Parse duration: (30s), (5m), (1h)
    fn parse_duration(&self, text: &str) -> Result<Duration> {
        let inner = text.trim_matches(|c| c == '(' || c == ')').to_lowercase();

        if let Some(secs) = inner.strip_suffix('s') {
            let val: u64 = secs
                .parse()
                .map_err(|_| anyhow!("Invalid duration: {}", text))?;
            return Ok(Duration::from_secs(val));
        }
        if let Some(mins) = inner.strip_suffix('m') {
            let val: u64 = mins
                .parse()
                .map_err(|_| anyhow!("Invalid duration: {}", text))?;
            return Ok(Duration::from_secs(val * 60));
        }
        if let Some(hours) = inner.strip_suffix('h') {
            let val: u64 = hours
                .parse()
                .map_err(|_| anyhow!("Invalid duration: {}", text))?;
            return Ok(Duration::from_secs(val * 3600));
        }

        // Assume milliseconds if no suffix
        let val: u64 = inner
            .parse()
            .map_err(|_| anyhow!("Invalid duration: {}", text))?;
        Ok(Duration::from_millis(val))
    }

    /// Parse memory size: (1GB), (512MB), (1024KB)
    fn parse_memory_size(&self, text: &str) -> Result<usize> {
        let inner = text.trim_matches(|c| c == '(' || c == ')').to_uppercase();

        if let Some(gb) = inner.strip_suffix("GB") {
            let val: usize = gb
                .parse()
                .map_err(|_| anyhow!("Invalid memory size: {}", text))?;
            return Ok(val * 1024 * 1024 * 1024);
        }
        if let Some(mb) = inner.strip_suffix("MB") {
            let val: usize = mb
                .parse()
                .map_err(|_| anyhow!("Invalid memory size: {}", text))?;
            return Ok(val * 1024 * 1024);
        }
        if let Some(kb) = inner.strip_suffix("KB") {
            let val: usize = kb
                .parse()
                .map_err(|_| anyhow!("Invalid memory size: {}", text))?;
            return Ok(val * 1024);
        }

        // Assume bytes if no suffix
        inner
            .parse()
            .map_err(|_| anyhow!("Invalid memory size: {}", text))
    }

    /// Parse index hint: (pattern, index_name)
    fn parse_index_hint(&self, text: &str) -> Result<(String, String)> {
        let inner = text.trim_matches(|c| c == '(' || c == ')');
        let parts: Vec<&str> = inner.split(',').map(|s| s.trim()).collect();

        if parts.len() != 2 {
            return Err(anyhow!(
                "Invalid index hint format: expected (pattern, index_name)"
            ));
        }

        Ok((parts[0].to_string(), parts[1].to_string()))
    }

    /// Get parsing statistics
    pub fn statistics(&self) -> HintParserStats {
        HintParserStats {
            hints_parsed: self.hints_parsed.load(Ordering::Relaxed),
            total_parse_time_ns: self.parse_timer.load(Ordering::Relaxed),
        }
    }
}

/// Hint parser statistics
#[derive(Debug, Clone)]
pub struct HintParserStats {
    /// Total hints parsed
    pub hints_parsed: u64,
    /// Total parse time in nanoseconds
    pub total_parse_time_ns: u64,
}

/// Regex for hint comments
fn regex() -> &'static Regex {
    static HINT_REGEX: OnceLock<Regex> = OnceLock::new();
    HINT_REGEX.get_or_init(|| {
        // Match /*+ hint content */ - capture everything between /*+ and */
        // Simple pattern: non-greedy match of any chars between markers
        Regex::new(r"/\*\+\s*(.+?)\s*\*/").expect("Invalid regex")
    })
}

/// Regex for line hint comments
fn line_hint_regex() -> &'static Regex {
    static LINE_HINT_REGEX: OnceLock<Regex> = OnceLock::new();
    LINE_HINT_REGEX.get_or_init(|| {
        // Match # /*+ hint content */
        Regex::new(r"#\s*/\*\+\s*(.+?)\s*\*/").expect("Invalid regex")
    })
}

/// Hint application result
#[derive(Debug, Clone)]
pub struct HintApplicationResult {
    /// Hints that were applied
    pub applied: Vec<String>,
    /// Hints that were ignored (e.g., not applicable)
    pub ignored: Vec<String>,
    /// Hints that conflicted and were resolved
    pub conflicts: Vec<String>,
}

impl HintApplicationResult {
    /// Create new result
    pub fn new() -> Self {
        Self {
            applied: Vec::new(),
            ignored: Vec::new(),
            conflicts: Vec::new(),
        }
    }

    /// Record an applied hint
    pub fn applied(&mut self, hint: &str) {
        self.applied.push(hint.to_string());
    }

    /// Record an ignored hint
    pub fn ignored(&mut self, hint: &str) {
        self.ignored.push(hint.to_string());
    }

    /// Record a conflict resolution
    pub fn conflict(&mut self, hint: &str) {
        self.conflicts.push(hint.to_string());
    }
}

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

/// Hint validator for checking hint consistency
pub struct HintValidator;

impl HintValidator {
    /// Validate hints for consistency and conflicts
    pub fn validate(hints: &QueryHints) -> Vec<HintValidationWarning> {
        let mut warnings = Vec::new();

        // Check for conflicting join hints on same variables
        let mut seen_vars: HashMap<String, Vec<JoinAlgorithmHint>> = HashMap::new();
        for hint in &hints.join_hints {
            for var in &hint.variables {
                seen_vars
                    .entry(var.clone())
                    .or_default()
                    .push(hint.algorithm);
            }
        }
        for (var, algorithms) in seen_vars {
            if algorithms.len() > 1 {
                let unique: std::collections::HashSet<_> = algorithms.iter().collect();
                if unique.len() > 1 {
                    warnings.push(HintValidationWarning {
                        severity: WarningSeverity::Warning,
                        message: format!(
                            "Conflicting join hints for variable '{}': {:?}",
                            var, algorithms
                        ),
                    });
                }
            }
        }

        // Check for streaming + parallelism conflict
        if let Some(ref mem) = hints.memory_hint {
            if mem.prefer_streaming {
                if let Some(ref par) = hints.parallelism_hints {
                    if par.enabled && par.threads.is_some_and(|t| t > 1) {
                        warnings.push(HintValidationWarning {
                            severity: WarningSeverity::Info,
                            message:
                                "Streaming mode with parallelism may reduce streaming benefits"
                                    .to_string(),
                        });
                    }
                }
            }
        }

        // Check for cardinality hints with very high values
        for hint in &hints.cardinality_hints {
            if hint.cardinality > 1_000_000_000 {
                warnings.push(HintValidationWarning {
                    severity: WarningSeverity::Warning,
                    message: format!(
                        "Very high cardinality hint for '{}': {} (may affect optimization)",
                        hint.variable, hint.cardinality
                    ),
                });
            }
        }

        // Check timeout is reasonable
        if let Some(timeout) = hints.timeout_hint {
            if timeout < Duration::from_millis(100) {
                warnings.push(HintValidationWarning {
                    severity: WarningSeverity::Warning,
                    message: format!(
                        "Very short timeout: {:?} (query may timeout immediately)",
                        timeout
                    ),
                });
            }
            if timeout > Duration::from_secs(3600) {
                warnings.push(HintValidationWarning {
                    severity: WarningSeverity::Info,
                    message: format!(
                        "Very long timeout: {:?} (consider using async execution)",
                        timeout
                    ),
                });
            }
        }

        warnings
    }
}

/// Hint validation warning
#[derive(Debug, Clone)]
pub struct HintValidationWarning {
    /// Warning severity
    pub severity: WarningSeverity,
    /// Warning message
    pub message: String,
}

/// Warning severity levels
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WarningSeverity {
    /// Informational
    Info,
    /// Warning (may affect performance)
    Warning,
    /// Error (hint will be ignored)
    Error,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_regex_matching() {
        let re = regex();
        let query = "/*+ HASH_JOIN(?s, ?o) */ SELECT ?s ?o WHERE { ?s ?p ?o }";

        let caps: Vec<_> = re.captures_iter(query).collect();
        println!("Number of captures: {}", caps.len());
        for cap in &caps {
            println!("Full match: {:?}", cap.get(0).map(|m| m.as_str()));
            println!("Group 1: {:?}", cap.get(1).map(|m| m.as_str()));
        }

        assert!(!caps.is_empty(), "Regex should match the hint comment");
    }

    #[test]
    fn test_parse_hash_join_hint() {
        let query = "/*+ HASH_JOIN(?s, ?o) */ SELECT ?s ?o WHERE { ?s ?p ?o }";
        let hints = HintParser::parse(query).unwrap();

        assert_eq!(hints.join_hints.len(), 1);
        assert_eq!(hints.join_hints[0].algorithm, JoinAlgorithmHint::HashJoin);
        assert_eq!(hints.join_hints[0].variables, vec!["s", "o"]);
    }

    #[test]
    fn test_parse_cardinality_hint() {
        let query = "/*+ CARDINALITY(?person, 1000) */ SELECT ?person WHERE { ?person a <Person> }";
        let hints = HintParser::parse(query).unwrap();

        assert_eq!(hints.cardinality_hints.len(), 1);
        assert_eq!(hints.cardinality_hints[0].variable, "person");
        assert_eq!(hints.cardinality_hints[0].cardinality, 1000);
    }

    #[test]
    fn test_parse_parallel_hint() {
        let query = "/*+ PARALLEL(4) */ SELECT * WHERE { ?s ?p ?o }";
        let hints = HintParser::parse(query).unwrap();

        assert!(hints.parallelism_hints.is_some());
        let par = hints.parallelism_hints.unwrap();
        assert!(par.enabled);
        assert_eq!(par.threads, Some(4));
    }

    #[test]
    fn test_parse_no_parallel_hint() {
        let query = "/*+ NO_PARALLEL */ SELECT * WHERE { ?s ?p ?o }";
        let hints = HintParser::parse(query).unwrap();

        assert!(hints.parallelism_hints.is_some());
        assert!(!hints.parallelism_hints.unwrap().enabled);
    }

    #[test]
    fn test_parse_timeout_hint() {
        let query = "/*+ TIMEOUT(30s) */ SELECT * WHERE { ?s ?p ?o }";
        let hints = HintParser::parse(query).unwrap();

        assert_eq!(hints.timeout_hint, Some(Duration::from_secs(30)));
    }

    #[test]
    fn test_parse_memory_limit_hint() {
        let query = "/*+ MEMORY_LIMIT(1GB) */ SELECT * WHERE { ?s ?p ?o }";
        let hints = HintParser::parse(query).unwrap();

        assert!(hints.memory_hint.is_some());
        assert_eq!(hints.memory_hint.unwrap().max_memory, 1024 * 1024 * 1024);
    }

    #[test]
    fn test_parse_no_cache_hint() {
        let query = "/*+ NO_CACHE */ SELECT * WHERE { ?s ?p ?o }";
        let hints = HintParser::parse(query).unwrap();

        assert!(hints.cache_hints.is_some());
        assert!(!hints.cache_hints.unwrap().use_cache);
    }

    #[test]
    fn test_parse_multiple_hints() {
        let query =
            "/*+ HASH_JOIN(?s, ?o) PARALLEL(8) TIMEOUT(60s) */ SELECT ?s ?o WHERE { ?s ?p ?o }";
        let hints = HintParser::parse(query).unwrap();

        assert_eq!(hints.join_hints.len(), 1);
        assert!(hints.parallelism_hints.is_some());
        assert_eq!(hints.timeout_hint, Some(Duration::from_secs(60)));
    }

    #[test]
    fn test_parse_line_comment_hint() {
        let query = r#"
            # /*+ CARDINALITY(?x, 5000) PARALLEL(2) */
            SELECT ?x WHERE { ?x a <Thing> }
        "#;
        let hints = HintParser::parse(query).unwrap();

        assert_eq!(hints.cardinality_hints.len(), 1);
        assert!(hints.parallelism_hints.is_some());
    }

    #[test]
    fn test_hints_builder() {
        let hints = QueryHints::builder()
            .hash_join(vec!["s", "o"])
            .cardinality("person", 1000)
            .parallel(4)
            .timeout_secs(30)
            .no_cache()
            .build();

        assert_eq!(hints.join_hints.len(), 1);
        assert_eq!(hints.cardinality_hints.len(), 1);
        assert!(hints.parallelism_hints.is_some());
        assert_eq!(hints.timeout_hint, Some(Duration::from_secs(30)));
        assert!(hints.cache_hints.is_some());
    }

    #[test]
    fn test_hint_validator() {
        let hints = QueryHints::builder()
            .hash_join(vec!["x", "y"])
            .merge_join(vec!["x", "z"]) // Conflicting hint on ?x
            .timeout(Duration::from_millis(50)) // Very short timeout
            .build();

        let warnings = HintValidator::validate(&hints);

        assert!(warnings.iter().any(|w| w.message.contains("Conflicting")));
        assert!(warnings.iter().any(|w| w.message.contains("short timeout")));
    }

    #[test]
    fn test_hint_merge() {
        let mut hints1 = QueryHints::builder().hash_join(vec!["a", "b"]).build();

        let hints2 = QueryHints::builder()
            .cardinality("c", 500)
            .parallel(4)
            .build();

        hints1.merge(hints2);

        assert_eq!(hints1.join_hints.len(), 1);
        assert_eq!(hints1.cardinality_hints.len(), 1);
        assert!(hints1.parallelism_hints.is_some());
    }

    #[test]
    fn test_empty_hints() {
        let hints = QueryHints::new();
        assert!(hints.is_empty());
        assert_eq!(hints.hint_count(), 0);
    }

    #[test]
    fn test_get_join_hint() {
        let hints = QueryHints::builder().hash_join(vec!["s", "o"]).build();

        let var_s = Variable::new("s").unwrap();
        let var_o = Variable::new("o").unwrap();

        let hint = hints.get_join_hint(&[var_s, var_o]);
        assert!(hint.is_some());
        assert_eq!(hint.unwrap().algorithm, JoinAlgorithmHint::HashJoin);
    }

    #[test]
    fn test_get_cardinality_hint() {
        let hints = QueryHints::builder().cardinality("person", 1000).build();

        let var = Variable::new("person").unwrap();
        let card = hints.get_cardinality_hint(&var);

        assert_eq!(card, Some(1000));
    }

    #[test]
    fn test_parse_index_hint() {
        let query = "/*+ USE_INDEX(pattern1, idx_subject) */ SELECT * WHERE { ?s ?p ?o }";
        let hints = HintParser::parse(query).unwrap();

        assert_eq!(hints.index_hints.len(), 1);
        assert_eq!(hints.index_hints[0].pattern_id, "pattern1");
        assert_eq!(hints.index_hints[0].directive, IndexDirective::Use);
    }

    #[test]
    fn test_parse_leading_hint() {
        let query = "/*+ LEADING(?a, ?b, ?c) */ SELECT * WHERE { ?a ?p ?b . ?b ?q ?c }";
        let hints = HintParser::parse(query).unwrap();

        assert!(hints.join_order_hint.is_some());
        let order = hints.join_order_hint.unwrap();
        assert_eq!(order.strategy, JoinOrderStrategy::Fixed);
        assert_eq!(order.order, vec!["a", "b", "c"]);
    }
}