qql-core 0.4.1

Parser, typed AST, validation, and transformations for the Qdrant Query Language
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
use alloc::boxed::Box;

use crate::ast::{
    CollectionConfig, CollectionParamsConfig, HnswRuntimeConfig, MemoryPlacement,
    MultivectorComparator, MultivectorConfig, OptimizersRuntimeConfig, QuantizationConfig,
    QuantizationType, QuantizationUpdate, ShardKey, SparseIndexConfig, Value, VectorDatatype,
    VectorsConfig,
};
use crate::error::{QqlError, Span};
use crate::token::TokenKind;

use super::config_validation::is_integer_val;
use super::{
    AstLowerer, ascii_equal, config_bool, config_float_range, config_has_key,
    config_max_optimization_threads, config_non_negative_u64, config_positive_u64, config_value,
    merge_collection_config, validate_hnsw_value, validate_optimizers_value, validate_params_value,
    validate_vectors_value,
};

fn validation_err(message: impl Into<alloc::borrow::Cow<'static, str>>, span: Span) -> QqlError {
    QqlError::validation("QQL-VALIDATION-CONFIG", message, Some(span))
}

/// Parse a `memory = 'cold' | 'cached' | 'pinned'` placement value.
///
/// Returns `Ok(None)` when the key is absent. Any non-string or unknown value
/// is a validation error (the enum is closed; a typo must not silently hit the
/// backend default). When `allow_pinned` is false (payload storage), `pinned`
/// is rejected here so callers cannot bypass the closed set.
fn config_memory(
    config: &[(alloc::string::String, Value)],
    key: &str,
    span: Span,
    allow_pinned: bool,
) -> Result<Option<MemoryPlacement>, QqlError> {
    match config_value(config, key) {
        None => Ok(None),
        Some(Value::Str(s)) => match MemoryPlacement::parse(s) {
            Some(MemoryPlacement::Pinned) if !allow_pinned => Err(validation_err(
                alloc::format!("{key} does not support 'pinned'"),
                span,
            )),
            Some(m) => Ok(Some(m)),
            None => Err(validation_err(
                alloc::format!("{key} must be 'cold', 'cached', or 'pinned', got '{s}'"),
                span,
            )),
        },
        Some(_) => Err(validation_err(
            alloc::format!("{key} must be a string ('cold', 'cached', or 'pinned')"),
            span,
        )),
    }
}

fn config_dense_datatype(
    config: &[(alloc::string::String, Value)],
    span: Span,
) -> Result<Option<VectorDatatype>, QqlError> {
    match config_value(config, "datatype") {
        None => Ok(None),
        Some(Value::Str(s)) => match VectorDatatype::parse(s) {
            Some(dt) => Ok(Some(dt)),
            None => Err(validation_err(
                "datatype must be float32, float16, uint8, or turbo4 for VECTOR",
                span,
            )),
        },
        Some(_) => Err(validation_err("datatype must be a string for VECTOR", span)),
    }
}

impl<'a> AstLowerer<'a> {
    // ── Config blocks ───────────────────────────────────────────

    pub fn parse_collection_config_blocks(
        &mut self,
        for_alter: bool,
    ) -> Result<Option<Box<CollectionConfig>>, QqlError> {
        let mut config: Option<CollectionConfig> = None;
        while self.peek()?.kind == TokenKind::With {
            self.advance()?;
            let block = self.parse_collection_config_clause(for_alter)?;
            match &mut config {
                None => config = Some(block),
                Some(c) => merge_collection_config(c, block, self.peek()?.span)?,
            }
        }
        Ok(config.map(Box::new))
    }

    pub fn parse_collection_config_clause(
        &mut self,
        for_alter: bool,
    ) -> Result<CollectionConfig, QqlError> {
        let tok = self.peek()?;
        match tok.kind {
            TokenKind::Hnsw => {
                self.advance()?;
                self.parse_hnsw_config_block()
            }
            TokenKind::Vector => {
                self.advance()?;
                if for_alter && self.peek()?.kind != TokenKind::Lparen {
                    return self.parse_vector_diff_clause();
                }
                self.parse_vectors_config_block()
            }
            TokenKind::Sparse if for_alter => {
                self.advance()?;
                self.parse_sparse_vector_diff_clause()
            }
            TokenKind::Optimizers => {
                self.advance()?;
                self.parse_optimizers_config_block()
            }
            TokenKind::Params => {
                self.advance()?;
                self.parse_collection_params_config_block(for_alter)
            }
            TokenKind::Wal => {
                self.advance()?;
                self.parse_wal_config_block(for_alter)
            }
            TokenKind::StrictMode => {
                self.advance()?;
                self.parse_strict_mode_config_block()
            }
            TokenKind::Metadata => {
                self.advance()?;
                self.parse_metadata_config_block()
            }
            TokenKind::Quantization => {
                self.advance()?;
                self.parse_quantization_config_block()
            }
            _ if tok.is_keyword_or_identifier() && ascii_equal(tok.text, "QUANTIZATION") => {
                self.advance()?;
                self.parse_quantization_config_block()
            }
            _ => Err(validation_err(
                alloc::format!(
                    "{}, got '{}'",
                    if for_alter {
                        "expected HNSW, VECTOR, SPARSE, OPTIMIZERS, PARAMS, QUANTIZATION, STRICT_MODE, or METADATA after WITH"
                    } else {
                        "expected HNSW, VECTOR, OPTIMIZERS, PARAMS, QUANTIZATION, WAL, STRICT_MODE, or METADATA after WITH"
                    },
                    tok.text
                ),
                tok.span,
            )),
        }
    }

    pub fn parse_hnsw_config_block(&mut self) -> Result<CollectionConfig, QqlError> {
        let config = self.parse_config_block()?;
        for (key, value) in &config {
            let lower = key.to_ascii_lowercase();
            match lower.as_str() {
                "m"
                | "ef_construct"
                | "full_scan_threshold"
                | "max_indexing_threads"
                | "on_disk"
                | "payload_m"
                | "inline_storage"
                | "memory" => {}
                _ => {
                    return Err(validation_err(
                        alloc::format!(
                            "unknown HNSW parameter '{}'. Expected: m, ef_construct, full_scan_threshold, max_indexing_threads, on_disk, payload_m, inline_storage, memory",
                            key
                        ),
                        self.peek()?.span,
                    ));
                }
            }
            validate_hnsw_value(key, value, self.peek()?.span)?;
        }

        if let Some(value) = config_value(&config, "m") {
            let m = match value {
                Value::Int(n) => Some(*n),
                Value::Float(f) if is_integer_val(value) => Some(*f as i64),
                _ => None,
            };
            if let Some(n) = m
                && n != 0
                && n < 4
            {
                return Err(validation_err("m must be 0 or >= 4", self.peek()?.span));
            }
        }

        let m_val = config_non_negative_u64(&config, "m", self.peek()?.span)?;
        let ef_construct = config_positive_u64(&config, "ef_construct", self.peek()?.span)?;
        let full_scan_threshold =
            config_non_negative_u64(&config, "full_scan_threshold", self.peek()?.span)?;
        let max_indexing_threads =
            config_positive_u64(&config, "max_indexing_threads", self.peek()?.span)?;
        let payload_m = config_positive_u64(&config, "payload_m", self.peek()?.span)?;

        Ok(CollectionConfig {
            vectors: None,
            hnsw: Some(Box::new(HnswRuntimeConfig {
                m: m_val,
                ef_construct,
                full_scan_threshold,
                max_indexing_threads,
                on_disk: config_bool(&config, "on_disk"),
                payload_m,
                inline_storage: config_bool(&config, "inline_storage"),
                memory: config_memory(&config, "memory", self.peek()?.span, true)?,
            })),
            optimizers: None,
            params: None,
            quantization: None,
            quantization_update: None,
            wal: None,
            strict_mode: None,
            metadata: None,
            vector_diffs: Vec::new(),
            sparse_vector_diffs: Vec::new(),
        })
    }

    pub fn parse_vectors_config_block(&mut self) -> Result<CollectionConfig, QqlError> {
        let config = self.parse_config_block()?;
        for (key, value) in &config {
            if !matches!(
                key.to_ascii_lowercase().as_str(),
                "on_disk" | "memory" | "datatype"
            ) {
                return Err(validation_err(
                    alloc::format!(
                        "unknown VECTOR parameter '{}'. Expected: on_disk, memory, datatype",
                        key
                    ),
                    self.peek()?.span,
                ));
            }
            validate_vectors_value(key, value, self.peek()?.span)?;
        }
        Ok(CollectionConfig {
            vectors: Some(Box::new(VectorsConfig {
                on_disk: config_bool(&config, "on_disk"),
                memory: config_memory(&config, "memory", self.peek()?.span, true)?,
                datatype: config_dense_datatype(&config, self.peek()?.span)?,
            })),
            hnsw: None,
            optimizers: None,
            params: None,
            quantization: None,
            quantization_update: None,
            wal: None,
            strict_mode: None,
            metadata: None,
            vector_diffs: Vec::new(),
            sparse_vector_diffs: Vec::new(),
        })
    }

    pub fn parse_optimizers_config_block(&mut self) -> Result<CollectionConfig, QqlError> {
        let config = self.parse_config_block()?;
        for (key, value) in &config {
            let lower = key.to_ascii_lowercase();
            match lower.as_str() {
                "deleted_threshold"
                | "vacuum_min_vector_number"
                | "default_segment_number"
                | "max_segment_size"
                | "memmap_threshold"
                | "indexing_threshold"
                | "flush_interval_sec"
                | "max_optimization_threads"
                | "prevent_unoptimized" => {}
                _ => {
                    return Err(validation_err(
                        alloc::format!(
                            "unknown OPTIMIZERS parameter '{}'. Expected: deleted_threshold, vacuum_min_vector_number, default_segment_number, max_segment_size, memmap_threshold, indexing_threshold, flush_interval_sec, max_optimization_threads, prevent_unoptimized",
                            key
                        ),
                        self.peek()?.span,
                    ));
                }
            }
            validate_optimizers_value(key, value, self.peek()?.span)?;

            if lower.as_str() == "deleted_threshold" {
                super::check_deleted_threshold(value, self.peek()?.span)?;
            }
            if lower.as_str() == "max_optimization_threads" {
                match value {
                    Value::Int(n) if *n <= 0 => {
                        return Err(validation_err(
                            "max_optimization_threads must be a positive integer or 'auto'",
                            self.peek()?.span,
                        ));
                    }
                    Value::Str(s) if !ascii_equal(s, "auto") => {
                        return Err(validation_err(
                            "max_optimization_threads must be a positive integer or 'auto'",
                            self.peek()?.span,
                        ));
                    }
                    _ => {}
                }
            }
        }

        Ok(CollectionConfig {
            vectors: None,
            hnsw: None,
            optimizers: Some(Box::new(OptimizersRuntimeConfig {
                deleted_threshold: config_float_range(&config, "deleted_threshold", 0.0, 1.0),
                vacuum_min_vector_number: config_positive_u64(
                    &config,
                    "vacuum_min_vector_number",
                    self.peek()?.span,
                )?,
                default_segment_number: config_positive_u64(
                    &config,
                    "default_segment_number",
                    self.peek()?.span,
                )?,
                max_segment_size: config_positive_u64(
                    &config,
                    "max_segment_size",
                    self.peek()?.span,
                )?,
                memmap_threshold: config_non_negative_u64(
                    &config,
                    "memmap_threshold",
                    self.peek()?.span,
                )?,
                indexing_threshold: config_non_negative_u64(
                    &config,
                    "indexing_threshold",
                    self.peek()?.span,
                )?,
                flush_interval_sec: config_positive_u64(
                    &config,
                    "flush_interval_sec",
                    self.peek()?.span,
                )?,
                max_optimization_threads: config_max_optimization_threads(
                    &config,
                    "max_optimization_threads",
                ),
                prevent_unoptimized: config_bool(&config, "prevent_unoptimized"),
            })),
            params: None,
            quantization: None,
            quantization_update: None,
            wal: None,
            strict_mode: None,
            metadata: None,
            vector_diffs: Vec::new(),
            sparse_vector_diffs: Vec::new(),
        })
    }

    pub fn parse_collection_params_config_block(
        &mut self,
        _for_alter: bool,
    ) -> Result<CollectionConfig, QqlError> {
        let config = self.parse_config_block()?;
        for (key, value) in &config {
            let lower = key.to_ascii_lowercase();
            match lower.as_str() {
                "replication_factor"
                | "write_consistency_factor"
                | "read_fan_out_factor"
                | "read_fan_out_delay_ms"
                | "on_disk_payload"
                | "payload_memory"
                | "shard_number"
                | "sharding_method"
                | "shard_keys" => {}
                _ => {
                    return Err(validation_err(
                        alloc::format!(
                            "unknown PARAMS parameter '{}'. Expected: replication_factor, write_consistency_factor, read_fan_out_factor, read_fan_out_delay_ms, on_disk_payload, payload_memory, shard_number, sharding_method, shard_keys",
                            key
                        ),
                        self.peek()?.span,
                    ));
                }
            }
            validate_params_value(key, value, self.peek()?.span)?;
        }

        // `read_fan_out_*` only exist on the update wire shape
        // (`CollectionParamsDiff`); create-time values are applied with a
        // follow-up PATCH (see `create_collection_deferred_params_rest`).

        Ok(CollectionConfig {
            vectors: None,
            hnsw: None,
            optimizers: None,
            params: Some(Box::new(CollectionParamsConfig {
                replication_factor: config_positive_u64(
                    &config,
                    "replication_factor",
                    self.peek()?.span,
                )?,
                write_consistency_factor: config_positive_u64(
                    &config,
                    "write_consistency_factor",
                    self.peek()?.span,
                )?,
                read_fan_out_factor: config_positive_u64(
                    &config,
                    "read_fan_out_factor",
                    self.peek()?.span,
                )?,
                read_fan_out_delay_ms: config_non_negative_u64(
                    &config,
                    "read_fan_out_delay_ms",
                    self.peek()?.span,
                )?,
                on_disk_payload: config_bool(&config, "on_disk_payload"),
                payload_memory: config_memory(&config, "payload_memory", self.peek()?.span, false)?,
                shard_number: config_positive_u64(&config, "shard_number", self.peek()?.span)?,
                sharding_method: match config_value(&config, "sharding_method") {
                    Some(Value::Str(s)) => Some(s.clone()),
                    Some(_) => {
                        return Err(validation_err(
                            "sharding_method must be a string ('auto' or 'custom')",
                            self.peek()?.span,
                        ));
                    }
                    None => None,
                },
                shard_keys: match config_value(&config, "shard_keys") {
                    Some(Value::List(items)) => {
                        let mut keys = Vec::with_capacity(items.len());
                        let entry_span = self.peek()?.span;
                        for item in items {
                            match item {
                                Value::Str(s) => keys.push(ShardKey::Keyword(s.clone())),
                                Value::Int(n) if *n >= 0 => {
                                    keys.push(ShardKey::Number(*n as u64));
                                }
                                Value::Param(name, span) => keys.push(ShardKey::Param(
                                    name.clone(),
                                    span.clone()
                                        .or_else(|| Some(alloc::boxed::Box::new(entry_span))),
                                )),
                                Value::PositionalParam(idx, span) => {
                                    keys.push(ShardKey::PositionalParam(
                                        *idx,
                                        span.clone()
                                            .or_else(|| Some(alloc::boxed::Box::new(entry_span))),
                                    ));
                                }
                                _ => {
                                    return Err(validation_err(
                                        "shard_keys entries must all be strings or non-negative integers",
                                        self.peek()?.span,
                                    ));
                                }
                            }
                        }
                        if keys.is_empty() {
                            return Err(validation_err(
                                "shard_keys must be a non-empty list of strings or non-negative integers",
                                self.peek()?.span,
                            ));
                        }
                        Some(keys)
                    }
                    Some(_) => {
                        return Err(validation_err(
                            "shard_keys must be a list of strings or non-negative integers",
                            self.peek()?.span,
                        ));
                    }
                    None => None,
                },
            })),
            quantization: None,
            quantization_update: None,
            wal: None,
            strict_mode: None,
            metadata: None,
            vector_diffs: Vec::new(),
            sparse_vector_diffs: Vec::new(),
        })
    }

    /// `WITH WAL (…)` — write-ahead log settings (CREATE-only; the update
    /// wire shape has no WAL field).
    pub fn parse_wal_config_block(
        &mut self,
        for_alter: bool,
    ) -> Result<CollectionConfig, QqlError> {
        if for_alter {
            return Err(validation_err(
                "WITH WAL is supported only for CREATE COLLECTION",
                self.peek()?.span,
            ));
        }
        let config = self.parse_config_block()?;
        for (key, value) in &config {
            let lower = key.to_ascii_lowercase();
            match lower.as_str() {
                "wal_capacity_mb" | "wal_segments_ahead" | "wal_retain_closed" => {}
                _ => {
                    return Err(validation_err(
                        alloc::format!(
                            "unknown WAL parameter '{}'. Expected: wal_capacity_mb, wal_segments_ahead, wal_retain_closed",
                            key
                        ),
                        self.peek()?.span,
                    ));
                }
            }
            super::validate_wal_value(key, value, self.peek()?.span)?;
        }
        Ok(CollectionConfig {
            vectors: None,
            hnsw: None,
            optimizers: None,
            params: None,
            quantization: None,
            quantization_update: None,
            wal: Some(config),
            strict_mode: None,
            metadata: None,
            vector_diffs: Vec::new(),
            sparse_vector_diffs: Vec::new(),
        })
    }

    /// `WITH STRICT_MODE (…)` — strict-mode settings (CREATE and ALTER; the
    /// plan layer validates value ranges and serializes the wire object).
    pub fn parse_strict_mode_config_block(&mut self) -> Result<CollectionConfig, QqlError> {
        let config = self.parse_config_block()?;
        for (key, value) in &config {
            if !super::is_strict_mode_key(key) {
                return Err(validation_err(
                    alloc::format!(
                        "unknown STRICT_MODE parameter '{}'. Expected: {}",
                        key,
                        super::STRICT_MODE_KEYS.join(", ")
                    ),
                    self.peek()?.span,
                ));
            }
            super::validate_strict_mode_value(key, value, self.peek()?.span)?;
        }
        Ok(CollectionConfig {
            vectors: None,
            hnsw: None,
            optimizers: None,
            params: None,
            quantization: None,
            quantization_update: None,
            wal: None,
            strict_mode: Some(config),
            metadata: None,
            vector_diffs: Vec::new(),
            sparse_vector_diffs: Vec::new(),
        })
    }

    /// `WITH METADATA (…)` — free-form collection metadata (CREATE and ALTER).
    /// Keys are unrestricted; values are any JSON value.
    pub fn parse_metadata_config_block(&mut self) -> Result<CollectionConfig, QqlError> {
        let config = self.parse_config_block()?;
        Ok(CollectionConfig {
            vectors: None,
            hnsw: None,
            optimizers: None,
            params: None,
            quantization: None,
            quantization_update: None,
            wal: None,
            strict_mode: None,
            metadata: Some(config),
            vector_diffs: Vec::new(),
            sparse_vector_diffs: Vec::new(),
        })
    }

    pub fn parse_quantization_config_block(&mut self) -> Result<CollectionConfig, QqlError> {
        let config = self.parse_config_block()?;

        if let Some(disabled_val) = config_bool(&config, "disabled")
            && disabled_val
        {
            return Ok(CollectionConfig {
                vectors: None,
                hnsw: None,
                optimizers: None,
                params: None,
                quantization: None,
                quantization_update: Some(Box::new(QuantizationUpdate {
                    disabled: true,
                    config: None,
                })),
                wal: None,
                strict_mode: None,
                metadata: None,
                vector_diffs: Vec::new(),
                sparse_vector_diffs: Vec::new(),
            });
        }

        let err_span = self.peek()?.span;
        let type_raw = config_value(&config, "type").ok_or_else(|| {
            validation_err(
                "QUANTIZATION config requires a 'type' (scalar, binary, product, turbo)",
                err_span,
            )
        })?;

        let type_str = match type_raw {
            Value::Str(s) => s,
            _ => {
                return Err(validation_err(
                    "QUANTIZATION 'type' must be a string",
                    self.peek()?.span,
                ));
            }
        };

        let qtype = match &type_str.to_ascii_lowercase()[..] {
            "scalar" => QuantizationType::Scalar,
            "binary" => QuantizationType::Binary,
            "product" => QuantizationType::Product,
            "turbo" => QuantizationType::Turbo,
            _ => {
                return Err(validation_err(
                    alloc::format!(
                        "unknown QUANTIZATION type '{}'. Expected scalar, binary, product, turbo",
                        type_str
                    ),
                    self.peek()?.span,
                ));
            }
        };

        // Each family accepts only its own sub-fields: misplaced keys (e.g.
        // `quantile` on binary, `bits` on scalar) fail closed instead of
        // being silently dropped.
        for (key, _) in &config {
            let allowed = match qtype {
                QuantizationType::Scalar => matches!(
                    key.to_ascii_lowercase().as_str(),
                    "type" | "quantile" | "always_ram" | "memory" | "disabled"
                ),
                QuantizationType::Binary => matches!(
                    key.to_ascii_lowercase().as_str(),
                    "type" | "encoding" | "query_encoding" | "always_ram" | "memory" | "disabled"
                ),
                QuantizationType::Product => matches!(
                    key.to_ascii_lowercase().as_str(),
                    "type" | "compression" | "always_ram" | "memory" | "disabled"
                ),
                QuantizationType::Turbo => matches!(
                    key.to_ascii_lowercase().as_str(),
                    "type" | "bits" | "always_ram" | "memory" | "disabled"
                ),
            };
            if !allowed {
                return Err(validation_err(
                    alloc::format!(
                        "unknown QUANTIZATION parameter '{}' for type '{}'",
                        key,
                        type_str.to_ascii_lowercase()
                    ),
                    self.peek()?.span,
                ));
            }
        }

        let always_ram = config_bool(&config, "always_ram").unwrap_or(false);

        let mut quantile: Option<f64> = None;
        if qtype == QuantizationType::Scalar && config_has_key(&config, "quantile") {
            quantile = config_float_range(&config, "quantile", 0.0, 1.0);
            if quantile.is_none() {
                return Err(validation_err(
                    "quantile must be between 0.0 and 1.0",
                    self.peek()?.span,
                ));
            }
        }

        let mut bits: Option<f64> = None;
        if qtype == QuantizationType::Turbo
            && let Some(v) = config_value(&config, "bits")
        {
            let bits_val = match v {
                Value::Int(n) => Some(*n as f64),
                Value::Float(f) => Some(*f),
                _ => None,
            };
            if let Some(b) = bits_val {
                if b != 1.0 && b != 1.5 && b != 2.0 && b != 4.0 {
                    return Err(validation_err(
                        "bits must be one of 1, 1.5, 2, or 4 for TURBO quantization",
                        self.peek()?.span,
                    ));
                }
                bits = Some(b);
            }
        }

        let mut compression: Option<String> = None;
        if qtype == QuantizationType::Product
            && let Some(Value::Str(c)) = config_value(&config, "compression")
        {
            let c_lower = c.to_ascii_lowercase();
            if matches!(c_lower.as_str(), "x4" | "x8" | "x16" | "x32" | "x64") {
                compression = Some(c_lower);
            } else {
                return Err(validation_err(
                    "compression must be x4, x8, x16, x32, or x64 for PRODUCT quantization",
                    self.peek()?.span,
                ));
            }
        }

        let mut encoding: Option<String> = None;
        let mut query_encoding: Option<String> = None;
        if qtype == QuantizationType::Binary {
            if let Some(e) = config_value(&config, "encoding") {
                let raw = match e {
                    Value::Str(s) => s.to_ascii_lowercase(),
                    Value::Int(n) => n.to_string(),
                    Value::Float(f) => {
                        if (*f - 1.5).abs() < f64::EPSILON {
                            "1.5".into()
                        } else if f.fract() == 0.0 {
                            format!("{}", *f as i64)
                        } else {
                            f.to_string()
                        }
                    }
                    _ => {
                        return Err(validation_err(
                            "encoding must be a string or number for BINARY quantization",
                            self.peek()?.span,
                        ));
                    }
                };
                // Canonicalize aliases so dump/plan always see one_bit|two_bits|one_and_half_bits.
                encoding = Some(match raw.as_str() {
                    "one_bit" | "onebit" | "1" => "one_bit".into(),
                    "two_bits" | "twobits" | "2" => "two_bits".into(),
                    "one_and_half_bits" | "oneandhalfbits" | "1.5" => "one_and_half_bits".into(),
                    _ => {
                        return Err(validation_err(
                            "encoding must be one_bit (1), two_bits (2), or one_and_half_bits (1.5) for BINARY quantization",
                            self.peek()?.span,
                        ));
                    }
                });
            }

            if let Some(Value::Str(qe)) = config_value(&config, "query_encoding") {
                let qe_lower = qe.to_ascii_lowercase();
                if matches!(
                    qe_lower.as_str(),
                    "default" | "binary" | "scalar4bits" | "scalar8bits"
                ) {
                    query_encoding = Some(qe_lower);
                } else {
                    return Err(validation_err(
                        "query_encoding must be default, binary, scalar4bits, or scalar8bits for BINARY quantization",
                        self.peek()?.span,
                    ));
                }
            }
        }

        let q_config = QuantizationConfig {
            qtype,
            always_ram,
            quantile,
            bits,
            compression,
            encoding,
            query_encoding,
            memory: config_memory(&config, "memory", self.peek()?.span, true)?,
        };

        Ok(CollectionConfig {
            vectors: None,
            hnsw: None,
            optimizers: None,
            params: None,
            quantization: Some(Box::new(q_config.clone())),
            quantization_update: Some(Box::new(QuantizationUpdate {
                disabled: false,
                config: Some(Box::new(q_config)),
            })),
            wal: None,
            strict_mode: None,
            metadata: None,
            vector_diffs: Vec::new(),
            sparse_vector_diffs: Vec::new(),
        })
    }

    pub fn parse_multivector_config_block(&mut self) -> Result<MultivectorConfig, QqlError> {
        let config = self.parse_config_block()?;
        let err_span = self.peek()?.span;
        let comp = config_value(&config, "comparator")
            .ok_or_else(|| validation_err("MULTIVECTOR config requires 'comparator'", err_span))?;
        let comparator = match comp {
            Value::Str(s) => s.to_ascii_lowercase(),
            _ => {
                return Err(validation_err(
                    "MULTIVECTOR comparator must be a string",
                    self.peek()?.span,
                ));
            }
        };
        if comparator != "max_sim" {
            return Err(validation_err(
                alloc::format!(
                    "MULTIVECTOR comparator must be 'max_sim', got '{}'",
                    comparator
                ),
                self.peek()?.span,
            ));
        }
        Ok(MultivectorConfig {
            comparator: MultivectorComparator::MaxSim,
        })
    }

    pub fn parse_sparse_config_block(
        &mut self,
    ) -> Result<(Option<Box<SparseIndexConfig>>, Option<String>), QqlError> {
        let config = self.parse_config_block()?;
        for (key, _) in &config {
            let lower = key.to_ascii_lowercase();
            if !matches!(
                lower.as_str(),
                "modifier" | "full_scan_threshold" | "on_disk" | "datatype" | "memory"
            ) {
                return Err(validation_err(
                    alloc::format!(
                        "unknown SPARSE/INDEX parameter '{}'. Expected: modifier, full_scan_threshold, on_disk, datatype, memory",
                        key
                    ),
                    self.peek()?.span,
                ));
            }
        }

        let mut modifier = None;
        if let Some(Value::Str(m)) = config_value(&config, "modifier") {
            let m_lower = m.to_ascii_lowercase();
            if matches!(m_lower.as_str(), "none" | "idf") {
                modifier = Some(m_lower);
            } else {
                return Err(validation_err(
                    "modifier must be none or idf for SPARSE vector",
                    self.peek()?.span,
                ));
            }
        }
        let full_scan_threshold =
            config_non_negative_u64(&config, "full_scan_threshold", self.peek()?.span)?;
        let on_disk = config_bool(&config, "on_disk");
        // `default` (and omitting the key) both mean "backend default" → None.
        let datatype = match config_value(&config, "datatype") {
            Some(Value::Str(s)) if s.eq_ignore_ascii_case("default") => None,
            Some(Value::Str(s)) => match VectorDatatype::parse_sparse(s) {
                Some(dt) => Some(dt),
                None => {
                    return Err(validation_err(
                        "datatype must be float32, uint8, float16, or default for SPARSE index",
                        self.peek()?.span,
                    ));
                }
            },
            Some(_) => {
                return Err(validation_err(
                    "datatype must be a string for SPARSE index",
                    self.peek()?.span,
                ));
            }
            None => None,
        };

        let index = if full_scan_threshold.is_some()
            || on_disk.is_some()
            || datatype.is_some()
            || config_has_key(&config, "memory")
        {
            Some(Box::new(SparseIndexConfig {
                full_scan_threshold,
                on_disk,
                datatype,
                memory: config_memory(&config, "memory", self.peek()?.span, true)?,
            }))
        } else {
            None
        };
        Ok((index, modifier))
    }
}