polyglot-sql 0.13.1

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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
//! Rewrites for Vertica-specific source semantics.
//!
//! Vertica shares most of its surface syntax with PostgreSQL, but a few functions
//! carry Vertica-only meaning that must be lowered before a foreign target sees them.

use super::*;
use crate::ast_transforms::{output_identifier, query_projections, AstNames};
use crate::expressions::{
    AggFunc, AtTimeZone, DateAddFunc, Interval, IntervalUnit, IntervalUnitSpec, VarArgFunc,
};

/// Validate before source transforms erase parameters or failure semantics.
/// These are correctness errors, independently of the diagnostic verbosity.
pub(in crate::dialects) fn validate_conversion(
    expression: &Expression,
    source: DialectType,
    target: DialectType,
) -> Result<()> {
    if source == DialectType::Vertica || target == DialectType::Vertica {
        for node in expression.dfs() {
            if let Expression::Select(select) = node {
                for lock in &select.locks {
                    if target == DialectType::Vertica
                        && (lock.update.is_none() || lock.key.is_some() || lock.wait.is_some())
                    {
                        return Err(crate::error::Error::unsupported(
                            "Vertica supports only FOR UPDATE [OF tables]",
                            target.to_string(),
                        ));
                    }
                    if source != target {
                        return Err(crate::error::Error::unsupported(
                            "Vertica locking semantics",
                            target.to_string(),
                        ));
                    }
                }
            }
        }
    }
    if source == target || (source != DialectType::Vertica && target != DialectType::Vertica) {
        return Ok(());
    }
    for node in expression.dfs() {
        if target == DialectType::Vertica {
            match node {
                Expression::Cast(cast) => {
                    crate::generator::Generator::validate_vertica_cast_type(&cast.to)?;
                }
                Expression::Function(function) => {
                    crate::generator::Generator::validate_vertica_function(function, Some(source))?;
                }
                _ => {}
            }
        }
        let feature = match node {
            Expression::Select(select)
                if source == DialectType::Vertica
                    && select
                        .vertica
                        .as_ref()
                        .is_some_and(|v| v.timeseries.is_some() || v.match_clause.is_some()) =>
            {
                Some("Vertica event-series query")
            }
            Expression::CreateTable(table)
                if source == DialectType::Vertica
                    && table.columns.iter().any(|column| column.encoding.is_some()) =>
            {
                Some("Vertica column encoding")
            }
            Expression::Subscript(_) | Expression::ArraySlice(_)
                if target == DialectType::Vertica =>
            {
                Some("array index and bound semantics when targeting Vertica")
            }
            Expression::Vertica(v)
                if source == DialectType::Vertica
                    && !matches!(
                        v.as_ref(),
                        crate::expressions::VerticaExpression::Binary { .. }
                            | crate::expressions::VerticaExpression::ArrayAccess { .. }
                            | crate::expressions::VerticaExpression::ArraySlice { .. }
                    ) =>
            {
                Some("native Vertica expression")
            }
            Expression::Function(f)
                if source == DialectType::Vertica
                    && !f.quoted
                    && matches!(
                        f.name.to_ascii_uppercase().as_str(),
                        "TIME_SLICE"
                            | "ROW"
                            | "MATCH_COLUMNS"
                            | "CONDITIONAL_TRUE_EVENT"
                            | "CONDITIONAL_CHANGE_EVENT"
                            | "APPROXIMATE_PERCENTILE"
                            | "REGEXP_SUBSTR"
                    ) =>
            {
                Some("Vertica function semantics without a verified target mapping")
            }
            Expression::Function(f)
                if source == DialectType::Vertica
                    && target == DialectType::DuckDB
                    && !f.quoted
                    && f.args.is_empty()
                    && matches!(
                        f.name.to_ascii_uppercase().as_str(),
                        "GETDATE" | "GETUTCDATE" | "SYSDATE"
                    ) =>
            {
                Some("Vertica statement-start timestamps have no verified DuckDB equivalent")
            }
            Expression::CurrentTimestamp(ts)
                if source == DialectType::Vertica
                    && target == DialectType::DuckDB
                    && ts.sysdate =>
            {
                Some("Vertica statement-start timestamps have no verified DuckDB equivalent")
            }
            Expression::Raw(_) | Expression::Command(_) if source == DialectType::Vertica => {
                Some("unstructured Vertica statement")
            }
            Expression::Select(select)
                if source == DialectType::Vertica && select.hint.is_some() =>
            {
                Some("Vertica optimizer hint")
            }
            Expression::ListAgg(_) => Some("Vertica LISTAGG byte limit and overflow behavior"),
            Expression::StringAgg(_) | Expression::GroupConcat(_)
                if target == DialectType::Vertica =>
            {
                Some("string aggregation without Vertica's byte limit and overflow behavior")
            }
            Expression::TryCast(_) | Expression::SafeCast(_) if target == DialectType::Vertica => {
                Some("safe casts: Vertica ::! does not suppress constant cast failures")
            }
            _ => None,
        };
        if let Some(feature) = feature {
            return Err(crate::error::Error::unsupported(
                feature,
                target.to_string(),
            ));
        }
    }
    Ok(())
}

fn known_local_datetime(expression: &Expression) -> bool {
    match expression {
        Expression::Cast(c) => matches!(
            c.to,
            DataType::Date
                | DataType::Timestamp {
                    timezone: false,
                    ..
                }
        ),
        Expression::Literal(l) => matches!(l.as_ref(), Literal::Date(_) | Literal::Timestamp(_)),
        _ => matches!(
            expression.inferred_type(),
            Some(
                DataType::Date
                    | DataType::Timestamp {
                        timezone: false,
                        ..
                    }
            )
        ),
    }
}

/// Resolve defaults while the enclosing ordering context is still available.
pub(in crate::dialects) fn prepare_conversion(
    expr: Expression,
    source: DialectType,
    target: DialectType,
) -> Result<Expression> {
    // BigQuery's COUNTIF returns zero for an empty input/frame. Preserve that
    // before the source transform converts it to a generic function call.
    if source == DialectType::BigQuery && target == DialectType::Vertica {
        return transform_recursive(expr, &|node| match node {
            Expression::CountIf(mut count) => {
                if !count.order_by.is_empty()
                    || count.limit.is_some()
                    || count.having_max.is_some()
                    || count.ignore_nulls.is_some()
                {
                    return Err(crate::error::Error::unsupported(
                        "COUNTIF modifiers",
                        "vertica",
                    ));
                }
                let mut predicate = count.this;
                if let Some(filter) = count.filter.take() {
                    predicate = Expression::And(Box::new(BinaryOp::new(predicate, filter)));
                }
                count.this = Expression::Case(Box::new(crate::expressions::Case {
                    operand: None,
                    whens: vec![(predicate, Expression::number(1))],
                    else_: None,
                    comments: Vec::new(),
                    inferred_type: None,
                }));
                Ok(Expression::Count(Box::new(crate::expressions::CountFunc {
                    this: Some(count.this),
                    star: false,
                    distinct: count.distinct,
                    filter: None,
                    ignore_nulls: None,
                    original_name: None,
                    inferred_type: count.inferred_type,
                })))
            }
            other => Ok(other),
        });
    }
    if source != DialectType::Vertica || source == target {
        return Ok(expr);
    }
    fn analytic(ordered: &mut crate::expressions::Ordered, target: DialectType) -> Result<()> {
        if ordered.nulls_auto {
            return Err(crate::error::Error::unsupported(
                "Vertica NULLS AUTO",
                target.to_string(),
            ));
        }
        ordered.nulls_first.get_or_insert(ordered.desc);
        Ok(())
    }
    transform_recursive(expr, &|mut node| {
        match &mut node {
            Expression::WindowFunction(window) => {
                for order in &mut window.over.order_by {
                    analytic(order, target)?;
                }
            }
            Expression::WithinGroup(group) => {
                for order in &mut group.order_by {
                    analytic(order, target)?;
                }
            }
            Expression::Select(select) => {
                if let Some(windows) = &mut select.windows {
                    for window in windows {
                        for order in &mut window.spec.order_by {
                            analytic(order, target)?;
                        }
                    }
                }
                if let Some(over) = select.vertica.as_mut().and_then(|v| v.limit_over.as_mut()) {
                    for order in &mut over.order_by {
                        analytic(order, target)?;
                    }
                }
                if let Some(order) = &mut select.order_by {
                    prepare_select_order(order, &select.expressions, target)?;
                }
            }
            Expression::Union(set) => {
                prepare_set_order(&mut set.order_by, &set.left, &set.right, target)?
            }
            Expression::Intersect(set) => {
                prepare_set_order(&mut set.order_by, &set.left, &set.right, target)?
            }
            Expression::Except(set) => {
                prepare_set_order(&mut set.order_by, &set.left, &set.right, target)?
            }
            Expression::Subquery(sub) => {
                if let Some(order) = &mut sub.order_by {
                    prepare_query_order(order, &[&sub.this], target)?;
                }
            }
            _ => {}
        }
        Ok(node)
    })
}

fn unknown_order(target: DialectType) -> crate::error::Error {
    crate::error::Error::unsupported(
        "Vertica ORDER BY requires a known, unambiguous sort-key type",
        target.to_string(),
    )
}

fn projection_index(key: &Expression, projections: &[Expression]) -> Option<usize> {
    match key {
        Expression::Literal(lit) => match lit.as_ref() {
            Literal::Number(n) => n
                .parse::<usize>()
                .ok()
                .and_then(|n| n.checked_sub(1))
                .filter(|&i| i < projections.len()),
            _ => None,
        },
        Expression::Column(c) if c.table.is_none() => {
            let mut matches = projections.iter().enumerate().filter(|(_, p)| {
                output_identifier(p).is_some_and(|n| n.name.eq_ignore_ascii_case(&c.name.name))
            });
            let first = matches.next()?.0;
            matches.next().is_none().then_some(first)
        }
        _ => None,
    }
}

fn nulls_low(mut key: &Expression) -> Option<bool> {
    while let Expression::Alias(a) = key {
        key = &a.this;
    }
    if let Expression::Paren(p) = key {
        return nulls_low(&p.this);
    }
    let data_type = match key {
        Expression::Cast(c) => Some(&c.to),
        _ => key.inferred_type(),
    };
    match data_type {
        Some(
            DataType::Int { .. }
            | DataType::BigInt { .. }
            | DataType::SmallInt { .. }
            | DataType::TinyInt { .. }
            | DataType::Date
            | DataType::Time { .. }
            | DataType::Timestamp { .. },
        ) => Some(true),
        Some(
            DataType::Float { .. }
            | DataType::Double { .. }
            | DataType::Boolean
            | DataType::Char { .. }
            | DataType::VarChar { .. }
            | DataType::Text
            | DataType::String { .. }
            | DataType::Array { .. },
        ) => Some(false),
        _ => match key {
            Expression::Literal(l) => match l.as_ref() {
                Literal::Number(n) => Some(n.parse::<i64>().is_ok()),
                Literal::String(_) => Some(false),
                Literal::Date(_) | Literal::Timestamp(_) => Some(true),
                _ => None,
            },
            Expression::Boolean(_) | Expression::Array(_) => Some(false),
            _ => None,
        },
    }
}

fn prepare_select_order(
    order: &mut crate::expressions::OrderBy,
    projections: &[Expression],
    target: DialectType,
) -> Result<()> {
    for ordered in &mut order.expressions {
        if ordered.nulls_auto {
            return Err(unknown_order(target));
        }
        if ordered.nulls_first.is_some() {
            continue;
        }
        let key =
            projection_index(&ordered.this, projections).map_or(&ordered.this, |i| &projections[i]);
        let low = nulls_low(key).ok_or_else(|| unknown_order(target))?;
        ordered.nulls_first = Some(low != ordered.desc);
    }
    Ok(())
}

fn query_nulls_low(query: &Expression, index: usize) -> Option<bool> {
    let mut pending = vec![query];
    let mut low = None;
    while let Some(query) = pending.pop() {
        let (left, right) = match query {
            Expression::Select(s) => {
                let current = nulls_low(s.expressions.get(index)?)?;
                if low.is_some_and(|low| low != current) {
                    return None;
                }
                low = Some(current);
                continue;
            }
            Expression::Subquery(s) => {
                pending.push(&s.this);
                continue;
            }
            Expression::Paren(p) => {
                pending.push(&p.this);
                continue;
            }
            Expression::Union(s) if !s.by_name && !s.corresponding => (&s.left, &s.right),
            Expression::Intersect(s) if !s.by_name && !s.corresponding => (&s.left, &s.right),
            Expression::Except(s) if !s.by_name && !s.corresponding => (&s.left, &s.right),
            _ => return None,
        };
        pending.extend([left, right]);
    }
    low
}

fn prepare_query_order(
    order: &mut crate::expressions::OrderBy,
    queries: &[&Expression],
    target: DialectType,
) -> Result<()> {
    let projections = query_projections(queries[0]).ok_or_else(|| unknown_order(target))?;
    for ordered in &mut order.expressions {
        if ordered.nulls_auto {
            return Err(unknown_order(target));
        }
        if ordered.nulls_first.is_some() {
            continue;
        }
        let index =
            projection_index(&ordered.this, projections).ok_or_else(|| unknown_order(target))?;
        let low = query_nulls_low(queries[0], index).ok_or_else(|| unknown_order(target))?;
        if queries
            .iter()
            .skip(1)
            .any(|q| query_nulls_low(q, index) != Some(low))
        {
            return Err(unknown_order(target));
        }
        ordered.nulls_first = Some(low != ordered.desc);
    }
    Ok(())
}

fn prepare_set_order(
    order: &mut Option<crate::expressions::OrderBy>,
    left: &Expression,
    right: &Expression,
    target: DialectType,
) -> Result<()> {
    if let Some(order) = order {
        prepare_query_order(order, &[left, right], target)?;
    }
    Ok(())
}

/// Rewrite a single node parsed as Vertica for a non-Vertica target.
pub(super) fn normalize_from_vertica(e: Expression, target: DialectType) -> Result<Expression> {
    match e {
        Expression::Select(select)
            if select
                .vertica
                .as_ref()
                .is_some_and(|v| v.limit_over.is_some()) =>
        {
            lower_partitioned_limit(*select, target)
        }
        Expression::DateDiff(diff) => {
            let unit = diff.unit.ok_or_else(|| {
                crate::error::Error::unsupported(
                    "Vertica DATEDIFF dynamic unit",
                    target.to_string(),
                )
            })?;
            if unit == IntervalUnit::Week {
                return Err(crate::error::Error::unsupported(
                    "Vertica DATEDIFF week cutoff requires engine verification",
                    target.to_string(),
                ));
            }
            if !known_local_datetime(&diff.this) || !known_local_datetime(&diff.expression) {
                return Err(crate::error::Error::unsupported(
                    "Vertica DATEDIFF requires known DATE or TIMESTAMP WITHOUT TIME ZONE operands",
                    target.to_string(),
                ));
            }
            Ok(Expression::Vertica(Box::new(
                crate::expressions::VerticaExpression::BoundaryDateDiff {
                    start: diff.expression,
                    end: diff.this,
                    unit,
                },
            )))
        }
        Expression::Function(f) if f.args.is_empty() && !f.quoted => {
            match f.name.to_ascii_uppercase().as_str() {
                // GETDATE() and SYSDATE are the statement-start local timestamp, not the
                // transaction-start CURRENT_TIMESTAMP.
                "GETDATE" | "SYSDATE" => {
                    Ok(statement_timestamp(target, false).unwrap_or(Expression::Function(f)))
                }
                "GETUTCDATE" => {
                    Ok(statement_timestamp(target, true).unwrap_or(Expression::Function(f)))
                }
                _ => Ok(Expression::Function(f)),
            }
        }
        Expression::CurrentTimestamp(ts) if ts.sysdate => {
            Ok(statement_timestamp(target, false).unwrap_or(Expression::CurrentTimestamp(ts)))
        }
        Expression::Function(f) if !f.quoted => {
            match (f.name.to_ascii_uppercase().as_str(), f.args.len()) {
                ("DATEDIFF" | "TIMESTAMPDIFF", 3) => {
                    let unit = timestamp_unit(&f.args[0]).ok_or_else(|| {
                        crate::error::Error::unsupported(
                            "Vertica DATEDIFF dynamic unit",
                            target.to_string(),
                        )
                    })?;
                    normalize_from_vertica(
                        Expression::DateDiff(Box::new(crate::expressions::DateDiffFunc {
                            this: f.args[2].clone(),
                            expression: f.args[1].clone(),
                            unit: Some(unit),
                        })),
                        target,
                    )
                }
                // ZEROIFNULL(x) -> COALESCE(x, 0), except where it is native
                ("NULLIFZERO", 1) => Ok(Expression::NullIf(Box::new(
                    crate::expressions::BinaryFunc {
                        original_name: None,
                        this: f.args[0].clone(),
                        expression: Expression::number(0),
                        inferred_type: None,
                    },
                ))),
                ("ZEROIFNULL", 1) if !matches!(target, DialectType::Snowflake) => {
                    let mut args = f.args;
                    args.push(Expression::number(0));
                    Ok(Expression::Coalesce(Box::new(VarArgFunc {
                        original_name: None,
                        expressions: args,
                        inferred_type: None,
                    })))
                }
                // TIMESTAMPADD(unit, n, ts) -> the portable date-add node
                ("TIMESTAMPADD", 3) => match timestamp_unit(&f.args[0]) {
                    Some(unit) => {
                        let mut args = f.args;
                        let this = args.pop().unwrap();
                        let interval = args.pop().unwrap();
                        if is_postgres_family(target) {
                            return Ok(postgres_interval_add(this, interval, unit));
                        }
                        Ok(Expression::DateAdd(Box::new(DateAddFunc {
                            this,
                            interval,
                            unit,
                        })))
                    }
                    None => Ok(Expression::Function(f)),
                },
                // APPROXIMATE_COUNT_DISTINCT(x) -> the portable approximate-distinct node
                ("APPROXIMATE_COUNT_DISTINCT", 1) => {
                    let this = f.args.into_iter().next().unwrap();
                    Ok(Expression::ApproxDistinct(Box::new(AggFunc {
                        this,
                        distinct: false,
                        filter: None,
                        order_by: Vec::new(),
                        name: None,
                        ignore_nulls: None,
                        having_max: None,
                        limit: None,
                        inferred_type: None,
                    })))
                }
                _ => Ok(Expression::Function(f)),
            }
        }
        other => Ok(other),
    }
}

/// Vertica statement-start timestamps, for targets that can express them.
fn statement_timestamp(target: DialectType, utc: bool) -> Option<Expression> {
    let timestamp = |this: Expression| {
        Expression::Cast(Box::new(Cast {
            this,
            to: DataType::Timestamp {
                precision: None,
                timezone: false,
            },
            trailing_comments: Vec::new(),
            double_colon_syntax: false,
            format: None,
            default: None,
            inferred_type: None,
        }))
    };
    let at_utc = |this: Expression| {
        Expression::AtTimeZone(Box::new(AtTimeZone {
            this,
            zone: Expression::string("UTC"),
        }))
    };
    match target {
        // PostgreSQL has a true statement-start clock
        DialectType::PostgreSQL => {
            let now = Expression::Function(Box::new(Function::new(
                "STATEMENT_TIMESTAMP".to_string(),
                vec![],
            )));
            Some(timestamp(if utc { at_utc(now) } else { now }))
        }
        // These targets have the same statement-start functions natively
        DialectType::TSQL | DialectType::Fabric => None,
        DialectType::Redshift if !utc => None,
        _ if utc => Some(timestamp(at_utc(Expression::CurrentTimestamp(
            crate::expressions::CurrentTimestamp {
                precision: None,
                sysdate: false,
            },
        )))),
        _ => None,
    }
}

/// Vertica datetime units accepted by TIMESTAMPADD, including the ODBC SQL_TSI_ forms.
fn timestamp_unit(unit: &Expression) -> Option<IntervalUnit> {
    let name = temporal::get_unit_str_static(unit);
    Some(match name.trim_start_matches("SQL_TSI_") {
        "YEAR" | "YEARS" | "YY" | "YYYY" => IntervalUnit::Year,
        "QUARTER" | "QUARTERS" | "QQ" | "Q" => IntervalUnit::Quarter,
        "MONTH" | "MONTHS" | "MM" | "M" => IntervalUnit::Month,
        "WEEK" | "WEEKS" | "WK" | "WW" => IntervalUnit::Week,
        "DAY" | "DAYS" | "DD" | "D" | "DAYOFYEAR" | "DY" | "Y" => IntervalUnit::Day,
        "HOUR" | "HOURS" | "HH" => IntervalUnit::Hour,
        "MINUTE" | "MINUTES" | "MI" | "N" => IntervalUnit::Minute,
        "SECOND" | "SECONDS" | "SS" | "S" => IntervalUnit::Second,
        "MILLISECOND" | "MILLISECONDS" | "MS" => IntervalUnit::Millisecond,
        "MICROSECOND" | "MICROSECONDS" | "US" | "MCS" => IntervalUnit::Microsecond,
        _ => return None,
    })
}

fn is_postgres_family(target: DialectType) -> bool {
    matches!(
        target,
        DialectType::PostgreSQL
            | DialectType::Materialize
            | DialectType::RisingWave
            | DialectType::CockroachDB
    )
}

/// `ts + INTERVAL 'n UNIT'` for literal amounts, `ts + INTERVAL '1 UNIT' * n` otherwise.
fn postgres_interval_add(ts: Expression, amount: Expression, unit: IntervalUnit) -> Expression {
    let interval = |value: String| {
        Expression::Interval(Box::new(Interval {
            this: Some(Expression::string(&value)),
            unit: Some(IntervalUnitSpec::Simple {
                unit,
                use_plural: false,
            }),
        }))
    };
    let offset = match amount {
        Expression::Literal(ref lit) if matches!(lit.as_ref(), Literal::Number(_)) => {
            let Literal::Number(n) = lit.as_ref() else {
                unreachable!()
            };
            interval(n.clone())
        }
        other => Expression::Mul(Box::new(BinaryOp::new(interval("1".to_string()), other))),
    };
    Expression::Add(Box::new(BinaryOp::new(ts, offset)))
}

fn lower_partitioned_limit(
    mut base: crate::expressions::Select,
    target: DialectType,
) -> Result<Expression> {
    use crate::expressions::{Alias, From, Select, Where, WindowFunction};
    let unsupported = || {
        crate::error::Error::unsupported("Vertica partitioned LIMIT requires named outputs, resolvable ordering and no locking or event-series clauses", target.to_string())
    };
    if !matches!(target, DialectType::PostgreSQL | DialectType::DuckDB)
        || !base.locks.is_empty()
        || base.into.is_some()
        || base.offset.is_some()
    {
        return Err(unsupported());
    }
    let extension = base.vertica.take().ok_or_else(unsupported)?;
    if extension.timeseries.is_some() || extension.match_clause.is_some() {
        return Err(unsupported());
    }
    let mut over = extension.limit_over.ok_or_else(unsupported)?;
    if over.partition_by.is_empty()
        || over.order_by.is_empty()
        || over.frame.is_some()
        || over.window_name.is_some()
    {
        return Err(unsupported());
    }
    let limit = base.limit.take().ok_or_else(unsupported)?;
    if !matches!(&limit.this, Expression::Literal(lit) if matches!(lit.as_ref(), Literal::Number(n) if n.parse::<u64>().is_ok_and(|n| n > 0)))
    {
        return Err(unsupported());
    }
    let mut outer_order = base.order_by.take();
    let outer_offset = base.offset.take();
    let outer_with = base.with.take();
    let outer_comments = std::mem::take(&mut base.leading_comments);
    let original = base.expressions.clone();
    let mut names = AstNames::default();
    // Collect identifiers directly, without cloning/serializing the complete AST.
    let base_node = Expression::Select(Box::new(base));
    names.collect(&base_node);
    for key in &over.partition_by {
        names.collect(key);
    }
    for key in &over.order_by {
        names.collect(&key.this);
    }
    if let Some(order) = &outer_order {
        for key in &order.expressions {
            names.collect(&key.this);
        }
    }
    let Expression::Select(base_box) = base_node else {
        unreachable!()
    };
    let mut base = *base_box;
    let source_alias = names.fresh("_vertica_source");
    let ranked_alias = names.fresh("_vertica_ranked");
    let rank_name = names.fresh("_vertica_row_number");
    let mut output_names = Vec::new();
    let mut internal_names = Vec::new();
    let mut visible_names = std::collections::HashSet::new();
    // Preserve the base query's aliases: GROUP BY, WHERE, HAVING and later
    // projections may still refer to them in the original scope.
    for expression in &original {
        let name = output_identifier(expression)
            .ok_or_else(unsupported)?
            .clone();
        if !visible_names.insert(name.name.to_ascii_lowercase()) {
            return Err(unsupported());
        }
        output_names.push(name.clone());
        internal_names.push(name);
    }
    let references_alias = |expression: &Expression| {
        expression.dfs().any(|node| {
            let Expression::Column(c) = node else {
                return false;
            };
            c.table.is_none()
                && original.iter().any(|p| {
                    matches!(p, Expression::Alias(a) if a.alias.name.eq_ignore_ascii_case(&c.name.name))
                })
        })
    };
    // PostgreSQL does not accept SELECT aliases in predicates. Without schema
    // information alias/input-name collisions cannot be resolved safely.
    if target == DialectType::PostgreSQL
        && (base
            .where_clause
            .as_ref()
            .is_some_and(|w| references_alias(&w.this))
            || base
                .having
                .as_ref()
                .is_some_and(|h| references_alias(&h.this)))
    {
        return Err(unsupported());
    }
    let visible_count = internal_names.len();
    let mut resolve = |key: &Expression, table: &str, ordinal: bool| -> Result<Expression> {
        if !ordinal
            && matches!(
                key,
                Expression::Literal(_) | Expression::Boolean(_) | Expression::Null(_)
            )
        {
            return Ok(key.clone());
        }
        let projected = match key {
            Expression::Literal(lit) if ordinal => match lit.as_ref() {
                Literal::Number(n) => n
                    .parse::<usize>()
                    .ok()
                    .and_then(|i| i.checked_sub(1))
                    .filter(|&i| i < visible_count),
                _ => None,
            },
            Expression::Column(column) if column.table.is_none() => output_names
                .iter()
                .position(|name| name.name.eq_ignore_ascii_case(&column.name.name)),
            _ => None,
        }
        .or_else(|| {
            original.iter().position(|expression| {
                let value = if let Expression::Alias(alias) = expression {
                    &alias.this
                } else {
                    expression
                };
                value == key
            })
        });
        let name = if let Some(i) = projected {
            internal_names[i].clone()
        } else {
            if base.distinct || base.group_by.is_some() || base.having.is_some() {
                return Err(unsupported());
            }
            if target == DialectType::PostgreSQL && references_alias(key) {
                return Err(unsupported());
            }
            let name = names.fresh("_vertica_hidden");
            base.expressions.push(key.clone().alias(&name));
            let name = Identifier::new(name);
            internal_names.push(name.clone());
            name
        };
        Ok(crate::ast_mutation::qualified_column(table, &name))
    };
    for key in &mut over.partition_by {
        *key = resolve(key, &source_alias, false)?;
    }
    for key in &mut over.order_by {
        key.this = resolve(&key.this, &source_alias, false)?;
        key.nulls_first.get_or_insert(key.desc);
    }
    if let Some(order) = &mut outer_order {
        for key in &mut order.expressions {
            key.this = resolve(&key.this, &ranked_alias, true)?;
        }
    }
    let subquery = |select: Select, name: String| {
        crate::ast_mutation::derived_table(
            Expression::Select(Box::new(select)),
            Some(Identifier::new(name)),
        )
    };
    let mut ranked = Select::new();
    ranked.expressions = internal_names
        .iter()
        .map(|name| crate::ast_mutation::qualified_column(&source_alias, name))
        .collect();
    ranked.expressions.push(
        Expression::WindowFunction(Box::new(WindowFunction {
            this: Expression::RowNumber(crate::expressions::RowNumber),
            over,
            keep: None,
            inferred_type: None,
        }))
        .alias(&rank_name),
    );
    ranked.from = Some(From {
        expressions: vec![subquery(base, source_alias)],
    });
    let mut result = Select::new();
    result.expressions = output_names
        .into_iter()
        .enumerate()
        .map(|(i, name)| {
            Expression::Alias(Box::new(Alias::new(
                crate::ast_mutation::qualified_column(&ranked_alias, &internal_names[i]),
                name,
            )))
        })
        .collect();
    result.where_clause = Some(Where {
        this: Expression::Lte(Box::new(BinaryOp::new(
            Expression::qualified_column(&ranked_alias, rank_name),
            limit.this,
        ))),
    });
    result.from = Some(From {
        expressions: vec![subquery(ranked, ranked_alias)],
    });
    result.order_by = outer_order;
    result.offset = outer_offset;
    result.with = outer_with;
    result.leading_comments = outer_comments;
    Ok(Expression::Select(Box::new(result)))
}