oxisql-parse 0.1.1

SQL parsing utilities for OxiSQL — thin sqlparser facade
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! SQL statement planner: converts parsed AST statements to [`LogicalPlan`]s.

use oxisql_core::OxiSqlError;
use sqlparser::ast::Expr;

use crate::plan::{JoinType, LogicalPlan, SortExpr, WindowFunctionDef};
use crate::{agg, setops, window};

// ── Planner functions ────────────────────────────────────────────────────────

/// Transform a parsed [`sqlparser::ast::Statement`] into a [`LogicalPlan`].
///
/// Supports `SELECT` (via [`plan_query`]), `INSERT`, `UPDATE`, and `DELETE`.
/// All other statement types return [`LogicalPlan::Empty`].
///
/// # Errors
///
/// Returns [`OxiSqlError::Parse`] if the statement structure is unexpected.
pub fn plan_statement(stmt: &sqlparser::ast::Statement) -> Result<LogicalPlan, OxiSqlError> {
    match stmt {
        sqlparser::ast::Statement::Query(query) => plan_query(query),
        sqlparser::ast::Statement::Insert(insert) => {
            let columns: Vec<String> = insert
                .columns
                .iter()
                .filter_map(|col| {
                    col.0
                        .last()
                        .and_then(|p| p.as_ident())
                        .map(|id| id.value.clone())
                })
                .collect();
            let rows = insert
                .source
                .as_ref()
                .and_then(|src| {
                    if let sqlparser::ast::SetExpr::Values(ref vals) = *src.body {
                        Some(vals.rows.len())
                    } else {
                        None
                    }
                })
                .unwrap_or(0);
            Ok(LogicalPlan::Values { columns, rows })
        }
        sqlparser::ast::Statement::Update(update) => {
            let (table, alias) = table_factor_name_alias(&update.table.relation);
            let base = LogicalPlan::Scan {
                table,
                alias,
                limit: None,
            };
            Ok(match &update.selection {
                Some(pred) => LogicalPlan::Filter {
                    input: Box::new(base),
                    predicate: pred.to_string(),
                },
                None => base,
            })
        }
        sqlparser::ast::Statement::Delete(delete) => {
            let base = match &delete.from {
                sqlparser::ast::FromTable::WithFromKeyword(twjs) => build_scan_from_twjs(twjs),
                sqlparser::ast::FromTable::WithoutKeyword(twjs) => build_scan_from_twjs(twjs),
            };
            Ok(match &delete.selection {
                Some(pred) => LogicalPlan::Filter {
                    input: Box::new(base),
                    predicate: pred.to_string(),
                },
                None => base,
            })
        }
        _ => Ok(LogicalPlan::Empty),
    }
}

/// Build a [`LogicalPlan`] from a parsed `SELECT`/`UNION`/subquery.
///
/// # Errors
///
/// Returns [`OxiSqlError::Parse`] for unsupported `SetExpr` variants.
pub fn plan_query(query: &sqlparser::ast::Query) -> Result<LogicalPlan, OxiSqlError> {
    let mut plan = plan_set_expr(&query.body)?;

    // ORDER BY
    if let Some(ref order_by) = query.order_by {
        if let sqlparser::ast::OrderByKind::Expressions(ref exprs) = order_by.kind {
            if !exprs.is_empty() {
                let sort_keys: Vec<SortExpr> = exprs
                    .iter()
                    .map(|e| SortExpr {
                        column: e.expr.to_string(),
                        ascending: e.options.asc.unwrap_or(true),
                    })
                    .collect();
                plan = LogicalPlan::Sort {
                    input: Box::new(plan),
                    order_by: sort_keys,
                };
            }
        }
    }

    // LIMIT / OFFSET
    if let Some(ref limit_clause) = query.limit_clause {
        let (count, offset) = extract_limit_offset(limit_clause);
        if count.is_some() || offset.is_some() {
            plan = LogicalPlan::Limit {
                input: Box::new(plan),
                count,
                offset,
            };
        }
    }

    // WITH / CTE — wrap the already-built body plan in Cte nodes, one per CTE
    // in declaration order (last CTE wraps the body, each prior one wraps the next).
    if let Some(ref with) = query.with {
        let recursive = with.recursive;
        for cte in with.cte_tables.iter().rev() {
            let cte_name = cte.alias.name.value.clone();
            let inner_plan = plan_query(&cte.query)?;
            plan = LogicalPlan::Cte {
                name: cte_name,
                query: Box::new(inner_plan),
                recursive,
            };
        }
    }

    Ok(plan)
}

/// Walk a `SetExpr`, building the inner part of the plan (up to ORDER BY/LIMIT).
pub(crate) fn plan_set_expr(body: &sqlparser::ast::SetExpr) -> Result<LogicalPlan, OxiSqlError> {
    match body {
        sqlparser::ast::SetExpr::Select(sel) => plan_select(sel),
        sqlparser::ast::SetExpr::Query(inner) => plan_query(inner),
        sqlparser::ast::SetExpr::SetOperation {
            op,
            set_quantifier,
            left,
            right,
        } => setops::plan_set_operation(op, set_quantifier, left, right, &plan_set_expr),
        _ => Ok(LogicalPlan::Empty),
    }
}

/// Extract a scalar subquery plan from a SELECT projection item, or `None`.
fn extract_subquery_from_select_item(
    item: &sqlparser::ast::SelectItem,
) -> Result<Option<LogicalPlan>, OxiSqlError> {
    let expr = match item {
        sqlparser::ast::SelectItem::UnnamedExpr(e) => e,
        sqlparser::ast::SelectItem::ExprWithAlias { expr: e, .. } => e,
        _ => return Ok(None),
    };
    if let Expr::Subquery(query) = expr {
        Ok(Some(plan_query(query)?))
    } else {
        Ok(None)
    }
}

/// Build the plan for a `SELECT` node.
pub(crate) fn plan_select(sel: &sqlparser::ast::Select) -> Result<LogicalPlan, OxiSqlError> {
    // 1 + 2. FROM + JOINs
    let mut plan = build_from_plan(&sel.from);

    // 3. WHERE — special-case Exists / InSubquery; fall through to Filter otherwise.
    if let Some(ref pred) = sel.selection {
        match pred {
            Expr::Exists { subquery, negated } => {
                let inner = plan_query(subquery)?;
                plan = LogicalPlan::Exists {
                    subquery: Box::new(inner),
                    negated: *negated,
                };
            }
            Expr::InSubquery {
                expr,
                subquery,
                negated,
            } => {
                let inner = plan_query(subquery)?;
                plan = LogicalPlan::InSubquery {
                    expr: expr.to_string(),
                    subquery: Box::new(inner),
                    negated: *negated,
                };
            }
            other => {
                plan = LogicalPlan::Filter {
                    input: Box::new(plan),
                    predicate: other.to_string(),
                };
            }
        }
    }

    // 4. GROUP BY / aggregates
    let group_by_exprs: Vec<String> = match &sel.group_by {
        sqlparser::ast::GroupByExpr::Expressions(exprs, _) => {
            exprs.iter().map(|e| e.to_string()).collect()
        }
        sqlparser::ast::GroupByExpr::All(_) => vec!["*".to_string()],
    };
    let aggregates: Vec<String> = sel
        .projection
        .iter()
        .filter(|item| agg::projection_item_is_aggregate(item))
        .map(|item| item.to_string())
        .collect();
    if !group_by_exprs.is_empty() || !aggregates.is_empty() {
        plan = LogicalPlan::Aggregate {
            input: Box::new(plan),
            group_by: group_by_exprs,
            aggregates,
        };
        // HAVING — wrap the Aggregate in a Filter node.
        if let Some(ref having_expr) = sel.having {
            plan = LogicalPlan::Filter {
                input: Box::new(plan),
                predicate: having_expr.to_string(),
            };
        }
    }

    // 5. Projection — skip if pure `SELECT *`
    let all_wildcard = sel.projection.iter().all(|item| {
        matches!(
            item,
            sqlparser::ast::SelectItem::Wildcard(_)
                | sqlparser::ast::SelectItem::QualifiedWildcard(_, _)
        )
    });
    if !all_wildcard
        && !matches!(plan, LogicalPlan::Aggregate { .. })
        && !matches!(plan, LogicalPlan::Filter {
            input: ref i, ..
        } if matches!(i.as_ref(), LogicalPlan::Aggregate { .. }))
    {
        // Collect scalar subqueries from the projection.
        let subquery_plans: Vec<LogicalPlan> = sel
            .projection
            .iter()
            .filter_map(|item| extract_subquery_from_select_item(item).ok().flatten())
            .collect();
        if !subquery_plans.is_empty() {
            for sub_plan in subquery_plans {
                let alias = sel.projection.iter().find_map(|it| match it {
                    sqlparser::ast::SelectItem::ExprWithAlias {
                        expr: Expr::Subquery(_),
                        alias,
                    } => Some(alias.value.clone()),
                    _ => None,
                });
                plan = LogicalPlan::Subquery {
                    query: Box::new(sub_plan),
                    alias,
                };
            }
        } else {
            let columns: Vec<String> = sel.projection.iter().map(|item| item.to_string()).collect();
            plan = LogicalPlan::Project {
                input: Box::new(plan),
                columns,
            };
        }
    }

    // 6. Window functions — wrap in a Window node if any OVER clauses are present.
    let window_defs: Vec<WindowFunctionDef> = sel
        .projection
        .iter()
        .filter(|item| window::select_item_is_windowed(item))
        .filter_map(window::extract_window_def)
        .collect();
    if !window_defs.is_empty() {
        plan = LogicalPlan::Window {
            input: Box::new(plan),
            functions: window_defs,
        };
    }

    Ok(plan)
}

/// Build a plan from the `FROM` clause (a list of `TableWithJoins`).
///
/// Multiple FROM items are treated as a cross-join chain.
pub(crate) fn build_from_plan(from: &[sqlparser::ast::TableWithJoins]) -> LogicalPlan {
    if from.is_empty() {
        return LogicalPlan::Empty;
    }

    // Build first table's plan (including its explicit JOINs).
    let mut plan = build_table_with_joins_plan(&from[0]);

    // Additional FROM items → cross-join.
    for twj in from.iter().skip(1) {
        let right = build_table_with_joins_plan(twj);
        plan = LogicalPlan::Join {
            left: Box::new(plan),
            right: Box::new(right),
            on: String::new(),
            join_type: JoinType::Cross,
            algo_hint: None,
        };
    }

    plan
}

/// Build a plan from one `TableWithJoins` entry (base relation + explicit joins).
fn build_table_with_joins_plan(twj: &sqlparser::ast::TableWithJoins) -> LogicalPlan {
    let (table, alias) = table_factor_name_alias(&twj.relation);
    let mut plan = LogicalPlan::Scan {
        table,
        alias,
        limit: None,
    };

    for join in &twj.joins {
        let (jt, alias) = table_factor_name_alias(&join.relation);
        let right = LogicalPlan::Scan {
            table: jt,
            alias,
            limit: None,
        };

        let (join_type, on_expr) = join_operator_info(&join.join_operator);

        plan = LogicalPlan::Join {
            left: Box::new(plan),
            right: Box::new(right),
            on: on_expr,
            join_type,
            algo_hint: None,
        };
    }

    plan
}

/// Extract table name and alias from a `TableFactor`.
pub(crate) fn table_factor_name_alias(
    factor: &sqlparser::ast::TableFactor,
) -> (String, Option<String>) {
    match factor {
        sqlparser::ast::TableFactor::Table { name, alias, .. } => {
            let tname = name.to_string();
            let talias = alias.as_ref().map(|a| a.name.value.clone());
            (tname, talias)
        }
        other => (other.to_string(), None),
    }
}

/// Extract `JoinType` and ON expression string from a `JoinOperator`.
fn join_operator_info(op: &sqlparser::ast::JoinOperator) -> (JoinType, String) {
    let constraint_on = |c: &sqlparser::ast::JoinConstraint| -> String {
        match c {
            sqlparser::ast::JoinConstraint::On(expr) => expr.to_string(),
            _ => String::new(),
        }
    };

    match op {
        sqlparser::ast::JoinOperator::Join(c)
        | sqlparser::ast::JoinOperator::Inner(c)
        | sqlparser::ast::JoinOperator::Semi(c)
        | sqlparser::ast::JoinOperator::LeftSemi(c)
        | sqlparser::ast::JoinOperator::RightSemi(c)
        | sqlparser::ast::JoinOperator::Anti(c)
        | sqlparser::ast::JoinOperator::LeftAnti(c)
        | sqlparser::ast::JoinOperator::RightAnti(c)
        | sqlparser::ast::JoinOperator::StraightJoin(c) => (JoinType::Inner, constraint_on(c)),
        sqlparser::ast::JoinOperator::Left(c) | sqlparser::ast::JoinOperator::LeftOuter(c) => {
            (JoinType::Left, constraint_on(c))
        }
        sqlparser::ast::JoinOperator::Right(c) | sqlparser::ast::JoinOperator::RightOuter(c) => {
            (JoinType::Right, constraint_on(c))
        }
        sqlparser::ast::JoinOperator::FullOuter(c) => (JoinType::Full, constraint_on(c)),
        sqlparser::ast::JoinOperator::CrossJoin(c) => (JoinType::Cross, constraint_on(c)),
        _ => (JoinType::Inner, String::new()),
    }
}

/// Build a scan from a slice of `TableWithJoins` (used by DELETE).
pub(crate) fn build_scan_from_twjs(twjs: &[sqlparser::ast::TableWithJoins]) -> LogicalPlan {
    build_from_plan(twjs)
}

/// Extract `(count, offset)` from a [`sqlparser::ast::LimitClause`].
fn extract_limit_offset(clause: &sqlparser::ast::LimitClause) -> (Option<u64>, Option<u64>) {
    match clause {
        sqlparser::ast::LimitClause::LimitOffset { limit, offset, .. } => {
            let count = limit.as_ref().and_then(expr_to_u64);
            let off = offset.as_ref().and_then(|o| expr_to_u64(&o.value));
            (count, off)
        }
        sqlparser::ast::LimitClause::OffsetCommaLimit { offset, limit } => {
            let count = expr_to_u64(limit);
            let off = expr_to_u64(offset);
            (count, Some(off.unwrap_or(0)))
        }
    }
}

/// Try to extract a `u64` from a literal `Expr::Value(Number(..))`.
fn expr_to_u64(expr: &Expr) -> Option<u64> {
    if let Expr::Value(sqlparser::ast::ValueWithSpan {
        value: sqlparser::ast::Value::Number(ref s, _),
        ..
    }) = expr
    {
        s.parse::<u64>().ok()
    } else {
        None
    }
}