Skip to main content

unisql_sqlparser_adapter/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2//! sqlparser adapter: text -> IR for the dialects that sqlparser can parse. The per-dialect
3//! differences are the [`Rules`]; no parser type escapes this crate. **Anything the IR cannot represent is `ReadError::NotInIr`, never a pass-through.**
4//!
5//! `Query`, `Select`, `Insert`, `Update` and `Delete` are destructured with every field named and no
6//! `..`: a sqlparser upgrade that adds a field fails the build here, forcing a decision rather than
7//! silently dropping a construct.
8
9use std::cell::RefCell;
10
11use sqlparser::ast as sp;
12use sqlparser::dialect::Dialect;
13use sqlparser::parser::Parser;
14use unisql_core::ast::*;
15use unisql_core::{Allowance, Column, ColumnClass, Read, ReadError, ReadNote, Schema};
16
17/// What differs between source dialects at the AST-to-IR step.
18pub struct Rules {
19    pub name: &'static str,
20    pub dialect: fn() -> Box<dyn Dialect>,
21    /// Quoted identifiers are case-insensitive too (SQLite): fold everything to lower case.
22    pub fold_quoted: bool,
23    /// `NULL` sorts as the smallest value (SQLite) rather than the largest (PostgreSQL).
24    pub nulls_smallest: bool,
25    /// Parameters: `$n` (PostgreSQL) or `?`, `?n`, `:name`, `@name`, `$name` (SQLite).
26    pub question_placeholders: bool,
27    /// `LIKE` is case-insensitive by default (SQLite, ASCII only).
28    pub like_ci_default: bool,
29    /// `LIKE` has a default escape character (PostgreSQL: backslash); otherwise none.
30    pub like_default_escape: bool,
31    /// A negative `LIMIT` means no limit (SQLite).
32    pub negative_limit_unlimited: bool,
33}
34
35pub static POSTGRES: Rules = Rules {
36    name: "postgres",
37    dialect: || Box::new(sqlparser::dialect::PostgreSqlDialect {}),
38    fold_quoted: false,
39    nulls_smallest: false,
40    question_placeholders: false,
41    like_ci_default: false,
42    like_default_escape: true,
43    negative_limit_unlimited: false,
44};
45
46pub static MYSQL: Rules = Rules {
47    name: "mysql",
48    dialect: || Box::new(sqlparser::dialect::MySqlDialect {}),
49    fold_quoted: true,
50    nulls_smallest: true,
51    question_placeholders: true,
52    like_ci_default: false,
53    like_default_escape: true,
54    negative_limit_unlimited: false,
55};
56
57pub static SQLITE: Rules = Rules {
58    name: "sqlite",
59    dialect: || Box::new(sqlparser::dialect::SQLiteDialect {}),
60    fold_quoted: true,
61    nulls_smallest: true,
62    question_placeholders: true,
63    like_ci_default: true,
64    like_default_escape: false,
65    negative_limit_unlimited: true,
66};
67
68struct Cx {
69    rules: &'static Rules,
70    max_param: u32,
71    names: Vec<(String, u32)>,
72    notes: Vec<ReadNote>,
73}
74
75thread_local! {
76    static CX: RefCell<Option<Cx>> = const { RefCell::new(None) };
77}
78
79fn rules() -> &'static Rules {
80    CX.with(|c| c.borrow().as_ref().expect("reader context").rules)
81}
82
83fn add_note(construct: &str, note: &str, allowance: Allowance) {
84    CX.with(|c| {
85        let mut b = c.borrow_mut();
86        let cx = b.as_mut().expect("reader context");
87        let n = ReadNote {
88            construct: construct.into(),
89            note: note.into(),
90            allowance,
91        };
92        if !cx.notes.contains(&n) {
93            cx.notes.push(n);
94        }
95    });
96}
97
98fn bump_param(explicit: Option<u32>, name: Option<&str>) -> u32 {
99    CX.with(|c| {
100        let mut b = c.borrow_mut();
101        let cx = b.as_mut().expect("reader context");
102        match (explicit, name) {
103            (Some(n), _) => {
104                cx.max_param = cx.max_param.max(n);
105                n
106            }
107            (None, Some(nm)) => {
108                if let Some((_, n)) = cx.names.iter().find(|(k, _)| k == nm) {
109                    return *n;
110                }
111                cx.max_param += 1;
112                let n = cx.max_param;
113                cx.names.push((nm.to_string(), n));
114                n
115            }
116            (None, None) => {
117                cx.max_param += 1;
118                cx.max_param
119            }
120        }
121    })
122}
123
124type R<T> = Result<T, ReadError>;
125
126fn no<T>(construct: &str, detail: &str) -> R<T> {
127    Err(ReadError::NotInIr {
128        construct: construct.to_string(),
129        detail: detail.to_string(),
130    })
131}
132
133/// Read exactly one statement written in the dialect described by `rules`.
134pub fn read(sql: &str, rules: &'static Rules) -> R<Read> {
135    CX.with(|c| {
136        *c.borrow_mut() = Some(Cx {
137            rules,
138            max_param: 0,
139            names: vec![],
140            notes: vec![],
141        })
142    });
143    let out = (|| {
144        let d = (rules.dialect)();
145        // MySQL's default sql_mode makes `||` a logical OR with OR's precedence, but the parser
146        // gives it concatenation's. Rewrite the operator token in the text (by position, so nothing
147        // else is re-spelled) before parsing, and say so.
148        let rewritten;
149        let sql = if rules.name == "mysql" {
150            match pipes_to_or(sql, d.as_ref()) {
151                Some(s) => {
152                    add_note(
153                        "pipes_as_or",
154                        "MySQL `||` is logical OR in the default sql_mode; under PIPES_AS_CONCAT it is concatenation",
155                        Allowance::PipesAsLogicalOr,
156                    );
157                    rewritten = s;
158                    rewritten.as_str()
159                }
160                None => sql,
161            }
162        } else {
163            sql
164        };
165        let mut stmts =
166            Parser::parse_sql(d.as_ref(), sql).map_err(|e| ReadError::Syntax(e.to_string()))?;
167        if stmts.len() != 1 {
168            return no(
169                "multiple_statements",
170                &format!("expected one statement, found {}", stmts.len()),
171            );
172        }
173        statement(stmts.remove(0))
174    })();
175    let cx = CX.with(|c| c.borrow_mut().take()).expect("reader context");
176    out.map(|stmt| Read {
177        stmt,
178        notes: cx.notes,
179    })
180}
181
182/// `sql` with each `||` operator token replaced by ` OR `; `None` when there is none (or the text does
183/// not tokenize, which the parser then reports).
184fn pipes_to_or(sql: &str, dialect: &dyn sqlparser::dialect::Dialect) -> Option<String> {
185    use sqlparser::tokenizer::{Token, Tokenizer};
186    let tokens = Tokenizer::new(dialect, sql).tokenize_with_location().ok()?;
187    let line_starts: Vec<usize> = std::iter::once(0)
188        .chain(
189            sql.chars()
190                .enumerate()
191                .filter(|(_, c)| *c == '\n')
192                .map(|(i, _)| i + 1),
193        )
194        .collect();
195    let mut spots: Vec<usize> = tokens
196        .iter()
197        .filter(|t| t.token == Token::StringConcat)
198        .filter_map(|t| {
199            let line = usize::try_from(t.span.start.line).ok()?.checked_sub(1)?;
200            let col = usize::try_from(t.span.start.column).ok()?.checked_sub(1)?;
201            Some(line_starts.get(line)? + col)
202        })
203        .collect();
204    if spots.is_empty() {
205        return None;
206    }
207    spots.sort_unstable();
208    let chars: Vec<char> = sql.chars().collect();
209    let mut out = String::new();
210    let mut at = 0;
211    for s in spots {
212        if s < at || s + 2 > chars.len() || chars[s] != '|' || chars[s + 1] != '|' {
213            return None;
214        }
215        out.extend(&chars[at..s]);
216        out.push_str(" OR ");
217        at = s + 2;
218    }
219    out.extend(&chars[at..]);
220    Some(out)
221}
222
223/// The classes of the base-table columns a DDL script declares, read for types only: this is not DDL
224/// translation, and no statement of it is ever written out.
225#[derive(Clone, Debug, Default)]
226pub struct SchemaRead {
227    pub schema: Schema,
228    /// Statements the parser could not read (a function body, a `DO` block): each head and reason.
229    /// They are skipped, which can only leave a column unregistered (unknown).
230    pub skipped: Vec<String>,
231}
232
233/// Class of a declared type, from its spelling (`None`: a type UniSQL does not classify). MySQL's
234/// `tinyint(1)` is numeric; `boolean` is `Boolean` only because the declared type says so.
235fn class_of_type(dt: &sp::DataType) -> Option<ColumnClass> {
236    let t = dt.to_string().to_ascii_lowercase();
237    if t.ends_with("[]") || t.starts_with("array") {
238        return None;
239    }
240    let base: String = t
241        .chars()
242        .take_while(|c| c.is_ascii_alphanumeric() || *c == '_' || *c == ' ')
243        .collect();
244    let base = base.trim();
245    let first = base.split_whitespace().next().unwrap_or("");
246    Some(match base {
247        "character varying" | "character" | "national character varying" | "national character" => {
248            ColumnClass::Text
249        }
250        "double precision" => ColumnClass::Numeric,
251        b if b.starts_with("timestamp")
252            || b == "time"
253            || b.starts_with("time with")
254            || b.starts_with("time without") =>
255        {
256            ColumnClass::Other
257        }
258        _ => match first {
259            "text" | "varchar" | "char" | "nchar" | "nvarchar" | "tinytext" | "mediumtext"
260            | "longtext" | "string" | "bpchar" | "clob" => ColumnClass::Text,
261            "int" | "integer" | "smallint" | "bigint" | "tinyint" | "mediumint" | "int2"
262            | "int4" | "int8" | "serial" | "bigserial" | "smallserial" | "decimal" | "numeric"
263            | "dec" | "float" | "float4" | "float8" | "real" | "double" | "number" => {
264                ColumnClass::Numeric
265            }
266            "boolean" | "bool" => ColumnClass::Boolean,
267            "date" | "datetime" | "timestamptz" | "year" | "interval" | "uuid" | "json"
268            | "jsonb" | "bytea" | "blob" | "tinyblob" | "mediumblob" | "longblob" | "binary"
269            | "varbinary" | "bit" | "inet" | "cidr" | "macaddr" => ColumnClass::Other,
270            _ => return None,
271        },
272    })
273}
274
275fn column_from_def(c: &sp::ColumnDef) -> Column {
276    let class = class_of_type(&c.data_type);
277    let collation = c.options.iter().find_map(|o| match &o.option {
278        sp::ColumnOption::Collation(n) => {
279            Some(n.to_string().trim_matches(['"', '`', '\'']).to_string())
280        }
281        _ => None,
282    });
283    let mut col = Column::new(class.unwrap_or(ColumnClass::Unknown));
284    col.collation = collation;
285    col
286}
287
288/// Read `CREATE TABLE`, `ALTER TABLE .. ADD|DROP|ALTER COLUMN` and `DROP TABLE` out of a DDL script
289/// (a migration file) in the dialect described by `rules`. Statements are split on `;` tokens, so a
290/// dollar-quoted body does not split; a statement that does not parse is listed in `skipped`.
291pub fn read_schema(ddl: &str, rules: &'static Rules) -> R<SchemaRead> {
292    // `ident` consults the reader context for quoted-name folding, so one must be installed.
293    CX.with(|c| {
294        *c.borrow_mut() = Some(Cx {
295            rules,
296            max_param: 0,
297            names: vec![],
298            notes: vec![],
299        })
300    });
301    let out = read_schema_inner(ddl, rules);
302    CX.with(|c| c.borrow_mut().take());
303    out
304}
305
306fn read_schema_inner(ddl: &str, rules: &'static Rules) -> R<SchemaRead> {
307    use sqlparser::tokenizer::{Token, Tokenizer};
308    let d = (rules.dialect)();
309    let tokens = Tokenizer::new(d.as_ref(), ddl)
310        .tokenize_with_location()
311        .map_err(|e| ReadError::Syntax(e.to_string()))?;
312    let chars: Vec<char> = ddl.chars().collect();
313    let line_starts: Vec<usize> = std::iter::once(0)
314        .chain(
315            chars
316                .iter()
317                .enumerate()
318                .filter(|(_, c)| **c == '\n')
319                .map(|(i, _)| i + 1),
320        )
321        .collect();
322    let offset = |loc: sqlparser::tokenizer::Location| -> Option<usize> {
323        let line = usize::try_from(loc.line).ok()?.checked_sub(1)?;
324        let col = usize::try_from(loc.column).ok()?.checked_sub(1)?;
325        Some(line_starts.get(line)? + col)
326    };
327    let mut cuts: Vec<usize> = tokens
328        .iter()
329        .filter(|t| t.token == Token::SemiColon)
330        .filter_map(|t| offset(t.span.start))
331        .collect();
332    cuts.push(chars.len());
333    let mut out = SchemaRead::default();
334    let mut from = 0;
335    for cut in cuts {
336        if cut < from || cut > chars.len() {
337            continue;
338        }
339        let text: String = chars[from..cut].iter().collect();
340        from = (cut + 1).min(chars.len());
341        if text.trim().is_empty() {
342            continue;
343        }
344        let stmts = match Parser::parse_sql(d.as_ref(), &text) {
345            Ok(s) => s,
346            Err(e) => {
347                let head: String = text
348                    .split_whitespace()
349                    .take(4)
350                    .collect::<Vec<_>>()
351                    .join(" ");
352                out.skipped.push(format!("{head}: {e}"));
353                continue;
354            }
355        };
356        for st in stmts {
357            apply_ddl(&mut out.schema, st);
358        }
359    }
360    Ok(out)
361}
362
363fn apply_ddl(schema: &mut Schema, st: sp::Statement) {
364    match st {
365        sp::Statement::CreateTable(ct) => {
366            if ct.query.is_some() || ct.like.is_some() {
367                return;
368            }
369            let Ok(name) = object_name(&ct.name) else {
370                return;
371            };
372            schema.drop_table(&name);
373            schema.mark_columns_complete(name.0.clone());
374            for c in &ct.columns {
375                schema.set_column(name.0.clone(), ident(&c.name), column_from_def(c));
376                for o in &c.options {
377                    if matches!(
378                        o.option,
379                        sp::ColumnOption::PrimaryKey(_) | sp::ColumnOption::Unique(_)
380                    ) {
381                        schema.add_unique_key(name.0.clone(), vec![ident(&c.name)]);
382                    }
383                }
384            }
385            for con in &ct.constraints {
386                add_constraint_key(schema, &name, con);
387            }
388        }
389        sp::Statement::CreateIndex(ci) => {
390            let Ok(name) = object_name(&ci.table_name) else {
391                return;
392            };
393            if ci.unique {
394                match index_columns(&ci.columns) {
395                    Some(cols) if ci.predicate.is_none() => schema.add_unique_key(name.0, cols),
396                    _ => schema.mark_keys_unreliable(name.0),
397                }
398            }
399        }
400        sp::Statement::AlterTable(at) => {
401            let Ok(name) = object_name(&at.name) else {
402                return;
403            };
404            for op in at.operations {
405                match op {
406                    sp::AlterTableOperation::AddColumn { column_def, .. } => {
407                        schema.set_column(
408                            name.0.clone(),
409                            ident(&column_def.name),
410                            column_from_def(&column_def),
411                        );
412                    }
413                    sp::AlterTableOperation::AddConstraint { constraint, .. } => {
414                        add_constraint_key(schema, &name, &constraint);
415                    }
416                    sp::AlterTableOperation::DropPrimaryKey { .. }
417                    | sp::AlterTableOperation::DropConstraint { .. } => {
418                        schema.mark_keys_unreliable(name.0.clone());
419                    }
420                    sp::AlterTableOperation::DropColumn { column_names, .. } => {
421                        for c in &column_names {
422                            schema.drop_column(&name, &ident(c));
423                        }
424                    }
425                    sp::AlterTableOperation::AlterColumn {
426                        column_name,
427                        op: sp::AlterColumnOperation::SetDataType { data_type, .. },
428                    } => {
429                        let class = class_of_type(&data_type).unwrap_or(ColumnClass::Unknown);
430                        // A new type resets the collation unless the statement states one.
431                        schema.set_column(name.0.clone(), ident(&column_name), Column::new(class));
432                    }
433                    _ => {}
434                }
435            }
436        }
437        sp::Statement::Drop {
438            object_type: sp::ObjectType::Index,
439            ..
440        } => schema.mark_all_keys_unreliable(),
441        sp::Statement::Drop {
442            object_type: sp::ObjectType::Table,
443            names,
444            ..
445        } => {
446            for n in &names {
447                if let Ok(name) = object_name(n) {
448                    schema.drop_table(&name);
449                }
450            }
451        }
452        _ => {}
453    }
454}
455
456/// The column names of an index definition when every part is a plain column.
457fn index_columns(cols: &[sp::IndexColumn]) -> Option<Vec<String>> {
458    cols.iter()
459        .map(|c| match &c.column.expr {
460            sp::Expr::Identifier(i) => Some(ident(i)),
461            _ => None,
462        })
463        .collect()
464}
465
466/// A PRIMARY KEY or UNIQUE table constraint: its columns are a unique key (an index over an
467/// expression makes the table's key list unreliable).
468fn add_constraint_key(schema: &mut Schema, table: &Name, con: &sp::TableConstraint) {
469    let cols = match con {
470        sp::TableConstraint::PrimaryKey(p) => &p.columns,
471        sp::TableConstraint::Unique(u) => &u.columns,
472        _ => return,
473    };
474    match index_columns(cols) {
475        Some(c) => schema.add_unique_key(table.0.clone(), c),
476        None => schema.mark_keys_unreliable(table.0.clone()),
477    }
478}
479
480fn statement(s: sp::Statement) -> R<Statement> {
481    match s {
482        sp::Statement::Query(q)
483            if matches!(
484                &*q.body,
485                sp::SetExpr::Insert(_) | sp::SetExpr::Update(_) | sp::SetExpr::Delete(_)
486            ) =>
487        {
488            // `WITH .. INSERT|UPDATE|DELETE ..` is a query node in the parser's tree.
489            let mut q = *q;
490            let with = q.with.take();
491            if q.to_string() != q.body.to_string() {
492                return no(
493                    "query_clause",
494                    "clauses around a WITH .. INSERT/UPDATE/DELETE",
495                );
496            }
497            let inner = match *q.body {
498                sp::SetExpr::Insert(st) | sp::SetExpr::Update(st) | sp::SetExpr::Delete(st) => {
499                    statement(st)?
500                }
501                _ => unreachable!(),
502            };
503            match with {
504                None => Ok(inner),
505                Some(w) => {
506                    let (ctes, recursive) = read_ctes(w)?;
507                    Ok(Statement::With(WithStatement {
508                        recursive,
509                        ctes,
510                        stmt: Box::new(inner),
511                    }))
512                }
513            }
514        }
515        sp::Statement::Query(q) => Ok(Statement::Query(Box::new(query(*q)?))),
516        sp::Statement::Insert(i) => insert(i),
517        sp::Statement::Update(u) => update(u),
518        sp::Statement::Delete(d) => delete(d),
519        other => no(
520            "statement",
521            &format!(
522                "{} is not a DML statement the first slice reads",
523                head(&other)
524            ),
525        ),
526    }
527}
528
529fn head(x: &impl std::fmt::Debug) -> String {
530    let s = format!("{x:?}");
531    s.split(|c: char| !c.is_alphanumeric() && c != '_')
532        .next()
533        .unwrap_or("")
534        .to_string()
535}
536
537/// Identifier folding: unquoted identifiers fold to lower case; quoted ones are exact, except where
538/// the dialect treats quoted names case-insensitively too (SQLite).
539fn ident(i: &sp::Ident) -> String {
540    if i.quote_style.is_some() && !rules().fold_quoted {
541        i.value.clone()
542    } else {
543        i.value.to_lowercase()
544    }
545}
546
547fn object_name(n: &sp::ObjectName) -> R<Name> {
548    let mut parts = Vec::new();
549    for p in &n.0 {
550        match p {
551            sp::ObjectNamePart::Identifier(i) => parts.push(ident(i)),
552            other => return no("object_name_part", &head(other)),
553        }
554    }
555    Ok(Name(parts))
556}
557
558fn single_name(n: &sp::ObjectName) -> R<String> {
559    let nm = object_name(n)?;
560    if nm.0.len() != 1 {
561        return no(
562            "qualified_column_target",
563            "expected an unqualified column name",
564        );
565    }
566    Ok(nm.0.into_iter().next().unwrap())
567}
568
569/// The `WITH` list of a query or of a data-modifying statement.
570fn read_ctes(w: sp::With) -> R<(Vec<Cte>, bool)> {
571    let sp::With {
572        with_token: _,
573        recursive,
574        cte_tables,
575    } = w;
576    let mut ctes = Vec::new();
577    for c in cte_tables {
578        let sp::Cte {
579            alias,
580            query: cq,
581            from,
582            materialized,
583            closing_paren_token: _,
584        } = c;
585        if from.is_some() || materialized.is_some() {
586            return no("cte_option", "MATERIALIZED / FROM in a CTE");
587        }
588        let mut columns = Vec::new();
589        for col in &alias.columns {
590            if col.data_type.is_some() {
591                return no("cte_column_type", "typed CTE column");
592            }
593            columns.push(ident(&col.name));
594        }
595        let dml = matches!(
596            &*cq.body,
597            sp::SetExpr::Insert(_) | sp::SetExpr::Update(_) | sp::SetExpr::Delete(_)
598        );
599        let cte_body = if dml {
600            // Nothing but the statement itself: a WITH / ORDER BY / LIMIT around it would be
601            // dropped silently otherwise.
602            if cq.to_string() != cq.body.to_string() {
603                return no("cte_dml_clause", "clauses around a data-modifying CTE");
604            }
605            match *cq.body {
606                sp::SetExpr::Insert(st) | sp::SetExpr::Update(st) | sp::SetExpr::Delete(st) => {
607                    CteBody::Dml(Box::new(statement(st)?))
608                }
609                _ => unreachable!(),
610            }
611        } else {
612            CteBody::Query(Box::new(query(*cq)?))
613        };
614        ctes.push(Cte {
615            name: ident(&alias.name),
616            columns,
617            body: cte_body,
618        });
619    }
620    Ok((ctes, recursive))
621}
622
623fn query(q: sp::Query) -> R<Query> {
624    let sp::Query {
625        with,
626        body,
627        order_by,
628        limit_clause,
629        fetch,
630        locks,
631        for_clause,
632        settings,
633        format_clause,
634        pipe_operators,
635    } = q;
636    if fetch.is_some() {
637        return no("fetch_first", "FETCH FIRST is not in the IR (use LIMIT)");
638    }
639    if for_clause.is_some()
640        || settings.is_some()
641        || format_clause.is_some()
642        || !pipe_operators.is_empty()
643    {
644        return no("query_clause", "FOR/SETTINGS/FORMAT/pipe operators");
645    }
646    let mut ctes = Vec::new();
647    let mut recursive = false;
648    if let Some(w) = with {
649        (ctes, recursive) = read_ctes(w)?;
650    }
651    let mut body = set_expr(*body)?;
652    let mut lock = None;
653    match locks.len() {
654        0 => {}
655        1 => {
656            let sp::LockClause {
657                lock_type,
658                of,
659                nonblock,
660            } = locks.into_iter().next().unwrap();
661            lock = Some(RowLockClause {
662                strength: match lock_type {
663                    sp::LockType::Update => RowLock::Update,
664                    sp::LockType::Share => RowLock::Share,
665                },
666                of: match of {
667                    Some(n) => Some(object_name(&n)?),
668                    None => None,
669                },
670                wait: match nonblock {
671                    None => LockWait::Block,
672                    Some(sp::NonBlock::Nowait) => LockWait::NoWait,
673                    Some(sp::NonBlock::SkipLocked) => LockWait::SkipLocked,
674                },
675            });
676        }
677        _ => return no("row_lock_multiple", "several locking clauses"),
678    }
679    if let Some(l) = lock {
680        match &mut body {
681            SetExpr::Select(s) => s.lock = Some(l),
682            _ => return no("row_lock_on_set_op", "locking clause on a set operation"),
683        }
684    }
685    let mut order = Vec::new();
686    if let Some(ob) = order_by {
687        let sp::OrderBy { kind, interpolate } = ob;
688        if interpolate.is_some() {
689            return no("order_by_interpolate", "INTERPOLATE");
690        }
691        match kind {
692            sp::OrderByKind::Expressions(items) => {
693                for it in items {
694                    order.push(order_item(it)?);
695                }
696            }
697            sp::OrderByKind::All(_) => return no("order_by_all", "ORDER BY ALL"),
698        }
699    }
700    let (mut limit, mut offset) = (None, None);
701    match limit_clause {
702        None => {}
703        Some(sp::LimitClause::LimitOffset {
704            limit: l,
705            offset: o,
706            limit_by,
707        }) => {
708            if !limit_by.is_empty() {
709                return no("limit_by", "LIMIT .. BY");
710            }
711            limit = l.map(expr).transpose()?;
712            if rules().negative_limit_unlimited {
713                // SQLite: a negative LIMIT means "no limit".
714                let neg = matches!(&limit, Some(Expr::Unary { op: UnaryOp::Minus, expr })
715                    if matches!(**expr, Expr::Literal(Literal::Number(_))));
716                if neg {
717                    limit = None;
718                }
719            }
720            if let Some(o) = o {
721                let sp::Offset { value, rows } = o;
722                if !matches!(rows, sp::OffsetRows::None) {
723                    return no("offset_rows", "OFFSET .. ROWS");
724                }
725                offset = Some(expr(value)?);
726            }
727        }
728        Some(sp::LimitClause::OffsetCommaLimit {
729            offset: o,
730            limit: l,
731        }) => {
732            offset = Some(expr(o)?);
733            limit = Some(expr(l)?);
734        }
735    }
736    Ok(Query {
737        with: ctes,
738        recursive,
739        body,
740        order_by: order,
741        limit,
742        offset,
743    })
744}
745
746fn order_item(o: sp::OrderByExpr) -> R<OrderItem> {
747    let sp::OrderByExpr {
748        expr: e,
749        options,
750        with_fill,
751    } = o;
752    if with_fill.is_some() {
753        return no("order_by_with_fill", "WITH FILL");
754    }
755    let desc = matches!(options.sort, Some(sp::OrderBySort::Desc));
756    // The source's default NULL placement, made explicit so any target preserves it: NULL is the
757    // largest value on PostgreSQL (first when descending) and the smallest on SQLite (first ascending).
758    let default_first = if rules().nulls_smallest { !desc } else { desc };
759    let nulls_first = options.nulls_first.unwrap_or(default_first);
760    Ok(OrderItem {
761        expr: expr(e)?,
762        desc,
763        nulls_first: Some(nulls_first),
764    })
765}
766
767fn set_expr(s: sp::SetExpr) -> R<SetExpr> {
768    match s {
769        sp::SetExpr::Select(sel) => Ok(SetExpr::Select(Box::new(select(*sel)?))),
770        sp::SetExpr::SetOperation {
771            left,
772            op,
773            set_quantifier,
774            right,
775        } => {
776            let op = match op {
777                sp::SetOperator::Union => SetOp::Union,
778                sp::SetOperator::Intersect => SetOp::Intersect,
779                sp::SetOperator::Except => SetOp::Except,
780                sp::SetOperator::Minus => return no("minus", "MINUS is not PostgreSQL"),
781            };
782            let all = match set_quantifier {
783                sp::SetQuantifier::All => true,
784                sp::SetQuantifier::Distinct | sp::SetQuantifier::None => false,
785                other => return no("set_quantifier", &head(&other)),
786            };
787            Ok(SetExpr::SetOp {
788                op,
789                all,
790                left: Box::new(set_expr(*left)?),
791                right: Box::new(set_expr(*right)?),
792            })
793        }
794        sp::SetExpr::Query(q) => {
795            let q = query(*q)?;
796            if !q.with.is_empty()
797                || !q.order_by.is_empty()
798                || q.limit.is_some()
799                || q.offset.is_some()
800            {
801                return no(
802                    "parenthesised_query",
803                    "parenthesised query with WITH/ORDER/LIMIT as a set operand",
804                );
805            }
806            Ok(q.body)
807        }
808        sp::SetExpr::Values(_) => no("values_query", "a bare VALUES query outside INSERT"),
809        other => no("set_expr", &head(&other)),
810    }
811}
812
813fn select(s: sp::Select) -> R<Select> {
814    let sp::Select {
815        select_token: _,
816        optimizer_hints,
817        distinct,
818        select_modifiers,
819        top,
820        top_before_distinct: _,
821        projection,
822        exclude,
823        into,
824        from,
825        lateral_views,
826        prewhere,
827        selection,
828        connect_by,
829        group_by,
830        cluster_by,
831        distribute_by,
832        sort_by,
833        having,
834        named_window,
835        qualify,
836        window_before_qualify: _,
837        value_table_mode,
838        flavor: _,
839    } = s;
840    if !optimizer_hints.is_empty()
841        || select_modifiers.is_some()
842        || top.is_some()
843        || exclude.is_some()
844        || into.is_some()
845        || !lateral_views.is_empty()
846        || prewhere.is_some()
847        || !connect_by.is_empty()
848        || !cluster_by.is_empty()
849        || !distribute_by.is_empty()
850        || !sort_by.is_empty()
851        || !named_window.is_empty()
852        || qualify.is_some()
853        || value_table_mode.is_some()
854    {
855        return no(
856            "select_clause",
857            "a SELECT clause the IR does not represent (TOP/INTO/WINDOW/QUALIFY/hints/...)",
858        );
859    }
860    let distinct = match distinct {
861        None | Some(sp::Distinct::All) => Distinct::No,
862        Some(sp::Distinct::Distinct) => Distinct::Yes,
863        Some(sp::Distinct::On(es)) => Distinct::On(es.into_iter().map(expr).collect::<R<_>>()?),
864    };
865    let mut group_by_rollup = false;
866    let group_by = match group_by {
867        sp::GroupByExpr::Expressions(es, mods) => {
868            match mods.as_slice() {
869                [] => {}
870                [sp::GroupByWithModifier::Rollup] if !es.is_empty() => group_by_rollup = true,
871                _ => return no("group_by_modifier", "CUBE/TOTALS/GROUPING SETS modifiers"),
872            }
873            es.into_iter().map(expr).collect::<R<Vec<_>>>()?
874        }
875        sp::GroupByExpr::All(_) => return no("group_by_all", "GROUP BY ALL"),
876    };
877    let mut proj = Vec::new();
878    for it in projection {
879        proj.push(select_item(it)?);
880    }
881    let mut from_refs = Vec::new();
882    for t in from {
883        from_refs.push(table_with_joins(t)?);
884    }
885    Ok(Select {
886        distinct,
887        projection: proj,
888        from: from_refs,
889        where_: selection.map(expr).transpose()?,
890        group_by,
891        group_by_rollup,
892        having: having.map(expr).transpose()?,
893        lock: None,
894    })
895}
896
897fn wildcard_plain(o: &sp::WildcardAdditionalOptions) -> R<()> {
898    let sp::WildcardAdditionalOptions {
899        wildcard_token: _,
900        opt_ilike,
901        opt_exclude,
902        opt_except,
903        opt_replace,
904        opt_rename,
905        opt_alias,
906    } = o;
907    if opt_ilike.is_some()
908        || opt_exclude.is_some()
909        || opt_except.is_some()
910        || opt_replace.is_some()
911        || opt_rename.is_some()
912        || opt_alias.is_some()
913    {
914        return no("wildcard_option", "EXCLUDE/EXCEPT/REPLACE/RENAME on *");
915    }
916    Ok(())
917}
918
919fn select_item(it: sp::SelectItem) -> R<SelectItem> {
920    match it {
921        sp::SelectItem::UnnamedExpr(e) => Ok(SelectItem::Expr {
922            expr: expr(e)?,
923            alias: None,
924        }),
925        sp::SelectItem::ExprWithAlias { expr: e, alias } => Ok(SelectItem::Expr {
926            expr: expr(e)?,
927            alias: Some(ident(&alias)),
928        }),
929        sp::SelectItem::Wildcard(o) => {
930            wildcard_plain(&o)?;
931            Ok(SelectItem::Wildcard)
932        }
933        sp::SelectItem::QualifiedWildcard(kind, o) => {
934            wildcard_plain(&o)?;
935            match kind {
936                sp::SelectItemQualifiedWildcardKind::ObjectName(n) => {
937                    Ok(SelectItem::QualifiedWildcard(object_name(&n)?))
938                }
939                other => no("qualified_wildcard", &head(&other)),
940            }
941        }
942        other => no("select_item", &head(&other)),
943    }
944}
945
946fn alias_name(a: Option<sp::TableAlias>) -> R<Option<String>> {
947    match a {
948        None => Ok(None),
949        Some(sp::TableAlias {
950            explicit: _,
951            name,
952            columns,
953            at,
954        }) => {
955            if !columns.is_empty() || at.is_some() {
956                return no("table_alias_columns", "column list on a table alias");
957            }
958            Ok(Some(ident(&name)))
959        }
960    }
961}
962
963fn table_factor(t: sp::TableFactor) -> R<TableRef> {
964    match t {
965        sp::TableFactor::Table {
966            name,
967            alias,
968            args,
969            with_hints,
970            version,
971            with_ordinality,
972            partitions,
973            json_path,
974            sample,
975            index_hints,
976        } => {
977            // A table-valued function: positional expression arguments only.
978            if let Some(targs) = args {
979                let plain = with_hints.is_empty()
980                    && version.is_none()
981                    && !with_ordinality
982                    && partitions.is_empty()
983                    && json_path.is_none()
984                    && sample.is_none()
985                    && index_hints.is_empty()
986                    && targs.settings.is_none();
987                let nm = object_name(&name)?;
988                if plain && nm.0.len() == 1 {
989                    let mut out = Vec::new();
990                    for a in targs.args {
991                        match a {
992                            sp::FunctionArg::Unnamed(sp::FunctionArgExpr::Expr(e)) => {
993                                out.push(expr(e)?)
994                            }
995                            _ => {
996                                return no(
997                                    "table_function_arg",
998                                    "named or wildcard argument of a table function",
999                                );
1000                            }
1001                        }
1002                    }
1003                    return Ok(TableRef::Function {
1004                        name: nm.0[0].clone(),
1005                        args: out,
1006                        alias: alias_name(alias)?,
1007                    });
1008                }
1009                return no(
1010                    "table_option",
1011                    "table function args/hints/version/sample/partitions",
1012                );
1013            }
1014            if !with_hints.is_empty()
1015                || version.is_some()
1016                || with_ordinality
1017                || !partitions.is_empty()
1018                || json_path.is_some()
1019                || sample.is_some()
1020                || !index_hints.is_empty()
1021            {
1022                return no(
1023                    "table_option",
1024                    "table function args/hints/version/sample/partitions",
1025                );
1026            }
1027            Ok(TableRef::Table {
1028                name: object_name(&name)?,
1029                alias: alias_name(alias)?,
1030            })
1031        }
1032        sp::TableFactor::Derived {
1033            lateral,
1034            subquery,
1035            alias,
1036            sample,
1037        } => {
1038            if lateral || sample.is_some() {
1039                return no("derived_option", "LATERAL / TABLESAMPLE");
1040            }
1041            let alias = alias_name(alias)?.ok_or_else(|| ReadError::NotInIr {
1042                construct: "unaliased_subquery".into(),
1043                detail: "PostgreSQL requires an alias on a FROM subquery".into(),
1044            })?;
1045            Ok(TableRef::Subquery {
1046                query: Box::new(query(*subquery)?),
1047                alias,
1048            })
1049        }
1050        other => no("table_factor", &head(&other)),
1051    }
1052}
1053
1054fn table_with_joins(t: sp::TableWithJoins) -> R<TableRef> {
1055    let sp::TableWithJoins { relation, joins } = t;
1056    let mut cur = table_factor(relation)?;
1057    for j in joins {
1058        let sp::Join {
1059            relation,
1060            global,
1061            join_operator,
1062        } = j;
1063        if global {
1064            return no("global_join", "GLOBAL JOIN");
1065        }
1066        let (kind, constraint) = match join_operator {
1067            sp::JoinOperator::Join(c) | sp::JoinOperator::Inner(c) => (JoinKind::Inner, c),
1068            sp::JoinOperator::Left(c) | sp::JoinOperator::LeftOuter(c) => (JoinKind::Left, c),
1069            sp::JoinOperator::Right(c) | sp::JoinOperator::RightOuter(c) => (JoinKind::Right, c),
1070            sp::JoinOperator::FullOuter(c) => (JoinKind::Full, c),
1071            sp::JoinOperator::CrossJoin(c) => (JoinKind::Cross, c),
1072            other => return no("join_kind", &head(&other)),
1073        };
1074        let (on, using, natural) = match constraint {
1075            sp::JoinConstraint::On(e) => (Some(expr(e)?), Vec::new(), false),
1076            sp::JoinConstraint::None => (None, Vec::new(), false),
1077            sp::JoinConstraint::Using(cols) => (
1078                None,
1079                cols.iter().map(single_name).collect::<R<Vec<_>>>()?,
1080                false,
1081            ),
1082            sp::JoinConstraint::Natural => (None, Vec::new(), true),
1083        };
1084        if natural && kind == JoinKind::Cross {
1085            return no("natural_join", "NATURAL CROSS JOIN");
1086        }
1087        cur = TableRef::Join {
1088            left: Box::new(cur),
1089            right: Box::new(table_factor(relation)?),
1090            kind,
1091            on,
1092            using,
1093            natural,
1094        };
1095    }
1096    Ok(cur)
1097}
1098
1099fn simple_table(t: sp::TableWithJoins) -> R<(Name, Option<String>)> {
1100    let sp::TableWithJoins { relation, joins } = t;
1101    if !joins.is_empty() {
1102        return no("dml_target_join", "joins in a DML target");
1103    }
1104    match table_factor(relation)? {
1105        TableRef::Table { name, alias } => Ok((name, alias)),
1106        _ => no("dml_target", "DML target is not a plain table"),
1107    }
1108}
1109
1110fn returning(r: Option<Vec<sp::SelectItem>>) -> R<Vec<SelectItem>> {
1111    r.unwrap_or_default().into_iter().map(select_item).collect()
1112}
1113
1114/// `SET` assignments. A column qualified with the target's own name or alias (`SET u.score = ..`,
1115/// written in MySQL's multi-table form) is the target's column; any other qualifier is refused.
1116fn assignments(a: Vec<sp::Assignment>, target_name: Option<&str>) -> R<Vec<Assignment>> {
1117    let mut out = Vec::new();
1118    for sp::Assignment { target, value } in a {
1119        let column = match target {
1120            sp::AssignmentTarget::ColumnName(n) => {
1121                let nm = object_name(&n)?;
1122                match nm.0.as_slice() {
1123                    [c] => c.clone(),
1124                    [q, c] if Some(q.as_str()) == target_name => c.clone(),
1125                    _ => {
1126                        return no(
1127                            "qualified_column_target",
1128                            "SET column qualified with a table other than the target",
1129                        );
1130                    }
1131                }
1132            }
1133            sp::AssignmentTarget::Tuple(_) => return no("tuple_assignment", "SET (a, b) = ..."),
1134        };
1135        out.push(Assignment {
1136            column,
1137            value: expr(value)?,
1138        });
1139    }
1140    Ok(out)
1141}
1142
1143fn insert(i: sp::Insert) -> R<Statement> {
1144    let sp::Insert {
1145        insert_token: _,
1146        optimizer_hints,
1147        or,
1148        ignore,
1149        into: _,
1150        table,
1151        table_alias,
1152        columns,
1153        overwrite,
1154        source,
1155        assignments: asg,
1156        partitioned,
1157        after_columns,
1158        has_table_keyword: _,
1159        on,
1160        returning: ret,
1161        output,
1162        replace_into,
1163        priority,
1164        insert_alias,
1165        settings,
1166        format_clause,
1167        multi_table_insert_type,
1168        multi_table_into_clauses,
1169        multi_table_when_clauses,
1170        multi_table_else_clause,
1171    } = i;
1172    if !optimizer_hints.is_empty()
1173        || or.is_some()
1174        || table_alias.is_some()
1175        || overwrite
1176        || (!asg.is_empty() && (!columns.is_empty() || source.is_some()))
1177        || partitioned.is_some()
1178        || !after_columns.is_empty()
1179        || output.is_some()
1180        || priority.is_some()
1181        || insert_alias.is_some()
1182        || settings.is_some()
1183        || format_clause.is_some()
1184        || multi_table_insert_type.is_some()
1185        || !multi_table_into_clauses.is_empty()
1186        || !multi_table_when_clauses.is_empty()
1187        || multi_table_else_clause.is_some()
1188    {
1189        return no(
1190            "insert_option",
1191            "an INSERT form other than INSERT INTO t [(cols)] VALUES|query",
1192        );
1193    }
1194    let table = match table {
1195        sp::TableObject::TableName(n) => object_name(&n)?,
1196        other => return no("insert_target", &head(&other)),
1197    };
1198    // MySQL `INSERT INTO t SET a = 1, b = 2` is `INSERT INTO t (a, b) VALUES (1, 2)`.
1199    let (columns, set_source) = if asg.is_empty() {
1200        (
1201            columns.iter().map(single_name).collect::<R<Vec<_>>>()?,
1202            None,
1203        )
1204    } else {
1205        let set = assignments(asg, None)?;
1206        let (cols, vals): (Vec<_>, Vec<_>) = set.into_iter().map(|a| (a.column, a.value)).unzip();
1207        (cols, Some(InsertSource::Values(vec![vals])))
1208    };
1209    let source = match source {
1210        None if set_source.is_some() => set_source.unwrap_or(InsertSource::DefaultValues),
1211        None => InsertSource::DefaultValues,
1212        Some(q) => {
1213            let q = *q;
1214            match *q.body {
1215                sp::SetExpr::Values(v)
1216                    if q.with.is_none()
1217                        && q.order_by.is_none()
1218                        && q.limit_clause.is_none()
1219                        && q.locks.is_empty() =>
1220                {
1221                    let sp::Values {
1222                        explicit_row,
1223                        value_keyword: _,
1224                        rows,
1225                    } = v;
1226                    if explicit_row {
1227                        return no("explicit_row", "ROW(..) in VALUES");
1228                    }
1229                    let mut out = Vec::new();
1230                    for r in rows {
1231                        out.push(r.content.into_iter().map(expr).collect::<R<Vec<_>>>()?);
1232                    }
1233                    InsertSource::Values(out)
1234                }
1235                body => {
1236                    let q2 = sp::Query {
1237                        body: Box::new(body),
1238                        ..q
1239                    };
1240                    InsertSource::Query(Box::new(query(q2)?))
1241                }
1242            }
1243        }
1244    };
1245    let on_conflict = match on {
1246        None => None,
1247        Some(sp::OnInsert::OnConflict(oc)) => {
1248            let sp::OnConflict {
1249                conflict_target,
1250                action,
1251            } = oc;
1252            let target = match conflict_target {
1253                None => Vec::new(),
1254                Some(sp::ConflictTarget::Columns(cols)) => cols.iter().map(ident).collect(),
1255                Some(sp::ConflictTarget::OnConstraint(_)) => {
1256                    return no("on_conflict_constraint", "ON CONFLICT ON CONSTRAINT");
1257                }
1258            };
1259            let action = match action {
1260                sp::OnConflictAction::DoNothing => ConflictAction::DoNothing,
1261                sp::OnConflictAction::DoUpdate(sp::DoUpdate {
1262                    assignments: a,
1263                    selection,
1264                }) => ConflictAction::DoUpdate {
1265                    set: assignments(a, None)?,
1266                    where_: selection.map(expr).transpose()?,
1267                },
1268            };
1269            Some(OnConflict { target, action })
1270        }
1271        Some(sp::OnInsert::DuplicateKeyUpdate(a)) => {
1272            // No conflict target: MySQL fires on ANY unique key. `VALUES(c)` is `excluded.c`.
1273            let set = assignments(a, None)?
1274                .into_iter()
1275                .map(|asg| Assignment {
1276                    column: asg.column,
1277                    value: excluded_refs(asg.value),
1278                })
1279                .collect();
1280            Some(OnConflict {
1281                target: vec![],
1282                action: ConflictAction::DoUpdate { set, where_: None },
1283            })
1284        }
1285        Some(other) => return no("on_insert", &head(&other)),
1286    };
1287    // MySQL `INSERT IGNORE`: a unique-key conflict skips the row, as `ON CONFLICT DO NOTHING`
1288    // (with no target) does. IGNORE also downgrades other errors, hence the allowance.
1289    let on_conflict = if ignore {
1290        if on_conflict.is_some() {
1291            return no(
1292                "insert_option",
1293                "IGNORE together with ON DUPLICATE KEY UPDATE",
1294            );
1295        }
1296        add_note(
1297            "insert_ignore",
1298            "INSERT IGNORE downgrades every error of the statement to a warning; ON CONFLICT DO NOTHING \
1299             skips unique-key conflicts only",
1300            Allowance::IgnoreCoversOnlyUniqueConflicts,
1301        );
1302        Some(OnConflict {
1303            target: vec![],
1304            action: ConflictAction::DoNothing,
1305        })
1306    } else {
1307        on_conflict
1308    };
1309    Ok(Statement::Insert(Insert {
1310        table,
1311        replace: replace_into,
1312        columns,
1313        source,
1314        on_conflict,
1315        returning: returning(ret)?,
1316    }))
1317}
1318
1319/// The tables of a MySQL multi-table `UPDATE`/`DELETE` source, in order, with the `ON` conditions of
1320/// its inner and cross joins pushed onto `on` (moving them into `WHERE` is equivalent for those
1321/// joins only; any outer join is refused).
1322fn flatten_inner(t: sp::TableWithJoins, on: &mut Vec<Expr>) -> R<Vec<TableRef>> {
1323    let sp::TableWithJoins { relation, joins } = t;
1324    let mut out = vec![table_factor(relation)?];
1325    for j in joins {
1326        if j.global {
1327            return no("global_join", "GLOBAL JOIN");
1328        }
1329        let c = match j.join_operator {
1330            sp::JoinOperator::Join(c)
1331            | sp::JoinOperator::Inner(c)
1332            | sp::JoinOperator::CrossJoin(c) => c,
1333            other => return no("dml_outer_join", &head(&other)),
1334        };
1335        match c {
1336            sp::JoinConstraint::On(e) => on.push(expr(e)?),
1337            sp::JoinConstraint::None => {}
1338            _ => return no("dml_join_using", "USING/NATURAL join in a DML statement"),
1339        }
1340        out.push(table_factor(j.relation)?);
1341    }
1342    Ok(out)
1343}
1344
1345/// `where_` with the moved join conditions in front, joined by AND.
1346fn with_conditions(on: Vec<Expr>, where_: Option<Expr>) -> Option<Expr> {
1347    on.into_iter().chain(where_).reduce(|l, r| Expr::Binary {
1348        op: BinaryOp::And,
1349        left: Box::new(l),
1350        right: Box::new(r),
1351    })
1352}
1353
1354/// The name a table is referred to by in its statement: its alias, else its own name.
1355fn ref_name(t: &TableRef) -> Option<&str> {
1356    match t {
1357        TableRef::Table { name, alias } => alias.as_deref().or(name.0.last().map(String::as_str)),
1358        _ => None,
1359    }
1360}
1361
1362fn update(u: sp::Update) -> R<Statement> {
1363    let sp::Update {
1364        update_token: _,
1365        optimizer_hints,
1366        table,
1367        assignments: asg,
1368        from,
1369        selection,
1370        returning: ret,
1371        output,
1372        or,
1373        order_by,
1374        limit,
1375    } = u;
1376    if !optimizer_hints.is_empty() || output.is_some() || or.is_some() {
1377        return no("update_option", "UPDATE with hints/OUTPUT/OR");
1378    }
1379    let mut on = Vec::new();
1380    let mut sources = flatten_inner(table, &mut on)?.into_iter();
1381    let (table, alias) = match sources.next() {
1382        Some(TableRef::Table { name, alias }) => (name, alias),
1383        _ => return no("dml_target", "DML target is not a plain table"),
1384    };
1385    let joined: Vec<TableRef> = sources.collect();
1386    // The parser does not know MySQL's `UPDATE LOW_PRIORITY|IGNORE t ...` and reads the modifier as
1387    // the table name and `t` as its alias: refuse rather than write another table's name.
1388    if alias.is_some()
1389        && table.0.len() == 1
1390        && matches!(table.0[0].as_str(), "low_priority" | "ignore")
1391    {
1392        return no("update_option", "UPDATE LOW_PRIORITY / IGNORE");
1393    }
1394    let from = match from {
1395        None => Vec::new(),
1396        Some(sp::UpdateTableFromKind::AfterSet(ts)) => ts
1397            .into_iter()
1398            .map(table_with_joins)
1399            .collect::<R<Vec<_>>>()?,
1400        Some(other) => return no("update_from_position", &head(&other)),
1401    };
1402    let from: Vec<TableRef> = joined.into_iter().chain(from).collect();
1403    let target = alias.as_deref().or(table.0.last().map(String::as_str));
1404    Ok(Statement::Update(Update {
1405        set: assignments(asg, target)?,
1406        from,
1407        where_: with_conditions(on, selection.map(expr).transpose()?),
1408        table,
1409        alias,
1410        order_by: order_by.into_iter().map(order_item).collect::<R<_>>()?,
1411        limit: limit.map(expr).transpose()?,
1412        returning: returning(ret)?,
1413    }))
1414}
1415
1416fn delete(d: sp::Delete) -> R<Statement> {
1417    let sp::Delete {
1418        delete_token: _,
1419        optimizer_hints,
1420        tables,
1421        from,
1422        using,
1423        selection,
1424        returning: ret,
1425        output,
1426        order_by,
1427        limit,
1428    } = d;
1429    if !optimizer_hints.is_empty() || output.is_some() {
1430        return no("delete_option", "DELETE with hints/OUTPUT");
1431    }
1432    let ts = match from {
1433        sp::FromTable::WithFromKeyword(ts) | sp::FromTable::WithoutKeyword(ts) => ts,
1434    };
1435    // MySQL's `DELETE o FROM orders o JOIN users u ON .. WHERE ..`: the named table is the target and
1436    // the other tables of the source become `USING` tables.
1437    if !tables.is_empty() {
1438        if tables.len() != 1 || using.is_some() {
1439            return no("delete_multi_target", "DELETE a, b FROM ..");
1440        }
1441        let target = object_name(&tables[0])?;
1442        let mut on = Vec::new();
1443        let mut sources = Vec::new();
1444        for t in ts {
1445            sources.extend(flatten_inner(t, &mut on)?);
1446        }
1447        let at = match target.0.as_slice() {
1448            [n] => sources.iter().position(|t| ref_name(t) == Some(n.as_str())),
1449            _ => None,
1450        };
1451        let Some(at) = at else {
1452            return no("delete_target", "DELETE target is not a table of its FROM");
1453        };
1454        let (name, alias) = match sources.remove(at) {
1455            TableRef::Table { name, alias } => (name, alias),
1456            _ => return no("dml_target", "DML target is not a plain table"),
1457        };
1458        return Ok(Statement::Delete(Delete {
1459            table: name,
1460            alias,
1461            using: sources,
1462            where_: with_conditions(on, selection.map(expr).transpose()?),
1463            order_by: order_by.into_iter().map(order_item).collect::<R<_>>()?,
1464            limit: limit.map(expr).transpose()?,
1465            returning: returning(ret)?,
1466        }));
1467    }
1468    if ts.len() != 1 {
1469        return no("delete_multi_target", "DELETE FROM a, b");
1470    }
1471    let (table, alias) = simple_table(ts.into_iter().next().unwrap())?;
1472    // MySQL's `DELETE FROM t USING <joined source>` names the target by its alias in the source, the
1473    // opposite of PostgreSQL's `USING`: not read as PostgreSQL's.
1474    if rules().name == "mysql" && using.is_some() {
1475        return no("delete_using_form", "MySQL DELETE FROM .. USING source");
1476    }
1477    let using = using
1478        .unwrap_or_default()
1479        .into_iter()
1480        .map(table_with_joins)
1481        .collect::<R<Vec<_>>>()?;
1482    Ok(Statement::Delete(Delete {
1483        table,
1484        alias,
1485        using,
1486        where_: selection.map(expr).transpose()?,
1487        order_by: order_by.into_iter().map(order_item).collect::<R<_>>()?,
1488        limit: limit.map(expr).transpose()?,
1489        returning: returning(ret)?,
1490    }))
1491}
1492
1493fn boxed(e: sp::Expr) -> R<Box<Expr>> {
1494    Ok(Box::new(expr(e)?))
1495}
1496
1497fn ceil_floor(name: &str, x: sp::Expr, field: sp::CeilFloorKind) -> R<Expr> {
1498    if !matches!(
1499        field,
1500        sp::CeilFloorKind::DateTimeField(sp::DateTimeField::NoDateTime)
1501    ) {
1502        return no("ceil_form", "CEIL/FLOOR(x TO unit) and the scale form");
1503    }
1504    Ok(Expr::Function(Function {
1505        name: name.to_string(),
1506        args: vec![FunctionArg::Expr(expr(x)?)],
1507        distinct: false,
1508        star: false,
1509        filter: None,
1510        over: None,
1511        order_by: vec![],
1512    }))
1513}
1514
1515fn is_bool(x: sp::Expr, value: bool, negated: bool) -> R<Expr> {
1516    Ok(Expr::IsBool {
1517        expr: boxed(x)?,
1518        value,
1519        negated,
1520    })
1521}
1522
1523fn expr(e: sp::Expr) -> R<Expr> {
1524    use sp::Expr as E;
1525    Ok(match e {
1526        // `DEFAULT` in VALUES / SET is parsed as an identifier; as a column it would be written
1527        // quoted and mean something else (PostgreSQL rejects it, MySQL would read a column).
1528        E::Identifier(i) if i.quote_style.is_none() && i.value.eq_ignore_ascii_case("default") => {
1529            return no("default_value", "DEFAULT as a value is not in the IR");
1530        }
1531        E::Identifier(i) => Expr::Column(Name::simple(ident(&i))),
1532        E::CompoundIdentifier(parts) => Expr::Column(Name(parts.iter().map(ident).collect())),
1533        E::Value(v) => value(v.value)?,
1534        E::Nested(inner) => Expr::Nested(boxed(*inner)?),
1535        E::IsNull(x) => Expr::IsNull {
1536            expr: boxed(*x)?,
1537            negated: false,
1538        },
1539        E::IsNotNull(x) => Expr::IsNull {
1540            expr: boxed(*x)?,
1541            negated: true,
1542        },
1543        E::IsTrue(x) => is_bool(*x, true, false)?,
1544        E::IsNotTrue(x) => is_bool(*x, true, true)?,
1545        E::IsFalse(x) => is_bool(*x, false, false)?,
1546        E::IsNotFalse(x) => is_bool(*x, false, true)?,
1547        E::InList {
1548            expr: x,
1549            list,
1550            negated,
1551        } => Expr::InList {
1552            expr: boxed(*x)?,
1553            list: list.into_iter().map(expr).collect::<R<_>>()?,
1554            negated,
1555        },
1556        E::InSubquery {
1557            expr: x,
1558            subquery,
1559            negated,
1560        } => Expr::InSubquery {
1561            expr: boxed(*x)?,
1562            query: Box::new(query(*subquery)?),
1563            negated,
1564        },
1565        E::Between {
1566            expr: x,
1567            negated,
1568            low,
1569            high,
1570        } => Expr::Between {
1571            expr: boxed(*x)?,
1572            low: boxed(*low)?,
1573            high: boxed(*high)?,
1574            negated,
1575        },
1576        E::Like {
1577            negated,
1578            any,
1579            expr: x,
1580            pattern,
1581            escape_char,
1582        } => {
1583            if any {
1584                return no("like_option", "LIKE ANY");
1585            }
1586            let ci = rules().like_ci_default;
1587            if ci {
1588                add_note(
1589                    "like",
1590                    "the source LIKE is ASCII case-insensitive only while its connection setting is at the default",
1591                    Allowance::LikeCaseSensitivityIsConnectionSetting,
1592                );
1593            }
1594            if rules().name == "mysql" {
1595                add_note(
1596                    "like",
1597                    "the source LIKE follows the column collation; read as case-sensitive (a binary collation)",
1598                    Allowance::LikeCaseSensitivityIsConnectionSetting,
1599                );
1600            }
1601            like_expr(*x, *pattern, escape_char, negated, ci)?
1602        }
1603        E::RLike {
1604            negated,
1605            expr: x,
1606            pattern,
1607            ..
1608        } => Expr::Regexp {
1609            expr: boxed(*x)?,
1610            pattern: boxed(*pattern)?,
1611            negated,
1612        },
1613        E::ILike {
1614            negated,
1615            any,
1616            expr: x,
1617            pattern,
1618            escape_char,
1619        } => {
1620            if any {
1621                return no("like_option", "ILIKE ANY");
1622            }
1623            like_expr(*x, *pattern, escape_char, negated, true)?
1624        }
1625        E::UnaryOp { op, expr: x } => {
1626            let op = match op {
1627                sp::UnaryOperator::Not => UnaryOp::Not,
1628                sp::UnaryOperator::Minus => UnaryOp::Minus,
1629                sp::UnaryOperator::Plus => UnaryOp::Plus,
1630                // `~x` is bitwise NOT on PostgreSQL and SQLite; MySQL makes it a 64-bit unsigned result.
1631                sp::UnaryOperator::BitwiseNot => {
1632                    return no(
1633                        "bitwise_not",
1634                        "`~x` differs in width and signedness per engine",
1635                    );
1636                }
1637                other => return no("unary_operator", &head(&other)),
1638            };
1639            Expr::Unary {
1640                op,
1641                expr: boxed(*x)?,
1642            }
1643        }
1644        E::BinaryOp { left, op, right }
1645            if matches!(
1646                op,
1647                sp::BinaryOperator::PGRegexMatch | sp::BinaryOperator::PGRegexNotMatch
1648            ) =>
1649        {
1650            Expr::Regexp {
1651                expr: boxed(*left)?,
1652                pattern: boxed(*right)?,
1653                negated: matches!(op, sp::BinaryOperator::PGRegexNotMatch),
1654            }
1655        }
1656        E::BinaryOp { left, op, right } => {
1657            use sp::BinaryOperator as B;
1658            let op = match op {
1659                B::Eq => BinaryOp::Eq,
1660                B::NotEq => BinaryOp::NotEq,
1661                B::Lt => BinaryOp::Lt,
1662                B::LtEq => BinaryOp::LtEq,
1663                B::Gt => BinaryOp::Gt,
1664                B::GtEq => BinaryOp::GtEq,
1665                B::And => BinaryOp::And,
1666                B::Or => BinaryOp::Or,
1667                B::Plus => BinaryOp::Plus,
1668                B::Minus => BinaryOp::Minus,
1669                B::Multiply => BinaryOp::Multiply,
1670                B::Divide => BinaryOp::Divide,
1671                B::Modulo => BinaryOp::Modulo,
1672                // MySQL's default sql_mode makes `||` a logical OR with OR's (lowest) precedence, but
1673                // sqlparser parses it as concatenation with concatenation's precedence, so the tree
1674                // is wrong either way: refuse instead of guessing.
1675                B::StringConcat if rules().name == "mysql" => {
1676                    return no(
1677                        "pipes_operator",
1678                        "MySQL `||` is logical OR (default sql_mode) but the parser gives it concatenation precedence; write OR, or CONCAT()",
1679                    );
1680                }
1681                B::StringConcat => BinaryOp::Concat,
1682                B::MyIntegerDivide => BinaryOp::IntDivide,
1683                B::Spaceship => BinaryOp::NullSafeEq,
1684                B::BitwiseAnd => BinaryOp::BitAnd,
1685                B::BitwiseOr => BinaryOp::BitOr,
1686                B::BitwiseXor | B::PGBitwiseXor => BinaryOp::BitXor,
1687                B::PGBitwiseShiftLeft => BinaryOp::ShiftLeft,
1688                B::PGBitwiseShiftRight => BinaryOp::ShiftRight,
1689                B::Xor => BinaryOp::Xor,
1690                B::Arrow => BinaryOp::JsonGet,
1691                B::LongArrow => BinaryOp::JsonGetText,
1692                other => {
1693                    return no(
1694                        "binary_operator",
1695                        &format!(
1696                            "{} (JSON/array/regex/bitwise operators are not in the IR)",
1697                            head(&other)
1698                        ),
1699                    );
1700                }
1701            };
1702            let is_hex = |e: &E| matches!(e, E::Value(v) if matches!(v.value, sp::Value::HexStringLiteral(_)));
1703            if (is_hex(&left) || is_hex(&right))
1704                && !matches!(
1705                    op,
1706                    BinaryOp::Eq
1707                        | BinaryOp::NotEq
1708                        | BinaryOp::Lt
1709                        | BinaryOp::LtEq
1710                        | BinaryOp::Gt
1711                        | BinaryOp::GtEq
1712                        | BinaryOp::NullSafeEq
1713                        | BinaryOp::And
1714                        | BinaryOp::Or
1715                        | BinaryOp::Xor
1716                )
1717            {
1718                return no(
1719                    "hex_literal_numeric",
1720                    "a hex literal is a byte string; MySQL `0x41 + 1` reads it as the number 65, so arithmetic on it is not mapped",
1721                );
1722            }
1723            if op == BinaryOp::Divide && rules().name == "mysql" {
1724                add_note(
1725                    "division",
1726                    "the source `/` is exact (DECIMAL) division; the target divides as double precision",
1727                    Allowance::DecimalDivisionAsFloat,
1728                );
1729                let l = Expr::Cast {
1730                    expr: boxed(*left)?,
1731                    ty: SqlType::Double,
1732                };
1733                return Ok(Expr::Binary {
1734                    op,
1735                    left: Box::new(l),
1736                    right: boxed(*right)?,
1737                });
1738            }
1739            // `ts +/- INTERVAL 'n unit'` is the same statement as `ts +/- make_interval(unit => n)`:
1740            // one IR form, so every target's interval arithmetic (and its refusal of months/years)
1741            // applies unchanged. `INTERVAL 'n unit' + ts` commutes for `+` only.
1742            match (op, *left, *right) {
1743                (BinaryOp::Plus | BinaryOp::Minus, l, E::Interval(iv)) => Expr::Binary {
1744                    op,
1745                    left: boxed(l)?,
1746                    right: Box::new(interval_call(iv)?),
1747                },
1748                (BinaryOp::Plus, E::Interval(iv), r) => Expr::Binary {
1749                    op,
1750                    left: boxed(r)?,
1751                    right: Box::new(interval_call(iv)?),
1752                },
1753                (op, l, r) => Expr::Binary {
1754                    op,
1755                    left: boxed(l)?,
1756                    right: boxed(r)?,
1757                },
1758            }
1759        }
1760        E::Case {
1761            case_token: _,
1762            end_token: _,
1763            operand,
1764            conditions,
1765            else_result,
1766        } => Expr::Case {
1767            operand: operand.map(|o| boxed(*o)).transpose()?,
1768            branches: conditions
1769                .into_iter()
1770                .map(|sp::CaseWhen { condition, result }| Ok((expr(condition)?, expr(result)?)))
1771                .collect::<R<_>>()?,
1772            else_: else_result.map(|o| boxed(*o)).transpose()?,
1773        },
1774        E::Cast {
1775            kind,
1776            expr: x,
1777            data_type,
1778            format,
1779        } => {
1780            if format.is_some() || !matches!(kind, sp::CastKind::Cast | sp::CastKind::DoubleColon) {
1781                return no("cast_form", "TRY_CAST / SAFE_CAST / CAST .. FORMAT");
1782            }
1783            Expr::Cast {
1784                expr: boxed(*x)?,
1785                ty: data_type_to_ir(&data_type)?,
1786            }
1787        }
1788        // MySQL `CONVERT(expr, type)` is `CAST(expr AS type)`; `CONVERT(expr USING cs)` converts the
1789        // character set, which is not a cast.
1790        E::Convert {
1791            is_try: false,
1792            expr: x,
1793            data_type: Some(data_type),
1794            charset: None,
1795            target_before_value: false,
1796            styles,
1797        } if styles.is_empty() => Expr::Cast {
1798            expr: boxed(*x)?,
1799            ty: data_type_to_ir(&data_type)?,
1800        },
1801        E::Convert { .. } => {
1802            return no(
1803                "convert_form",
1804                "CONVERT(.. USING charset), TRY_CONVERT and the target-first (MSSQL) form",
1805            );
1806        }
1807        // `CEIL(x)` / `FLOOR(x)` of a number. `CEIL(x TO unit)` and the scale form are not mapped.
1808        E::Ceil { expr: x, field } => ceil_floor("ceil", *x, field)?,
1809        E::Floor { expr: x, field } => ceil_floor("floor", *x, field)?,
1810        // `POSITION(sub IN str)` is `strpos(str, sub)`.
1811        E::Position { expr: sub, r#in } => Expr::Function(Function {
1812            name: "strpos".to_string(),
1813            args: vec![
1814                FunctionArg::Expr(expr(*r#in)?),
1815                FunctionArg::Expr(expr(*sub)?),
1816            ],
1817            distinct: false,
1818            star: false,
1819            filter: None,
1820            over: None,
1821            order_by: vec![],
1822        }),
1823        E::Trim {
1824            trim_where,
1825            trim_what,
1826            expr: x,
1827            trim_characters,
1828        } => {
1829            // `trim(chars FROM x)` / `trim(BOTH chars FROM x)`; the comma form is `trim_characters`.
1830            let side = match trim_where {
1831                None | Some(sp::TrimWhereField::Both) => TrimSide::Both,
1832                Some(sp::TrimWhereField::Leading) => TrimSide::Leading,
1833                Some(sp::TrimWhereField::Trailing) => TrimSide::Trailing,
1834            };
1835            let chars = match (trim_what, trim_characters) {
1836                (Some(w), None) => Some(boxed(*w)?),
1837                (None, None) => None,
1838                (None, Some(mut cs)) if cs.len() == 1 => Some(boxed(cs.remove(0))?),
1839                _ => return no("trim_form", "TRIM with several character arguments"),
1840            };
1841            Expr::Trim {
1842                expr: boxed(*x)?,
1843                side,
1844                chars,
1845            }
1846        }
1847        E::Substring {
1848            expr: x,
1849            substring_from,
1850            substring_for,
1851            special: _,
1852            shorthand: _,
1853        } => {
1854            if rules().name == "mysql" {
1855                // From position 1 and a non-negative length the engines agree; any other start
1856                // (0, negative, an expression) or length is the caller's stated assumption.
1857                let positive = |e: &E, min: u64| {
1858                    matches!(
1859                        e,
1860                        E::Value(v) if matches!(&v.value, sp::Value::Number(n, _)
1861                            if n.parse::<u64>().is_ok_and(|x| x >= min))
1862                    )
1863                };
1864                let start_ok = substring_from.as_deref().is_some_and(|f| positive(f, 1));
1865                let len_ok = substring_for.as_deref().is_none_or(|l| positive(l, 0));
1866                if !(start_ok && len_ok) {
1867                    add_note(
1868                        "fn:substr.start_position",
1869                        "MySQL SUBSTRING counts a start of 0 as empty and a negative start from the end; \
1870                         the target does not",
1871                        Allowance::SubstringStartAssumedPositive,
1872                    );
1873                }
1874            }
1875            // `substring(x FROM a FOR b)` and `substring(x, a, b)` are `substr(x, a, b)`.
1876            let mut args = vec![FunctionArg::Expr(expr(*x)?)];
1877            match (substring_from, substring_for) {
1878                (Some(f), Some(l)) => {
1879                    args.push(FunctionArg::Expr(expr(*f)?));
1880                    args.push(FunctionArg::Expr(expr(*l)?));
1881                }
1882                (Some(f), None) => args.push(FunctionArg::Expr(expr(*f)?)),
1883                (None, Some(l)) => {
1884                    // FOR without FROM starts at position 1.
1885                    args.push(FunctionArg::Expr(Expr::Literal(Literal::Number(
1886                        "1".into(),
1887                    ))));
1888                    args.push(FunctionArg::Expr(expr(*l)?));
1889                }
1890                (None, None) => return no("substring_form", "SUBSTRING without FROM or FOR"),
1891            }
1892            Expr::Function(Function {
1893                name: "substr".into(),
1894                args,
1895                distinct: false,
1896                star: false,
1897                filter: None,
1898                over: None,
1899                order_by: vec![],
1900            })
1901        }
1902        E::AnyOp {
1903            left,
1904            compare_op,
1905            right,
1906            is_some: _,
1907        } => {
1908            if !matches!(compare_op, sp::BinaryOperator::Eq) {
1909                return no("any_operator", "only `= ANY(..)` is in the IR");
1910            }
1911            quantified(*left, *right, false)?
1912        }
1913        E::AllOp {
1914            left,
1915            compare_op,
1916            right,
1917        } => {
1918            if !matches!(compare_op, sp::BinaryOperator::NotEq) {
1919                return no("all_operator", "only `<> ALL(..)` is in the IR");
1920            }
1921            quantified(*left, *right, true)?
1922        }
1923        E::Exists { subquery, negated } => Expr::Exists {
1924            query: Box::new(query(*subquery)?),
1925            negated,
1926        },
1927        E::Subquery(q) => Expr::Subquery(Box::new(query(*q)?)),
1928        E::Extract {
1929            field,
1930            syntax: _,
1931            expr: x,
1932        } => Expr::Extract {
1933            field: field.to_string().to_ascii_lowercase(),
1934            expr: boxed(*x)?,
1935        },
1936        E::Array(arr) => Expr::ArrayLiteral(arr.elem.into_iter().map(expr).collect::<R<Vec<_>>>()?),
1937        E::AtTimeZone {
1938            timestamp,
1939            time_zone,
1940        } => Expr::AtTimeZone {
1941            expr: boxed(*timestamp)?,
1942            zone: boxed(*time_zone)?,
1943        },
1944        E::Function(f) => function(f)?,
1945        other => return no("expression", &head(&other)),
1946    })
1947}
1948
1949/// `x = ANY(rhs)` / `x <> ALL(rhs)`: an array parameter, an array literal, or a subquery.
1950fn quantified(left: sp::Expr, right: sp::Expr, negated: bool) -> R<Expr> {
1951    let lhs = boxed(left)?;
1952    // `$1::int[]` is only a type hint on the parameter.
1953    let mut right = right;
1954    if let sp::Expr::Cast {
1955        kind: sp::CastKind::Cast | sp::CastKind::DoubleColon,
1956        expr: inner,
1957        data_type: sp::DataType::Array(_),
1958        format: None,
1959    } = right.clone()
1960    {
1961        right = *inner;
1962    }
1963    match right {
1964        sp::Expr::Value(v) => match v.value {
1965            sp::Value::Placeholder(_) => match value(v.value)? {
1966                Expr::Param(n) => Ok(Expr::AnyParam {
1967                    expr: lhs,
1968                    param: n,
1969                    negated,
1970                }),
1971                _ => no("array_param", "expected a numbered parameter"),
1972            },
1973            other => no("array_operand", &head(&other)),
1974        },
1975        sp::Expr::Array(sp::Array { elem, named: _ }) => Ok(Expr::InList {
1976            expr: lhs,
1977            list: elem.into_iter().map(expr).collect::<R<Vec<_>>>()?,
1978            negated,
1979        }),
1980        sp::Expr::Subquery(q) => Ok(Expr::InSubquery {
1981            expr: lhs,
1982            query: Box::new(query(*q)?),
1983            negated,
1984        }),
1985        other => no("array_operand", &head(&other)),
1986    }
1987}
1988
1989/// MySQL's `VALUES(col)` inside `ON DUPLICATE KEY UPDATE` refers to the row proposed for insertion.
1990fn excluded_refs(e: Expr) -> Expr {
1991    match e {
1992        Expr::Function(f) if f.name == "values" && f.args.len() == 1 => {
1993            match f.args.into_iter().next() {
1994                Some(FunctionArg::Expr(Expr::Column(n))) => {
1995                    Expr::Column(Name(vec!["excluded".to_string(), n.last().to_string()]))
1996                }
1997                other => Expr::Function(Function {
1998                    name: "values".into(),
1999                    args: other.into_iter().collect(),
2000                    distinct: false,
2001                    star: false,
2002                    filter: None,
2003                    over: None,
2004                    order_by: vec![],
2005                }),
2006            }
2007        }
2008        Expr::Binary { op, left, right } => Expr::Binary {
2009            op,
2010            left: Box::new(excluded_refs(*left)),
2011            right: Box::new(excluded_refs(*right)),
2012        },
2013        Expr::Nested(x) => Expr::Nested(Box::new(excluded_refs(*x))),
2014        Expr::Unary { op, expr } => Expr::Unary {
2015            op,
2016            expr: Box::new(excluded_refs(*expr)),
2017        },
2018        other => other,
2019    }
2020}
2021
2022fn like_expr(
2023    x: sp::Expr,
2024    pattern: sp::Expr,
2025    escape_char: Option<Box<sp::Expr>>,
2026    negated: bool,
2027    case_insensitive: bool,
2028) -> R<Expr> {
2029    let escape = match escape_char {
2030        Some(e) => Some(boxed(*e)?),
2031        None if rules().like_default_escape => {
2032            Some(Box::new(Expr::Literal(Literal::String("\\".into()))))
2033        }
2034        None => None,
2035    };
2036    Ok(Expr::Like {
2037        expr: boxed(x)?,
2038        pattern: boxed(pattern)?,
2039        negated,
2040        case_insensitive,
2041        escape,
2042    })
2043}
2044
2045fn value(v: sp::Value) -> R<Expr> {
2046    Ok(match v {
2047        sp::Value::Number(n, long) => {
2048            if long {
2049                return no("numeric_suffix", "L suffix");
2050            }
2051            Expr::Literal(Literal::Number(n))
2052        }
2053        sp::Value::SingleQuotedString(s) => Expr::Literal(Literal::String(s)),
2054        // The tokenizer has already resolved the backslash escapes of `E'..'`.
2055        sp::Value::EscapedStringLiteral(s) => Expr::Literal(Literal::String(s)),
2056        // PostgreSQL's `X'..'` is a bit string, not bytes; MySQL and SQLite read it as a byte string.
2057        sp::Value::HexStringLiteral(h) if rules().name == "postgres" => {
2058            return no(
2059                "hex_literal",
2060                &format!("X'{h}' is a bit string on PostgreSQL; write '\\x{h}'::bytea"),
2061            );
2062        }
2063        sp::Value::HexStringLiteral(h) => {
2064            if h.is_empty() || h.len() % 2 != 0 || !h.bytes().all(|c| c.is_ascii_hexdigit()) {
2065                return no(
2066                    "hex_literal",
2067                    &format!("`{h}` is not an even-length run of hex digits"),
2068                );
2069            }
2070            let bytes = (0..h.len())
2071                .step_by(2)
2072                .map(|i| u8::from_str_radix(&h[i..i + 2], 16))
2073                .collect::<Result<Vec<u8>, _>>()
2074                .or_else(|_| no("hex_literal", "invalid hex digits"))?;
2075            Expr::Literal(Literal::Bytes(bytes))
2076        }
2077        // A bit string is `bit(n)` on PostgreSQL and a byte string on MySQL; the two disagree on width.
2078        sp::Value::SingleQuotedByteStringLiteral(b)
2079        | sp::Value::DoubleQuotedByteStringLiteral(b) => {
2080            return no(
2081                "bit_literal",
2082                &format!("B'{b}' reads differently on each engine"),
2083            );
2084        }
2085        sp::Value::Boolean(b) if rules().name == "mysql" => {
2086            // MySQL TRUE/FALSE are the integers 1 and 0.
2087            Expr::Literal(Literal::Number(if b { "1" } else { "0" }.into()))
2088        }
2089        sp::Value::Boolean(b) => Expr::Literal(Literal::Bool(b)),
2090        sp::Value::Null => Expr::Literal(Literal::Null),
2091        sp::Value::Placeholder(p) => {
2092            let bad = || ReadError::NotInIr {
2093                construct: "placeholder".into(),
2094                detail: format!(
2095                    "`{p}` is not a placeholder form of dialect `{}`",
2096                    rules().name
2097                ),
2098            };
2099            if rules().question_placeholders {
2100                if p == "?" {
2101                    Expr::Param(bump_param(None, None))
2102                } else if let Some(d) = p.strip_prefix('?') {
2103                    let n = d.parse::<u32>().ok().filter(|n| *n >= 1).ok_or_else(bad)?;
2104                    Expr::Param(bump_param(Some(n), None))
2105                } else if let Some(nm) = p.strip_prefix([':', '@', '$']) {
2106                    Expr::Param(bump_param(None, Some(nm)))
2107                } else {
2108                    return Err(bad());
2109                }
2110            } else {
2111                let n = p
2112                    .strip_prefix('$')
2113                    .and_then(|d| d.parse::<u32>().ok())
2114                    .filter(|n| *n >= 1)
2115                    .ok_or_else(bad)?;
2116                Expr::Param(n)
2117            }
2118        }
2119        other => return no("literal", &head(&other)),
2120    })
2121}
2122
2123fn function(f: sp::Function) -> R<Expr> {
2124    let sp::Function {
2125        name,
2126        uses_odbc_syntax,
2127        parameters,
2128        args,
2129        within_group,
2130        filter,
2131        null_treatment,
2132        over,
2133    } = f;
2134    if uses_odbc_syntax
2135        || !matches!(parameters, sp::FunctionArguments::None)
2136        || !within_group.is_empty()
2137        || null_treatment.is_some()
2138    {
2139        return no(
2140            "function_option",
2141            "ODBC syntax/parameters/WITHIN GROUP/FILTER/IGNORE NULLS",
2142        );
2143    }
2144    let over = match over {
2145        None => None,
2146        Some(sp::WindowType::NamedWindow(_)) => {
2147            return no(
2148                "named_window",
2149                "OVER w (a WINDOW clause) is not in the IR yet",
2150            );
2151        }
2152        Some(sp::WindowType::WindowSpec(spec)) => Some(window_spec(spec)?),
2153    };
2154    let filter = filter.map(|f| expr(*f).map(Box::new)).transpose()?;
2155    let name = object_name(&name)?;
2156    let base = name.last().to_string();
2157    if name.0.len() != 1 {
2158        return no("qualified_function", &name.0.join("."));
2159    }
2160    let args = match args {
2161        // `CURRENT_TIMESTAMP` is a function without parentheses.
2162        sp::FunctionArguments::None => {
2163            if base == "current_timestamp" {
2164                if rules().name == "sqlite" {
2165                    add_note(
2166                        "fn:now",
2167                        "the source CURRENT_TIMESTAMP is text 'YYYY-MM-DD HH:MM:SS' (UTC), not a timestamptz",
2168                        Allowance::NowAsText,
2169                    );
2170                }
2171                return Ok(Expr::Function(Function {
2172                    name: "now".to_string(),
2173                    args: vec![],
2174                    distinct: false,
2175                    star: false,
2176                    filter: None,
2177                    over: None,
2178                    order_by: vec![],
2179                }));
2180            }
2181            // The session identity keywords are zero-argument functions.
2182            if matches!(
2183                base.as_str(),
2184                "current_user" | "session_user" | "current_role"
2185            ) {
2186                return Ok(Expr::Function(Function {
2187                    name: base,
2188                    args: vec![],
2189                    distinct: false,
2190                    star: false,
2191                    filter: None,
2192                    over: None,
2193                    order_by: vec![],
2194                }));
2195            }
2196            // `CURRENT_DATE`: a date in the session time zone (PostgreSQL, MySQL), text of the UTC
2197            // date on SQLite.
2198            if base == "current_date" {
2199                if rules().name == "sqlite" {
2200                    add_note(
2201                        "fn:current_date",
2202                        "the source CURRENT_DATE is text 'YYYY-MM-DD' (the UTC date), not a date",
2203                        Allowance::DateAsText,
2204                    );
2205                }
2206                return Ok(Expr::Function(Function {
2207                    name: "current_date".to_string(),
2208                    args: vec![],
2209                    distinct: false,
2210                    star: false,
2211                    filter: None,
2212                    over: None,
2213                    order_by: vec![],
2214                }));
2215            }
2216            return no("bare_function", &format!("`{base}` without parentheses"));
2217        }
2218        sp::FunctionArguments::Subquery(q) if base == "array" => {
2219            return Ok(Expr::ArraySubquery(Box::new(query(*q)?)));
2220        }
2221        sp::FunctionArguments::Subquery(_) => {
2222            return no("function_subquery", "function over a subquery");
2223        }
2224        sp::FunctionArguments::List(l) => l,
2225    };
2226    let sp::FunctionArgumentList {
2227        duplicate_treatment,
2228        args,
2229        clauses,
2230    } = args;
2231    let mut agg_order = Vec::new();
2232    let mut separator: Option<String> = None;
2233    for cl in clauses {
2234        match cl {
2235            sp::FunctionArgumentClause::OrderBy(items) => {
2236                for it in items {
2237                    agg_order.push(order_item(it)?);
2238                }
2239            }
2240            sp::FunctionArgumentClause::Separator(v) => match v.value {
2241                sp::Value::SingleQuotedString(s) => separator = Some(s),
2242                other => return no("separator", &head(&other)),
2243            },
2244            other => {
2245                return no(
2246                    "function_clause",
2247                    &format!("{} inside a function call", head(&other)),
2248                );
2249            }
2250        }
2251    }
2252    let distinct = match duplicate_treatment {
2253        None | Some(sp::DuplicateTreatment::All) => false,
2254        Some(sp::DuplicateTreatment::Distinct) => true,
2255    };
2256    // MySQL `DATE_ADD(x, INTERVAL n unit)` / `ADDDATE(..)` and `DATE_SUB` / `SUBDATE(..)` are
2257    // `x +/- INTERVAL n unit`: the same IR form as the operator, so the targets' interval
2258    // arithmetic (and its refusal of months/years) applies unchanged.
2259    if rules().name == "mysql"
2260        && !distinct
2261        && agg_order.is_empty()
2262        && matches!(
2263            base.as_str(),
2264            "date_add" | "adddate" | "date_sub" | "subdate"
2265        )
2266        && args.len() == 2
2267        && matches!(
2268            args[1],
2269            sp::FunctionArg::Unnamed(sp::FunctionArgExpr::Expr(sp::Expr::Interval(_)))
2270        )
2271    {
2272        let op = if matches!(base.as_str(), "date_add" | "adddate") {
2273            BinaryOp::Plus
2274        } else {
2275            BinaryOp::Minus
2276        };
2277        let mut it = args.into_iter();
2278        let (Some(sp::FunctionArg::Unnamed(sp::FunctionArgExpr::Expr(l))), Some(second)) =
2279            (it.next(), it.next())
2280        else {
2281            return no("function_argument", "first argument of a date function");
2282        };
2283        let sp::FunctionArg::Unnamed(sp::FunctionArgExpr::Expr(sp::Expr::Interval(iv))) = second
2284        else {
2285            unreachable!("matched above")
2286        };
2287        return Ok(Expr::Binary {
2288            op,
2289            left: Box::new(expr(l)?),
2290            right: Box::new(interval_call(iv)?),
2291        });
2292    }
2293    // `ADDDATE(x, 7)` / `SUBDATE(x, 7)` with a plain number are `x +/- INTERVAL 7 DAY`.
2294    if rules().name == "mysql"
2295        && !distinct
2296        && agg_order.is_empty()
2297        && matches!(base.as_str(), "adddate" | "subdate")
2298        && args.len() == 2
2299        && matches!(
2300            &args[1],
2301            sp::FunctionArg::Unnamed(sp::FunctionArgExpr::Expr(sp::Expr::Value(v)))
2302                if matches!(v.value, sp::Value::Number(..))
2303        )
2304    {
2305        let op = if base == "adddate" {
2306            BinaryOp::Plus
2307        } else {
2308            BinaryOp::Minus
2309        };
2310        let mut it = args.into_iter();
2311        let (
2312            Some(sp::FunctionArg::Unnamed(sp::FunctionArgExpr::Expr(l))),
2313            Some(sp::FunctionArg::Unnamed(sp::FunctionArgExpr::Expr(sp::Expr::Value(v)))),
2314        ) = (it.next(), it.next())
2315        else {
2316            return no("function_argument", "argument of a date function");
2317        };
2318        let sp::Value::Number(n, _) = v.value else {
2319            unreachable!("matched above")
2320        };
2321        return Ok(Expr::Binary {
2322            op,
2323            left: Box::new(expr(l)?),
2324            right: Box::new(make_interval("days", n)),
2325        });
2326    }
2327    let mut out = Vec::new();
2328    let mut star = false;
2329    for a in args {
2330        match a {
2331            sp::FunctionArg::Unnamed(sp::FunctionArgExpr::Expr(e)) => {
2332                out.push(FunctionArg::Expr(expr(e)?))
2333            }
2334            sp::FunctionArg::Unnamed(sp::FunctionArgExpr::Wildcard) => star = true,
2335            sp::FunctionArg::Named {
2336                name,
2337                arg: sp::FunctionArgExpr::Expr(e),
2338                operator: sp::FunctionArgOperator::RightArrow,
2339            } => out.push(FunctionArg::Named {
2340                name: ident(&name),
2341                value: expr(e)?,
2342            }),
2343            // sqlparser produces `ExprNamed` when the name is parsed as an expression.
2344            sp::FunctionArg::ExprNamed {
2345                name: sp::Expr::Identifier(name),
2346                arg: sp::FunctionArgExpr::Expr(e),
2347                operator: sp::FunctionArgOperator::RightArrow,
2348            } => out.push(FunctionArg::Named {
2349                name: ident(&name),
2350                value: expr(e)?,
2351            }),
2352            other => return no("function_argument", &head(&other)),
2353        }
2354    }
2355    let mut name = base;
2356    if name == "current_timestamp" {
2357        name = "now".to_string();
2358    }
2359    // CEILING is CEIL; MOD(a, b) is `a % b`.
2360    if name == "ceiling" {
2361        name = "ceil".to_string();
2362    }
2363    if name == "mod" && out.len() == 2 {
2364        let mut it = out.into_iter().map(|a| match a {
2365            FunctionArg::Expr(e) => e,
2366            FunctionArg::Named { value, .. } => value,
2367        });
2368        let (l, r) = (it.next().unwrap(), it.next().unwrap());
2369        return Ok(Expr::Binary {
2370            op: BinaryOp::Modulo,
2371            left: Box::new(l),
2372            right: Box::new(r),
2373        });
2374    }
2375    if rules().name == "mysql" {
2376        match name.as_str() {
2377            "ifnull" => name = "coalesce".to_string(),
2378            "curdate" if out.is_empty() => name = "current_date".to_string(),
2379            "group_concat" => {
2380                name = "string_agg".to_string();
2381                out.push(FunctionArg::Expr(Expr::Literal(Literal::String(
2382                    separator.clone().unwrap_or_else(|| ",".to_string()),
2383                ))));
2384                add_note(
2385                    "fn:string_agg",
2386                    "the source GROUP_CONCAT truncates silently at group_concat_max_len",
2387                    Allowance::AggregateStringTruncation,
2388                );
2389            }
2390            // MySQL LENGTH() counts bytes.
2391            "length" => name = "octet_length".to_string(),
2392            // `LOCATE(sub, str)` and `INSTR(str, sub)` are `strpos(str, sub)`: 1-based, 0 when absent,
2393            // NULL when either is NULL. The three-argument LOCATE (start position) is not mapped.
2394            "locate" | "instr" if out.len() == 2 => {
2395                if name == "locate" {
2396                    out.swap(0, 1);
2397                }
2398                name = "strpos".to_string();
2399                add_note(
2400                    "fn:strpos",
2401                    "the source LOCATE/INSTR compare by the column collation (case-insensitive unless binary)",
2402                    Allowance::TextCollation,
2403                );
2404            }
2405            "concat" if out.len() >= 2 => {
2406                let mut it = out.into_iter().map(|a| match a {
2407                    FunctionArg::Expr(e) => e,
2408                    FunctionArg::Named { value, .. } => value,
2409                });
2410                let first = it.next().expect("two arguments");
2411                let chain = it.fold(first, |l, r| Expr::Binary {
2412                    op: BinaryOp::Concat,
2413                    left: Box::new(l),
2414                    right: Box::new(r),
2415                });
2416                return Ok(chain);
2417            }
2418            "if" if out.len() == 3 => {
2419                let mut it = out.into_iter().map(|a| match a {
2420                    FunctionArg::Expr(e) => e,
2421                    FunctionArg::Named { value, .. } => value,
2422                });
2423                let (c, t, f) = (it.next().unwrap(), it.next().unwrap(), it.next().unwrap());
2424                return Ok(Expr::Case {
2425                    operand: None,
2426                    branches: vec![(c, t)],
2427                    else_: Some(Box::new(f)),
2428                });
2429            }
2430            _ => {}
2431        }
2432    }
2433    if rules().name == "sqlite" {
2434        match name.as_str() {
2435            "ifnull" => name = "coalesce".to_string(),
2436            "instr" if out.len() == 2 => name = "strpos".to_string(),
2437            "group_concat" => {
2438                name = "string_agg".to_string();
2439                if out.len() == 1 {
2440                    out.push(FunctionArg::Expr(Expr::Literal(Literal::String(
2441                        ",".into(),
2442                    ))));
2443                }
2444            }
2445            _ => {}
2446        }
2447    }
2448    Ok(Expr::Function(Function {
2449        name,
2450        args: out,
2451        distinct,
2452        star,
2453        filter,
2454        over,
2455        order_by: agg_order,
2456    }))
2457}
2458
2459/// `INTERVAL '3 days'` / `INTERVAL 3 DAY` as `make_interval(days => 3)`. Only a single `<number> <unit>`
2460/// is represented; compound intervals (`'1 day 2 hours'`), ranges (`DAY TO SECOND`) and unit-less
2461/// strings are not in the IR. Months and years are read (and then refused by the targets, whose
2462/// month arithmetic differs), not guessed.
2463fn interval_call(iv: sp::Interval) -> R<Expr> {
2464    let sp::Interval {
2465        value,
2466        leading_field,
2467        leading_precision,
2468        last_field,
2469        fractional_seconds_precision,
2470    } = iv;
2471    if leading_precision.is_some() || last_field.is_some() || fractional_seconds_precision.is_some()
2472    {
2473        return no(
2474            "interval",
2475            "interval ranges and precisions are not in the IR",
2476        );
2477    }
2478    let (amount, unit) = match (*value, leading_field) {
2479        // MySQL `INTERVAL 3 DAY`
2480        (sp::Expr::Value(v), Some(field)) => (
2481            match v.value {
2482                sp::Value::Number(n, _) => n,
2483                other => return no("interval", &head(&other)),
2484            },
2485            field.to_string(),
2486        ),
2487        // PostgreSQL / standard `INTERVAL '3 days'`
2488        (sp::Expr::Value(v), None) => match v.value {
2489            sp::Value::SingleQuotedString(s) => {
2490                let mut it = s.split_whitespace();
2491                match (it.next(), it.next(), it.next()) {
2492                    (Some(a), Some(u), None) => (a.to_string(), u.to_string()),
2493                    _ => {
2494                        return no(
2495                            "interval",
2496                            "only a single `<number> <unit>` interval is in the IR",
2497                        );
2498                    }
2499                }
2500            }
2501            other => return no("interval", &head(&other)),
2502        },
2503        (other, _) => return no("interval", &head(&other)),
2504    };
2505    let numeric = !amount.is_empty()
2506        && amount
2507            .trim_start_matches('-')
2508            .chars()
2509            .all(|c| c.is_ascii_digit() || c == '.')
2510        && amount
2511            .trim_start_matches('-')
2512            .chars()
2513            .any(|c| c.is_ascii_digit());
2514    if !numeric {
2515        return no("interval", "the interval amount is not a plain number");
2516    }
2517    let unit = match unit.to_ascii_lowercase().trim_end_matches('s') {
2518        "second" | "sec" => "secs",
2519        "minute" | "min" => "mins",
2520        "hour" => "hours",
2521        "day" => "days",
2522        "week" => "weeks",
2523        "month" | "mon" => "months",
2524        "year" => "years",
2525        other => return no("interval", &format!("unit `{other}` is not in the IR")),
2526    };
2527    Ok(make_interval(unit, amount))
2528}
2529
2530fn make_interval(unit: &str, amount: String) -> Expr {
2531    Expr::Function(Function {
2532        name: "make_interval".to_string(),
2533        args: vec![FunctionArg::Named {
2534            name: unit.to_string(),
2535            value: Expr::Literal(Literal::Number(amount)),
2536        }],
2537        distinct: false,
2538        star: false,
2539        filter: None,
2540        over: None,
2541        order_by: vec![],
2542    })
2543}
2544
2545fn window_spec(w: sp::WindowSpec) -> R<Window> {
2546    let sp::WindowSpec {
2547        window_name,
2548        partition_by,
2549        order_by,
2550        window_frame,
2551    } = w;
2552    if window_name.is_some() {
2553        return no("named_window", "OVER (w ..) refinement of a named window");
2554    }
2555    let frame = match window_frame {
2556        None => None,
2557        Some(sp::WindowFrame {
2558            units,
2559            start_bound,
2560            end_bound,
2561        }) => {
2562            let bound = |b: sp::WindowFrameBound| -> R<FrameBound> {
2563                Ok(match b {
2564                    sp::WindowFrameBound::CurrentRow => FrameBound::CurrentRow,
2565                    sp::WindowFrameBound::Preceding(e) => {
2566                        FrameBound::Preceding(e.map(|x| expr(*x).map(Box::new)).transpose()?)
2567                    }
2568                    sp::WindowFrameBound::Following(e) => {
2569                        FrameBound::Following(e.map(|x| expr(*x).map(Box::new)).transpose()?)
2570                    }
2571                })
2572            };
2573            Some(Frame {
2574                units: match units {
2575                    sp::WindowFrameUnits::Rows => FrameUnits::Rows,
2576                    sp::WindowFrameUnits::Range => FrameUnits::Range,
2577                    sp::WindowFrameUnits::Groups => FrameUnits::Groups,
2578                },
2579                start: bound(start_bound)?,
2580                end: end_bound.map(bound).transpose()?,
2581            })
2582        }
2583    };
2584    Ok(Window {
2585        partition_by: partition_by.into_iter().map(expr).collect::<R<_>>()?,
2586        order_by: order_by.into_iter().map(order_item).collect::<R<_>>()?,
2587        frame,
2588    })
2589}
2590
2591fn data_type_to_ir(t: &sp::DataType) -> R<SqlType> {
2592    use sp::DataType as D;
2593    Ok(match t {
2594        D::SmallInt(_) | D::Int2(_) => SqlType::SmallInt,
2595        D::Int(_) | D::Integer(_) | D::Int4(_) => SqlType::Integer,
2596        D::BigInt(_) | D::Int8(_) => SqlType::BigInt,
2597        D::Real | D::Float4 => SqlType::Real,
2598        D::Double(_) | D::DoublePrecision | D::Float8 => SqlType::Double,
2599        D::Numeric(info) | D::Decimal(info) => match info {
2600            sp::ExactNumberInfo::None => SqlType::Numeric {
2601                precision: None,
2602                scale: None,
2603            },
2604            sp::ExactNumberInfo::Precision(p) => SqlType::Numeric {
2605                precision: Some(*p as u32),
2606                scale: None,
2607            },
2608            sp::ExactNumberInfo::PrecisionAndScale(p, s) => SqlType::Numeric {
2609                precision: Some(*p as u32),
2610                scale: Some(*s as u32),
2611            },
2612        },
2613        D::Text => SqlType::Text,
2614        D::Signed | D::SignedInteger | D::Unsigned | D::UnsignedInteger => SqlType::BigInt,
2615        D::Char(None) | D::Character(None) => SqlType::Text,
2616        // MySQL `CAST(x AS CHAR(n))` truncates to n characters without padding: a varchar(n) cast.
2617        // PostgreSQL's `char(n)` pads, so it stays refused there.
2618        D::Char(Some(sp::CharacterLength::IntegerLength { length, unit: None }))
2619        | D::Character(Some(sp::CharacterLength::IntegerLength { length, unit: None }))
2620            if rules().name == "mysql" =>
2621        {
2622            SqlType::Varchar(Some(*length as u32))
2623        }
2624        D::Datetime(None) => SqlType::Timestamp { with_tz: false },
2625        D::Varchar(None) | D::CharVarying(None) => SqlType::Varchar(None),
2626        D::Varchar(Some(sp::CharacterLength::IntegerLength { length, unit: None }))
2627        | D::CharVarying(Some(sp::CharacterLength::IntegerLength { length, unit: None })) => {
2628            SqlType::Varchar(Some(*length as u32))
2629        }
2630        D::Boolean | D::Bool => SqlType::Boolean,
2631        D::Date => SqlType::Date,
2632        D::Timestamp(None, tz) => SqlType::Timestamp {
2633            with_tz: match tz {
2634                sp::TimezoneInfo::None | sp::TimezoneInfo::WithoutTimeZone => false,
2635                sp::TimezoneInfo::WithTimeZone | sp::TimezoneInfo::Tz => true,
2636            },
2637        },
2638        D::Uuid => SqlType::Uuid,
2639        D::Bytea => SqlType::Bytes,
2640        D::JSON => SqlType::Json,
2641        D::JSONB => SqlType::Jsonb,
2642        other => return no("data_type", &format!("{other}")),
2643    })
2644}