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        | Expr::SimilarTo { .. } => {}
467    }
468}
469
470/// Annotate children of a TypedFunction.
471fn annotate_typed_function_children<S: Schema>(
472    func: &TypedFunction,
473    ctx: &AnnotationContext<S>,
474    ann: &mut TypeAnnotations,
475) {
476    // Use walk_children to visit all child expressions and annotate each
477    func.walk_children(&mut |child| {
478        annotate_expr(child, ctx, ann);
479        true
480    });
481}
482
483// ═══════════════════════════════════════════════════════════════════════
484// Type inference for a single expression node
485// ═══════════════════════════════════════════════════════════════════════
486
487fn infer_type<S: Schema>(
488    expr: &Expr,
489    ctx: &AnnotationContext<S>,
490    ann: &TypeAnnotations,
491) -> Option<DataType> {
492    match expr {
493        // ── Literals ───────────────────────────────────────────────────
494        Expr::Number(s) => Some(infer_number_type(s)),
495        Expr::StringLiteral(_) | Expr::NationalStringLiteral(_) => Some(DataType::Varchar(None)),
496        Expr::Boolean(_) => Some(DataType::Boolean),
497        Expr::Null => Some(DataType::Null),
498
499        // ── Column reference ──────────────────────────────────────────
500        Expr::Column { table, name, .. } => ctx.resolve_column_type(table.as_deref(), name),
501
502        // ── Binary operators ──────────────────────────────────────────
503        Expr::BinaryOp { left, op, right } => {
504            infer_binary_op_type(op, ann.get_type(left), ann.get_type(right))
505        }
506
507        // ── Unary operators ───────────────────────────────────────────
508        Expr::UnaryOp { op, expr: inner } => match op {
509            UnaryOperator::Not => Some(DataType::Boolean),
510            UnaryOperator::Minus | UnaryOperator::Plus => ann.get_type(inner).cloned(),
511            UnaryOperator::BitwiseNot => ann.get_type(inner).cloned(),
512        },
513
514        // ── CAST / TRY_CAST ──────────────────────────────────────────
515        Expr::Cast { data_type, .. } | Expr::TryCast { data_type, .. } => Some(data_type.clone()),
516
517        // ── CASE expression ──────────────────────────────────────────
518        Expr::Case {
519            when_clauses,
520            else_clause,
521            ..
522        } => {
523            let mut result_types: Vec<&DataType> = Vec::new();
524            for (_, result) in when_clauses {
525                if let Some(t) = ann.get_type(result) {
526                    result_types.push(t);
527                }
528            }
529            if let Some(el) = else_clause {
530                if let Some(t) = ann.get_type(el.as_ref()) {
531                    result_types.push(t);
532                }
533            }
534            common_type(&result_types)
535        }
536
537        // ── IF expression ────────────────────────────────────────────
538        Expr::If {
539            true_val,
540            false_val,
541            ..
542        } => {
543            let mut types = Vec::new();
544            if let Some(t) = ann.get_type(true_val) {
545                types.push(t);
546            }
547            if let Some(fv) = false_val {
548                if let Some(t) = ann.get_type(fv.as_ref()) {
549                    types.push(t);
550                }
551            }
552            common_type(&types)
553        }
554
555        // ── COALESCE ─────────────────────────────────────────────────
556        Expr::Coalesce(items) => {
557            let types: Vec<&DataType> = items.iter().filter_map(|e| ann.get_type(e)).collect();
558            common_type(&types)
559        }
560
561        // ── NULLIF ───────────────────────────────────────────────────
562        Expr::NullIf { expr: e, .. } => ann.get_type(e.as_ref()).cloned(),
563
564        // ── Generic function ─────────────────────────────────────────
565        Expr::Function { name, args, .. } => infer_generic_function_type(name, args, ctx, ann),
566
567        // ── Typed functions ──────────────────────────────────────────
568        Expr::TypedFunction { func, .. } => infer_typed_function_type(func, ann),
569
570        // ── Subquery (scalar) ────────────────────────────────────────
571        Expr::Subquery(sub) => infer_subquery_type(sub, ann),
572
573        // ── EXISTS → Boolean ─────────────────────────────────────────
574        Expr::Exists { .. } => Some(DataType::Boolean),
575
576        // ── Boolean predicates ───────────────────────────────────────
577        Expr::Between { .. }
578        | Expr::InList { .. }
579        | Expr::InSubquery { .. }
580        | Expr::IsNull { .. }
581        | Expr::IsBool { .. }
582        | Expr::Like { .. }
583        | Expr::ILike { .. }
584        | Expr::AnyOp { .. }
585        | Expr::AllOp { .. } => Some(DataType::Boolean),
586
587        // ── EXTRACT → numeric ────────────────────────────────────────
588        Expr::Extract { .. } => Some(DataType::Int),
589
590        // ── INTERVAL → Interval ──────────────────────────────────────
591        Expr::Interval { .. } => Some(DataType::Interval),
592
593        // ── Array literal ────────────────────────────────────────────
594        Expr::ArrayLiteral(items) => {
595            let elem_types: Vec<&DataType> = items.iter().filter_map(|e| ann.get_type(e)).collect();
596            let elem = common_type(&elem_types);
597            Some(DataType::Array(elem.map(Box::new)))
598        }
599
600        // ── Tuple ────────────────────────────────────────────────────
601        Expr::Tuple(items) => {
602            let types: Vec<DataType> = items
603                .iter()
604                .map(|e| ann.get_type(e).cloned().unwrap_or(DataType::Null))
605                .collect();
606            Some(DataType::Tuple(types))
607        }
608
609        // ── Array index → element type ───────────────────────────────
610        Expr::ArrayIndex { expr: e, .. } => match ann.get_type(e.as_ref()) {
611            Some(DataType::Array(Some(elem))) => Some(elem.as_ref().clone()),
612            _ => None,
613        },
614
615        // ── JSON access ──────────────────────────────────────────────
616        Expr::JsonAccess { as_text, .. } => {
617            if *as_text {
618                Some(DataType::Text)
619            } else {
620                Some(DataType::Json)
621            }
622        }
623
624        // ── Nested / Alias — pass through ────────────────────────────
625        Expr::Nested(inner) => ann.get_type(inner.as_ref()).cloned(),
626        Expr::Alias { expr: e, .. } => ann.get_type(e.as_ref()).cloned(),
627
628        // ── Collate → Varchar ────────────────────────────────────────
629        Expr::Collate { .. } => Some(DataType::Varchar(None)),
630
631        // ── TypeExpr ─────────────────────────────────────────────────
632        Expr::TypeExpr(dt) => Some(dt.clone()),
633
634        // ── Others — no type ─────────────────────────────────────────
635        Expr::Wildcard
636        | Expr::Star
637        | Expr::QualifiedWildcard { .. }
638        | Expr::Parameter(_)
639        | Expr::Lambda { .. }
640        | Expr::Default
641        | Expr::Cube { .. }
642        | Expr::Rollup { .. }
643        | Expr::GroupingSets { .. }
644        | Expr::SimilarTo { .. }
645        | Expr::Commented { .. } => None,
646    }
647}
648
649// ═══════════════════════════════════════════════════════════════════════
650// Number type inference
651// ═══════════════════════════════════════════════════════════════════════
652
653fn infer_number_type(s: &str) -> DataType {
654    if s.contains('.') || s.contains('e') || s.contains('E') {
655        DataType::Double
656    } else if let Ok(v) = s.parse::<i64>() {
657        if v >= i32::MIN as i64 && v <= i32::MAX as i64 {
658            DataType::Int
659        } else {
660            DataType::BigInt
661        }
662    } else {
663        // Very large numbers or special formats
664        DataType::BigInt
665    }
666}
667
668// ═══════════════════════════════════════════════════════════════════════
669// Binary operator type inference
670// ═══════════════════════════════════════════════════════════════════════
671
672fn infer_binary_op_type(
673    op: &BinaryOperator,
674    left: Option<&DataType>,
675    right: Option<&DataType>,
676) -> Option<DataType> {
677    use BinaryOperator::*;
678    match op {
679        // Comparison operators → Boolean
680        Eq | Neq | Lt | Gt | LtEq | GtEq | AtArrow | ArrowAt => Some(DataType::Boolean),
681
682        // Logical operators → Boolean
683        And | Or | Xor => Some(DataType::Boolean),
684
685        // String concatenation → Varchar
686        Concat => Some(DataType::Varchar(None)),
687
688        // Arithmetic → coerce operand types
689        Plus | Minus | Multiply | Divide | Modulo => match (left, right) {
690            (Some(l), Some(r)) => Some(coerce_numeric(l, r)),
691            (Some(l), None) => Some(l.clone()),
692            (None, Some(r)) => Some(r.clone()),
693            (None, None) => None,
694        },
695
696        // Bitwise → integer type
697        BitwiseAnd | BitwiseOr | BitwiseXor | ShiftLeft | ShiftRight => match (left, right) {
698            (Some(l), Some(r)) => Some(coerce_numeric(l, r)),
699            (Some(l), None) => Some(l.clone()),
700            (None, Some(r)) => Some(r.clone()),
701            (None, None) => Some(DataType::Int),
702        },
703
704        // JSON operators
705        Arrow => Some(DataType::Json),
706        DoubleArrow => Some(DataType::Text),
707    }
708}
709
710// ═══════════════════════════════════════════════════════════════════════
711// Generic (untyped) function return type inference
712// ═══════════════════════════════════════════════════════════════════════
713
714fn infer_generic_function_type<S: Schema>(
715    name: &str,
716    args: &[Expr],
717    ctx: &AnnotationContext<S>,
718    ann: &TypeAnnotations,
719) -> Option<DataType> {
720    let upper = name.to_uppercase();
721    match upper.as_str() {
722        // Aggregate functions
723        "COUNT" | "COUNT_BIG" => Some(DataType::BigInt),
724        "SUM" => args
725            .first()
726            .and_then(|a| ann.get_type(a))
727            .map(|t| coerce_sum_type(t)),
728        "AVG" => Some(DataType::Double),
729        "MIN" | "MAX" => args.first().and_then(|a| ann.get_type(a)).cloned(),
730        "VARIANCE" | "VAR_SAMP" | "VAR_POP" | "STDDEV" | "STDDEV_SAMP" | "STDDEV_POP" => {
731            Some(DataType::Double)
732        }
733        "APPROX_COUNT_DISTINCT" | "APPROX_DISTINCT" => Some(DataType::BigInt),
734
735        // String functions
736        "CONCAT" | "UPPER" | "LOWER" | "TRIM" | "LTRIM" | "RTRIM" | "LPAD" | "RPAD" | "REPLACE"
737        | "REVERSE" | "SUBSTRING" | "SUBSTR" | "LEFT" | "RIGHT" | "INITCAP" | "REPEAT"
738        | "TRANSLATE" | "FORMAT" | "CONCAT_WS" | "SPACE" | "REPLICATE" => {
739            Some(DataType::Varchar(None))
740        }
741        "LENGTH" | "LEN" | "CHAR_LENGTH" | "CHARACTER_LENGTH" | "OCTET_LENGTH" | "BIT_LENGTH" => {
742            Some(DataType::Int)
743        }
744        "POSITION" | "STRPOS" | "LOCATE" | "INSTR" | "CHARINDEX" => Some(DataType::Int),
745        "ASCII" => Some(DataType::Int),
746        "CHR" | "CHAR" => Some(DataType::Varchar(Some(1))),
747
748        // Math functions
749        "ABS" | "CEIL" | "CEILING" | "FLOOR" => args.first().and_then(|a| ann.get_type(a)).cloned(),
750        "ROUND" | "TRUNCATE" | "TRUNC" => args.first().and_then(|a| ann.get_type(a)).cloned(),
751        "SQRT" | "LN" | "LOG" | "LOG2" | "LOG10" | "EXP" | "POWER" | "POW" | "ACOS" | "ASIN"
752        | "ATAN" | "ATAN2" | "COS" | "SIN" | "TAN" | "COT" | "DEGREES" | "RADIANS" | "PI"
753        | "SIGN" => Some(DataType::Double),
754        "MOD" => {
755            match (
756                args.first().and_then(|a| ann.get_type(a)),
757                args.get(1).and_then(|a| ann.get_type(a)),
758            ) {
759                (Some(l), Some(r)) => Some(coerce_numeric(l, r)),
760                (Some(l), _) => Some(l.clone()),
761                (_, Some(r)) => Some(r.clone()),
762                _ => Some(DataType::Int),
763            }
764        }
765        "GREATEST" | "LEAST" => {
766            let types: Vec<&DataType> = args.iter().filter_map(|a| ann.get_type(a)).collect();
767            common_type(&types)
768        }
769        "RANDOM" | "RAND" => Some(DataType::Double),
770
771        // Date/Time functions
772        "CURRENT_DATE" | "CURDATE" | "TODAY" => Some(DataType::Date),
773        "CURRENT_TIMESTAMP" | "NOW" | "GETDATE" | "SYSDATE" | "SYSTIMESTAMP" | "LOCALTIMESTAMP" => {
774            Some(DataType::Timestamp {
775                precision: None,
776                with_tz: false,
777            })
778        }
779        "CURRENT_TIME" | "CURTIME" => Some(DataType::Time { precision: None }),
780        "DATE" | "TO_DATE" | "DATE_TRUNC" | "DATE_ADD" | "DATE_SUB" | "DATEADD" | "DATESUB"
781        | "ADDDATE" | "SUBDATE" => Some(DataType::Date),
782        "TIMESTAMP" | "TO_TIMESTAMP" => Some(DataType::Timestamp {
783            precision: None,
784            with_tz: false,
785        }),
786        "YEAR" | "MONTH" | "DAY" | "DAYOFWEEK" | "DAYOFYEAR" | "HOUR" | "MINUTE" | "SECOND"
787        | "QUARTER" | "WEEK" | "EXTRACT" | "DATEDIFF" | "TIMESTAMPDIFF" | "MONTHS_BETWEEN" => {
788            Some(DataType::Int)
789        }
790
791        // Type conversion
792        "CAST" | "TRY_CAST" | "SAFE_CAST" | "CONVERT" => None, // handled by Expr::Cast
793
794        // Boolean functions
795        "COALESCE" => {
796            let types: Vec<&DataType> = args.iter().filter_map(|a| ann.get_type(a)).collect();
797            common_type(&types)
798        }
799        "NULLIF" => args.first().and_then(|a| ann.get_type(a)).cloned(),
800        "IF" | "IIF" => {
801            // IF(cond, true_val, false_val) — type from true_val
802            args.get(1).and_then(|a| ann.get_type(a)).cloned()
803        }
804        "IFNULL" | "NVL" | "ISNULL" => {
805            let types: Vec<&DataType> = args.iter().filter_map(|a| ann.get_type(a)).collect();
806            common_type(&types)
807        }
808
809        // JSON functions
810        "JSON_EXTRACT" | "JSON_QUERY" | "GET_JSON_OBJECT" => Some(DataType::Json),
811        "JSON_EXTRACT_SCALAR" | "JSON_VALUE" | "JSON_EXTRACT_PATH_TEXT" => {
812            Some(DataType::Varchar(None))
813        }
814        "TO_JSON" | "JSON_OBJECT" | "JSON_ARRAY" | "JSON_BUILD_OBJECT" | "JSON_BUILD_ARRAY" => {
815            Some(DataType::Json)
816        }
817        "PARSE_JSON" | "JSON_PARSE" | "JSON" => Some(DataType::Json),
818
819        // Array functions
820        "ARRAY_AGG" | "COLLECT_LIST" | "COLLECT_SET" => {
821            let elem = args.first().and_then(|a| ann.get_type(a)).cloned();
822            Some(DataType::Array(elem.map(Box::new)))
823        }
824        "ARRAY_LENGTH" | "ARRAY_SIZE" | "CARDINALITY" => Some(DataType::Int),
825        "ARRAY" | "ARRAY_CONSTRUCT" => {
826            let types: Vec<&DataType> = args.iter().filter_map(|a| ann.get_type(a)).collect();
827            let elem = common_type(&types);
828            Some(DataType::Array(elem.map(Box::new)))
829        }
830        "ARRAY_CONTAINS" | "ARRAY_POSITION" => Some(DataType::Boolean),
831
832        // Window ranking
833        "ROW_NUMBER" | "RANK" | "DENSE_RANK" | "NTILE" | "CUME_DIST" | "PERCENT_RANK" => {
834            Some(DataType::BigInt)
835        }
836
837        // Hash / crypto
838        "MD5" | "SHA1" | "SHA" | "SHA2" | "SHA256" | "SHA512" => Some(DataType::Varchar(None)),
839        "HEX" | "TO_HEX" => Some(DataType::Varchar(None)),
840        "UNHEX" | "FROM_HEX" => Some(DataType::Varbinary(None)),
841        "CRC32" | "HASH" => Some(DataType::BigInt),
842
843        // Type checking
844        "TYPEOF" | "TYPE_OF" => Some(DataType::Varchar(None)),
845
846        // UDFs — check schema
847        _ => ctx.schema.get_udf_type(&upper).cloned(),
848    }
849}
850
851// ═══════════════════════════════════════════════════════════════════════
852// TypedFunction return type inference
853// ═══════════════════════════════════════════════════════════════════════
854
855fn infer_typed_function_type(func: &TypedFunction, ann: &TypeAnnotations) -> Option<DataType> {
856    match func {
857        // ── Date/Time → Date or Timestamp ────────────────────────────
858        TypedFunction::DateAdd { .. }
859        | TypedFunction::DateSub { .. }
860        | TypedFunction::DateTrunc { .. }
861        | TypedFunction::TsOrDsToDate { .. } => Some(DataType::Date),
862        TypedFunction::DateDiff { .. } => Some(DataType::Int),
863        TypedFunction::CurrentDate => Some(DataType::Date),
864        TypedFunction::CurrentTime => Some(DataType::Time { precision: None }),
865        TypedFunction::CurrentTimestamp => Some(DataType::Timestamp {
866            precision: None,
867            with_tz: false,
868        }),
869        TypedFunction::StrToTime { .. } => Some(DataType::Timestamp {
870            precision: None,
871            with_tz: false,
872        }),
873        TypedFunction::TimeToStr { .. } => Some(DataType::Varchar(None)),
874        TypedFunction::Year { .. } | TypedFunction::Month { .. } | TypedFunction::Day { .. } => {
875            Some(DataType::Int)
876        }
877
878        // ── String → Varchar ─────────────────────────────────────────
879        TypedFunction::Trim { .. }
880        | TypedFunction::Substring { .. }
881        | TypedFunction::Upper { .. }
882        | TypedFunction::Lower { .. }
883        | TypedFunction::Initcap { .. }
884        | TypedFunction::Replace { .. }
885        | TypedFunction::Reverse { .. }
886        | TypedFunction::Left { .. }
887        | TypedFunction::Right { .. }
888        | TypedFunction::Lpad { .. }
889        | TypedFunction::Rpad { .. }
890        | TypedFunction::ConcatWs { .. } => Some(DataType::Varchar(None)),
891        TypedFunction::Length { .. } => Some(DataType::Int),
892        TypedFunction::RegexpLike { .. } => Some(DataType::Boolean),
893        TypedFunction::RegexpExtract { .. } => Some(DataType::Varchar(None)),
894        TypedFunction::RegexpReplace { .. } => Some(DataType::Varchar(None)),
895        TypedFunction::Split { .. } => {
896            Some(DataType::Array(Some(Box::new(DataType::Varchar(None)))))
897        }
898
899        // ── Aggregates ───────────────────────────────────────────────
900        TypedFunction::Count { .. } => Some(DataType::BigInt),
901        TypedFunction::Sum { expr, .. } => ann.get_type(expr.as_ref()).map(|t| coerce_sum_type(t)),
902        TypedFunction::Avg { .. } => Some(DataType::Double),
903        TypedFunction::Min { expr } | TypedFunction::Max { expr } => {
904            ann.get_type(expr.as_ref()).cloned()
905        }
906        TypedFunction::ArrayAgg { expr, .. } => {
907            let elem = ann.get_type(expr.as_ref()).cloned();
908            Some(DataType::Array(elem.map(Box::new)))
909        }
910        TypedFunction::ApproxDistinct { .. } => Some(DataType::BigInt),
911        TypedFunction::Variance { .. }
912        | TypedFunction::VariancePop { .. }
913        | TypedFunction::Stddev { .. }
914        | TypedFunction::StddevPop { .. } => Some(DataType::Double),
915        TypedFunction::GroupConcat { .. } => Some(DataType::Varchar(None)),
916
917        // ── Array ────────────────────────────────────────────────────
918        TypedFunction::ArrayConcat { arrays } => {
919            // Type is the same as input arrays
920            arrays.first().and_then(|a| ann.get_type(a)).cloned()
921        }
922        TypedFunction::ArrayContains { .. } => Some(DataType::Boolean),
923        TypedFunction::ArraySize { .. } => Some(DataType::Int),
924        TypedFunction::Explode { expr } => {
925            // Unwrap array element type
926            match ann.get_type(expr.as_ref()) {
927                Some(DataType::Array(Some(elem))) => Some(elem.as_ref().clone()),
928                _ => None,
929            }
930        }
931        TypedFunction::GenerateSeries { .. } => Some(DataType::Int),
932        TypedFunction::Flatten { expr } => ann.get_type(expr.as_ref()).cloned(),
933
934        // ── JSON ─────────────────────────────────────────────────────
935        TypedFunction::JSONExtract { .. } => Some(DataType::Json),
936        TypedFunction::JSONExtractScalar { .. } => Some(DataType::Varchar(None)),
937        TypedFunction::ParseJSON { .. } | TypedFunction::JSONFormat { .. } => Some(DataType::Json),
938
939        // ── Window ───────────────────────────────────────────────────
940        TypedFunction::RowNumber | TypedFunction::Rank | TypedFunction::DenseRank => {
941            Some(DataType::BigInt)
942        }
943        TypedFunction::NTile { .. } => Some(DataType::BigInt),
944        TypedFunction::Lead { expr, .. }
945        | TypedFunction::Lag { expr, .. }
946        | TypedFunction::FirstValue { expr }
947        | TypedFunction::LastValue { expr } => ann.get_type(expr.as_ref()).cloned(),
948
949        // ── Math ─────────────────────────────────────────────────────
950        TypedFunction::Abs { expr }
951        | TypedFunction::Ceil { expr }
952        | TypedFunction::Floor { expr } => ann.get_type(expr.as_ref()).cloned(),
953        TypedFunction::Round { expr, .. } => ann.get_type(expr.as_ref()).cloned(),
954        TypedFunction::Log { .. }
955        | TypedFunction::Ln { .. }
956        | TypedFunction::Pow { .. }
957        | TypedFunction::Sqrt { .. } => Some(DataType::Double),
958        TypedFunction::Greatest { exprs } | TypedFunction::Least { exprs } => {
959            let types: Vec<&DataType> = exprs.iter().filter_map(|e| ann.get_type(e)).collect();
960            common_type(&types)
961        }
962        TypedFunction::Mod { left, right } => {
963            match (ann.get_type(left.as_ref()), ann.get_type(right.as_ref())) {
964                (Some(l), Some(r)) => Some(coerce_numeric(l, r)),
965                (Some(l), _) => Some(l.clone()),
966                (_, Some(r)) => Some(r.clone()),
967                _ => Some(DataType::Int),
968            }
969        }
970
971        // ── Conversion ───────────────────────────────────────────────
972        TypedFunction::Hex { .. } | TypedFunction::Md5 { .. } | TypedFunction::Sha { .. } => {
973            Some(DataType::Varchar(None))
974        }
975        TypedFunction::Sha2 { .. } => Some(DataType::Varchar(None)),
976        TypedFunction::Unhex { .. } => Some(DataType::Varbinary(None)),
977    }
978}
979
980// ═══════════════════════════════════════════════════════════════════════
981// Subquery type inference
982// ═══════════════════════════════════════════════════════════════════════
983
984fn infer_subquery_type(sub: &Statement, ann: &TypeAnnotations) -> Option<DataType> {
985    // The type of a scalar subquery is the type of its single output column
986    if let Statement::Select(sel) = sub {
987        if let Some(SelectItem::Expr { expr, .. }) = sel.columns.first() {
988            return ann.get_type(expr).cloned();
989        }
990    }
991    None
992}
993
994// ═══════════════════════════════════════════════════════════════════════
995// Type coercion helpers
996// ═══════════════════════════════════════════════════════════════════════
997
998/// Numeric type widening precedence (higher = wider).
999fn numeric_precedence(dt: &DataType) -> u8 {
1000    match dt {
1001        DataType::Boolean => 1,
1002        DataType::TinyInt => 2,
1003        DataType::SmallInt => 3,
1004        DataType::Int | DataType::Serial => 4,
1005        DataType::BigInt | DataType::BigSerial => 5,
1006        DataType::Real | DataType::Float => 6,
1007        DataType::Double => 7,
1008        DataType::Decimal { .. } | DataType::Numeric { .. } => 8,
1009        _ => 0,
1010    }
1011}
1012
1013/// Coerce two numeric types to their common wider type.
1014fn coerce_numeric(left: &DataType, right: &DataType) -> DataType {
1015    let lp = numeric_precedence(left);
1016    let rp = numeric_precedence(right);
1017    if lp == 0 && rp == 0 {
1018        // Neither is numeric — fall back to left
1019        return left.clone();
1020    }
1021    if lp >= rp {
1022        left.clone()
1023    } else {
1024        right.clone()
1025    }
1026}
1027
1028/// Determine the return type of SUM based on input type.
1029fn coerce_sum_type(input: &DataType) -> DataType {
1030    match input {
1031        DataType::TinyInt | DataType::SmallInt | DataType::Int | DataType::BigInt => {
1032            DataType::BigInt
1033        }
1034        DataType::Float | DataType::Real => DataType::Double,
1035        DataType::Double => DataType::Double,
1036        DataType::Decimal { precision, scale } => DataType::Decimal {
1037            precision: *precision,
1038            scale: *scale,
1039        },
1040        DataType::Numeric { precision, scale } => DataType::Numeric {
1041            precision: *precision,
1042            scale: *scale,
1043        },
1044        _ => DataType::BigInt,
1045    }
1046}
1047
1048/// Find the common (widest) type among a set of types.
1049fn common_type(types: &[&DataType]) -> Option<DataType> {
1050    if types.is_empty() {
1051        return None;
1052    }
1053    let mut result = types[0];
1054    for t in &types[1..] {
1055        // Skip NULL — it doesn't contribute to the common type
1056        if **t == DataType::Null {
1057            continue;
1058        }
1059        if *result == DataType::Null {
1060            result = t;
1061            continue;
1062        }
1063        // If both are numeric, pick the wider one
1064        let lp = numeric_precedence(result);
1065        let rp = numeric_precedence(t);
1066        if lp > 0 && rp > 0 {
1067            if rp > lp {
1068                result = t;
1069            }
1070            continue;
1071        }
1072        // If both are string-like, prefer VARCHAR
1073        if is_string_type(result) && is_string_type(t) {
1074            result = if matches!(result, DataType::Text) || matches!(t, DataType::Text) {
1075                if matches!(result, DataType::Text) {
1076                    result
1077                } else {
1078                    t
1079                }
1080            } else {
1081                result // keep first
1082            };
1083            continue;
1084        }
1085        // Otherwise keep the first non-null type
1086    }
1087    Some(result.clone())
1088}
1089
1090fn is_string_type(dt: &DataType) -> bool {
1091    matches!(
1092        dt,
1093        DataType::Varchar(_)
1094            | DataType::Char(_)
1095            | DataType::NChar(_)
1096            | DataType::NVarchar(_)
1097            | DataType::VarcharMax
1098            | DataType::NvarcharMax
1099            | DataType::Varchar2(_)
1100            | DataType::NVarchar2(_)
1101            | DataType::Text
1102            | DataType::String
1103    )
1104}
1105
1106// ═══════════════════════════════════════════════════════════════════════
1107// Tests
1108// ═══════════════════════════════════════════════════════════════════════
1109
1110#[cfg(test)]
1111mod tests {
1112    use super::*;
1113    use crate::dialects::Dialect;
1114    use crate::parser::Parser;
1115    use crate::schema::{MappingSchema, Schema};
1116
1117    fn setup_schema() -> MappingSchema {
1118        let mut schema = MappingSchema::new(Dialect::Ansi);
1119        schema
1120            .add_table(
1121                &["users"],
1122                vec![
1123                    ("id".to_string(), DataType::Int),
1124                    ("name".to_string(), DataType::Varchar(Some(255))),
1125                    ("age".to_string(), DataType::Int),
1126                    ("salary".to_string(), DataType::Double),
1127                    ("active".to_string(), DataType::Boolean),
1128                    (
1129                        "created_at".to_string(),
1130                        DataType::Timestamp {
1131                            precision: None,
1132                            with_tz: false,
1133                        },
1134                    ),
1135                ],
1136            )
1137            .unwrap();
1138        schema
1139            .add_table(
1140                &["orders"],
1141                vec![
1142                    ("id".to_string(), DataType::Int),
1143                    ("user_id".to_string(), DataType::Int),
1144                    (
1145                        "amount".to_string(),
1146                        DataType::Decimal {
1147                            precision: Some(10),
1148                            scale: Some(2),
1149                        },
1150                    ),
1151                    ("status".to_string(), DataType::Varchar(Some(50))),
1152                ],
1153            )
1154            .unwrap();
1155        schema
1156    }
1157
1158    fn parse_and_annotate(sql: &str, schema: &MappingSchema) -> (Statement, TypeAnnotations) {
1159        let stmt = Parser::new(sql).unwrap().parse_statement().unwrap();
1160        let ann = annotate_types(&stmt, schema);
1161        (stmt, ann)
1162    }
1163
1164    /// Helper: get the type of the first SELECT column
1165    fn first_col_type(stmt: &Statement, ann: &TypeAnnotations) -> Option<DataType> {
1166        if let Statement::Select(sel) = stmt {
1167            if let Some(SelectItem::Expr { expr, .. }) = sel.columns.first() {
1168                return ann.get_type(expr).cloned();
1169            }
1170        }
1171        None
1172    }
1173
1174    // ── Literal type inference ────────────────────────────────────────
1175
1176    #[test]
1177    fn test_number_literal_int() {
1178        let schema = setup_schema();
1179        let (stmt, ann) = parse_and_annotate("SELECT 42", &schema);
1180        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Int));
1181    }
1182
1183    #[test]
1184    fn test_number_literal_big_int() {
1185        let schema = setup_schema();
1186        let (stmt, ann) = parse_and_annotate("SELECT 9999999999", &schema);
1187        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::BigInt));
1188    }
1189
1190    #[test]
1191    fn test_number_literal_double() {
1192        let schema = setup_schema();
1193        let (stmt, ann) = parse_and_annotate("SELECT 3.14", &schema);
1194        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Double));
1195    }
1196
1197    #[test]
1198    fn test_string_literal() {
1199        let schema = setup_schema();
1200        let (stmt, ann) = parse_and_annotate("SELECT 'hello'", &schema);
1201        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Varchar(None)));
1202    }
1203
1204    #[test]
1205    fn test_boolean_literal() {
1206        let schema = setup_schema();
1207        let (stmt, ann) = parse_and_annotate("SELECT TRUE", &schema);
1208        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1209    }
1210
1211    #[test]
1212    fn test_null_literal() {
1213        let schema = setup_schema();
1214        let (stmt, ann) = parse_and_annotate("SELECT NULL", &schema);
1215        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Null));
1216    }
1217
1218    // ── Column reference type lookup ─────────────────────────────────
1219
1220    #[test]
1221    fn test_column_type_from_schema() {
1222        let schema = setup_schema();
1223        let (stmt, ann) = parse_and_annotate("SELECT id FROM users", &schema);
1224        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Int));
1225    }
1226
1227    #[test]
1228    fn test_qualified_column_type() {
1229        let schema = setup_schema();
1230        let (stmt, ann) = parse_and_annotate("SELECT users.name FROM users", &schema);
1231        assert_eq!(
1232            first_col_type(&stmt, &ann),
1233            Some(DataType::Varchar(Some(255)))
1234        );
1235    }
1236
1237    #[test]
1238    fn test_aliased_table_column_type() {
1239        let schema = setup_schema();
1240        let (stmt, ann) = parse_and_annotate("SELECT u.salary FROM users AS u", &schema);
1241        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Double));
1242    }
1243
1244    // ── Binary operator type inference ───────────────────────────────
1245
1246    #[test]
1247    fn test_int_plus_int() {
1248        let schema = setup_schema();
1249        let (stmt, ann) = parse_and_annotate("SELECT id + age FROM users", &schema);
1250        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Int));
1251    }
1252
1253    #[test]
1254    fn test_int_plus_double() {
1255        let schema = setup_schema();
1256        let (stmt, ann) = parse_and_annotate("SELECT id + salary FROM users", &schema);
1257        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Double));
1258    }
1259
1260    #[test]
1261    fn test_comparison_returns_boolean() {
1262        let schema = setup_schema();
1263        let (stmt, ann) = parse_and_annotate("SELECT id > 5 FROM users", &schema);
1264        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1265    }
1266
1267    #[test]
1268    fn test_and_returns_boolean() {
1269        let schema = setup_schema();
1270        let (stmt, ann) = parse_and_annotate("SELECT id > 5 AND age < 30 FROM users", &schema);
1271        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1272    }
1273
1274    // ── CAST type inference ──────────────────────────────────────────
1275
1276    #[test]
1277    fn test_cast_type() {
1278        let schema = setup_schema();
1279        let (stmt, ann) = parse_and_annotate("SELECT CAST(id AS BIGINT) FROM users", &schema);
1280        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::BigInt));
1281    }
1282
1283    #[test]
1284    fn test_cast_to_varchar() {
1285        let schema = setup_schema();
1286        let (stmt, ann) = parse_and_annotate("SELECT CAST(id AS VARCHAR) FROM users", &schema);
1287        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Varchar(None)));
1288    }
1289
1290    // ── CASE expression ──────────────────────────────────────────────
1291
1292    #[test]
1293    fn test_case_expression_type() {
1294        let schema = setup_schema();
1295        let (stmt, ann) = parse_and_annotate(
1296            "SELECT CASE WHEN id > 1 THEN salary ELSE 0.0 END FROM users",
1297            &schema,
1298        );
1299        let t = first_col_type(&stmt, &ann);
1300        assert!(
1301            matches!(t, Some(DataType::Double)),
1302            "Expected Double, got {t:?}"
1303        );
1304    }
1305
1306    // ── Function return types ────────────────────────────────────────
1307
1308    #[test]
1309    fn test_count_returns_bigint() {
1310        let schema = setup_schema();
1311        let (stmt, ann) = parse_and_annotate("SELECT COUNT(*) FROM users", &schema);
1312        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::BigInt));
1313    }
1314
1315    #[test]
1316    fn test_sum_returns_bigint_for_int() {
1317        let schema = setup_schema();
1318        let (stmt, ann) = parse_and_annotate("SELECT SUM(id) FROM users", &schema);
1319        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::BigInt));
1320    }
1321
1322    #[test]
1323    fn test_avg_returns_double() {
1324        let schema = setup_schema();
1325        let (stmt, ann) = parse_and_annotate("SELECT AVG(age) FROM users", &schema);
1326        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Double));
1327    }
1328
1329    #[test]
1330    fn test_min_preserves_type() {
1331        let schema = setup_schema();
1332        let (stmt, ann) = parse_and_annotate("SELECT MIN(salary) FROM users", &schema);
1333        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Double));
1334    }
1335
1336    #[test]
1337    fn test_upper_returns_varchar() {
1338        let schema = setup_schema();
1339        let (stmt, ann) = parse_and_annotate("SELECT UPPER(name) FROM users", &schema);
1340        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Varchar(None)));
1341    }
1342
1343    #[test]
1344    fn test_length_returns_int() {
1345        let schema = setup_schema();
1346        let (stmt, ann) = parse_and_annotate("SELECT LENGTH(name) FROM users", &schema);
1347        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Int));
1348    }
1349
1350    // ── Predicate types ──────────────────────────────────────────────
1351
1352    #[test]
1353    fn test_between_returns_boolean() {
1354        let schema = setup_schema();
1355        let (stmt, ann) = parse_and_annotate("SELECT age BETWEEN 18 AND 65 FROM users", &schema);
1356        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1357    }
1358
1359    #[test]
1360    fn test_in_list_returns_boolean() {
1361        let schema = setup_schema();
1362        let (stmt, ann) = parse_and_annotate("SELECT id IN (1, 2, 3) FROM users", &schema);
1363        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1364    }
1365
1366    #[test]
1367    fn test_is_null_returns_boolean() {
1368        let schema = setup_schema();
1369        let (stmt, ann) = parse_and_annotate("SELECT name IS NULL FROM users", &schema);
1370        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1371    }
1372
1373    #[test]
1374    fn test_like_returns_boolean() {
1375        let schema = setup_schema();
1376        let (stmt, ann) = parse_and_annotate("SELECT name LIKE '%test%' FROM users", &schema);
1377        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1378    }
1379
1380    // ── Exists ───────────────────────────────────────────────────────
1381
1382    #[test]
1383    fn test_exists_returns_boolean() {
1384        let schema = setup_schema();
1385        let (stmt, ann) =
1386            parse_and_annotate("SELECT EXISTS (SELECT 1 FROM orders) FROM users", &schema);
1387        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Boolean));
1388    }
1389
1390    // ── Nested expressions ───────────────────────────────────────────
1391
1392    #[test]
1393    fn test_nested_expression_propagation() {
1394        let schema = setup_schema();
1395        let (stmt, ann) = parse_and_annotate("SELECT (id + age) * salary FROM users", &schema);
1396        let t = first_col_type(&stmt, &ann);
1397        // INT + INT = INT, INT * DOUBLE = DOUBLE
1398        assert!(
1399            matches!(t, Some(DataType::Double)),
1400            "Expected Double, got {t:?}"
1401        );
1402    }
1403
1404    // ── EXTRACT ──────────────────────────────────────────────────────
1405
1406    #[test]
1407    fn test_extract_returns_int() {
1408        let schema = setup_schema();
1409        let (stmt, ann) =
1410            parse_and_annotate("SELECT EXTRACT(YEAR FROM created_at) FROM users", &schema);
1411        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Int));
1412    }
1413
1414    // ── Multiple columns ─────────────────────────────────────────────
1415
1416    #[test]
1417    fn test_multiple_columns_annotated() {
1418        let schema = setup_schema();
1419        let (stmt, ann) = parse_and_annotate("SELECT id, name, salary FROM users", &schema);
1420        if let Statement::Select(sel) = &stmt {
1421            assert_eq!(sel.columns.len(), 3);
1422            // id → Int
1423            if let SelectItem::Expr { expr, .. } = &sel.columns[0] {
1424                assert_eq!(ann.get_type(expr), Some(&DataType::Int));
1425            }
1426            // name → Varchar(255)
1427            if let SelectItem::Expr { expr, .. } = &sel.columns[1] {
1428                assert_eq!(ann.get_type(expr), Some(&DataType::Varchar(Some(255))));
1429            }
1430            // salary → Double
1431            if let SelectItem::Expr { expr, .. } = &sel.columns[2] {
1432                assert_eq!(ann.get_type(expr), Some(&DataType::Double));
1433            }
1434        }
1435    }
1436
1437    // ── WHERE clause annotation ──────────────────────────────────────
1438
1439    #[test]
1440    fn test_where_clause_annotated() {
1441        let schema = setup_schema();
1442        // Don't move stmt after annotation — raw pointers for inline fields
1443        // (like where_clause: Option<Expr>) are invalidated on move.
1444        let stmt = Parser::new("SELECT id FROM users WHERE age > 21")
1445            .unwrap()
1446            .parse_statement()
1447            .unwrap();
1448        let ann = annotate_types(&stmt, &schema);
1449        if let Statement::Select(sel) = &stmt {
1450            if let Some(wh) = &sel.where_clause {
1451                assert_eq!(ann.get_type(wh), Some(&DataType::Boolean));
1452            }
1453        }
1454    }
1455
1456    // ── Coercion rules ──────────────────────────────────────────────
1457
1458    #[test]
1459    fn test_int_and_bigint_coercion() {
1460        assert_eq!(
1461            coerce_numeric(&DataType::Int, &DataType::BigInt),
1462            DataType::BigInt
1463        );
1464    }
1465
1466    #[test]
1467    fn test_float_and_double_coercion() {
1468        assert_eq!(
1469            coerce_numeric(&DataType::Float, &DataType::Double),
1470            DataType::Double
1471        );
1472    }
1473
1474    #[test]
1475    fn test_int_and_double_coercion() {
1476        assert_eq!(
1477            coerce_numeric(&DataType::Int, &DataType::Double),
1478            DataType::Double
1479        );
1480    }
1481
1482    // ── Common type ─────────────────────────────────────────────────
1483
1484    #[test]
1485    fn test_common_type_nulls_skipped() {
1486        let types = vec![&DataType::Null, &DataType::Int, &DataType::Null];
1487        assert_eq!(common_type(&types), Some(DataType::Int));
1488    }
1489
1490    #[test]
1491    fn test_common_type_numeric_widening() {
1492        let types = vec![&DataType::Int, &DataType::Double, &DataType::Float];
1493        assert_eq!(common_type(&types), Some(DataType::Double));
1494    }
1495
1496    #[test]
1497    fn test_common_type_empty() {
1498        let types: Vec<&DataType> = vec![];
1499        assert_eq!(common_type(&types), None);
1500    }
1501
1502    // ── UDF type support ─────────────────────────────────────────────
1503
1504    #[test]
1505    fn test_udf_return_type() {
1506        let mut schema = setup_schema();
1507        schema.add_udf("my_func", DataType::Varchar(None));
1508        let (stmt, ann) = parse_and_annotate("SELECT my_func(id) FROM users", &schema);
1509        assert_eq!(first_col_type(&stmt, &ann), Some(DataType::Varchar(None)));
1510    }
1511
1512    // ── Annotation count ─────────────────────────────────────────────
1513
1514    #[test]
1515    fn test_annotations_not_empty() {
1516        let schema = setup_schema();
1517        let (_, ann) = parse_and_annotate("SELECT id, name FROM users WHERE age > 21", &schema);
1518        assert!(!ann.is_empty());
1519        // Should have at least the SELECT columns and WHERE predicate
1520        assert!(ann.len() >= 3);
1521    }
1522
1523    // ── SUM of DECIMAL preserves precision ───────────────────────────
1524
1525    #[test]
1526    fn test_sum_decimal_preserves_type() {
1527        let schema = setup_schema();
1528        let (stmt, ann) = parse_and_annotate("SELECT SUM(amount) FROM orders", &schema);
1529        assert_eq!(
1530            first_col_type(&stmt, &ann),
1531            Some(DataType::Decimal {
1532                precision: Some(10),
1533                scale: Some(2)
1534            })
1535        );
1536    }
1537}