oxisql-parse 0.3.3

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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Decorrelation of correlated subqueries into joins.
//!
//! Rewrites correlated `EXISTS` / `NOT EXISTS` → `LeftSemi` / `LeftAnti` joins
//! and correlated `IN` → `LeftSemi` joins by pulling the correlation predicates
//! out of the inner subquery's `WHERE` clause and using them as the join `ON`
//! condition.

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

use crate::optimizer::expr_util::{collect_colrefs, join_conjuncts, render, split_conjuncts};
use crate::plan::{JoinType, LogicalPlan};

// ── PlannerOptions ────────────────────────────────────────────────────────────

/// Options that influence how `plan_query_with` builds the logical plan.
#[derive(Debug, Clone)]
pub struct PlannerOptions {
    /// When `true`, correlated `EXISTS` / `NOT EXISTS` / `IN` subqueries are
    /// rewritten into `LeftSemi` / `LeftAnti` joins at plan time.
    pub decorrelate: bool,
}

impl Default for PlannerOptions {
    fn default() -> Self {
        Self { decorrelate: true }
    }
}

// ── Public entry point ────────────────────────────────────────────────────────

/// Post-facto decorrelation pass over an already-built `LogicalPlan`.
///
/// For `Filter` nodes whose predicate contains correlated `EXISTS` or
/// `InSubquery` conjuncts, the conjuncts are replaced by `LeftSemi` /
/// `LeftAnti` joins against the inner plan.  Uncorrelated subqueries and
/// structural `Exists` / `InSubquery` plan nodes without an accessible outer
/// plan are left unchanged.
///
/// `outer_scope` carries the table/alias names that are in scope from an
/// enclosing query block; pass `&[]` at the top level.
pub fn decorrelate_plan(plan: LogicalPlan, outer_scope: &[String]) -> LogicalPlan {
    match plan {
        LogicalPlan::Filter { input, predicate } => {
            let input = decorrelate_plan(*input, outer_scope);
            // Collect outer scope from the (already-decorrelated) input plan.
            let mut scope: Vec<String> = collect_plan_table_names(&input);
            scope.extend_from_slice(outer_scope);

            match apply_decorrelated_filter(input, &predicate, &scope) {
                Ok(new_plan) => new_plan,
                Err(_) => LogicalPlan::Filter {
                    input: Box::new(decorrelate_plan(
                        LogicalPlan::Scan {
                            table: "__err__".to_string(),
                            alias: None,
                            limit: None,
                        },
                        outer_scope,
                    )),
                    predicate,
                },
            }
        }

        // Recursively decorrelate children of all other variants.
        LogicalPlan::Project { input, columns } => LogicalPlan::Project {
            input: Box::new(decorrelate_plan(*input, outer_scope)),
            columns,
        },
        LogicalPlan::Aggregate {
            input,
            group_by,
            aggregates,
        } => LogicalPlan::Aggregate {
            input: Box::new(decorrelate_plan(*input, outer_scope)),
            group_by,
            aggregates,
        },
        LogicalPlan::Sort { input, order_by } => LogicalPlan::Sort {
            input: Box::new(decorrelate_plan(*input, outer_scope)),
            order_by,
        },
        LogicalPlan::Limit {
            input,
            count,
            offset,
        } => LogicalPlan::Limit {
            input: Box::new(decorrelate_plan(*input, outer_scope)),
            count,
            offset,
        },
        LogicalPlan::Window { input, functions } => LogicalPlan::Window {
            input: Box::new(decorrelate_plan(*input, outer_scope)),
            functions,
        },
        LogicalPlan::Compute { input, bindings } => LogicalPlan::Compute {
            input: Box::new(decorrelate_plan(*input, outer_scope)),
            bindings,
        },
        LogicalPlan::Join {
            left,
            right,
            on,
            join_type,
            algo_hint,
        } => LogicalPlan::Join {
            left: Box::new(decorrelate_plan(*left, outer_scope)),
            right: Box::new(decorrelate_plan(*right, outer_scope)),
            on,
            join_type,
            algo_hint,
        },
        LogicalPlan::SetOp {
            op,
            all,
            left,
            right,
        } => LogicalPlan::SetOp {
            op,
            all,
            left: Box::new(decorrelate_plan(*left, outer_scope)),
            right: Box::new(decorrelate_plan(*right, outer_scope)),
        },
        LogicalPlan::Cte {
            name,
            query,
            recursive,
        } => LogicalPlan::Cte {
            name,
            query: Box::new(decorrelate_plan(*query, outer_scope)),
            recursive,
        },
        LogicalPlan::Subquery { query, alias } => LogicalPlan::Subquery {
            query: Box::new(decorrelate_plan(*query, outer_scope)),
            alias,
        },
        LogicalPlan::Exists { subquery, negated } => LogicalPlan::Exists {
            subquery: Box::new(decorrelate_plan(*subquery, outer_scope)),
            negated,
        },
        LogicalPlan::InSubquery {
            expr,
            subquery,
            negated,
        } => LogicalPlan::InSubquery {
            expr,
            subquery: Box::new(decorrelate_plan(*subquery, outer_scope)),
            negated,
        },
        // Leaves
        other => other,
    }
}

/// Try to rewrite correlated EXISTS / IN conjuncts in `predicate` into
/// LeftSemi / LeftAnti joins on top of `outer_plan`.
///
/// Returns the (possibly modified) plan with the decorrelated subqueries
/// turned into joins and any remaining regular predicates wrapped in a Filter.
fn apply_decorrelated_filter(
    outer_plan: LogicalPlan,
    predicate: &str,
    outer_scope: &[String],
) -> Result<LogicalPlan, OxiSqlError> {
    let Some(pred_expr) = crate::parse_predicate(predicate) else {
        // Can't parse; keep as-is.
        return Ok(LogicalPlan::Filter {
            input: Box::new(outer_plan),
            predicate: predicate.to_string(),
        });
    };

    let conjuncts = split_conjuncts(pred_expr);
    let mut plan = outer_plan;
    let mut remaining: Vec<Expr> = Vec::new();

    for conjunct in conjuncts {
        match conjunct {
            Expr::Exists {
                ref subquery,
                negated,
            } => {
                let inner = crate::planner::plan_query(subquery)?;
                let (non_corr_plan, corr_preds) = extract_correlated_filter(inner, outer_scope);
                if corr_preds.is_empty() {
                    // Uncorrelated — keep as a structural filter expression.
                    remaining.push(conjunct);
                } else {
                    let join_on = corr_preds.join(" AND ");
                    let join_type = if negated {
                        JoinType::LeftAnti
                    } else {
                        JoinType::LeftSemi
                    };
                    plan = LogicalPlan::Join {
                        left: Box::new(plan),
                        right: Box::new(non_corr_plan),
                        on: join_on,
                        join_type,
                        algo_hint: None,
                    };
                }
            }

            Expr::InSubquery {
                ref expr,
                ref subquery,
                negated,
            } => {
                let inner = crate::planner::plan_query(subquery)?;
                let proj_cols = extract_projection_cols(&inner);
                let (non_corr_plan, mut corr_preds) = extract_correlated_filter(inner, outer_scope);

                if corr_preds.is_empty() {
                    // Uncorrelated.
                    remaining.push(conjunct);
                } else {
                    let expr_str = expr.to_string();
                    // Prepend the equality between the outer expr and the inner projected column.
                    if let Some(proj_col) = proj_cols.first() {
                        corr_preds.insert(0, format!("{expr_str} = {proj_col}"));
                    }
                    let join_on = corr_preds.join(" AND ");
                    let join_type = if negated {
                        JoinType::LeftAnti
                    } else {
                        JoinType::LeftSemi
                    };
                    plan = LogicalPlan::Join {
                        left: Box::new(plan),
                        right: Box::new(non_corr_plan),
                        on: join_on,
                        join_type,
                        algo_hint: None,
                    };
                }
            }

            other => remaining.push(other),
        }
    }

    // Wrap with a Filter for any remaining (non-decorrelatable) conjuncts.
    if let Some(e) = join_conjuncts(remaining) {
        plan = LogicalPlan::Filter {
            input: Box::new(plan),
            predicate: render(&e),
        };
    }

    Ok(plan)
}

// ── Internals ─────────────────────────────────────────────────────────────────

/// Entry point called from `plan_select_with_opts` in `planner.rs`.
///
/// Applies decorrelation to the WHERE expression `pred` given the outer
/// table scope `outer_scope`.
pub(crate) fn apply_decorrelated_where(
    outer_plan: LogicalPlan,
    pred: &Expr,
    outer_scope: &[String],
) -> Result<LogicalPlan, OxiSqlError> {
    let conjuncts = split_conjuncts(pred.clone());
    let mut plan = outer_plan;
    let mut remaining: Vec<Expr> = Vec::new();

    for conjunct in conjuncts {
        match conjunct {
            Expr::Exists {
                ref subquery,
                negated,
            } => {
                let opts_no_decorr = PlannerOptions { decorrelate: false };
                let inner = crate::planner::plan_query_with_opts(subquery, &opts_no_decorr)?;
                let (non_corr_plan, corr_preds) = extract_correlated_filter(inner, outer_scope);

                if corr_preds.is_empty() {
                    remaining.push(conjunct);
                } else {
                    let join_on = corr_preds.join(" AND ");
                    let join_type = if negated {
                        JoinType::LeftAnti
                    } else {
                        JoinType::LeftSemi
                    };
                    plan = LogicalPlan::Join {
                        left: Box::new(plan),
                        right: Box::new(non_corr_plan),
                        on: join_on,
                        join_type,
                        algo_hint: None,
                    };
                }
            }

            Expr::InSubquery {
                ref expr,
                ref subquery,
                negated,
            } => {
                let opts_no_decorr = PlannerOptions { decorrelate: false };
                let inner = crate::planner::plan_query_with_opts(subquery, &opts_no_decorr)?;
                let proj_cols = extract_projection_cols(&inner);
                let (non_corr_plan, mut corr_preds) = extract_correlated_filter(inner, outer_scope);

                if corr_preds.is_empty() {
                    remaining.push(conjunct);
                } else {
                    let expr_str = expr.to_string();
                    if let Some(proj_col) = proj_cols.first() {
                        corr_preds.insert(0, format!("{expr_str} = {proj_col}"));
                    }
                    let join_on = corr_preds.join(" AND ");
                    let join_type = if negated {
                        JoinType::LeftAnti
                    } else {
                        JoinType::LeftSemi
                    };
                    plan = LogicalPlan::Join {
                        left: Box::new(plan),
                        right: Box::new(non_corr_plan),
                        on: join_on,
                        join_type,
                        algo_hint: None,
                    };
                }
            }

            other => remaining.push(other),
        }
    }

    if let Some(e) = join_conjuncts(remaining) {
        plan = LogicalPlan::Filter {
            input: Box::new(plan),
            predicate: render(&e),
        };
    }

    Ok(plan)
}

/// Split a filter's predicate into correlated and non-correlated conjuncts.
///
/// Returns `(modified_plan_without_corr_predicates, correlated_conjuncts_as_strings)`.
/// Recursion descends through `Project` to find the first `Filter`.
fn extract_correlated_filter(
    plan: LogicalPlan,
    outer_scope: &[String],
) -> (LogicalPlan, Vec<String>) {
    match plan {
        LogicalPlan::Filter { input, predicate } => {
            let Some(e) = crate::parse_predicate(&predicate) else {
                return (LogicalPlan::Filter { input, predicate }, vec![]);
            };
            let conjuncts = split_conjuncts(e);
            let mut corr: Vec<String> = Vec::new();
            let mut non_corr: Vec<Expr> = Vec::new();

            for c in conjuncts {
                let refs = collect_colrefs(&c);
                let is_corr = refs.iter().any(|r| {
                    r.qualifier
                        .as_deref()
                        .map(|q| outer_scope.iter().any(|s| s == q))
                        .unwrap_or(false)
                });
                if is_corr {
                    corr.push(render(&c));
                } else {
                    non_corr.push(c);
                }
            }

            let new_plan = if let Some(e) = join_conjuncts(non_corr) {
                LogicalPlan::Filter {
                    input,
                    predicate: render(&e),
                }
            } else {
                *input
            };
            (new_plan, corr)
        }

        LogicalPlan::Project { input, columns } => {
            let (new_input, corr) = extract_correlated_filter(*input, outer_scope);
            (
                LogicalPlan::Project {
                    input: Box::new(new_input),
                    columns,
                },
                corr,
            )
        }

        // For other variants, no correlation found at this level.
        other => (other, vec![]),
    }
}

/// Extract projected column expressions from the top-level of a plan.
fn extract_projection_cols(plan: &LogicalPlan) -> Vec<String> {
    match plan {
        LogicalPlan::Project { columns, .. } => columns.clone(),
        LogicalPlan::Filter { input, .. } => extract_projection_cols(input),
        _ => vec![],
    }
}

/// Collect all table names and aliases reachable from `plan`.
fn collect_plan_table_names(plan: &LogicalPlan) -> Vec<String> {
    let mut out = Vec::new();
    collect_names_inner(plan, &mut out);
    out
}

fn collect_names_inner(plan: &LogicalPlan, out: &mut Vec<String>) {
    match plan {
        LogicalPlan::Scan { table, alias, .. } => {
            out.push(table.clone());
            if let Some(a) = alias {
                out.push(a.clone());
            }
        }
        LogicalPlan::Filter { input, .. }
        | LogicalPlan::Project { input, .. }
        | LogicalPlan::Aggregate { input, .. }
        | LogicalPlan::Sort { input, .. }
        | LogicalPlan::Limit { input, .. }
        | LogicalPlan::Window { input, .. }
        | LogicalPlan::Compute { input, .. } => collect_names_inner(input, out),
        LogicalPlan::Join { left, right, .. } | LogicalPlan::SetOp { left, right, .. } => {
            collect_names_inner(left, out);
            collect_names_inner(right, out);
        }
        _ => {}
    }
}