llama-gguf 0.14.0

A high-performance Rust implementation of llama.cpp - LLM inference engine with full GGUF support
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
//! RAG vector store with pgvector

use deadpool_postgres::{Config, Pool, Runtime};
use pgvector::Vector;
use tokio_postgres::NoTls;
use serde::{Deserialize, Serialize};

use super::{RagConfig, RagError, RagResult};

/// Metadata filter for search queries
/// 
/// Filters can be combined using AND/OR logic and support various comparison operators
/// for JSONB fields in PostgreSQL.
/// 
/// # Example
/// 
/// ```rust,ignore
/// use llama_gguf::rag::MetadataFilter;
/// 
/// // Simple equality filter
/// let filter = MetadataFilter::eq("source", "docs/readme.md");
/// 
/// // Combine filters with AND
/// let filter = MetadataFilter::and(vec![
///     MetadataFilter::eq("type", "documentation"),
///     MetadataFilter::gte("version", 2),
/// ]);
/// 
/// // Complex filter with OR
/// let filter = MetadataFilter::or(vec![
///     MetadataFilter::eq("category", "api"),
///     MetadataFilter::contains("tags", "important"),
/// ]);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "op", rename_all = "snake_case")]
pub enum MetadataFilter {
    /// Exact equality: metadata->>'field' = 'value'
    Eq { field: String, value: serde_json::Value },
    
    /// Not equal: metadata->>'field' != 'value'
    Ne { field: String, value: serde_json::Value },
    
    /// Greater than: (metadata->>'field')::numeric > value
    Gt { field: String, value: serde_json::Value },
    
    /// Greater than or equal: (metadata->>'field')::numeric >= value
    Gte { field: String, value: serde_json::Value },
    
    /// Less than: (metadata->>'field')::numeric < value
    Lt { field: String, value: serde_json::Value },
    
    /// Less than or equal: (metadata->>'field')::numeric <= value
    Lte { field: String, value: serde_json::Value },
    
    /// Field exists: metadata ? 'field'
    Exists { field: String },
    
    /// Field does not exist: NOT (metadata ? 'field')
    NotExists { field: String },
    
    /// String contains (case-insensitive): metadata->>'field' ILIKE '%value%'
    Contains { field: String, value: String },
    
    /// String starts with: metadata->>'field' LIKE 'value%'
    StartsWith { field: String, value: String },
    
    /// String ends with: metadata->>'field' LIKE '%value'
    EndsWith { field: String, value: String },
    
    /// Value in array: metadata->'field' ? 'value' (for array fields)
    InArray { field: String, value: String },
    
    /// Value in list: metadata->>'field' IN ('a', 'b', 'c')
    In { field: String, values: Vec<serde_json::Value> },
    
    /// Value not in list: metadata->>'field' NOT IN ('a', 'b', 'c')
    NotIn { field: String, values: Vec<serde_json::Value> },
    
    /// JSON path exists: metadata @? '$.field[*] ? (@ == "value")'
    JsonPath { path: String },
    
    /// Logical AND of multiple filters
    And { filters: Vec<MetadataFilter> },
    
    /// Logical OR of multiple filters
    Or { filters: Vec<MetadataFilter> },
    
    /// Logical NOT
    Not { filter: Box<MetadataFilter> },
}

impl MetadataFilter {
    // Convenience constructors
    
    /// Create an equality filter
    pub fn eq(field: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        Self::Eq { field: field.into(), value: value.into() }
    }
    
    /// Create a not-equal filter
    pub fn ne(field: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        Self::Ne { field: field.into(), value: value.into() }
    }
    
    /// Create a greater-than filter
    pub fn gt(field: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        Self::Gt { field: field.into(), value: value.into() }
    }
    
    /// Create a greater-than-or-equal filter
    pub fn gte(field: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        Self::Gte { field: field.into(), value: value.into() }
    }
    
    /// Create a less-than filter
    pub fn lt(field: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        Self::Lt { field: field.into(), value: value.into() }
    }
    
    /// Create a less-than-or-equal filter
    pub fn lte(field: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
        Self::Lte { field: field.into(), value: value.into() }
    }
    
    /// Create an exists filter
    pub fn exists(field: impl Into<String>) -> Self {
        Self::Exists { field: field.into() }
    }
    
    /// Create a not-exists filter
    pub fn not_exists(field: impl Into<String>) -> Self {
        Self::NotExists { field: field.into() }
    }
    
    /// Create a contains filter (case-insensitive substring match)
    pub fn contains(field: impl Into<String>, value: impl Into<String>) -> Self {
        Self::Contains { field: field.into(), value: value.into() }
    }
    
    /// Create a starts-with filter
    pub fn starts_with(field: impl Into<String>, value: impl Into<String>) -> Self {
        Self::StartsWith { field: field.into(), value: value.into() }
    }
    
    /// Create an ends-with filter
    pub fn ends_with(field: impl Into<String>, value: impl Into<String>) -> Self {
        Self::EndsWith { field: field.into(), value: value.into() }
    }
    
    /// Create a filter for checking if value is in a JSON array field
    pub fn in_array(field: impl Into<String>, value: impl Into<String>) -> Self {
        Self::InArray { field: field.into(), value: value.into() }
    }
    
    /// Create an IN filter
    pub fn in_values(field: impl Into<String>, values: Vec<serde_json::Value>) -> Self {
        Self::In { field: field.into(), values }
    }
    
    /// Create a NOT IN filter
    pub fn not_in(field: impl Into<String>, values: Vec<serde_json::Value>) -> Self {
        Self::NotIn { field: field.into(), values }
    }
    
    /// Create a JSON path filter
    pub fn json_path(path: impl Into<String>) -> Self {
        Self::JsonPath { path: path.into() }
    }
    
    /// Create an AND filter combining multiple filters
    pub fn and(filters: Vec<MetadataFilter>) -> Self {
        Self::And { filters }
    }
    
    /// Create an OR filter combining multiple filters
    pub fn or(filters: Vec<MetadataFilter>) -> Self {
        Self::Or { filters }
    }
    
    /// Create a NOT filter
    pub fn not(filter: MetadataFilter) -> Self {
        Self::Not { filter: Box::new(filter) }
    }
    
    /// Convert the filter to a SQL WHERE clause fragment
    ///
    /// Returns the SQL string and a list of parameter values.
    /// Returns an error if any field name contains invalid characters.
    pub fn to_sql(&self, param_offset: usize) -> RagResult<(String, Vec<String>)> {
        let mut params = Vec::new();
        let sql = self.to_sql_inner(param_offset, &mut params)?;
        Ok((sql, params))
    }

    fn to_sql_inner(&self, param_offset: usize, params: &mut Vec<String>) -> RagResult<String> {
        match self {
            Self::Eq { field, value } => {
                let field = validate_field_name(field)?;
                let param_idx = param_offset + params.len() + 1;
                params.push(json_value_to_string(value));
                Ok(format!("metadata->>'{}' = ${}", field, param_idx))
            }

            Self::Ne { field, value } => {
                let field = validate_field_name(field)?;
                let param_idx = param_offset + params.len() + 1;
                params.push(json_value_to_string(value));
                Ok(format!("metadata->>'{}' != ${}", field, param_idx))
            }

            Self::Gt { field, value } => {
                let field = validate_field_name(field)?;
                let param_idx = param_offset + params.len() + 1;
                params.push(json_value_to_string(value));
                Ok(format!("(metadata->>'{}')::numeric > ${}::numeric", field, param_idx))
            }

            Self::Gte { field, value } => {
                let field = validate_field_name(field)?;
                let param_idx = param_offset + params.len() + 1;
                params.push(json_value_to_string(value));
                Ok(format!("(metadata->>'{}')::numeric >= ${}::numeric", field, param_idx))
            }

            Self::Lt { field, value } => {
                let field = validate_field_name(field)?;
                let param_idx = param_offset + params.len() + 1;
                params.push(json_value_to_string(value));
                Ok(format!("(metadata->>'{}')::numeric < ${}::numeric", field, param_idx))
            }

            Self::Lte { field, value } => {
                let field = validate_field_name(field)?;
                let param_idx = param_offset + params.len() + 1;
                params.push(json_value_to_string(value));
                Ok(format!("(metadata->>'{}')::numeric <= ${}::numeric", field, param_idx))
            }

            Self::Exists { field } => {
                let field = validate_field_name(field)?;
                Ok(format!("metadata ? '{}'", field))
            }

            Self::NotExists { field } => {
                let field = validate_field_name(field)?;
                Ok(format!("NOT (metadata ? '{}')", field))
            }

            Self::Contains { field, value } => {
                let field = validate_field_name(field)?;
                let param_idx = param_offset + params.len() + 1;
                params.push(format!("%{}%", value));
                Ok(format!("metadata->>'{}' ILIKE ${}", field, param_idx))
            }

            Self::StartsWith { field, value } => {
                let field = validate_field_name(field)?;
                let param_idx = param_offset + params.len() + 1;
                params.push(format!("{}%", value));
                Ok(format!("metadata->>'{}' LIKE ${}", field, param_idx))
            }

            Self::EndsWith { field, value } => {
                let field = validate_field_name(field)?;
                let param_idx = param_offset + params.len() + 1;
                params.push(format!("%{}", value));
                Ok(format!("metadata->>'{}' LIKE ${}", field, param_idx))
            }

            Self::InArray { field, value } => {
                let field = validate_field_name(field)?;
                let param_idx = param_offset + params.len() + 1;
                params.push(value.clone());
                Ok(format!("metadata->'{}' ? ${}", field, param_idx))
            }

            Self::In { field, values } => {
                let field = validate_field_name(field)?;
                if values.is_empty() {
                    return Ok("FALSE".to_string());
                }
                let placeholders: Vec<String> = values.iter().enumerate().map(|(i, _v)| {
                    let param_idx = param_offset + params.len() + 1 + i;
                    format!("${}", param_idx)
                }).collect();
                for v in values {
                    params.push(json_value_to_string(v));
                }
                Ok(format!("metadata->>'{}' IN ({})", field, placeholders.join(", ")))
            }

            Self::NotIn { field, values } => {
                let field = validate_field_name(field)?;
                if values.is_empty() {
                    return Ok("TRUE".to_string());
                }
                let placeholders: Vec<String> = values.iter().enumerate().map(|(i, _)| {
                    let param_idx = param_offset + params.len() + 1 + i;
                    format!("${}", param_idx)
                }).collect();
                for v in values {
                    params.push(json_value_to_string(v));
                }
                Ok(format!("metadata->>'{}' NOT IN ({})", field, placeholders.join(", ")))
            }

            Self::JsonPath { path } => {
                Ok(format!("metadata @? '{}'", path.replace('\'', "''")))
            }

            Self::And { filters } => {
                if filters.is_empty() {
                    return Ok("TRUE".to_string());
                }
                let mut parts = Vec::new();
                for f in filters {
                    // Pass param_offset unchanged — each child reads params.len()
                    // internally to determine its own parameter index.
                    parts.push(f.to_sql_inner(param_offset, params)?);
                }
                Ok(format!("({})", parts.join(" AND ")))
            }

            Self::Or { filters } => {
                if filters.is_empty() {
                    return Ok("FALSE".to_string());
                }
                let mut parts = Vec::new();
                for f in filters {
                    parts.push(f.to_sql_inner(param_offset, params)?);
                }
                Ok(format!("({})", parts.join(" OR ")))
            }

            Self::Not { filter } => {
                let inner = filter.to_sql_inner(param_offset, params)?;
                Ok(format!("NOT ({})", inner))
            }
        }
    }
    
    /// Parse a filter from a simple string syntax
    /// 
    /// Supported formats:
    /// - `field=value` - equality
    /// - `field!=value` - not equal
    /// - `field>value` - greater than
    /// - `field>=value` - greater than or equal
    /// - `field<value` - less than
    /// - `field<=value` - less than or equal
    /// - `field~value` - contains (case-insensitive)
    /// - `field^value` - starts with
    /// - `field$value` - ends with
    /// - `field?` - exists
    /// - `!field?` - not exists
    pub fn parse(s: &str) -> Result<Self, String> {
        let s = s.trim();
        
        // Check for exists/not exists
        if s.ends_with('?') {
            if s.starts_with('!') {
                return Ok(Self::not_exists(&s[1..s.len()-1]));
            }
            return Ok(Self::exists(&s[..s.len()-1]));
        }
        
        // Try to find operator
        let operators = [">=", "<=", "!=", "=", ">", "<", "~", "^", "$"];
        
        for op in &operators {
            if let Some(pos) = s.find(op) {
                let field = s[..pos].trim();
                let value = s[pos + op.len()..].trim();
                
                // Try to parse as number first, then as string
                let json_value: serde_json::Value = if let Ok(n) = value.parse::<i64>() {
                    serde_json::Value::Number(n.into())
                } else if let Ok(n) = value.parse::<f64>() {
                    serde_json::Number::from_f64(n)
                        .map(serde_json::Value::Number)
                        .unwrap_or_else(|| serde_json::Value::String(value.to_string()))
                } else if value == "true" {
                    serde_json::Value::Bool(true)
                } else if value == "false" {
                    serde_json::Value::Bool(false)
                } else if value == "null" {
                    serde_json::Value::Null
                } else {
                    serde_json::Value::String(value.to_string())
                };
                
                return Ok(match *op {
                    "=" => Self::eq(field, json_value),
                    "!=" => Self::ne(field, json_value),
                    ">" => Self::gt(field, json_value),
                    ">=" => Self::gte(field, json_value),
                    "<" => Self::lt(field, json_value),
                    "<=" => Self::lte(field, json_value),
                    "~" => Self::contains(field, value),
                    "^" => Self::starts_with(field, value),
                    "$" => Self::ends_with(field, value),
                    _ => unreachable!(),
                });
            }
        }
        
        Err(format!("Invalid filter syntax: {}", s))
    }
    
    /// Parse multiple filters from a string, combining with AND
    /// 
    /// Filters are separated by `;` or newlines
    pub fn parse_many(s: &str) -> Result<Self, String> {
        let filters: Result<Vec<_>, _> = s
            .split([';', '\n'])
            .map(|p| p.trim())
            .filter(|p| !p.is_empty())
            .map(Self::parse)
            .collect();
        
        let filters = filters?;
        
        if filters.is_empty() {
            return Err("No filters provided".to_string());
        }
        
        if filters.len() == 1 {
            Ok(filters.into_iter().next().unwrap())
        } else {
            Ok(Self::and(filters))
        }
    }
}

/// Validate a field name for safe use in SQL
/// Only allows alphanumeric, underscore, and dot characters
fn validate_field_name(field: &str) -> Result<&str, RagError> {
    if field.is_empty() {
        return Err(RagError::QueryFailed("Empty field name".into()));
    }
    if field.len() > 128 {
        return Err(RagError::QueryFailed("Field name too long".into()));
    }
    if !field.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '.') {
        return Err(RagError::QueryFailed(
            format!("Invalid field name '{}': only alphanumeric, underscore, and dot allowed", field)
        ));
    }
    Ok(field)
}

/// Convert a JSON value to a string for use as a SQL parameter
fn json_value_to_string(value: &serde_json::Value) -> String {
    match value {
        serde_json::Value::String(s) => s.clone(),
        serde_json::Value::Number(n) => n.to_string(),
        serde_json::Value::Bool(b) => b.to_string(),
        serde_json::Value::Null => String::new(),
        _ => value.to_string(),
    }
}

/// A document with its embedding stored in the vector database
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Document {
    /// Unique identifier
    pub id: i64,
    /// Text content
    pub content: String,
    /// Optional metadata as JSON
    pub metadata: Option<serde_json::Value>,
    /// Similarity score from search (only populated in search results)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub score: Option<f32>,
}

/// A document to be inserted (without ID)
#[derive(Debug, Clone)]
pub struct NewDocument {
    /// Text content
    pub content: String,
    /// Embedding vector
    pub embedding: Vec<f32>,
    /// Optional metadata
    pub metadata: Option<serde_json::Value>,
}

/// RAG vector store backed by PostgreSQL + pgvector
pub struct RagStore {
    pool: Pool,
    config: RagConfig,
}

impl RagStore {
    /// Connect to the vector store
    pub async fn connect(config: RagConfig) -> RagResult<Self> {
        // Validate config first
        config.validate()?;
        
        let mut pg_config = Config::new();
        
        // Parse connection string
        let url = url::Url::parse(config.connection_string())
            .map_err(|e| RagError::ConfigError(format!("Invalid connection string: {}", e)))?;
        
        pg_config.host = url.host_str().map(String::from);
        pg_config.port = url.port();
        pg_config.user = if url.username().is_empty() { None } else { Some(url.username().to_string()) };
        pg_config.password = url.password().map(String::from);
        pg_config.dbname = Some(url.path().trim_start_matches('/').to_string());
        
        let pool = pg_config
            .create_pool(Some(Runtime::Tokio1), NoTls)
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        
        // Test connection
        let client = pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        
        // Verify pgvector extension is available
        client.query_one("SELECT extversion FROM pg_extension WHERE extname = 'vector'", &[])
            .await
            .map_err(|_| RagError::ConnectionFailed(
                "pgvector extension not installed. Run: CREATE EXTENSION vector;".into()
            ))?;
        
        Ok(Self { pool, config })
    }
    
    /// Connect using configuration loaded from file and/or environment
    pub async fn connect_with_config(config_path: Option<&str>) -> RagResult<Self> {
        let config = RagConfig::load(config_path)?;
        Self::connect(config).await
    }
    
    /// Create the embeddings table if it doesn't exist
    ///
    /// The table schema and indexes are driven by the [`RagConfig`]:
    /// - When `search_type == Hybrid`, a generated `content_tsv` tsvector
    ///   column and a GIN index on it are added for full-text keyword search.
    /// - The vector index type (HNSW or IVFFlat) is determined by
    ///   [`IndexType`](super::IndexType) from the config.
    pub async fn create_table(&self) -> RagResult<()> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;

        let tsv_column = if self.config.search_type() == super::SearchType::Hybrid {
            format!(
                ", content_tsv tsvector GENERATED ALWAYS AS (to_tsvector('{}', content)) STORED",
                self.config.text_search_language()
            )
        } else {
            String::new()
        };

        let create_table = format!(
            r#"
            CREATE TABLE IF NOT EXISTS {} (
                id BIGSERIAL PRIMARY KEY,
                content TEXT NOT NULL,
                embedding vector({}) NOT NULL,
                metadata JSONB,
                created_at TIMESTAMPTZ DEFAULT NOW(){}
            )
            "#,
            self.config.table_name(),
            self.config.embedding_dim(),
            tsv_column,
        );

        client.execute(&create_table, &[]).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;

        // Create vector and (optionally) GIN indexes
        self.create_index_inner(&client).await?;

        Ok(())
    }

    /// Recreate the vector (and optional GIN) indexes.
    ///
    /// This is useful after a large bulk insert where you want to drop
    /// and rebuild the index for optimal search performance.
    pub async fn create_index(&self) -> RagResult<()> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        self.create_index_inner(&client).await
    }

    /// Shared helper that creates vector and GIN indexes using an
    /// already-acquired pool client.
    async fn create_index_inner(&self, client: &deadpool_postgres::Object) -> RagResult<()> {
        let index_type = self.config.index_type();
        let ops = self.config.distance_metric().index_ops();
        let (method, ops_class, with_clause) = index_type.index_sql(ops);

        if !method.is_empty() {
            let create_vec_idx = format!(
                "CREATE INDEX IF NOT EXISTS {table}_embedding_idx ON {table} USING {method} (embedding {ops_class}) {with_clause}",
                table = self.config.table_name(),
                method = method,
                ops_class = ops_class,
                with_clause = with_clause,
            );

            // Index creation may fail if table is empty (IVFFlat); that's okay
            let _ = client.execute(&create_vec_idx, &[]).await;
        }

        // GIN index for hybrid text search
        if self.config.search_type() == super::SearchType::Hybrid {
            let create_gin_idx = format!(
                "CREATE INDEX IF NOT EXISTS {}_content_tsv_idx ON {} USING gin (content_tsv)",
                self.config.table_name(),
                self.config.table_name(),
            );
            let _ = client.execute(&create_gin_idx, &[]).await;
        }

        Ok(())
    }

    /// Set the HNSW `ef_search` parameter for the current connection,
    /// controlling the trade-off between search quality and speed.
    ///
    /// Higher values yield more accurate results at the cost of latency.
    /// This is a session-level setting and does not persist across connections.
    pub async fn set_hnsw_ef_search(&self, ef_search: u16) -> RagResult<()> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;

        let query = format!("SET hnsw.ef_search = {}", ef_search);
        client.execute(&query, &[]).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;

        Ok(())
    }
    
    /// Insert a document with its embedding
    pub async fn insert(&self, doc: NewDocument) -> RagResult<i64> {
        if doc.embedding.len() != self.config.embedding_dim() {
            return Err(RagError::DimensionMismatch {
                expected: self.config.embedding_dim(),
                actual: doc.embedding.len(),
            });
        }
        
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        
        let embedding = Vector::from(doc.embedding);
        
        let query = format!(
            "INSERT INTO {} (content, embedding, metadata) VALUES ($1, $2, $3) RETURNING id",
            self.config.table_name()
        );
        
        let row = client.query_one(&query, &[&doc.content, &embedding, &doc.metadata]).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
        
        Ok(row.get(0))
    }

    /// Insert or update a document.
    ///
    /// When `id` is `Some` and a row with that ID already exists, the
    /// content, embedding, and metadata columns are updated. When `id` is
    /// `None` a new row is inserted. In both cases the row ID is returned.
    pub async fn upsert(&self, id: Option<i64>, doc: NewDocument) -> RagResult<i64> {
        if doc.embedding.len() != self.config.embedding_dim() {
            return Err(RagError::DimensionMismatch {
                expected: self.config.embedding_dim(),
                actual: doc.embedding.len(),
            });
        }

        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;

        let embedding = Vector::from(doc.embedding);

        if let Some(id) = id {
            let query = format!(
                r#"INSERT INTO {} (id, content, embedding, metadata)
                   VALUES ($1, $2, $3, $4)
                   ON CONFLICT (id) DO UPDATE
                   SET content = EXCLUDED.content,
                       embedding = EXCLUDED.embedding,
                       metadata = EXCLUDED.metadata
                   RETURNING id"#,
                self.config.table_name()
            );
            let row = client.query_one(&query, &[&id, &doc.content, &embedding, &doc.metadata]).await
                .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
            Ok(row.get(0))
        } else {
            let query = format!(
                "INSERT INTO {} (content, embedding, metadata) VALUES ($1, $2, $3) RETURNING id",
                self.config.table_name()
            );
            let row = client.query_one(&query, &[&doc.content, &embedding, &doc.metadata]).await
                .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
            Ok(row.get(0))
        }
    }

    /// Insert multiple documents in a batch using transactions.
    ///
    /// All embedding dimensions are validated upfront before any database
    /// work begins. Documents are then processed in batches of 100 inside
    /// transactions, with the INSERT statement prepared once per batch for
    /// pipelining.
    pub async fn insert_batch(&self, docs: Vec<NewDocument>) -> RagResult<Vec<i64>> {
        // Validate all embedding dimensions upfront
        for (i, doc) in docs.iter().enumerate() {
            if doc.embedding.len() != self.config.embedding_dim() {
                return Err(RagError::DimensionMismatch {
                    expected: self.config.embedding_dim(),
                    actual: doc.embedding.len(),
                });
            }
            // Provide context for which document failed (0-indexed)
            let _ = i;
        }

        let mut ids = Vec::with_capacity(docs.len());
        let insert_sql = format!(
            "INSERT INTO {} (content, embedding, metadata) VALUES ($1, $2, $3) RETURNING id",
            self.config.table_name()
        );

        const BATCH_SIZE: usize = 100;

        for chunk in docs.chunks(BATCH_SIZE) {
            let mut client = self.pool.get().await
                .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;

            let tx = client.transaction().await
                .map_err(|e| RagError::QueryFailed(format!("begin transaction: {}", e)))?;

            let stmt = tx.prepare(&insert_sql).await
                .map_err(|e| RagError::QueryFailed(format!("prepare: {}", e)))?;

            for doc in chunk {
                let embedding = Vector::from(doc.embedding.clone());
                let row = tx.query_one(&stmt, &[&doc.content, &embedding, &doc.metadata]).await
                    .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
                ids.push(row.get::<_, i64>(0));
            }

            tx.commit().await
                .map_err(|e| RagError::QueryFailed(format!("commit: {}", e)))?;
        }

        Ok(ids)
    }
    
    /// Search for similar documents using vector similarity
    pub async fn search(&self, query_embedding: &[f32], limit: Option<usize>) -> RagResult<Vec<Document>> {
        self.search_with_filter(query_embedding, limit, None).await
    }

    /// Search for similar documents with metadata filtering
    ///
    /// # Example
    ///
    /// ```rust,ignore
    /// use llama_gguf::rag::{RagStore, MetadataFilter};
    ///
    /// // Search with a simple filter
    /// let filter = MetadataFilter::eq("type", "documentation");
    /// let results = store.search_with_filter(&embedding, Some(10), Some(filter)).await?;
    ///
    /// // Search with multiple filters
    /// let filter = MetadataFilter::and(vec![
    ///     MetadataFilter::eq("source", "docs"),
    ///     MetadataFilter::gte("version", 2),
    /// ]);
    /// let results = store.search_with_filter(&embedding, Some(5), Some(filter)).await?;
    /// ```
    pub async fn search_with_filter(
        &self,
        query_embedding: &[f32],
        limit: Option<usize>,
        filter: Option<MetadataFilter>,
    ) -> RagResult<Vec<Document>> {
        self.search_vector_inner(query_embedding, limit, filter).await
    }

    /// Core vector-similarity search implementation.
    async fn search_vector_inner(
        &self,
        query_embedding: &[f32],
        limit: Option<usize>,
        filter: Option<MetadataFilter>,
    ) -> RagResult<Vec<Document>> {
        if query_embedding.len() != self.config.embedding_dim() {
            return Err(RagError::DimensionMismatch {
                expected: self.config.embedding_dim(),
                actual: query_embedding.len(),
            });
        }

        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;

        let embedding = Vector::from(query_embedding.to_vec());
        let limit = limit.unwrap_or(self.config.max_results()) as i64;
        let operator = self.config.distance_metric().operator();

        // For cosine distance, convert to similarity (1 - distance)
        let score_expr = match self.config.distance_metric() {
            super::DistanceMetric::Cosine => format!("1 - (embedding {} $1)", operator),
            super::DistanceMetric::L2 => format!("1 / (1 + (embedding {} $1))", operator),
            super::DistanceMetric::InnerProduct => format!("-(embedding {} $1)", operator),
        };

        // Cast to f64 so tokio-postgres sends float8 (double precision),
        // matching the return type of pgvector distance operators.
        let min_sim = self.config.min_similarity() as f64;

        // Build the WHERE clause
        let (filter_clause, filter_params) = if let Some(f) = filter {
            let (sql, params) = f.to_sql(3)?; // Start after $1 (embedding), $2 (min_sim), $3 (limit)
            (format!(" AND {}", sql), params)
        } else {
            (String::new(), Vec::new())
        };

        let query = format!(
            r#"
            SELECT id, content, metadata, ({})::float4 as score
            FROM {}
            WHERE {} >= $2{}
            ORDER BY embedding {} $1
            LIMIT $3
            "#,
            score_expr,
            self.config.table_name(),
            score_expr,
            filter_clause,
            operator
        );

        // Build params dynamically
        use tokio_postgres::types::ToSql;
        let mut params: Vec<&(dyn ToSql + Sync)> = vec![&embedding, &min_sim, &limit];
        let filter_param_refs: Vec<&str> = filter_params.iter().map(|s| s.as_str()).collect();
        for p in &filter_param_refs {
            params.push(p);
        }

        let rows = client.query(&query, &params).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;

        let docs = rows.iter().map(|row| {
            Document {
                id: row.get(0),
                content: row.get(1),
                metadata: row.get(2),
                score: Some(row.get(3)),
            }
        }).collect();

        Ok(docs)
    }

    /// Perform a keyword-only search using PostgreSQL full-text search.
    ///
    /// Returns `(id, ts_rank_score)` pairs ordered by relevance.
    /// Requires `search_type = "hybrid"` in the config so that the
    /// `content_tsv` generated column exists.
    pub async fn search_keyword(
        &self,
        query_text: &str,
        limit: usize,
        filter: Option<MetadataFilter>,
    ) -> RagResult<Vec<(i64, f32)>> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;

        let lang = self.config.text_search_language();

        let (filter_clause, filter_params) = if let Some(f) = filter {
            let (sql, params) = f.to_sql(2)?; // $1 = query_text, $2 = limit
            (format!(" AND {}", sql), params)
        } else {
            (String::new(), Vec::new())
        };

        let limit_i64 = limit as i64;

        let query = format!(
            r#"
            SELECT id, ts_rank(content_tsv, plainto_tsquery('{lang}', $1)) as rank
            FROM {table}
            WHERE content_tsv @@ plainto_tsquery('{lang}', $1){filter}
            ORDER BY rank DESC
            LIMIT $2
            "#,
            lang = lang,
            table = self.config.table_name(),
            filter = filter_clause,
        );

        use tokio_postgres::types::ToSql;
        let mut params: Vec<&(dyn ToSql + Sync)> = vec![&query_text, &limit_i64];
        let filter_param_refs: Vec<&str> = filter_params.iter().map(|s| s.as_str()).collect();
        for p in &filter_param_refs {
            params.push(p);
        }

        let rows = client.query(&query, &params).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;

        let results = rows.iter().map(|row| {
            let id: i64 = row.get(0);
            let score: f32 = row.get(1);
            (id, score)
        }).collect();

        Ok(results)
    }

    /// Perform a hybrid search combining vector similarity and keyword
    /// relevance via Reciprocal Rank Fusion (RRF).
    ///
    /// Both the vector and keyword searches are run, then their results
    /// are merged using [`rrf_fuse`] and the final documents are returned
    /// sorted by the fused score.
    pub async fn search_hybrid(
        &self,
        query_embedding: &[f32],
        query_text: &str,
        limit: Option<usize>,
        filter: Option<MetadataFilter>,
    ) -> RagResult<Vec<Document>> {
        let limit = limit.unwrap_or(self.config.max_results());
        let oversampled = limit * self.config.hybrid_oversampling() as usize;

        // Run vector search (oversampled)
        let vec_docs = self.search_vector_inner(query_embedding, Some(oversampled), filter.clone()).await?;
        let vector_results: Vec<(i64, f32)> = vec_docs.iter().map(|d| (d.id, d.score.unwrap_or(0.0))).collect();

        // Run keyword search (oversampled)
        let keyword_results = self.search_keyword(query_text, oversampled, filter).await?;

        // Fuse with RRF
        let fused = rrf_fuse(&vector_results, &keyword_results, self.config.rrf_k(), limit);

        // Build a map of id -> Document from vector results
        let mut doc_map: std::collections::HashMap<i64, Document> = vec_docs.into_iter().map(|d| (d.id, d)).collect();

        // Identify keyword-only IDs that we need to fetch
        let missing_ids: Vec<i64> = fused.iter()
            .filter(|(id, _)| !doc_map.contains_key(id))
            .map(|(id, _)| *id)
            .collect();

        if !missing_ids.is_empty() {
            let client = self.pool.get().await
                .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;

            // Fetch missing docs by ID
            let placeholders: Vec<String> = (1..=missing_ids.len()).map(|i| format!("${}", i)).collect();
            let query = format!(
                "SELECT id, content, metadata FROM {} WHERE id IN ({})",
                self.config.table_name(),
                placeholders.join(", ")
            );

            use tokio_postgres::types::ToSql;
            let params: Vec<&(dyn ToSql + Sync)> = missing_ids.iter().map(|id| id as &(dyn ToSql + Sync)).collect();

            let rows = client.query(&query, &params).await
                .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;

            for row in &rows {
                let doc = Document {
                    id: row.get(0),
                    content: row.get(1),
                    metadata: row.get(2),
                    score: None,
                };
                doc_map.insert(doc.id, doc);
            }
        }

        // Assemble final results in fused order
        let results = fused.into_iter().filter_map(|(id, score)| {
            doc_map.remove(&id).map(|mut doc| {
                doc.score = Some(score);
                doc
            })
        }).collect();

        Ok(results)
    }

    /// Count documents matching a filter
    pub async fn count_with_filter(&self, filter: Option<MetadataFilter>) -> RagResult<i64> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        
        let (filter_clause, filter_params) = if let Some(f) = filter {
            let (sql, params) = f.to_sql(0)?;
            (format!(" WHERE {}", sql), params)
        } else {
            (String::new(), Vec::new())
        };
        
        let query = format!(
            "SELECT COUNT(*) FROM {}{}",
            self.config.table_name(),
            filter_clause
        );
        
        use tokio_postgres::types::ToSql;
        let filter_param_refs: Vec<&str> = filter_params.iter().map(|s| s.as_str()).collect();
        let params: Vec<&(dyn ToSql + Sync)> = filter_param_refs.iter().map(|p| p as &(dyn ToSql + Sync)).collect();
        
        let row = client.query_one(&query, &params).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
        
        Ok(row.get(0))
    }
    
    /// Delete documents matching a filter
    pub async fn delete_with_filter(&self, filter: MetadataFilter) -> RagResult<u64> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        
        let (filter_sql, filter_params) = filter.to_sql(0)?;
        
        let query = format!(
            "DELETE FROM {} WHERE {}",
            self.config.table_name(),
            filter_sql
        );
        
        use tokio_postgres::types::ToSql;
        let filter_param_refs: Vec<&str> = filter_params.iter().map(|s| s.as_str()).collect();
        let params: Vec<&(dyn ToSql + Sync)> = filter_param_refs.iter().map(|p| p as &(dyn ToSql + Sync)).collect();
        
        let affected = client.execute(&query, &params).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
        
        Ok(affected)
    }
    
    /// List unique values for a metadata field
    pub async fn list_metadata_values(&self, field: &str, limit: Option<usize>) -> RagResult<Vec<String>> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        
        let limit = limit.unwrap_or(100) as i64;
        
        let field = validate_field_name(field)?;

        let query = format!(
            "SELECT DISTINCT metadata->>'{}' as val FROM {} WHERE metadata ? '{}' ORDER BY val LIMIT $1",
            field,
            self.config.table_name(),
            field
        );
        
        let rows = client.query(&query, &[&limit]).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
        
        let values = rows.iter()
            .filter_map(|row| row.get::<_, Option<String>>(0))
            .collect();
        
        Ok(values)
    }
    
    /// Get a document by ID
    pub async fn get(&self, id: i64) -> RagResult<Option<Document>> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        
        let query = format!(
            "SELECT id, content, metadata FROM {} WHERE id = $1",
            self.config.table_name()
        );
        
        let row = client.query_opt(&query, &[&id]).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
        
        Ok(row.map(|r| Document {
            id: r.get(0),
            content: r.get(1),
            metadata: r.get(2),
            score: None,
        }))
    }
    
    /// Delete a document by ID
    pub async fn delete(&self, id: i64) -> RagResult<bool> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        
        let query = format!("DELETE FROM {} WHERE id = $1", self.config.table_name());
        let affected = client.execute(&query, &[&id]).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
        
        Ok(affected > 0)
    }
    
    /// Count total documents
    pub async fn count(&self) -> RagResult<i64> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        
        let query = format!("SELECT COUNT(*) FROM {}", self.config.table_name());
        let row = client.query_one(&query, &[]).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
        
        Ok(row.get(0))
    }
    
    /// Clear all documents from the table
    pub async fn clear(&self) -> RagResult<u64> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;
        
        let query = format!("DELETE FROM {}", self.config.table_name());
        let affected = client.execute(&query, &[]).await
            .map_err(|e| RagError::QueryFailed(format!("{}", e)))?;
        
        Ok(affected)
    }
    
    /// Get the configuration
    pub fn config(&self) -> &RagConfig {
        &self.config
    }

    /// Verify the database connection is alive by executing `SELECT 1`.
    pub async fn health_check(&self) -> RagResult<()> {
        let client = self.pool.get().await
            .map_err(|e| RagError::ConnectionFailed(format!("{}", e)))?;

        client.query_one("SELECT 1", &[]).await
            .map_err(|e| RagError::QueryFailed(format!("health check failed: {}", e)))?;

        Ok(())
    }
}

/// Builder for creating RAG context from search results
pub struct RagContextBuilder {
    docs: Vec<Document>,
    separator: String,
    max_tokens: Option<usize>,
    include_scores: bool,
}

impl RagContextBuilder {
    /// Create a new context builder from search results
    pub fn new(docs: Vec<Document>) -> Self {
        Self {
            docs,
            separator: "\n\n".to_string(),
            max_tokens: None,
            include_scores: false,
        }
    }
    
    /// Set the separator between documents
    pub fn with_separator(mut self, sep: impl Into<String>) -> Self {
        self.separator = sep.into();
        self
    }
    
    /// Set approximate maximum tokens (characters / 4)
    pub fn with_max_tokens(mut self, max: usize) -> Self {
        self.max_tokens = Some(max);
        self
    }
    
    /// Include similarity scores in output
    pub fn with_scores(mut self, include: bool) -> Self {
        self.include_scores = include;
        self
    }
    
    /// Build the context string
    pub fn build(self) -> String {
        let mut parts = Vec::new();
        let mut total_chars = 0;
        let max_chars = self.max_tokens.map(|t| t * 4);
        
        for doc in &self.docs {
            let part = if self.include_scores {
                if let Some(score) = doc.score {
                    format!("[{:.2}] {}", score, doc.content)
                } else {
                    doc.content.clone()
                }
            } else {
                doc.content.clone()
            };
            
            if let Some(max) = max_chars
                && total_chars + part.len() > max {
                    break;
                }
            
            total_chars += part.len() + self.separator.len();
            parts.push(part);
        }
        
        parts.join(&self.separator)
    }
    
    /// Build a prompt with context and question
    pub fn build_prompt(self, question: &str) -> String {
        let context = self.build();
        format!(
            "Use the following context to answer the question.\n\n\
            Context:\n{}\n\n\
            Question: {}\n\n\
            Answer:",
            context,
            question
        )
    }
}

/// Reciprocal Rank Fusion - merge two ranked result lists
///
/// Each input is a list of (id, score) pairs ordered by relevance.
/// Returns fused results sorted by RRF score, limited to `limit`.
pub(crate) fn rrf_fuse(
    vector_results: &[(i64, f32)],
    keyword_results: &[(i64, f32)],
    k: u32,
    limit: usize,
) -> Vec<(i64, f32)> {
    use std::collections::HashMap;

    let mut scores: HashMap<i64, f32> = HashMap::new();

    for (rank, (id, _)) in vector_results.iter().enumerate() {
        *scores.entry(*id).or_default() += 1.0 / (k as f32 + rank as f32 + 1.0);
    }

    for (rank, (id, _)) in keyword_results.iter().enumerate() {
        *scores.entry(*id).or_default() += 1.0 / (k as f32 + rank as f32 + 1.0);
    }

    let mut fused: Vec<(i64, f32)> = scores.into_iter().collect();
    fused.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
    fused.truncate(limit);
    fused
}

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

    #[test]
    fn test_validate_field_name_valid() {
        assert!(validate_field_name("source").is_ok());
        assert!(validate_field_name("my_field").is_ok());
        assert!(validate_field_name("field123").is_ok());
        assert!(validate_field_name("a.b").is_ok());
    }

    #[test]
    fn test_validate_field_name_rejects_sql_injection() {
        assert!(validate_field_name("'; DROP TABLE --").is_err());
        assert!(validate_field_name("field; DELETE").is_err());
        assert!(validate_field_name("").is_err());
        assert!(validate_field_name("a\"b").is_err());
    }

    #[test]
    fn test_rrf_fusion_basic() {
        let vector_results = vec![(1i64, 0.95f32), (2, 0.85), (3, 0.75)];
        let keyword_results = vec![(2i64, 0.9f32), (3, 0.8), (4, 0.7)];
        let fused = rrf_fuse(&vector_results, &keyword_results, 60, 3);
        // ID 2 appears in both, should rank highest
        assert_eq!(fused[0].0, 2);
        assert!(fused.iter().all(|(_, score)| *score > 0.0));
        assert!(fused.len() <= 3);
    }

    #[test]
    fn test_rrf_fusion_disjoint() {
        let vector_results = vec![(1i64, 0.9f32)];
        let keyword_results = vec![(2i64, 0.9f32)];
        let fused = rrf_fuse(&vector_results, &keyword_results, 60, 10);
        assert_eq!(fused.len(), 2);
    }

    #[test]
    fn test_rrf_fusion_empty() {
        let fused = rrf_fuse(&[], &[], 60, 5);
        assert!(fused.is_empty());
    }

    #[test]
    fn test_metadata_filter_to_sql_validates_fields() {
        let filter = MetadataFilter::eq("valid_field", "value");
        assert!(filter.to_sql(0).is_ok());

        let bad_filter = MetadataFilter::eq("'; DROP TABLE", "value");
        assert!(bad_filter.to_sql(0).is_err());
    }
}