Skip to main content

sqlglot_rust/optimizer/
annotate_types.rs

1//! Type annotation pass for SQL expressions.
2//!
3//! Infers and propagates SQL data types across AST nodes using schema metadata.
4//! Inspired by Python sqlglot's `annotate_types` optimizer pass.
5//!
6//! # Overview
7//!
8//! The pass walks the AST bottom-up, resolving types for:
9//! - **Literals**: `42` → `Int`, `'hello'` → `Varchar`, `TRUE` → `Boolean`
10//! - **Column references**: looked up from the provided [`Schema`]
11//! - **Binary operators**: result type from operand coercion (e.g. `INT + FLOAT → FLOAT`)
12//! - **CAST / TRY_CAST**: the target data type
13//! - **Functions**: return type based on function signature and argument types
14//! - **CASE**: common type across all THEN / ELSE branches
15//! - **Aggregates**: `COUNT → BigInt`, `SUM` depends on input, etc.
16//! - **Subqueries**: type of the single output column
17//!
18//! # Example
19//!
20//! ```rust
21//! use sqlglot_rust::optimizer::annotate_types::annotate_types;
22//! use sqlglot_rust::schema::{MappingSchema, Schema};
23//! use sqlglot_rust::ast::DataType;
24//! use sqlglot_rust::{parse, Dialect};
25//!
26//! let mut schema = MappingSchema::new(Dialect::Ansi);
27//! schema.add_table(&["t"], vec![
28//!     ("id".to_string(), DataType::Int),
29//!     ("name".to_string(), DataType::Varchar(Some(255))),
30//! ]).unwrap();
31//!
32//! let stmt = parse("SELECT id, name FROM t WHERE id > 1", Dialect::Ansi).unwrap();
33//! let annotations = annotate_types(&stmt, &schema);
34//! // annotations now contains inferred types for every expression node
35//! ```
36
37use std::collections::HashMap;
38
39use crate::ast::*;
40use crate::schema::Schema;
41
42// ═══════════════════════════════════════════════════════════════════════
43// TypeAnnotations — the result of type inference
44// ═══════════════════════════════════════════════════════════════════════
45
46/// Stores inferred [`DataType`] annotations for expression nodes in an AST.
47///
48/// Annotations are keyed by raw pointer identity, so this structure is valid
49/// only as long as the underlying AST is not moved, cloned, or dropped.
50/// Intended for single-pass analysis over a borrowed AST.
51pub struct TypeAnnotations {
52    types: HashMap<*const Expr, DataType>,
53}
54
55// Raw pointers are not Send/Sync by default, but our usage is safe because
56// the pointers are derived from shared references with a known lifetime.
57unsafe impl Send for TypeAnnotations {}
58unsafe impl Sync for TypeAnnotations {}
59
60impl TypeAnnotations {
61    fn new() -> Self {
62        Self {
63            types: HashMap::new(),
64        }
65    }
66
67    fn set(&mut self, expr: &Expr, dt: DataType) {
68        self.types.insert(expr as *const Expr, dt);
69    }
70
71    /// Retrieve the inferred type of an expression, if annotated.
72    #[must_use]
73    pub fn get_type(&self, expr: &Expr) -> Option<&DataType> {
74        self.types.get(&(expr as *const Expr))
75    }
76
77    /// Number of annotated nodes.
78    #[must_use]
79    pub fn len(&self) -> usize {
80        self.types.len()
81    }
82
83    /// Returns `true` if no annotations were recorded.
84    #[must_use]
85    pub fn is_empty(&self) -> bool {
86        self.types.is_empty()
87    }
88}
89
90// ═══════════════════════════════════════════════════════════════════════
91// Public entry point
92// ═══════════════════════════════════════════════════════════════════════
93
94/// Annotate all expression nodes in a statement with inferred SQL types.
95///
96/// Walks the AST bottom-up, resolving types from literals, schema column
97/// lookups, operator/function signatures, and type coercion rules.
98///
99/// The returned [`TypeAnnotations`] is valid only while the borrowed `stmt`
100/// is alive and unmodified.
101#[must_use]
102pub fn annotate_types<S: Schema>(stmt: &Statement, schema: &S) -> TypeAnnotations {
103    let mut ann = TypeAnnotations::new();
104    let mut ctx = AnnotationContext::new(schema);
105    annotate_statement(stmt, &mut ctx, &mut ann);
106    ann
107}
108
109// ═══════════════════════════════════════════════════════════════════════
110// Internal context
111// ═══════════════════════════════════════════════════════════════════════
112
113/// Carries schema reference and table alias mappings through the walk.
114struct AnnotationContext<'s, S: Schema> {
115    schema: &'s S,
116    /// Maps table alias or name → table path for column type lookups.
117    table_aliases: HashMap<String, Vec<String>>,
118}
119
120impl<'s, S: Schema> AnnotationContext<'s, S> {
121    fn new(schema: &'s S) -> Self {
122        Self {
123            schema,
124            table_aliases: HashMap::new(),
125        }
126    }
127
128    /// Register a table (by ref) so that columns can be looked up by alias.
129    fn register_table(&mut self, table_ref: &TableRef) {
130        let path = vec![table_ref.name.clone()];
131        let alias = table_ref
132            .alias
133            .as_deref()
134            .unwrap_or(&table_ref.name)
135            .to_string();
136        self.table_aliases.insert(alias, path);
137    }
138
139    /// Look up the type of a column, resolving through table aliases.
140    fn resolve_column_type(&self, table: Option<&str>, column: &str) -> Option<DataType> {
141        if let Some(tbl) = table {
142            // Qualified column — look up via alias map
143            if let Some(path) = self.table_aliases.get(tbl) {
144                let path_refs: Vec<&str> = path.iter().map(String::as_str).collect();
145                return self.schema.get_column_type(&path_refs, column).ok();
146            }
147            // Try the table name directly
148            return self.schema.get_column_type(&[tbl], column).ok();
149        }
150        // Unqualified — search all registered tables
151        for path in self.table_aliases.values() {
152            let path_refs: Vec<&str> = path.iter().map(String::as_str).collect();
153            if let Ok(dt) = self.schema.get_column_type(&path_refs, column) {
154                return Some(dt);
155            }
156        }
157        None
158    }
159}
160
161// ═══════════════════════════════════════════════════════════════════════
162// Statement-level annotation
163// ═══════════════════════════════════════════════════════════════════════
164
165fn annotate_statement<S: Schema>(
166    stmt: &Statement,
167    ctx: &mut AnnotationContext<S>,
168    ann: &mut TypeAnnotations,
169) {
170    match stmt {
171        Statement::Select(sel) => annotate_select(sel, ctx, ann),
172        Statement::SetOperation(set_op) => {
173            annotate_statement(&set_op.left, ctx, ann);
174            annotate_statement(&set_op.right, ctx, ann);
175        }
176        Statement::Insert(ins) => {
177            if let InsertSource::Query(q) = &ins.source {
178                annotate_statement(q, ctx, ann);
179            }
180            for row in match &ins.source {
181                InsertSource::Values(rows) => rows.as_slice(),
182                _ => &[],
183            } {
184                for expr in row {
185                    annotate_expr(expr, ctx, ann);
186                }
187            }
188        }
189        Statement::Update(upd) => {
190            for (_, expr) in &upd.assignments {
191                annotate_expr(expr, ctx, ann);
192            }
193            if let Some(wh) = &upd.where_clause {
194                annotate_expr(wh, ctx, ann);
195            }
196        }
197        Statement::Delete(del) => {
198            if let Some(wh) = &del.where_clause {
199                annotate_expr(wh, ctx, ann);
200            }
201        }
202        Statement::Expression(expr) => {
203            annotate_expr(expr, ctx, ann);
204        }
205        Statement::Explain(expl) => {
206            annotate_statement(&expl.statement, ctx, ann);
207        }
208        // DDL / transaction / other statements — no expression types to annotate
209        _ => {}
210    }
211}
212
213fn annotate_select<S: Schema>(
214    sel: &SelectStatement,
215    ctx: &mut AnnotationContext<S>,
216    ann: &mut TypeAnnotations,
217) {
218    // 1. Register CTEs
219    for cte in &sel.ctes {
220        annotate_statement(&cte.query, ctx, ann);
221    }
222
223    // 2. Register FROM sources
224    if let Some(from) = &sel.from {
225        register_table_source(&from.source, ctx);
226    }
227    for join in &sel.joins {
228        register_table_source(&join.table, ctx);
229    }
230
231    // 3. Annotate WHERE clause
232    if let Some(wh) = &sel.where_clause {
233        annotate_expr(wh, ctx, ann);
234    }
235
236    // 4. Annotate SELECT columns
237    for item in &sel.columns {
238        if let SelectItem::Expr { expr, .. } = item {
239            annotate_expr(expr, ctx, ann);
240        }
241    }
242
243    // 5. Annotate GROUP BY
244    for expr in &sel.group_by {
245        annotate_expr(expr, ctx, ann);
246    }
247
248    // 6. Annotate HAVING
249    if let Some(having) = &sel.having {
250        annotate_expr(having, ctx, ann);
251    }
252
253    // 7. Annotate ORDER BY
254    for ob in &sel.order_by {
255        annotate_expr(&ob.expr, ctx, ann);
256    }
257
258    // 8. Annotate LIMIT / OFFSET
259    if let Some(limit) = &sel.limit {
260        annotate_expr(limit, ctx, ann);
261    }
262    if let Some(offset) = &sel.offset {
263        annotate_expr(offset, ctx, ann);
264    }
265    if let Some(fetch) = &sel.fetch_first {
266        annotate_expr(fetch, ctx, ann);
267    }
268
269    // 9. Annotate QUALIFY
270    if let Some(qualify) = &sel.qualify {
271        annotate_expr(qualify, ctx, ann);
272    }
273
274    // 10. Annotate JOIN ON conditions
275    for join in &sel.joins {
276        if let Some(on) = &join.on {
277            annotate_expr(on, ctx, ann);
278        }
279    }
280}
281
282fn register_table_source<S: Schema>(source: &TableSource, ctx: &mut AnnotationContext<S>) {
283    match source {
284        TableSource::Table(tref) => ctx.register_table(tref),
285        TableSource::Subquery { alias, .. } => {
286            // Subqueries as sources don't have schema entries to register.
287            // Their output column types would come from recursive annotation.
288            let _ = alias;
289        }
290        TableSource::TableFunction { alias, .. } => {
291            let _ = alias;
292        }
293        TableSource::Lateral { source } => register_table_source(source, ctx),
294        TableSource::Pivot { source, .. } | TableSource::Unpivot { source, .. } => {
295            register_table_source(source, ctx);
296        }
297        TableSource::Unnest { .. } => {}
298    }
299}
300
301// ═══════════════════════════════════════════════════════════════════════
302// Expression-level annotation (bottom-up)
303// ═══════════════════════════════════════════════════════════════════════
304
305fn annotate_expr<S: Schema>(expr: &Expr, ctx: &AnnotationContext<S>, ann: &mut TypeAnnotations) {
306    // First annotate children, then determine this node's type.
307    annotate_children(expr, ctx, ann);
308
309    let dt = infer_type(expr, ctx, ann);
310    if let Some(t) = dt {
311        ann.set(expr, t);
312    }
313}
314
315/// Recursively annotate child expressions before the parent.
316fn annotate_children<S: Schema>(
317    expr: &Expr,
318    ctx: &AnnotationContext<S>,
319    ann: &mut TypeAnnotations,
320) {
321    match expr {
322        Expr::BinaryOp { left, right, .. } => {
323            annotate_expr(left, ctx, ann);
324            annotate_expr(right, ctx, ann);
325        }
326        Expr::UnaryOp { expr: inner, .. } => annotate_expr(inner, ctx, ann),
327        Expr::Function { args, filter, .. } => {
328            for arg in args {
329                annotate_expr(arg, ctx, ann);
330            }
331            if let Some(f) = filter {
332                annotate_expr(f, ctx, ann);
333            }
334        }
335        Expr::Between {
336            expr: e, low, high, ..
337        } => {
338            annotate_expr(e, ctx, ann);
339            annotate_expr(low, ctx, ann);
340            annotate_expr(high, ctx, ann);
341        }
342        Expr::InList { expr: e, list, .. } => {
343            annotate_expr(e, ctx, ann);
344            for item in list {
345                annotate_expr(item, ctx, ann);
346            }
347        }
348        Expr::InSubquery {
349            expr: e, subquery, ..
350        } => {
351            annotate_expr(e, ctx, ann);
352            let mut sub_ctx = AnnotationContext::new(ctx.schema);
353            annotate_statement(subquery, &mut sub_ctx, ann);
354        }
355        Expr::IsNull { expr: e, .. } | Expr::IsBool { expr: e, .. } => {
356            annotate_expr(e, ctx, ann);
357        }
358        Expr::Like {
359            expr: e,
360            pattern,
361            escape,
362            ..
363        }
364        | Expr::ILike {
365            expr: e,
366            pattern,
367            escape,
368            ..
369        } => {
370            annotate_expr(e, ctx, ann);
371            annotate_expr(pattern, ctx, ann);
372            if let Some(esc) = escape {
373                annotate_expr(esc, ctx, ann);
374            }
375        }
376        Expr::Case {
377            operand,
378            when_clauses,
379            else_clause,
380        } => {
381            if let Some(op) = operand {
382                annotate_expr(op, ctx, ann);
383            }
384            for (cond, result) in when_clauses {
385                annotate_expr(cond, ctx, ann);
386                annotate_expr(result, ctx, ann);
387            }
388            if let Some(el) = else_clause {
389                annotate_expr(el, ctx, ann);
390            }
391        }
392        Expr::Nested(inner) => annotate_expr(inner, ctx, ann),
393        Expr::Cast { expr: e, .. } | Expr::TryCast { expr: e, .. } => {
394            annotate_expr(e, ctx, ann);
395        }
396        Expr::Extract { expr: e, .. } => annotate_expr(e, ctx, ann),
397        Expr::Interval { value, .. } => annotate_expr(value, ctx, ann),
398        Expr::ArrayLiteral(items) | Expr::Tuple(items) | Expr::Coalesce(items) => {
399            for item in items {
400                annotate_expr(item, ctx, ann);
401            }
402        }
403        Expr::If {
404            condition,
405            true_val,
406            false_val,
407        } => {
408            annotate_expr(condition, ctx, ann);
409            annotate_expr(true_val, ctx, ann);
410            if let Some(fv) = false_val {
411                annotate_expr(fv, ctx, ann);
412            }
413        }
414        Expr::NullIf { expr: e, r#else } => {
415            annotate_expr(e, ctx, ann);
416            annotate_expr(r#else, ctx, ann);
417        }
418        Expr::Collate { expr: e, .. } => annotate_expr(e, ctx, ann),
419        Expr::Alias { expr: e, .. } => annotate_expr(e, ctx, ann),
420        Expr::ArrayIndex { expr: e, index } => {
421            annotate_expr(e, ctx, ann);
422            annotate_expr(index, ctx, ann);
423        }
424        Expr::JsonAccess { expr: e, path, .. } => {
425            annotate_expr(e, ctx, ann);
426            annotate_expr(path, ctx, ann);
427        }
428        Expr::Lambda { body, .. } => annotate_expr(body, ctx, ann),
429        Expr::AnyOp { expr: e, right, .. } | Expr::AllOp { expr: e, right, .. } => {
430            annotate_expr(e, ctx, ann);
431            annotate_expr(right, ctx, ann);
432        }
433        Expr::Subquery(sub) => {
434            let mut sub_ctx = AnnotationContext::new(ctx.schema);
435            annotate_statement(sub, &mut sub_ctx, ann);
436        }
437        Expr::Exists { subquery, .. } => {
438            let mut sub_ctx = AnnotationContext::new(ctx.schema);
439            annotate_statement(subquery, &mut sub_ctx, ann);
440        }
441        Expr::TypedFunction { func, filter, .. } => {
442            annotate_typed_function_children(func, ctx, ann);
443            if let Some(f) = filter {
444                annotate_expr(f, ctx, ann);
445            }
446        }
447        Expr::Cube { exprs } | Expr::Rollup { exprs } | Expr::GroupingSets { sets: exprs } => {
448            for item in exprs {
449                annotate_expr(item, ctx, ann);
450            }
451        }
452        // Leaf nodes — no children to annotate
453        Expr::Column { .. }
454        | Expr::Number(_)
455        | Expr::StringLiteral(_)
456        | Expr::NationalStringLiteral(_)
457        | Expr::Boolean(_)
458        | Expr::Null
459        | Expr::Wildcard
460        | Expr::Star
461        | Expr::Parameter(_)
462        | Expr::TypeExpr(_)
463        | Expr::QualifiedWildcard { .. }
464        | Expr::Default
465        | Expr::Commented { .. } => {}
466    }
467}
468
469/// Annotate children of a TypedFunction.
470fn annotate_typed_function_children<S: Schema>(
471    func: &TypedFunction,
472    ctx: &AnnotationContext<S>,
473    ann: &mut TypeAnnotations,
474) {
475    // Use walk_children to visit all child expressions and annotate each
476    func.walk_children(&mut |child| {
477        annotate_expr(child, ctx, ann);
478        true
479    });
480}
481
482// ═══════════════════════════════════════════════════════════════════════
483// Type inference for a single expression node
484// ═══════════════════════════════════════════════════════════════════════
485
486fn infer_type<S: Schema>(
487    expr: &Expr,
488    ctx: &AnnotationContext<S>,
489    ann: &TypeAnnotations,
490) -> Option<DataType> {
491    match expr {
492        // ── Literals ───────────────────────────────────────────────────
493        Expr::Number(s) => Some(infer_number_type(s)),
494        Expr::StringLiteral(_) | Expr::NationalStringLiteral(_) => Some(DataType::Varchar(None)),
495        Expr::Boolean(_) => Some(DataType::Boolean),
496        Expr::Null => Some(DataType::Null),
497
498        // ── Column reference ──────────────────────────────────────────
499        Expr::Column { table, name, .. } => ctx.resolve_column_type(table.as_deref(), name),
500
501        // ── Binary operators ──────────────────────────────────────────
502        Expr::BinaryOp { left, op, right } => {
503            infer_binary_op_type(op, ann.get_type(left), ann.get_type(right))
504        }
505
506        // ── Unary operators ───────────────────────────────────────────
507        Expr::UnaryOp { op, expr: inner } => match op {
508            UnaryOperator::Not => Some(DataType::Boolean),
509            UnaryOperator::Minus | UnaryOperator::Plus => ann.get_type(inner).cloned(),
510            UnaryOperator::BitwiseNot => ann.get_type(inner).cloned(),
511        },
512
513        // ── CAST / TRY_CAST ──────────────────────────────────────────
514        Expr::Cast { data_type, .. } | Expr::TryCast { data_type, .. } => Some(data_type.clone()),
515
516        // ── CASE expression ──────────────────────────────────────────
517        Expr::Case {
518            when_clauses,
519            else_clause,
520            ..
521        } => {
522            let mut result_types: Vec<&DataType> = Vec::new();
523            for (_, result) in when_clauses {
524                if let Some(t) = ann.get_type(result) {
525                    result_types.push(t);
526                }
527            }
528            if let Some(el) = else_clause {
529                if let Some(t) = ann.get_type(el.as_ref()) {
530                    result_types.push(t);
531                }
532            }
533            common_type(&result_types)
534        }
535
536        // ── IF expression ────────────────────────────────────────────
537        Expr::If {
538            true_val,
539            false_val,
540            ..
541        } => {
542            let mut types = Vec::new();
543            if let Some(t) = ann.get_type(true_val) {
544                types.push(t);
545            }
546            if let Some(fv) = false_val {
547                if let Some(t) = ann.get_type(fv.as_ref()) {
548                    types.push(t);
549                }
550            }
551            common_type(&types)
552        }
553
554        // ── COALESCE ─────────────────────────────────────────────────
555        Expr::Coalesce(items) => {
556            let types: Vec<&DataType> = items.iter().filter_map(|e| ann.get_type(e)).collect();
557            common_type(&types)
558        }
559
560        // ── NULLIF ───────────────────────────────────────────────────
561        Expr::NullIf { expr: e, .. } => ann.get_type(e.as_ref()).cloned(),
562
563        // ── Generic function ─────────────────────────────────────────
564        Expr::Function { name, args, .. } => infer_generic_function_type(name, args, ctx, ann),
565
566        // ── Typed functions ──────────────────────────────────────────
567        Expr::TypedFunction { func, .. } => infer_typed_function_type(func, ann),
568
569        // ── Subquery (scalar) ────────────────────────────────────────
570        Expr::Subquery(sub) => infer_subquery_type(sub, ann),
571
572        // ── EXISTS → Boolean ─────────────────────────────────────────
573        Expr::Exists { .. } => Some(DataType::Boolean),
574
575        // ── Boolean predicates ───────────────────────────────────────
576        Expr::Between { .. }
577        | Expr::InList { .. }
578        | Expr::InSubquery { .. }
579        | Expr::IsNull { .. }
580        | Expr::IsBool { .. }
581        | Expr::Like { .. }
582        | Expr::ILike { .. }
583        | Expr::AnyOp { .. }
584        | Expr::AllOp { .. } => Some(DataType::Boolean),
585
586        // ── EXTRACT → numeric ────────────────────────────────────────
587        Expr::Extract { .. } => Some(DataType::Int),
588
589        // ── INTERVAL → Interval ──────────────────────────────────────
590        Expr::Interval { .. } => Some(DataType::Interval),
591
592        // ── Array literal ────────────────────────────────────────────
593        Expr::ArrayLiteral(items) => {
594            let elem_types: Vec<&DataType> = items.iter().filter_map(|e| ann.get_type(e)).collect();
595            let elem = common_type(&elem_types);
596            Some(DataType::Array(elem.map(Box::new)))
597        }
598
599        // ── Tuple ────────────────────────────────────────────────────
600        Expr::Tuple(items) => {
601            let types: Vec<DataType> = items
602                .iter()
603                .map(|e| ann.get_type(e).cloned().unwrap_or(DataType::Null))
604                .collect();
605            Some(DataType::Tuple(types))
606        }
607
608        // ── Array index → element type ───────────────────────────────
609        Expr::ArrayIndex { expr: e, .. } => match ann.get_type(e.as_ref()) {
610            Some(DataType::Array(Some(elem))) => Some(elem.as_ref().clone()),
611            _ => None,
612        },
613
614        // ── JSON access ──────────────────────────────────────────────
615        Expr::JsonAccess { as_text, .. } => {
616            if *as_text {
617                Some(DataType::Text)
618            } else {
619                Some(DataType::Json)
620            }
621        }
622
623        // ── Nested / Alias — pass through ────────────────────────────
624        Expr::Nested(inner) => ann.get_type(inner.as_ref()).cloned(),
625        Expr::Alias { expr: e, .. } => ann.get_type(e.as_ref()).cloned(),
626
627        // ── Collate → Varchar ────────────────────────────────────────
628        Expr::Collate { .. } => Some(DataType::Varchar(None)),
629
630        // ── TypeExpr ─────────────────────────────────────────────────
631        Expr::TypeExpr(dt) => Some(dt.clone()),
632
633        // ── Others — no type ─────────────────────────────────────────
634        Expr::Wildcard
635        | Expr::Star
636        | Expr::QualifiedWildcard { .. }
637        | Expr::Parameter(_)
638        | Expr::Lambda { .. }
639        | Expr::Default
640        | Expr::Cube { .. }
641        | Expr::Rollup { .. }
642        | Expr::GroupingSets { .. }
643        | Expr::Commented { .. } => None,
644    }
645}
646
647// ═══════════════════════════════════════════════════════════════════════
648// Number type inference
649// ═══════════════════════════════════════════════════════════════════════
650
651fn infer_number_type(s: &str) -> DataType {
652    if s.contains('.') || s.contains('e') || s.contains('E') {
653        DataType::Double
654    } else if let Ok(v) = s.parse::<i64>() {
655        if v >= i32::MIN as i64 && v <= i32::MAX as i64 {
656            DataType::Int
657        } else {
658            DataType::BigInt
659        }
660    } else {
661        // Very large numbers or special formats
662        DataType::BigInt
663    }
664}
665
666// ═══════════════════════════════════════════════════════════════════════
667// Binary operator type inference
668// ═══════════════════════════════════════════════════════════════════════
669
670fn infer_binary_op_type(
671    op: &BinaryOperator,
672    left: Option<&DataType>,
673    right: Option<&DataType>,
674) -> Option<DataType> {
675    use BinaryOperator::*;
676    match op {
677        // Comparison operators → Boolean
678        Eq | Neq | Lt | Gt | LtEq | GtEq => Some(DataType::Boolean),
679
680        // Logical operators → Boolean
681        And | Or | Xor => Some(DataType::Boolean),
682
683        // String concatenation → Varchar
684        Concat => Some(DataType::Varchar(None)),
685
686        // Arithmetic → coerce operand types
687        Plus | Minus | Multiply | Divide | Modulo => match (left, right) {
688            (Some(l), Some(r)) => Some(coerce_numeric(l, r)),
689            (Some(l), None) => Some(l.clone()),
690            (None, Some(r)) => Some(r.clone()),
691            (None, None) => None,
692        },
693
694        // Bitwise → integer type
695        BitwiseAnd | BitwiseOr | BitwiseXor | ShiftLeft | ShiftRight => match (left, right) {
696            (Some(l), Some(r)) => Some(coerce_numeric(l, r)),
697            (Some(l), None) => Some(l.clone()),
698            (None, Some(r)) => Some(r.clone()),
699            (None, None) => Some(DataType::Int),
700        },
701
702        // JSON operators
703        Arrow => Some(DataType::Json),
704        DoubleArrow => Some(DataType::Text),
705    }
706}
707
708// ═══════════════════════════════════════════════════════════════════════
709// Generic (untyped) function return type inference
710// ═══════════════════════════════════════════════════════════════════════
711
712fn infer_generic_function_type<S: Schema>(
713    name: &str,
714    args: &[Expr],
715    ctx: &AnnotationContext<S>,
716    ann: &TypeAnnotations,
717) -> Option<DataType> {
718    let upper = name.to_uppercase();
719    match upper.as_str() {
720        // Aggregate functions
721        "COUNT" | "COUNT_BIG" => Some(DataType::BigInt),
722        "SUM" => args
723            .first()
724            .and_then(|a| ann.get_type(a))
725            .map(|t| coerce_sum_type(t)),
726        "AVG" => Some(DataType::Double),
727        "MIN" | "MAX" => args.first().and_then(|a| ann.get_type(a)).cloned(),
728        "VARIANCE" | "VAR_SAMP" | "VAR_POP" | "STDDEV" | "STDDEV_SAMP" | "STDDEV_POP" => {
729            Some(DataType::Double)
730        }
731        "APPROX_COUNT_DISTINCT" | "APPROX_DISTINCT" => Some(DataType::BigInt),
732
733        // String functions
734        "CONCAT" | "UPPER" | "LOWER" | "TRIM" | "LTRIM" | "RTRIM" | "LPAD" | "RPAD" | "REPLACE"
735        | "REVERSE" | "SUBSTRING" | "SUBSTR" | "LEFT" | "RIGHT" | "INITCAP" | "REPEAT"
736        | "TRANSLATE" | "FORMAT" | "CONCAT_WS" | "SPACE" | "REPLICATE" => {
737            Some(DataType::Varchar(None))
738        }
739        "LENGTH" | "LEN" | "CHAR_LENGTH" | "CHARACTER_LENGTH" | "OCTET_LENGTH" | "BIT_LENGTH" => {
740            Some(DataType::Int)
741        }
742        "POSITION" | "STRPOS" | "LOCATE" | "INSTR" | "CHARINDEX" => Some(DataType::Int),
743        "ASCII" => Some(DataType::Int),
744        "CHR" | "CHAR" => Some(DataType::Varchar(Some(1))),
745
746        // Math functions
747        "ABS" | "CEIL" | "CEILING" | "FLOOR" => args.first().and_then(|a| ann.get_type(a)).cloned(),
748        "ROUND" | "TRUNCATE" | "TRUNC" => args.first().and_then(|a| ann.get_type(a)).cloned(),
749        "SQRT" | "LN" | "LOG" | "LOG2" | "LOG10" | "EXP" | "POWER" | "POW" | "ACOS" | "ASIN"
750        | "ATAN" | "ATAN2" | "COS" | "SIN" | "TAN" | "COT" | "DEGREES" | "RADIANS" | "PI"
751        | "SIGN" => Some(DataType::Double),
752        "MOD" => {
753            match (
754                args.first().and_then(|a| ann.get_type(a)),
755                args.get(1).and_then(|a| ann.get_type(a)),
756            ) {
757                (Some(l), Some(r)) => Some(coerce_numeric(l, r)),
758                (Some(l), _) => Some(l.clone()),
759                (_, Some(r)) => Some(r.clone()),
760                _ => Some(DataType::Int),
761            }
762        }
763        "GREATEST" | "LEAST" => {
764            let types: Vec<&DataType> = args.iter().filter_map(|a| ann.get_type(a)).collect();
765            common_type(&types)
766        }
767        "RANDOM" | "RAND" => Some(DataType::Double),
768
769        // Date/Time functions
770        "CURRENT_DATE" | "CURDATE" | "TODAY" => Some(DataType::Date),
771        "CURRENT_TIMESTAMP" | "NOW" | "GETDATE" | "SYSDATE" | "SYSTIMESTAMP" | "LOCALTIMESTAMP" => {
772            Some(DataType::Timestamp {
773                precision: None,
774                with_tz: false,
775            })
776        }
777        "CURRENT_TIME" | "CURTIME" => Some(DataType::Time { precision: None }),
778        "DATE" | "TO_DATE" | "DATE_TRUNC" | "DATE_ADD" | "DATE_SUB" | "DATEADD" | "DATESUB"
779        | "ADDDATE" | "SUBDATE" => Some(DataType::Date),
780        "TIMESTAMP" | "TO_TIMESTAMP" => Some(DataType::Timestamp {
781            precision: None,
782            with_tz: false,
783        }),
784        "YEAR" | "MONTH" | "DAY" | "DAYOFWEEK" | "DAYOFYEAR" | "HOUR" | "MINUTE" | "SECOND"
785        | "QUARTER" | "WEEK" | "EXTRACT" | "DATEDIFF" | "TIMESTAMPDIFF" | "MONTHS_BETWEEN" => {
786            Some(DataType::Int)
787        }
788
789        // Type conversion
790        "CAST" | "TRY_CAST" | "SAFE_CAST" | "CONVERT" => None, // handled by Expr::Cast
791
792        // Boolean functions
793        "COALESCE" => {
794            let types: Vec<&DataType> = args.iter().filter_map(|a| ann.get_type(a)).collect();
795            common_type(&types)
796        }
797        "NULLIF" => args.first().and_then(|a| ann.get_type(a)).cloned(),
798        "IF" | "IIF" => {
799            // IF(cond, true_val, false_val) — type from true_val
800            args.get(1).and_then(|a| ann.get_type(a)).cloned()
801        }
802        "IFNULL" | "NVL" | "ISNULL" => {
803            let types: Vec<&DataType> = args.iter().filter_map(|a| ann.get_type(a)).collect();
804            common_type(&types)
805        }
806
807        // JSON functions
808        "JSON_EXTRACT" | "JSON_QUERY" | "GET_JSON_OBJECT" => Some(DataType::Json),
809        "JSON_EXTRACT_SCALAR" | "JSON_VALUE" | "JSON_EXTRACT_PATH_TEXT" => {
810            Some(DataType::Varchar(None))
811        }
812        "TO_JSON" | "JSON_OBJECT" | "JSON_ARRAY" | "JSON_BUILD_OBJECT" | "JSON_BUILD_ARRAY" => {
813            Some(DataType::Json)
814        }
815        "PARSE_JSON" | "JSON_PARSE" | "JSON" => Some(DataType::Json),
816
817        // Array functions
818        "ARRAY_AGG" | "COLLECT_LIST" | "COLLECT_SET" => {
819            let elem = args.first().and_then(|a| ann.get_type(a)).cloned();
820            Some(DataType::Array(elem.map(Box::new)))
821        }
822        "ARRAY_LENGTH" | "ARRAY_SIZE" | "CARDINALITY" => Some(DataType::Int),
823        "ARRAY" | "ARRAY_CONSTRUCT" => {
824            let types: Vec<&DataType> = args.iter().filter_map(|a| ann.get_type(a)).collect();
825            let elem = common_type(&types);
826            Some(DataType::Array(elem.map(Box::new)))
827        }
828        "ARRAY_CONTAINS" | "ARRAY_POSITION" => Some(DataType::Boolean),
829
830        // Window ranking
831        "ROW_NUMBER" | "RANK" | "DENSE_RANK" | "NTILE" | "CUME_DIST" | "PERCENT_RANK" => {
832            Some(DataType::BigInt)
833        }
834
835        // Hash / crypto
836        "MD5" | "SHA1" | "SHA" | "SHA2" | "SHA256" | "SHA512" => Some(DataType::Varchar(None)),
837        "HEX" | "TO_HEX" => Some(DataType::Varchar(None)),
838        "UNHEX" | "FROM_HEX" => Some(DataType::Varbinary(None)),
839        "CRC32" | "HASH" => Some(DataType::BigInt),
840
841        // Type checking
842        "TYPEOF" | "TYPE_OF" => Some(DataType::Varchar(None)),
843
844        // UDFs — check schema
845        _ => ctx.schema.get_udf_type(&upper).cloned(),
846    }
847}
848
849// ═══════════════════════════════════════════════════════════════════════
850// TypedFunction return type inference
851// ═══════════════════════════════════════════════════════════════════════
852
853fn infer_typed_function_type(func: &TypedFunction, ann: &TypeAnnotations) -> Option<DataType> {
854    match func {
855        // ── Date/Time → Date or Timestamp ────────────────────────────
856        TypedFunction::DateAdd { .. }
857        | TypedFunction::DateSub { .. }
858        | TypedFunction::DateTrunc { .. }
859        | TypedFunction::TsOrDsToDate { .. } => Some(DataType::Date),
860        TypedFunction::DateDiff { .. } => Some(DataType::Int),
861        TypedFunction::CurrentDate => Some(DataType::Date),
862        TypedFunction::CurrentTimestamp => Some(DataType::Timestamp {
863            precision: None,
864            with_tz: false,
865        }),
866        TypedFunction::StrToTime { .. } => Some(DataType::Timestamp {
867            precision: None,
868            with_tz: false,
869        }),
870        TypedFunction::TimeToStr { .. } => Some(DataType::Varchar(None)),
871        TypedFunction::Year { .. } | TypedFunction::Month { .. } | TypedFunction::Day { .. } => {
872            Some(DataType::Int)
873        }
874
875        // ── String → Varchar ─────────────────────────────────────────
876        TypedFunction::Trim { .. }
877        | TypedFunction::Substring { .. }
878        | TypedFunction::Upper { .. }
879        | TypedFunction::Lower { .. }
880        | TypedFunction::Initcap { .. }
881        | TypedFunction::Replace { .. }
882        | TypedFunction::Reverse { .. }
883        | TypedFunction::Left { .. }
884        | TypedFunction::Right { .. }
885        | TypedFunction::Lpad { .. }
886        | TypedFunction::Rpad { .. }
887        | TypedFunction::ConcatWs { .. } => Some(DataType::Varchar(None)),
888        TypedFunction::Length { .. } => Some(DataType::Int),
889        TypedFunction::RegexpLike { .. } => Some(DataType::Boolean),
890        TypedFunction::RegexpExtract { .. } => Some(DataType::Varchar(None)),
891        TypedFunction::RegexpReplace { .. } => Some(DataType::Varchar(None)),
892        TypedFunction::Split { .. } => {
893            Some(DataType::Array(Some(Box::new(DataType::Varchar(None)))))
894        }
895
896        // ── Aggregates ───────────────────────────────────────────────
897        TypedFunction::Count { .. } => Some(DataType::BigInt),
898        TypedFunction::Sum { expr, .. } => ann.get_type(expr.as_ref()).map(|t| coerce_sum_type(t)),
899        TypedFunction::Avg { .. } => Some(DataType::Double),
900        TypedFunction::Min { expr } | TypedFunction::Max { expr } => {
901            ann.get_type(expr.as_ref()).cloned()
902        }
903        TypedFunction::ArrayAgg { expr, .. } => {
904            let elem = ann.get_type(expr.as_ref()).cloned();
905            Some(DataType::Array(elem.map(Box::new)))
906        }
907        TypedFunction::ApproxDistinct { .. } => Some(DataType::BigInt),
908        TypedFunction::Variance { .. } | TypedFunction::Stddev { .. } => Some(DataType::Double),
909        TypedFunction::GroupConcat { .. } => Some(DataType::Varchar(None)),
910
911        // ── Array ────────────────────────────────────────────────────
912        TypedFunction::ArrayConcat { arrays } => {
913            // Type is the same as input arrays
914            arrays.first().and_then(|a| ann.get_type(a)).cloned()
915        }
916        TypedFunction::ArrayContains { .. } => Some(DataType::Boolean),
917        TypedFunction::ArraySize { .. } => Some(DataType::Int),
918        TypedFunction::Explode { expr } => {
919            // Unwrap array element type
920            match ann.get_type(expr.as_ref()) {
921                Some(DataType::Array(Some(elem))) => Some(elem.as_ref().clone()),
922                _ => None,
923            }
924        }
925        TypedFunction::GenerateSeries { .. } => Some(DataType::Int),
926        TypedFunction::Flatten { expr } => ann.get_type(expr.as_ref()).cloned(),
927
928        // ── JSON ─────────────────────────────────────────────────────
929        TypedFunction::JSONExtract { .. } => Some(DataType::Json),
930        TypedFunction::JSONExtractScalar { .. } => Some(DataType::Varchar(None)),
931        TypedFunction::ParseJSON { .. } | TypedFunction::JSONFormat { .. } => Some(DataType::Json),
932
933        // ── Window ───────────────────────────────────────────────────
934        TypedFunction::RowNumber | TypedFunction::Rank | TypedFunction::DenseRank => {
935            Some(DataType::BigInt)
936        }
937        TypedFunction::NTile { .. } => Some(DataType::BigInt),
938        TypedFunction::Lead { expr, .. }
939        | TypedFunction::Lag { expr, .. }
940        | TypedFunction::FirstValue { expr }
941        | TypedFunction::LastValue { expr } => ann.get_type(expr.as_ref()).cloned(),
942
943        // ── Math ─────────────────────────────────────────────────────
944        TypedFunction::Abs { expr }
945        | TypedFunction::Ceil { expr }
946        | TypedFunction::Floor { expr } => ann.get_type(expr.as_ref()).cloned(),
947        TypedFunction::Round { expr, .. } => ann.get_type(expr.as_ref()).cloned(),
948        TypedFunction::Log { .. }
949        | TypedFunction::Ln { .. }
950        | TypedFunction::Pow { .. }
951        | TypedFunction::Sqrt { .. } => Some(DataType::Double),
952        TypedFunction::Greatest { exprs } | TypedFunction::Least { exprs } => {
953            let types: Vec<&DataType> = exprs.iter().filter_map(|e| ann.get_type(e)).collect();
954            common_type(&types)
955        }
956        TypedFunction::Mod { left, right } => {
957            match (ann.get_type(left.as_ref()), ann.get_type(right.as_ref())) {
958                (Some(l), Some(r)) => Some(coerce_numeric(l, r)),
959                (Some(l), _) => Some(l.clone()),
960                (_, Some(r)) => Some(r.clone()),
961                _ => Some(DataType::Int),
962            }
963        }
964
965        // ── Conversion ───────────────────────────────────────────────
966        TypedFunction::Hex { .. } | TypedFunction::Md5 { .. } | TypedFunction::Sha { .. } => {
967            Some(DataType::Varchar(None))
968        }
969        TypedFunction::Sha2 { .. } => Some(DataType::Varchar(None)),
970        TypedFunction::Unhex { .. } => Some(DataType::Varbinary(None)),
971    }
972}
973
974// ═══════════════════════════════════════════════════════════════════════
975// Subquery type inference
976// ═══════════════════════════════════════════════════════════════════════
977
978fn infer_subquery_type(sub: &Statement, ann: &TypeAnnotations) -> Option<DataType> {
979    // The type of a scalar subquery is the type of its single output column
980    if let Statement::Select(sel) = sub {
981        if let Some(SelectItem::Expr { expr, .. }) = sel.columns.first() {
982            return ann.get_type(expr).cloned();
983        }
984    }
985    None
986}
987
988// ═══════════════════════════════════════════════════════════════════════
989// Type coercion helpers
990// ═══════════════════════════════════════════════════════════════════════
991
992/// Numeric type widening precedence (higher = wider).
993fn numeric_precedence(dt: &DataType) -> u8 {
994    match dt {
995        DataType::Boolean => 1,
996        DataType::TinyInt => 2,
997        DataType::SmallInt => 3,
998        DataType::Int | DataType::Serial => 4,
999        DataType::BigInt | DataType::BigSerial => 5,
1000        DataType::Real | DataType::Float => 6,
1001        DataType::Double => 7,
1002        DataType::Decimal { .. } | DataType::Numeric { .. } => 8,
1003        _ => 0,
1004    }
1005}
1006
1007/// Coerce two numeric types to their common wider type.
1008fn coerce_numeric(left: &DataType, right: &DataType) -> DataType {
1009    let lp = numeric_precedence(left);
1010    let rp = numeric_precedence(right);
1011    if lp == 0 && rp == 0 {
1012        // Neither is numeric — fall back to left
1013        return left.clone();
1014    }
1015    if lp >= rp {
1016        left.clone()
1017    } else {
1018        right.clone()
1019    }
1020}
1021
1022/// Determine the return type of SUM based on input type.
1023fn coerce_sum_type(input: &DataType) -> DataType {
1024    match input {
1025        DataType::TinyInt | DataType::SmallInt | DataType::Int | DataType::BigInt => {
1026            DataType::BigInt
1027        }
1028        DataType::Float | DataType::Real => DataType::Double,
1029        DataType::Double => DataType::Double,
1030        DataType::Decimal { precision, scale } => DataType::Decimal {
1031            precision: *precision,
1032            scale: *scale,
1033        },
1034        DataType::Numeric { precision, scale } => DataType::Numeric {
1035            precision: *precision,
1036            scale: *scale,
1037        },
1038        _ => DataType::BigInt,
1039    }
1040}
1041
1042/// Find the common (widest) type among a set of types.
1043fn common_type(types: &[&DataType]) -> Option<DataType> {
1044    if types.is_empty() {
1045        return None;
1046    }
1047    let mut result = types[0];
1048    for t in &types[1..] {
1049        // Skip NULL — it doesn't contribute to the common type
1050        if **t == DataType::Null {
1051            continue;
1052        }
1053        if *result == DataType::Null {
1054            result = t;
1055            continue;
1056        }
1057        // If both are numeric, pick the wider one
1058        let lp = numeric_precedence(result);
1059        let rp = numeric_precedence(t);
1060        if lp > 0 && rp > 0 {
1061            if rp > lp {
1062                result = t;
1063            }
1064            continue;
1065        }
1066        // If both are string-like, prefer VARCHAR
1067        if is_string_type(result) && is_string_type(t) {
1068            result = if matches!(result, DataType::Text) || matches!(t, DataType::Text) {
1069                if matches!(result, DataType::Text) {
1070                    result
1071                } else {
1072                    t
1073                }
1074            } else {
1075                result // keep first
1076            };
1077            continue;
1078        }
1079        // Otherwise keep the first non-null type
1080    }
1081    Some(result.clone())
1082}
1083
1084fn is_string_type(dt: &DataType) -> bool {
1085    matches!(
1086        dt,
1087        DataType::Varchar(_) | DataType::Char(_) | DataType::Text | DataType::String
1088    )
1089}
1090
1091// ═══════════════════════════════════════════════════════════════════════
1092// Tests
1093// ═══════════════════════════════════════════════════════════════════════
1094
1095#[cfg(test)]
1096mod tests {
1097    use super::*;
1098    use crate::dialects::Dialect;
1099    use crate::parser::Parser;
1100    use crate::schema::{MappingSchema, Schema};
1101
1102    fn setup_schema() -> MappingSchema {
1103        let mut schema = MappingSchema::new(Dialect::Ansi);
1104        schema
1105            .add_table(
1106                &["users"],
1107                vec![
1108                    ("id".to_string(), DataType::Int),
1109                    ("name".to_string(), DataType::Varchar(Some(255))),
1110                    ("age".to_string(), DataType::Int),
1111                    ("salary".to_string(), DataType::Double),
1112                    ("active".to_string(), DataType::Boolean),
1113                    (
1114                        "created_at".to_string(),
1115                        DataType::Timestamp {
1116                            precision: None,
1117                            with_tz: false,
1118                        },
1119                    ),
1120                ],
1121            )
1122            .unwrap();
1123        schema
1124            .add_table(
1125                &["orders"],
1126                vec![
1127                    ("id".to_string(), DataType::Int),
1128                    ("user_id".to_string(), DataType::Int),
1129                    (
1130                        "amount".to_string(),
1131                        DataType::Decimal {
1132                            precision: Some(10),
1133                            scale: Some(2),
1134                        },
1135                    ),
1136                    ("status".to_string(), DataType::Varchar(Some(50))),
1137                ],
1138            )
1139            .unwrap();
1140        schema
1141    }
1142
1143    fn parse_and_annotate(sql: &str, schema: &MappingSchema) -> (Statement, TypeAnnotations) {
1144        let stmt = Parser::new(sql).unwrap().parse_statement().unwrap();
1145        let ann = annotate_types(&stmt, schema);
1146        (stmt, ann)
1147    }
1148
1149    /// Helper: get the type of the first SELECT column
1150    fn first_col_type(stmt: &Statement, ann: &TypeAnnotations) -> Option<DataType> {
1151        if let Statement::Select(sel) = stmt {
1152            if let Some(SelectItem::Expr { expr, .. }) = sel.columns.first() {
1153                return ann.get_type(expr).cloned();
1154            }
1155        }
1156        None
1157    }
1158
1159    // ── Literal type inference ────────────────────────────────────────
1160
1161    #[test]
1162    fn test_number_literal_int() {
1163        let schema = setup_schema();
1164        let (stmt, ann) = parse_and_annotate("SELECT 42", &schema);
1165        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Int));
1166    }
1167
1168    #[test]
1169    fn test_number_literal_big_int() {
1170        let schema = setup_schema();
1171        let (stmt, ann) = parse_and_annotate("SELECT 9999999999", &schema);
1172        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::BigInt));
1173    }
1174
1175    #[test]
1176    fn test_number_literal_double() {
1177        let schema = setup_schema();
1178        let (stmt, ann) = parse_and_annotate("SELECT 3.14", &schema);
1179        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Double));
1180    }
1181
1182    #[test]
1183    fn test_string_literal() {
1184        let schema = setup_schema();
1185        let (stmt, ann) = parse_and_annotate("SELECT 'hello'", &schema);
1186        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Varchar(None)));
1187    }
1188
1189    #[test]
1190    fn test_boolean_literal() {
1191        let schema = setup_schema();
1192        let (stmt, ann) = parse_and_annotate("SELECT TRUE", &schema);
1193        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1194    }
1195
1196    #[test]
1197    fn test_null_literal() {
1198        let schema = setup_schema();
1199        let (stmt, ann) = parse_and_annotate("SELECT NULL", &schema);
1200        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Null));
1201    }
1202
1203    // ── Column reference type lookup ─────────────────────────────────
1204
1205    #[test]
1206    fn test_column_type_from_schema() {
1207        let schema = setup_schema();
1208        let (stmt, ann) = parse_and_annotate("SELECT id FROM users", &schema);
1209        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Int));
1210    }
1211
1212    #[test]
1213    fn test_qualified_column_type() {
1214        let schema = setup_schema();
1215        let (stmt, ann) = parse_and_annotate("SELECT users.name FROM users", &schema);
1216        assert_eq!(
1217            first_col_type(&stmt, &ann),
1218            Some(DataType::Varchar(Some(255)))
1219        );
1220    }
1221
1222    #[test]
1223    fn test_aliased_table_column_type() {
1224        let schema = setup_schema();
1225        let (stmt, ann) = parse_and_annotate("SELECT u.salary FROM users AS u", &schema);
1226        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Double));
1227    }
1228
1229    // ── Binary operator type inference ───────────────────────────────
1230
1231    #[test]
1232    fn test_int_plus_int() {
1233        let schema = setup_schema();
1234        let (stmt, ann) = parse_and_annotate("SELECT id + age FROM users", &schema);
1235        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Int));
1236    }
1237
1238    #[test]
1239    fn test_int_plus_double() {
1240        let schema = setup_schema();
1241        let (stmt, ann) = parse_and_annotate("SELECT id + salary FROM users", &schema);
1242        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Double));
1243    }
1244
1245    #[test]
1246    fn test_comparison_returns_boolean() {
1247        let schema = setup_schema();
1248        let (stmt, ann) = parse_and_annotate("SELECT id > 5 FROM users", &schema);
1249        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1250    }
1251
1252    #[test]
1253    fn test_and_returns_boolean() {
1254        let schema = setup_schema();
1255        let (stmt, ann) = parse_and_annotate("SELECT id > 5 AND age < 30 FROM users", &schema);
1256        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1257    }
1258
1259    // ── CAST type inference ──────────────────────────────────────────
1260
1261    #[test]
1262    fn test_cast_type() {
1263        let schema = setup_schema();
1264        let (stmt, ann) = parse_and_annotate("SELECT CAST(id AS BIGINT) FROM users", &schema);
1265        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::BigInt));
1266    }
1267
1268    #[test]
1269    fn test_cast_to_varchar() {
1270        let schema = setup_schema();
1271        let (stmt, ann) = parse_and_annotate("SELECT CAST(id AS VARCHAR) FROM users", &schema);
1272        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Varchar(None)));
1273    }
1274
1275    // ── CASE expression ──────────────────────────────────────────────
1276
1277    #[test]
1278    fn test_case_expression_type() {
1279        let schema = setup_schema();
1280        let (stmt, ann) = parse_and_annotate(
1281            "SELECT CASE WHEN id > 1 THEN salary ELSE 0.0 END FROM users",
1282            &schema,
1283        );
1284        let t = first_col_type(&stmt, &ann);
1285        assert!(
1286            matches!(t, Some(DataType::Double)),
1287            "Expected Double, got {t:?}"
1288        );
1289    }
1290
1291    // ── Function return types ────────────────────────────────────────
1292
1293    #[test]
1294    fn test_count_returns_bigint() {
1295        let schema = setup_schema();
1296        let (stmt, ann) = parse_and_annotate("SELECT COUNT(*) FROM users", &schema);
1297        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::BigInt));
1298    }
1299
1300    #[test]
1301    fn test_sum_returns_bigint_for_int() {
1302        let schema = setup_schema();
1303        let (stmt, ann) = parse_and_annotate("SELECT SUM(id) FROM users", &schema);
1304        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::BigInt));
1305    }
1306
1307    #[test]
1308    fn test_avg_returns_double() {
1309        let schema = setup_schema();
1310        let (stmt, ann) = parse_and_annotate("SELECT AVG(age) FROM users", &schema);
1311        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Double));
1312    }
1313
1314    #[test]
1315    fn test_min_preserves_type() {
1316        let schema = setup_schema();
1317        let (stmt, ann) = parse_and_annotate("SELECT MIN(salary) FROM users", &schema);
1318        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Double));
1319    }
1320
1321    #[test]
1322    fn test_upper_returns_varchar() {
1323        let schema = setup_schema();
1324        let (stmt, ann) = parse_and_annotate("SELECT UPPER(name) FROM users", &schema);
1325        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Varchar(None)));
1326    }
1327
1328    #[test]
1329    fn test_length_returns_int() {
1330        let schema = setup_schema();
1331        let (stmt, ann) = parse_and_annotate("SELECT LENGTH(name) FROM users", &schema);
1332        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Int));
1333    }
1334
1335    // ── Predicate types ──────────────────────────────────────────────
1336
1337    #[test]
1338    fn test_between_returns_boolean() {
1339        let schema = setup_schema();
1340        let (stmt, ann) = parse_and_annotate("SELECT age BETWEEN 18 AND 65 FROM users", &schema);
1341        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1342    }
1343
1344    #[test]
1345    fn test_in_list_returns_boolean() {
1346        let schema = setup_schema();
1347        let (stmt, ann) = parse_and_annotate("SELECT id IN (1, 2, 3) FROM users", &schema);
1348        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1349    }
1350
1351    #[test]
1352    fn test_is_null_returns_boolean() {
1353        let schema = setup_schema();
1354        let (stmt, ann) = parse_and_annotate("SELECT name IS NULL FROM users", &schema);
1355        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1356    }
1357
1358    #[test]
1359    fn test_like_returns_boolean() {
1360        let schema = setup_schema();
1361        let (stmt, ann) = parse_and_annotate("SELECT name LIKE '%test%' FROM users", &schema);
1362        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1363    }
1364
1365    // ── Exists ───────────────────────────────────────────────────────
1366
1367    #[test]
1368    fn test_exists_returns_boolean() {
1369        let schema = setup_schema();
1370        let (stmt, ann) =
1371            parse_and_annotate("SELECT EXISTS (SELECT 1 FROM orders) FROM users", &schema);
1372        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1373    }
1374
1375    // ── Nested expressions ───────────────────────────────────────────
1376
1377    #[test]
1378    fn test_nested_expression_propagation() {
1379        let schema = setup_schema();
1380        let (stmt, ann) = parse_and_annotate("SELECT (id + age) * salary FROM users", &schema);
1381        let t = first_col_type(&stmt, &ann);
1382        // INT + INT = INT, INT * DOUBLE = DOUBLE
1383        assert!(
1384            matches!(t, Some(DataType::Double)),
1385            "Expected Double, got {t:?}"
1386        );
1387    }
1388
1389    // ── EXTRACT ──────────────────────────────────────────────────────
1390
1391    #[test]
1392    fn test_extract_returns_int() {
1393        let schema = setup_schema();
1394        let (stmt, ann) =
1395            parse_and_annotate("SELECT EXTRACT(YEAR FROM created_at) FROM users", &schema);
1396        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Int));
1397    }
1398
1399    // ── Multiple columns ─────────────────────────────────────────────
1400
1401    #[test]
1402    fn test_multiple_columns_annotated() {
1403        let schema = setup_schema();
1404        let (stmt, ann) = parse_and_annotate("SELECT id, name, salary FROM users", &schema);
1405        if let Statement::Select(sel) = &stmt {
1406            assert_eq!(sel.columns.len(), 3);
1407            // id → Int
1408            if let SelectItem::Expr { expr, .. } = &sel.columns[0] {
1409                assert_eq!(ann.get_type(expr), Some(&DataType::Int));
1410            }
1411            // name → Varchar(255)
1412            if let SelectItem::Expr { expr, .. } = &sel.columns[1] {
1413                assert_eq!(ann.get_type(expr), Some(&DataType::Varchar(Some(255))));
1414            }
1415            // salary → Double
1416            if let SelectItem::Expr { expr, .. } = &sel.columns[2] {
1417                assert_eq!(ann.get_type(expr), Some(&DataType::Double));
1418            }
1419        }
1420    }
1421
1422    // ── WHERE clause annotation ──────────────────────────────────────
1423
1424    #[test]
1425    fn test_where_clause_annotated() {
1426        let schema = setup_schema();
1427        // Don't move stmt after annotation — raw pointers for inline fields
1428        // (like where_clause: Option<Expr>) are invalidated on move.
1429        let stmt = Parser::new("SELECT id FROM users WHERE age > 21")
1430            .unwrap()
1431            .parse_statement()
1432            .unwrap();
1433        let ann = annotate_types(&stmt, &schema);
1434        if let Statement::Select(sel) = &stmt {
1435            if let Some(wh) = &sel.where_clause {
1436                assert_eq!(ann.get_type(wh), Some(&DataType::Boolean));
1437            }
1438        }
1439    }
1440
1441    // ── Coercion rules ──────────────────────────────────────────────
1442
1443    #[test]
1444    fn test_int_and_bigint_coercion() {
1445        assert_eq!(
1446            coerce_numeric(&DataType::Int, &DataType::BigInt),
1447            DataType::BigInt
1448        );
1449    }
1450
1451    #[test]
1452    fn test_float_and_double_coercion() {
1453        assert_eq!(
1454            coerce_numeric(&DataType::Float, &DataType::Double),
1455            DataType::Double
1456        );
1457    }
1458
1459    #[test]
1460    fn test_int_and_double_coercion() {
1461        assert_eq!(
1462            coerce_numeric(&DataType::Int, &DataType::Double),
1463            DataType::Double
1464        );
1465    }
1466
1467    // ── Common type ─────────────────────────────────────────────────
1468
1469    #[test]
1470    fn test_common_type_nulls_skipped() {
1471        let types = vec![&DataType::Null, &DataType::Int, &DataType::Null];
1472        assert_eq!(common_type(&types), Some(DataType::Int));
1473    }
1474
1475    #[test]
1476    fn test_common_type_numeric_widening() {
1477        let types = vec![&DataType::Int, &DataType::Double, &DataType::Float];
1478        assert_eq!(common_type(&types), Some(DataType::Double));
1479    }
1480
1481    #[test]
1482    fn test_common_type_empty() {
1483        let types: Vec<&DataType> = vec![];
1484        assert_eq!(common_type(&types), None);
1485    }
1486
1487    // ── UDF type support ─────────────────────────────────────────────
1488
1489    #[test]
1490    fn test_udf_return_type() {
1491        let mut schema = setup_schema();
1492        schema.add_udf("my_func", DataType::Varchar(None));
1493        let (stmt, ann) = parse_and_annotate("SELECT my_func(id) FROM users", &schema);
1494        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Varchar(None)));
1495    }
1496
1497    // ── Annotation count ─────────────────────────────────────────────
1498
1499    #[test]
1500    fn test_annotations_not_empty() {
1501        let schema = setup_schema();
1502        let (_, ann) = parse_and_annotate("SELECT id, name FROM users WHERE age > 21", &schema);
1503        assert!(!ann.is_empty());
1504        // Should have at least the SELECT columns and WHERE predicate
1505        assert!(ann.len() >= 3);
1506    }
1507
1508    // ── SUM of DECIMAL preserves precision ───────────────────────────
1509
1510    #[test]
1511    fn test_sum_decimal_preserves_type() {
1512        let schema = setup_schema();
1513        let (stmt, ann) = parse_and_annotate("SELECT SUM(amount) FROM orders", &schema);
1514        assert_eq!(
1515            first_col_type(&stmt, &ann),
1516            Some(DataType::Decimal {
1517                precision: Some(10),
1518                scale: Some(2)
1519            })
1520        );
1521    }
1522}