scythe-core 0.15.0

Core SQL parsing, catalog building, and type inference for scythe
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
use sqlparser::ast::{BinaryOperator, Expr};
use sqlparser::tokenizer::Span;

use super::helpers::*;
use super::type_conversion::datatype_to_neutral;
use super::types::*;

impl<'a> Analyzer<'a> {
    /// Resolve a placeholder string to a position number.
    /// For `$N` placeholders, returns the parsed number -- already idempotent,
    /// so `span` is ignored and the memo is never consulted or updated for
    /// this branch.
    /// For `?` (MySQL positional), the same source-text occurrence can reach
    /// this function twice (`collect_params_from_where` and `infer_expr_type`
    /// both visit the same projection expression -- see `analyze_select`), so
    /// `span` identifies the occurrence: a span already in
    /// `self.resolved_placeholders` returns its previously assigned position
    /// instead of auto-incrementing again (#170). `Span::empty()` -- which
    /// only synthetic/test AST nodes carry -- bypasses the memo entirely and
    /// falls back to the old always-increment behavior, since every such
    /// node would otherwise collapse onto one memo entry.
    /// For any other token (e.g. `:bucket` named placeholders) this is an error:
    /// a message is pushed to `self.type_errors` and `None` is returned so the
    /// caller's no-match path skips param registration. The error surfaces through
    /// `analyze()` which checks `type_errors` before returning.
    /// Note: Oracle `:N` numeric placeholders are converted to `?` by
    /// `preprocess_oracle_sql` before the AST is built, so they never reach here.
    pub(super) fn resolve_placeholder_position(&mut self, placeholder: &str, span: Span) -> Option<i64> {
        if let Some(pos) = parse_placeholder(placeholder) {
            Some(pos)
        } else if is_positional_placeholder(placeholder) {
            let memo_key = (span != Span::empty()).then_some(span);
            if let Some(key) = memo_key
                && let Some(&pos) = self.resolved_placeholders.get(&key)
            {
                return Some(pos);
            }
            self.positional_param_counter += 1;
            let pos = self.positional_param_counter;
            if let Some(key) = memo_key {
                self.resolved_placeholders.insert(key, pos);
            }
            Some(pos)
        } else {
            self.type_errors.push(format!(
                "unsupported placeholder \"{placeholder}\": named placeholders are not supported; \
                 use $N (PostgreSQL) or ? (MySQL/SQLite) instead",
            ));
            None
        }
    }

    pub(super) fn register_param(
        &mut self,
        position: i64,
        name: Option<String>,
        neutral_type: Option<String>,
        nullable: bool,
        source_relation: Option<String>,
    ) {
        if let Some(existing) = self.params.iter_mut().find(|p| p.position == position) {
            if existing.name.is_none() && name.is_some() {
                existing.name = name;
            }
            if existing.neutral_type.is_none() && neutral_type.is_some() {
                existing.neutral_type = neutral_type;
            }
            if existing.source_relation.is_none() && source_relation.is_some() {
                existing.source_relation = source_relation;
            }
        } else {
            self.params.push(ParamInfo {
                position,
                name,
                neutral_type,
                nullable,
                source_relation,
            });
        }
    }

    pub(super) fn collect_params_from_where(&mut self, expr: &Expr, scope: &Scope) {
        match expr {
            Expr::BinaryOp { left, op, right } => match op {
                BinaryOperator::Eq
                | BinaryOperator::NotEq
                | BinaryOperator::Lt
                | BinaryOperator::LtEq
                | BinaryOperator::Gt
                | BinaryOperator::GtEq => {
                    self.try_bind_param_from_comparison(left, right, scope, Some(op));
                    self.try_bind_param_from_comparison(right, left, scope, Some(op));
                    let left_ti = self.infer_expr_type(left, scope);
                    let right_ti = self.infer_expr_type(right, scope);
                    if left_ti.neutral_type != "unknown"
                        && right_ti.neutral_type != "unknown"
                        && !left_ti.neutral_type.starts_with("__")
                        && !right_ti.neutral_type.starts_with("__")
                        && left_ti.neutral_type != right_ti.neutral_type
                        && !is_comparable_types(&left_ti.neutral_type, &right_ti.neutral_type)
                    {
                        let left_sql = neutral_to_sql_label(&left_ti.neutral_type);
                        let right_sql = neutral_to_sql_label(&right_ti.neutral_type);
                        let op_str = format!("{}", op);
                        self.type_errors.push(format!(
                            "operator does not exist: {} {} {}",
                            left_sql, op_str, right_sql
                        ));
                    }
                }
                BinaryOperator::And | BinaryOperator::Or => {
                    self.collect_params_from_where(left, scope);
                    self.collect_params_from_where(right, scope);
                }
                _ => {
                    self.collect_params_from_where(left, scope);
                    self.collect_params_from_where(right, scope);
                }
            },
            Expr::Between {
                expr: col_expr,
                low,
                high,
                ..
            } => {
                let col_ti = self.infer_expr_type(col_expr, scope);
                self.collect_param_from_expr_with_type(low, &col_ti.neutral_type, "start");
                self.collect_param_from_expr_with_type(high, &col_ti.neutral_type, "end");
            }
            Expr::Like {
                expr: col_expr,
                pattern,
                ..
            }
            | Expr::ILike {
                expr: col_expr,
                pattern,
                ..
            } => {
                // A LIKE pattern is not always a bare placeholder literal --
                // `'%' || $1 || '%'` (a BinaryOp concat) is the common
                // idiom for a "contains" search, and was previously only
                // matched when `pattern` was exactly `Expr::Value`, so the
                // placeholder inside the concatenation silently registered
                // nothing (#171). `collect_param_from_expr_with_type`
                // already walks `BinaryOp`/`Cast`/`Nested`/... to find the
                // placeholder wherever it sits -- the same helper
                // `infer_expr_type`'s LIKE arm uses, so both traversals of
                // a LIKE pattern agree on what gets registered.
                let name = expr_to_name(col_expr);
                self.collect_param_from_expr_with_type(pattern, "string", &name);
            }
            Expr::InList {
                expr: col_expr, list, ..
            } => {
                let col_ti = self.infer_expr_type(col_expr, scope);
                let col_name = expr_to_name(col_expr);
                for item in list {
                    if let Expr::Value(vws) = item
                        && let Some(p) = value_is_placeholder(vws)
                        && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
                    {
                        // No `source_relation`: each `$N` in the list stands for one element
                        // of a set the column is tested against, not a single bound value
                        // compared to that column the way `col = $N` is.
                        let neutral_type = Some(col_ti.neutral_type.clone());
                        self.register_param(pos, Some(col_name.clone()), neutral_type, false, None);
                    }
                }
            }
            // The operand was previously never descended into, so a
            // placeholder used inside it (e.g. `WHERE coalesce(x, $1) IS
            // NULL`) registered nothing and silently dropped a parameter
            // from the signature while `$1` stayed in the emitted SQL
            // (#171). `IS NULL`/`IS NOT NULL` themselves are never
            // placeholders -- there is nothing dialect-specific to bind at
            // this node -- so this only needs to keep walking.
            Expr::IsNull(inner) | Expr::IsNotNull(inner) => self.collect_params_from_where(inner, scope),
            Expr::Nested(inner) => self.collect_params_from_where(inner, scope),
            Expr::UnaryOp { expr: inner, .. } => self.collect_params_from_where(inner, scope),
            Expr::AnyOp { left, right, .. } => {
                let left_ti = self.infer_expr_type(left, scope);
                if let Expr::Value(vws) = right.as_ref()
                    && let Some(p) = value_is_placeholder(vws)
                    && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
                {
                    let array_type = format!("array<{}>", left_ti.neutral_type);
                    let name = pluralize(&expr_to_name(left));
                    self.register_param(pos, Some(name), Some(array_type), false, None);
                }
                self.collect_param_from_any(right, &left_ti, &expr_to_name(left));
            }
            Expr::InSubquery { subquery, .. } => {
                let _ = self.analyze_query(subquery);
            }
            Expr::Exists { subquery, .. } => {
                let _ = self.analyze_query(subquery);
            }
            Expr::Subquery(subquery) => {
                let _ = self.analyze_query(subquery);
            }
            Expr::Case {
                conditions,
                else_result,
                ..
            } => {
                for case_when in conditions {
                    self.collect_params_from_where(&case_when.condition, scope);
                    let _ = self.infer_expr_type(&case_when.result, scope);
                }
                if let Some(else_expr) = else_result {
                    let _ = self.infer_expr_type(else_expr, scope);
                }
            }
            Expr::Function(func) => {
                let _ = self.infer_function_type(func, scope);
                let args = self.get_function_args(func);
                for arg in &args {
                    self.collect_params_from_where(arg, scope);
                }
            }
            Expr::Cast {
                expr: inner, data_type, ..
            } => {
                let neutral = datatype_to_neutral(data_type, self.catalog);
                self.collect_param_type_from_cast(inner, &neutral);
                self.collect_params_from_where(inner, scope);
            }
            _ => {
                let _ = self.infer_expr_type(expr, scope);
            }
        }
    }

    pub(super) fn try_bind_param_from_comparison(
        &mut self,
        param_side: &Expr,
        col_side: &Expr,
        scope: &Scope,
        op: Option<&BinaryOperator>,
    ) {
        match param_side {
            Expr::Value(vws) => {
                if let Some(p) = value_is_placeholder(vws)
                    && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
                {
                    let col_ti = self.infer_expr_type(col_side, scope);
                    let col_name = expr_to_name(col_side);
                    let param_name = derive_param_name_from_comparison(&col_name, col_side, param_side, op);
                    // The one shape where "the column this parameter is bound against" is
                    // unambiguous -- `col_side`'s own `source_relation`, the same field
                    // `AnalyzedColumn` carries for the same reason (#189). Everywhere else
                    // `register_param` is called from, the relation stays `None`.
                    self.register_param(
                        pos,
                        Some(param_name),
                        Some(col_ti.neutral_type),
                        false,
                        col_ti.source_relation,
                    );
                }
            }
            Expr::Cast {
                expr: inner, data_type, ..
            } => {
                if let Expr::Value(vws) = inner.as_ref()
                    && let Some(p) = value_is_placeholder(vws)
                    && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
                {
                    let neutral = datatype_to_neutral(data_type, self.catalog);
                    let col_ti = self.infer_expr_type(col_side, scope);
                    let col_name = expr_to_name(col_side);
                    let param_name = derive_param_name_from_comparison(&col_name, col_side, param_side, op);
                    self.register_param(pos, Some(param_name), Some(neutral), false, col_ti.source_relation);
                }
            }
            _ => {}
        }
    }

    pub(super) fn collect_param_from_expr(&mut self, expr: &Expr, name: &str, type_str: &str) {
        if let Expr::Value(vws) = expr {
            if let Some(p) = value_is_placeholder(vws)
                && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
            {
                self.register_param(pos, Some(name.to_string()), Some(type_str.to_string()), false, None);
            }
        } else if let Expr::Cast {
            expr: inner, data_type, ..
        } = expr
            && let Expr::Value(vws) = inner.as_ref()
            && let Some(p) = value_is_placeholder(vws)
            && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
        {
            let neutral = datatype_to_neutral(data_type, self.catalog);
            self.register_param(pos, Some(name.to_string()), Some(neutral), false, None);
        }
    }

    pub(super) fn collect_param_from_expr_with_type(&mut self, expr: &Expr, type_str: &str, name: &str) {
        self.collect_param_from_expr_with_type_nullable(expr, type_str, name, false);
    }

    pub(super) fn collect_param_from_expr_with_type_nullable(
        &mut self,
        expr: &Expr,
        type_str: &str,
        name: &str,
        nullable: bool,
    ) {
        match expr {
            Expr::Value(vws) => {
                if let Some(p) = value_is_placeholder(vws)
                    && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
                {
                    self.register_param(pos, Some(name.to_string()), Some(type_str.to_string()), nullable, None);
                }
            }
            Expr::Cast {
                expr: inner, data_type, ..
            } => {
                if let Expr::Value(vws) = inner.as_ref()
                    && let Some(p) = value_is_placeholder(vws)
                    && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
                {
                    let neutral = datatype_to_neutral(data_type, self.catalog);
                    self.register_param(pos, Some(name.to_string()), Some(neutral), nullable, None);
                } else {
                    self.collect_param_from_expr_with_type_nullable(inner, type_str, name, nullable);
                }
            }
            Expr::Nested(inner) => {
                self.collect_param_from_expr_with_type_nullable(inner, type_str, name, nullable);
            }
            Expr::UnaryOp { expr: inner, .. } => {
                self.collect_param_from_expr_with_type_nullable(inner, type_str, name, nullable);
            }
            Expr::BinaryOp { left, right, .. } => {
                self.collect_param_from_expr_with_type_nullable(left, type_str, name, nullable);
                self.collect_param_from_expr_with_type_nullable(right, type_str, name, nullable);
            }
            Expr::Function(func) => {
                for arg in self.get_function_args(func) {
                    self.collect_param_from_expr_with_type_nullable(&arg, type_str, name, nullable);
                }
            }
            Expr::Case {
                operand,
                conditions,
                else_result,
                ..
            } => {
                if let Some(op) = operand {
                    self.collect_param_from_expr_with_type_nullable(op, type_str, name, nullable);
                }
                for case_when in conditions {
                    self.collect_param_from_expr_with_type_nullable(&case_when.condition, type_str, name, nullable);
                    self.collect_param_from_expr_with_type_nullable(&case_when.result, type_str, name, nullable);
                }
                if let Some(else_expr) = else_result {
                    self.collect_param_from_expr_with_type_nullable(else_expr, type_str, name, nullable);
                }
            }
            _ => {}
        }
    }

    pub(super) fn collect_param_type_from_cast(&mut self, expr: &Expr, neutral_type: &str) {
        if let Expr::Value(vws) = expr
            && let Some(p) = value_is_placeholder(vws)
            && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
        {
            let name = match neutral_type {
                "interval" => Some("duration".to_string()),
                _ => None,
            };
            self.register_param(pos, name, Some(neutral_type.to_string()), false, None);
        }
    }

    pub(super) fn collect_param_from_any(&mut self, expr: &Expr, left_ti: &TypeInfo, left_name: &str) {
        match expr {
            Expr::Value(vws) => {
                if let Some(p) = value_is_placeholder(vws)
                    && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
                {
                    let array_type = format!("array<{}>", left_ti.neutral_type);
                    let name = pluralize(left_name);
                    self.register_param(pos, Some(name), Some(array_type), false, None);
                }
            }
            Expr::Cast {
                expr: inner, data_type, ..
            } => {
                if let Expr::Value(vws) = inner.as_ref()
                    && let Some(p) = value_is_placeholder(vws)
                    && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
                {
                    let neutral = datatype_to_neutral(data_type, self.catalog);
                    self.register_param(pos, None, Some(neutral), false, None);
                }
            }
            Expr::Nested(inner) => self.collect_param_from_any(inner, left_ti, left_name),
            Expr::Array(arr) => {
                for (i, elem) in arr.elem.iter().enumerate() {
                    if let Expr::Value(vws) = elem
                        && let Some(p) = value_is_placeholder(vws)
                        && let Some(pos) = self.resolve_placeholder_position(p, vws.span)
                    {
                        let name = format!("{}{}", left_name, i + 1);
                        self.register_param(pos, Some(name), Some(left_ti.neutral_type.clone()), false, None);
                    }
                }
            }
            _ => {}
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::catalog::Catalog;
    use ahash::AHashMap;
    use sqlparser::ast::{Ident, Value, ValueWithSpan};
    use sqlparser::tokenizer::{Location, Span};

    fn empty_catalog() -> Catalog {
        Catalog::from_ddl(&[]).unwrap()
    }

    fn make_analyzer(catalog: &Catalog) -> Analyzer<'_> {
        Analyzer {
            catalog,
            params: Vec::new(),
            ctes: AHashMap::new(),
            type_errors: Vec::new(),
            positional_param_counter: 0,
            pending_nested: Vec::new(),
            next_nested_id: 0,
            resolved_placeholders: AHashMap::new(),
        }
    }

    fn placeholder_expr(pos: &str) -> Expr {
        placeholder_expr_at(pos, Span::empty())
    }

    fn placeholder_expr_at(pos: &str, span: Span) -> Expr {
        Expr::Value(ValueWithSpan {
            value: Value::Placeholder(pos.to_string()),
            span,
        })
    }

    fn span_at(offset: u64) -> Span {
        // Distinct, non-empty spans standing in for two different
        // occurrences of the same placeholder token in real source text --
        // `Span::empty()` deliberately bypasses the memo (see
        // `resolve_placeholder_position`), so exercising the memo itself
        // needs spans sqlparser would actually hand back.
        Span::new(Location::new(1, offset), Location::new(1, offset + 1))
    }

    #[test]
    fn test_register_param_new() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        analyzer.register_param(1, Some("id".to_string()), Some("int32".to_string()), false, None);
        assert_eq!(analyzer.params.len(), 1);
        assert_eq!(analyzer.params[0].position, 1);
        assert_eq!(analyzer.params[0].name, Some("id".to_string()));
        assert_eq!(analyzer.params[0].neutral_type, Some("int32".to_string()));
        assert!(!analyzer.params[0].nullable);
    }

    #[test]
    fn test_register_param_dedup_fills_missing() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        analyzer.register_param(1, None, None, false, None);
        assert_eq!(analyzer.params.len(), 1);
        assert_eq!(analyzer.params[0].name, None);
        assert_eq!(analyzer.params[0].neutral_type, None);

        analyzer.register_param(1, Some("id".to_string()), Some("int32".to_string()), false, None);
        assert_eq!(analyzer.params.len(), 1);
        assert_eq!(analyzer.params[0].name, Some("id".to_string()));
        assert_eq!(analyzer.params[0].neutral_type, Some("int32".to_string()));
    }

    #[test]
    fn test_register_param_does_not_overwrite_existing() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        analyzer.register_param(1, Some("id".to_string()), Some("int32".to_string()), false, None);
        analyzer.register_param(1, Some("new_name".to_string()), Some("string".to_string()), true, None);
        assert_eq!(analyzer.params.len(), 1);
        assert_eq!(analyzer.params[0].name, Some("id".to_string()));
        assert_eq!(analyzer.params[0].neutral_type, Some("int32".to_string()));
    }

    #[test]
    fn test_register_param_fills_missing_source_relation() {
        // Same fill-in-the-blank merge as name/neutral_type: a first registration with no
        // relation (e.g. from `collect_param_from_expr`) must not permanently lock the
        // param out of later picking one up from a comparison site.
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        analyzer.register_param(1, Some("id".to_string()), Some("int32".to_string()), false, None);
        assert_eq!(analyzer.params[0].source_relation, None);
        analyzer.register_param(
            1,
            Some("id".to_string()),
            Some("int32".to_string()),
            false,
            Some("users".to_string()),
        );
        assert_eq!(analyzer.params.len(), 1, "still one param, not a second entry");
        assert_eq!(analyzer.params[0].source_relation, Some("users".to_string()));
    }

    #[test]
    fn test_register_param_does_not_overwrite_existing_source_relation() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        analyzer.register_param(
            1,
            Some("id".to_string()),
            Some("int32".to_string()),
            false,
            Some("users".to_string()),
        );
        analyzer.register_param(
            1,
            Some("id".to_string()),
            Some("int32".to_string()),
            false,
            Some("orders".to_string()),
        );
        assert_eq!(analyzer.params[0].source_relation, Some("users".to_string()));
    }

    #[test]
    fn test_register_multiple_params() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        analyzer.register_param(1, Some("name".to_string()), Some("string".to_string()), false, None);
        analyzer.register_param(2, Some("age".to_string()), Some("int32".to_string()), false, None);
        assert_eq!(analyzer.params.len(), 2);
        assert_eq!(analyzer.params[0].position, 1);
        assert_eq!(analyzer.params[1].position, 2);
    }

    #[test]
    fn test_try_bind_param_from_comparison_basic() {
        let catalog = Catalog::from_ddl(&["CREATE TABLE users (id INTEGER NOT NULL, name TEXT NOT NULL);"]).unwrap();
        let mut analyzer = Analyzer {
            catalog: &catalog,
            params: Vec::new(),
            ctes: AHashMap::new(),
            type_errors: Vec::new(),
            positional_param_counter: 0,
            pending_nested: Vec::new(),
            next_nested_id: 0,
            resolved_placeholders: AHashMap::new(),
        };
        let scope = Scope {
            sources: vec![ScopeSource {
                alias: "users".to_string(),
                table_name: "users".to_string(),
                columns: vec![
                    ScopeColumn::new("id", "int32", false),
                    ScopeColumn::new("name", "string", false),
                ],
                nullable_from_join: false,
            }],
        };

        let param_side = placeholder_expr("$1");
        let col_side = Expr::Identifier(Ident::new("id"));
        analyzer.try_bind_param_from_comparison(&param_side, &col_side, &scope, Some(&BinaryOperator::Eq));

        assert_eq!(analyzer.params.len(), 1);
        assert_eq!(analyzer.params[0].position, 1);
        assert_eq!(analyzer.params[0].name, Some("id".to_string()));
        assert_eq!(analyzer.params[0].neutral_type, Some("int32".to_string()));
        assert_eq!(
            analyzer.params[0].source_relation,
            Some("users".to_string()),
            "a direct `col = $N` comparison must carry the column's owning relation, the same \
             way AnalyzedColumn::source_relation does (#189)"
        );
    }

    /// The core of #189's remainder: `WHERE u.id = $1` must resolve `source_relation` to the
    /// real table name (`users`), not the query alias (`u`) -- exactly the distinction
    /// `AnalyzedColumn::source_relation` already makes (99227e8e). Must fail before the fix:
    /// `AnalyzedParam` carried no `source_relation` field at all, so a qualified `users.id`
    /// override on a parameter matched nothing outside `SELECT *`.
    #[test]
    fn test_try_bind_param_from_comparison_uses_real_table_name_not_alias() {
        let catalog = Catalog::from_ddl(&["CREATE TABLE users (id INTEGER NOT NULL);"]).unwrap();
        let mut analyzer = Analyzer {
            catalog: &catalog,
            params: Vec::new(),
            ctes: AHashMap::new(),
            type_errors: Vec::new(),
            positional_param_counter: 0,
            pending_nested: Vec::new(),
            next_nested_id: 0,
            resolved_placeholders: AHashMap::new(),
        };
        let scope = Scope {
            sources: vec![ScopeSource {
                alias: "u".to_string(),
                table_name: "users".to_string(),
                columns: vec![ScopeColumn::new("id", "int32", false)],
                nullable_from_join: false,
            }],
        };

        let param_side = placeholder_expr("$1");
        let col_side = Expr::CompoundIdentifier(vec![Ident::new("u"), Ident::new("id")]);
        analyzer.try_bind_param_from_comparison(&param_side, &col_side, &scope, Some(&BinaryOperator::Eq));

        assert_eq!(
            analyzer.params[0].source_relation,
            Some("users".to_string()),
            "must resolve to the real table name, not the `u` alias"
        );
    }

    /// A param bound through an `IN` list has a column it is tested against, but not a single
    /// bound value compared to it the way `col = $N` is -- per #189's remainder, this must
    /// stay `None` rather than guess at a relation.
    #[test]
    fn test_collect_params_from_where_in_list_has_no_source_relation() {
        let catalog = Catalog::from_ddl(&["CREATE TABLE users (id INTEGER NOT NULL);"]).unwrap();
        let mut analyzer = Analyzer {
            catalog: &catalog,
            params: Vec::new(),
            ctes: AHashMap::new(),
            type_errors: Vec::new(),
            positional_param_counter: 0,
            pending_nested: Vec::new(),
            next_nested_id: 0,
            resolved_placeholders: AHashMap::new(),
        };
        let scope = Scope {
            sources: vec![ScopeSource {
                alias: "users".to_string(),
                table_name: "users".to_string(),
                columns: vec![ScopeColumn::new("id", "int32", false)],
                nullable_from_join: false,
            }],
        };
        let expr = Expr::InList {
            expr: Box::new(Expr::Identifier(Ident::new("id"))),
            list: vec![placeholder_expr("$1")],
            negated: false,
        };
        analyzer.collect_params_from_where(&expr, &scope);
        assert_eq!(analyzer.params.len(), 1);
        assert_eq!(
            analyzer.params[0].source_relation, None,
            "an IN-list binding has no single owning column to target with a qualified override"
        );
    }

    #[test]
    fn test_collect_param_type_from_cast_placeholder() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let expr = placeholder_expr("$1");
        analyzer.collect_param_type_from_cast(&expr, "int32");
        assert_eq!(analyzer.params.len(), 1);
        assert_eq!(analyzer.params[0].neutral_type, Some("int32".to_string()));
    }

    #[test]
    fn test_collect_param_type_from_cast_interval_name() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let expr = placeholder_expr("$1");
        analyzer.collect_param_type_from_cast(&expr, "interval");
        assert_eq!(analyzer.params.len(), 1);
        assert_eq!(analyzer.params[0].name, Some("duration".to_string()));
        assert_eq!(analyzer.params[0].neutral_type, Some("interval".to_string()));
    }

    #[test]
    fn test_collect_param_type_from_cast_non_placeholder_ignored() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let expr = Expr::Identifier(Ident::new("x"));
        analyzer.collect_param_type_from_cast(&expr, "int32");
        assert_eq!(analyzer.params.len(), 0, "non-placeholder should not register a param");
    }

    #[test]
    fn test_resolve_placeholder_position_dollar_n() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        assert_eq!(analyzer.resolve_placeholder_position("$1", Span::empty()), Some(1));
        assert_eq!(analyzer.resolve_placeholder_position("$99", Span::empty()), Some(99));
        assert!(analyzer.type_errors.is_empty());
    }

    #[test]
    fn test_resolve_placeholder_position_question_mark() {
        // Two calls with `Span::empty()` -- the identity synthetic/test
        // nodes carry when they don't come from `Parser::parse_sql` --
        // deliberately bypass the memo and keep auto-incrementing, so this
        // still models two genuinely different occurrences.
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        assert_eq!(analyzer.resolve_placeholder_position("?", Span::empty()), Some(1));
        assert_eq!(analyzer.resolve_placeholder_position("?", Span::empty()), Some(2));
        assert!(analyzer.type_errors.is_empty());
    }

    #[test]
    fn test_resolve_placeholder_position_question_mark_same_span_is_idempotent() {
        // The core of #170: the same source-text `?` occurrence, identified
        // by its span, must resolve to the same position on a second visit
        // instead of minting a new one.
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let span = span_at(10);
        assert_eq!(analyzer.resolve_placeholder_position("?", span), Some(1));
        assert_eq!(
            analyzer.resolve_placeholder_position("?", span),
            Some(1),
            "revisiting the same occurrence must not advance the counter"
        );
    }

    #[test]
    fn test_resolve_placeholder_position_question_mark_distinct_spans_differ() {
        // Two real, distinct occurrences (different spans) must still get
        // different positions -- the memo must not collapse unrelated `?`s.
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        assert_eq!(analyzer.resolve_placeholder_position("?", span_at(1)), Some(1));
        assert_eq!(analyzer.resolve_placeholder_position("?", span_at(2)), Some(2));
        assert_eq!(
            analyzer.resolve_placeholder_position("?", span_at(1)),
            Some(1),
            "revisiting the first occurrence must still return its original position"
        );
    }

    #[test]
    fn test_resolve_placeholder_position_named_param_errors() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let result = analyzer.resolve_placeholder_position(":bucket", Span::empty());
        assert_eq!(result, None, "named placeholder must return None");
        assert_eq!(analyzer.type_errors.len(), 1, "one error must be recorded");
        assert!(
            analyzer.type_errors[0].contains(":bucket"),
            "error must name the offending token"
        );
        assert!(
            analyzer.type_errors[0].contains("not supported"),
            "error must explain the problem"
        );
    }

    #[test]
    fn test_collect_params_from_where_between_placeholder_visited_twice_counts_once() {
        // `analyze_select` calls `collect_params_from_where` and then
        // `infer_expr_type` over the same projection expression, so a
        // `Between` node's placeholder is reached by both. Simulating that
        // here by calling `collect_params_from_where` and then
        // `infer_expr_type` on the same `Expr::Between` node reproduces
        // #170: the second visit must resolve to the same position instead
        // of registering a second parameter.
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let scope = Scope { sources: Vec::new() };
        let span = span_at(20);
        let expr = Expr::Between {
            expr: Box::new(Expr::Identifier(Ident::new("age"))),
            negated: false,
            low: Box::new(placeholder_expr_at("?", span)),
            high: Box::new(placeholder_expr_at("$2", Span::empty())),
        };
        analyzer.collect_params_from_where(&expr, &scope);
        let _ = analyzer.infer_expr_type(&expr, &scope);
        assert_eq!(
            analyzer.params.len(),
            2,
            "a BETWEEN with one `?` and one `$2` must report exactly two parameters, not more"
        );
        assert!(
            analyzer.params.iter().any(|p| p.position == 1),
            "the `?` must have resolved to position 1"
        );
        assert!(
            analyzer.params.iter().any(|p| p.position == 2),
            "the `$2` must still resolve to its explicit position"
        );
    }

    #[test]
    fn test_collect_param_from_expr_placeholder() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let expr = placeholder_expr("$2");
        analyzer.collect_param_from_expr(&expr, "email", "string");
        assert_eq!(analyzer.params.len(), 1);
        assert_eq!(analyzer.params[0].position, 2);
        assert_eq!(analyzer.params[0].name, Some("email".to_string()));
        assert_eq!(analyzer.params[0].neutral_type, Some("string".to_string()));
    }

    // Regression tests for #171: a parameter dropped from the generated
    // function signature while the placeholder stays in the emitted SQL.

    #[test]
    fn test_collect_params_from_where_is_null_descends_into_operand() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let scope = Scope { sources: Vec::new() };
        // `$1 IS NULL` is a degenerate stand-in for the realistic shape
        // (`coalesce(x, $1) IS NULL`) that exercises the same operand
        // descent without needing to build a full function-call AST node.
        let expr = Expr::IsNull(Box::new(placeholder_expr("$1")));
        analyzer.collect_params_from_where(&expr, &scope);
        assert_eq!(
            analyzer.params.len(),
            1,
            "a placeholder inside an IS NULL operand must not be dropped"
        );
        assert_eq!(analyzer.params[0].position, 1);
    }

    #[test]
    fn test_collect_params_from_where_is_not_null_descends_into_operand() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let scope = Scope { sources: Vec::new() };
        let expr = Expr::IsNotNull(Box::new(placeholder_expr("$2")));
        analyzer.collect_params_from_where(&expr, &scope);
        assert_eq!(
            analyzer.params.len(),
            1,
            "a placeholder inside an IS NOT NULL operand must not be dropped"
        );
        assert_eq!(analyzer.params[0].position, 2);
    }

    #[test]
    fn test_collect_params_from_where_like_literal_pattern_still_registers() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let scope = Scope { sources: Vec::new() };
        let expr = Expr::Like {
            negated: false,
            any: false,
            expr: Box::new(Expr::Identifier(Ident::new("name"))),
            pattern: Box::new(placeholder_expr("$1")),
            escape_char: None,
        };
        analyzer.collect_params_from_where(&expr, &scope);
        assert_eq!(
            analyzer.params.len(),
            1,
            "the pre-existing literal-pattern case must keep working"
        );
        assert_eq!(analyzer.params[0].position, 1);
        assert_eq!(analyzer.params[0].name, Some("name".to_string()));
        assert_eq!(analyzer.params[0].neutral_type, Some("string".to_string()));
    }

    #[test]
    fn test_collect_params_from_where_like_non_literal_pattern_registers() {
        let catalog = empty_catalog();
        let mut analyzer = make_analyzer(&catalog);
        let scope = Scope { sources: Vec::new() };
        // `'%' || $1` is not an `Expr::Value`, so the old literal-only
        // check silently registered nothing for it (#171).
        let expr = Expr::Like {
            negated: false,
            any: false,
            expr: Box::new(Expr::Identifier(Ident::new("name"))),
            pattern: Box::new(Expr::BinaryOp {
                left: Box::new(Expr::Value(ValueWithSpan {
                    value: Value::SingleQuotedString("%".to_string()),
                    span: Span::empty(),
                })),
                op: BinaryOperator::StringConcat,
                right: Box::new(placeholder_expr("$1")),
            }),
            escape_char: None,
        };
        analyzer.collect_params_from_where(&expr, &scope);
        assert_eq!(
            analyzer.params.len(),
            1,
            "a placeholder inside a non-literal LIKE pattern must not be dropped"
        );
        assert_eq!(analyzer.params[0].position, 1);
        assert_eq!(analyzer.params[0].name, Some("name".to_string()));
        assert_eq!(analyzer.params[0].neutral_type, Some("string".to_string()));
    }
}