polyglot-sql 0.3.3

SQL parsing, validating, formatting, and dialect translation library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//! Canonicalization Module
//!
//! This module provides functionality for converting SQL expressions into a
//! standard canonical form. This includes:
//! - Converting string addition to CONCAT
//! - Replacing date functions with casts
//! - Removing redundant type casts
//! - Ensuring boolean predicates
//! - Removing unnecessary ASC from ORDER BY
//!
//! Ported from sqlglot's optimizer/canonicalize.py

use crate::dialects::DialectType;
use crate::expressions::{DataType, Expression, Literal, Null};
use crate::helper::{is_iso_date, is_iso_datetime};

/// Converts a SQL expression into a standard canonical form.
///
/// This transformation relies on type annotations because many of the
/// conversions depend on type inference.
///
/// # Arguments
/// * `expression` - The expression to canonicalize
/// * `dialect` - Optional dialect for dialect-specific behavior
///
/// # Returns
/// The canonicalized expression
pub fn canonicalize(expression: Expression, dialect: Option<DialectType>) -> Expression {
    canonicalize_recursive(expression, dialect)
}

/// Recursively canonicalize an expression and its children
fn canonicalize_recursive(expression: Expression, dialect: Option<DialectType>) -> Expression {
    let expr = match expression {
        Expression::Select(mut select) => {
            // Canonicalize SELECT expressions
            select.expressions = select
                .expressions
                .into_iter()
                .map(|e| canonicalize_recursive(e, dialect))
                .collect();

            // Canonicalize FROM
            if let Some(mut from) = select.from {
                from.expressions = from
                    .expressions
                    .into_iter()
                    .map(|e| canonicalize_recursive(e, dialect))
                    .collect();
                select.from = Some(from);
            }

            // Canonicalize WHERE
            if let Some(mut where_clause) = select.where_clause {
                where_clause.this = canonicalize_recursive(where_clause.this, dialect);
                where_clause.this = ensure_bools(where_clause.this);
                select.where_clause = Some(where_clause);
            }

            // Canonicalize HAVING
            if let Some(mut having) = select.having {
                having.this = canonicalize_recursive(having.this, dialect);
                having.this = ensure_bools(having.this);
                select.having = Some(having);
            }

            // Canonicalize ORDER BY
            if let Some(mut order_by) = select.order_by {
                order_by.expressions = order_by
                    .expressions
                    .into_iter()
                    .map(|mut o| {
                        o.this = canonicalize_recursive(o.this, dialect);
                        o = remove_ascending_order(o);
                        o
                    })
                    .collect();
                select.order_by = Some(order_by);
            }

            // Canonicalize JOINs
            select.joins = select
                .joins
                .into_iter()
                .map(|mut j| {
                    j.this = canonicalize_recursive(j.this, dialect);
                    if let Some(on) = j.on {
                        j.on = Some(canonicalize_recursive(on, dialect));
                    }
                    j
                })
                .collect();

            Expression::Select(select)
        }

        // Binary operations that might involve string addition
        Expression::Add(bin) => {
            let left = canonicalize_recursive(bin.left, dialect);
            let right = canonicalize_recursive(bin.right, dialect);
            let result = Expression::Add(Box::new(crate::expressions::BinaryOp {
                left,
                right,
                left_comments: bin.left_comments,
                operator_comments: bin.operator_comments,
                trailing_comments: bin.trailing_comments,
                inferred_type: None,
            }));
            add_text_to_concat(result)
        }

        // Other binary operations
        Expression::And(bin) => {
            let left = ensure_bools(canonicalize_recursive(bin.left, dialect));
            let right = ensure_bools(canonicalize_recursive(bin.right, dialect));
            Expression::And(Box::new(crate::expressions::BinaryOp {
                left,
                right,
                left_comments: bin.left_comments,
                operator_comments: bin.operator_comments,
                trailing_comments: bin.trailing_comments,
                inferred_type: None,
            }))
        }
        Expression::Or(bin) => {
            let left = ensure_bools(canonicalize_recursive(bin.left, dialect));
            let right = ensure_bools(canonicalize_recursive(bin.right, dialect));
            Expression::Or(Box::new(crate::expressions::BinaryOp {
                left,
                right,
                left_comments: bin.left_comments,
                operator_comments: bin.operator_comments,
                trailing_comments: bin.trailing_comments,
                inferred_type: None,
            }))
        }

        Expression::Not(un) => {
            let inner = ensure_bools(canonicalize_recursive(un.this, dialect));
            Expression::Not(Box::new(crate::expressions::UnaryOp {
                this: inner,
                inferred_type: None,
            }))
        }

        // Comparison operations - check for date coercion
        Expression::Eq(bin) => canonicalize_comparison(Expression::Eq, *bin, dialect),
        Expression::Neq(bin) => canonicalize_comparison(Expression::Neq, *bin, dialect),
        Expression::Lt(bin) => canonicalize_comparison(Expression::Lt, *bin, dialect),
        Expression::Lte(bin) => canonicalize_comparison(Expression::Lte, *bin, dialect),
        Expression::Gt(bin) => canonicalize_comparison(Expression::Gt, *bin, dialect),
        Expression::Gte(bin) => canonicalize_comparison(Expression::Gte, *bin, dialect),

        Expression::Sub(bin) => canonicalize_comparison(Expression::Sub, *bin, dialect),
        Expression::Mul(bin) => canonicalize_binary(Expression::Mul, *bin, dialect),
        Expression::Div(bin) => canonicalize_binary(Expression::Div, *bin, dialect),

        // Cast - check for redundancy
        Expression::Cast(cast) => {
            let inner = canonicalize_recursive(cast.this, dialect);
            let result = Expression::Cast(Box::new(crate::expressions::Cast {
                this: inner,
                to: cast.to,
                trailing_comments: cast.trailing_comments,
                double_colon_syntax: cast.double_colon_syntax,
                format: cast.format,
                default: cast.default,
                inferred_type: None,
            }));
            remove_redundant_casts(result)
        }

        // Function expressions
        Expression::Function(func) => {
            let args = func
                .args
                .into_iter()
                .map(|e| canonicalize_recursive(e, dialect))
                .collect();
            Expression::Function(Box::new(crate::expressions::Function {
                name: func.name,
                args,
                distinct: func.distinct,
                trailing_comments: func.trailing_comments,
                use_bracket_syntax: func.use_bracket_syntax,
                no_parens: func.no_parens,
                quoted: func.quoted,
                span: None,
                inferred_type: None,
            }))
        }

        Expression::AggregateFunction(agg) => {
            let args = agg
                .args
                .into_iter()
                .map(|e| canonicalize_recursive(e, dialect))
                .collect();
            Expression::AggregateFunction(Box::new(crate::expressions::AggregateFunction {
                name: agg.name,
                args,
                distinct: agg.distinct,
                filter: agg.filter.map(|f| canonicalize_recursive(f, dialect)),
                order_by: agg.order_by,
                limit: agg.limit,
                ignore_nulls: agg.ignore_nulls,
                inferred_type: None,
            }))
        }

        // Alias
        Expression::Alias(alias) => {
            let inner = canonicalize_recursive(alias.this, dialect);
            Expression::Alias(Box::new(crate::expressions::Alias {
                this: inner,
                alias: alias.alias,
                column_aliases: alias.column_aliases,
                pre_alias_comments: alias.pre_alias_comments,
                trailing_comments: alias.trailing_comments,
                inferred_type: None,
            }))
        }

        // Paren
        Expression::Paren(paren) => {
            let inner = canonicalize_recursive(paren.this, dialect);
            Expression::Paren(Box::new(crate::expressions::Paren {
                this: inner,
                trailing_comments: paren.trailing_comments,
            }))
        }

        // Case
        Expression::Case(case) => {
            let operand = case.operand.map(|e| canonicalize_recursive(e, dialect));
            let whens = case
                .whens
                .into_iter()
                .map(|(w, t)| {
                    (
                        canonicalize_recursive(w, dialect),
                        canonicalize_recursive(t, dialect),
                    )
                })
                .collect();
            let else_ = case.else_.map(|e| canonicalize_recursive(e, dialect));
            Expression::Case(Box::new(crate::expressions::Case {
                operand,
                whens,
                else_,
                comments: Vec::new(),
                inferred_type: None,
            }))
        }

        // Between - check for date coercion
        Expression::Between(between) => {
            let this = canonicalize_recursive(between.this, dialect);
            let low = canonicalize_recursive(between.low, dialect);
            let high = canonicalize_recursive(between.high, dialect);
            Expression::Between(Box::new(crate::expressions::Between {
                this,
                low,
                high,
                not: between.not,
                symmetric: between.symmetric,
            }))
        }

        // In
        Expression::In(in_expr) => {
            let this = canonicalize_recursive(in_expr.this, dialect);
            let expressions = in_expr
                .expressions
                .into_iter()
                .map(|e| canonicalize_recursive(e, dialect))
                .collect();
            let query = in_expr.query.map(|q| canonicalize_recursive(q, dialect));
            Expression::In(Box::new(crate::expressions::In {
                this,
                expressions,
                query,
                not: in_expr.not,
                global: in_expr.global,
                unnest: in_expr.unnest,
                is_field: in_expr.is_field,
            }))
        }

        // Subquery
        Expression::Subquery(subquery) => {
            let this = canonicalize_recursive(subquery.this, dialect);
            Expression::Subquery(Box::new(crate::expressions::Subquery {
                this,
                alias: subquery.alias,
                column_aliases: subquery.column_aliases,
                order_by: subquery.order_by,
                limit: subquery.limit,
                offset: subquery.offset,
                distribute_by: subquery.distribute_by,
                sort_by: subquery.sort_by,
                cluster_by: subquery.cluster_by,
                lateral: subquery.lateral,
                modifiers_inside: subquery.modifiers_inside,
                trailing_comments: subquery.trailing_comments,
                inferred_type: None,
            }))
        }

        // Set operations
        Expression::Union(union) => {
            let mut u = *union;
            let left = std::mem::replace(&mut u.left, Expression::Null(Null));
            u.left = canonicalize_recursive(left, dialect);
            let right = std::mem::replace(&mut u.right, Expression::Null(Null));
            u.right = canonicalize_recursive(right, dialect);
            Expression::Union(Box::new(u))
        }
        Expression::Intersect(intersect) => {
            let mut i = *intersect;
            let left = std::mem::replace(&mut i.left, Expression::Null(Null));
            i.left = canonicalize_recursive(left, dialect);
            let right = std::mem::replace(&mut i.right, Expression::Null(Null));
            i.right = canonicalize_recursive(right, dialect);
            Expression::Intersect(Box::new(i))
        }
        Expression::Except(except) => {
            let mut e = *except;
            let left = std::mem::replace(&mut e.left, Expression::Null(Null));
            e.left = canonicalize_recursive(left, dialect);
            let right = std::mem::replace(&mut e.right, Expression::Null(Null));
            e.right = canonicalize_recursive(right, dialect);
            Expression::Except(Box::new(e))
        }

        // Leaf nodes - return unchanged
        other => other,
    };

    expr
}

/// Convert string addition to CONCAT.
///
/// When two TEXT types are added with +, convert to CONCAT.
/// This is used by dialects like T-SQL and Redshift.
fn add_text_to_concat(expression: Expression) -> Expression {
    // In a full implementation, we would check if the operands are TEXT types
    // and convert to CONCAT. For now, we return unchanged.
    expression
}

/// Remove redundant cast expressions.
///
/// If casting to the same type the expression already is, remove the cast.
fn remove_redundant_casts(expression: Expression) -> Expression {
    if let Expression::Cast(cast) = &expression {
        // Check if the inner expression's type matches the cast target
        // In a full implementation with type annotations, we would compare types
        // For now, just check simple cases

        // If casting a literal to its natural type, we might be able to simplify
        if let Expression::Literal(lit) = &cast.this {
            if let Literal::String(_) = lit.as_ref() {
                if matches!(&cast.to, DataType::VarChar { .. } | DataType::Text) {
                    return cast.this.clone();
                }
            }
        }
        if let Expression::Literal(lit) = &cast.this {
            if let Literal::Number(_) = lit.as_ref() {
                if matches!(
                    &cast.to,
                    DataType::Int { .. }
                        | DataType::BigInt { .. }
                        | DataType::Decimal { .. }
                        | DataType::Float { .. }
                ) {
                    // Could potentially remove cast, but be conservative
                }
            }
        }
    }
    expression
}

/// Ensure expressions used as boolean predicates are actually boolean.
///
/// For example, in some dialects, integers can be used as booleans.
/// This function ensures proper boolean semantics.
fn ensure_bools(expression: Expression) -> Expression {
    // In a full implementation, we would check if the expression is an integer
    // and convert it to a comparison (e.g., x != 0).
    // For now, return unchanged.
    expression
}

/// Remove explicit ASC from ORDER BY clauses.
///
/// Since ASC is the default, `ORDER BY a ASC` can be simplified to `ORDER BY a`.
fn remove_ascending_order(mut ordered: crate::expressions::Ordered) -> crate::expressions::Ordered {
    // If ASC was explicitly written (not DESC), remove the explicit flag
    // since ASC is the default ordering
    if !ordered.desc && ordered.explicit_asc {
        ordered.explicit_asc = false;
    }
    ordered
}

/// Canonicalize a binary comparison operation.
fn canonicalize_comparison<F>(
    constructor: F,
    bin: crate::expressions::BinaryOp,
    dialect: Option<DialectType>,
) -> Expression
where
    F: FnOnce(Box<crate::expressions::BinaryOp>) -> Expression,
{
    let left = canonicalize_recursive(bin.left, dialect);
    let right = canonicalize_recursive(bin.right, dialect);

    // Check for date coercion opportunities
    let (left, right) = coerce_date_operands(left, right);

    constructor(Box::new(crate::expressions::BinaryOp {
        left,
        right,
        left_comments: bin.left_comments,
        operator_comments: bin.operator_comments,
        trailing_comments: bin.trailing_comments,
        inferred_type: None,
    }))
}

/// Canonicalize a regular binary operation.
fn canonicalize_binary<F>(
    constructor: F,
    bin: crate::expressions::BinaryOp,
    dialect: Option<DialectType>,
) -> Expression
where
    F: FnOnce(Box<crate::expressions::BinaryOp>) -> Expression,
{
    let left = canonicalize_recursive(bin.left, dialect);
    let right = canonicalize_recursive(bin.right, dialect);

    constructor(Box::new(crate::expressions::BinaryOp {
        left,
        right,
        left_comments: bin.left_comments,
        operator_comments: bin.operator_comments,
        trailing_comments: bin.trailing_comments,
        inferred_type: None,
    }))
}

/// Coerce date operands in comparisons.
///
/// When comparing a date/datetime column with a string literal,
/// add appropriate CAST to the string.
fn coerce_date_operands(left: Expression, right: Expression) -> (Expression, Expression) {
    // Check if we should cast string literals to date/datetime
    let left = coerce_date_string(left, &right);
    let right = coerce_date_string(right, &left);
    (left, right)
}

/// Coerce a string literal to date/datetime if comparing with a temporal type.
fn coerce_date_string(expr: Expression, _other: &Expression) -> Expression {
    if let Expression::Literal(ref lit) = expr {
        if let Literal::String(ref s) = lit.as_ref() {
            // Check if the string is an ISO date or datetime
            if is_iso_date(s) {
                // In a full implementation, we would add CAST to DATE
                // For now, return unchanged
            } else if is_iso_datetime(s) {
                // In a full implementation, we would add CAST to DATETIME/TIMESTAMP
                // For now, return unchanged
            }
        }
    }
    expr
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::generator::Generator;
    use crate::parser::Parser;

    fn gen(expr: &Expression) -> String {
        Generator::new().generate(expr).unwrap()
    }

    fn parse(sql: &str) -> Expression {
        Parser::parse_sql(sql).expect("Failed to parse")[0].clone()
    }

    #[test]
    fn test_canonicalize_simple() {
        let expr = parse("SELECT a FROM t");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("SELECT"));
    }

    #[test]
    fn test_canonicalize_preserves_structure() {
        let expr = parse("SELECT a, b FROM t WHERE c = 1");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("WHERE"));
    }

    #[test]
    fn test_canonicalize_and_or() {
        let expr = parse("SELECT 1 WHERE a AND b OR c");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("AND") || sql.contains("OR"));
    }

    #[test]
    fn test_canonicalize_comparison() {
        let expr = parse("SELECT 1 WHERE a = 1 AND b > 2");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("=") && sql.contains(">"));
    }

    #[test]
    fn test_canonicalize_case() {
        let expr = parse("SELECT CASE WHEN a = 1 THEN 'yes' ELSE 'no' END FROM t");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("CASE") && sql.contains("WHEN"));
    }

    #[test]
    fn test_canonicalize_subquery() {
        let expr = parse("SELECT a FROM (SELECT b FROM t) AS sub");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("SELECT") && sql.contains("sub"));
    }

    #[test]
    fn test_canonicalize_order_by() {
        let expr = parse("SELECT a FROM t ORDER BY a");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("ORDER BY"));
    }

    #[test]
    fn test_canonicalize_union() {
        let expr = parse("SELECT a FROM t UNION SELECT b FROM s");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("UNION"));
    }

    #[test]
    fn test_add_text_to_concat_passthrough() {
        // Test that non-text additions pass through
        let expr = parse("SELECT 1 + 2");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("+"));
    }

    #[test]
    fn test_canonicalize_function() {
        let expr = parse("SELECT MAX(a) FROM t");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("MAX"));
    }

    #[test]
    fn test_canonicalize_between() {
        let expr = parse("SELECT 1 WHERE a BETWEEN 1 AND 10");
        let result = canonicalize(expr, None);
        let sql = gen(&result);
        assert!(sql.contains("BETWEEN"));
    }
}