vlorql-core 0.2.0

A high-performance SQL query optimizer with constant folding, predicate pushdown, column pruning, and cost-based join reordering
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
//! Aggregating validation for controlled SQL dialect features.

use crate::errors::{ValidationErrorKind, VlorQLError};
use crate::schema::{
    DialectProfile, Expression, InTarget, Predicate, Projection, QueryPlan, WindowFrame,
    WindowFrameBound,
};
use serde_json::json;

/// Entry points for validating controlled SQL dialect features.
#[derive(Debug, Clone, Copy, Default)]
pub struct DialectValidator;

impl DialectValidator {
    /// Creates a validator bound to one dialect profile.
    pub fn bind(profile: &DialectProfile) -> BoundDialectValidator<'_> {
        BoundDialectValidator { profile }
    }

    /// Validates a plan directly against a dialect profile.
    pub fn validate(plan: &QueryPlan, profile: &DialectProfile) -> Result<(), Vec<VlorQLError>> {
        Self::bind(profile).validate(plan)
    }

    /// Alias for [`DialectValidator::validate`].
    pub fn validate_plan(
        plan: &QueryPlan,
        profile: &DialectProfile,
    ) -> Result<(), Vec<VlorQLError>> {
        Self::validate(plan, profile)
    }
}

/// A dialect validator borrowing one profile for repeated validation.
#[derive(Debug, Clone, Copy)]
pub struct BoundDialectValidator<'a> {
    profile: &'a DialectProfile,
}

impl BoundDialectValidator<'_> {
    /// Checks a plan and nested CTEs, collecting every dialect violation.
    pub fn validate(&self, plan: &QueryPlan) -> Result<(), Vec<VlorQLError>> {
        let mut errors = Vec::new();
        self.validate_inner(plan, &mut errors);
        if errors.is_empty() {
            Ok(())
        } else {
            Err(errors)
        }
    }

    fn validate_inner(&self, plan: &QueryPlan, errors: &mut Vec<VlorQLError>) {
        if plan.ctes.as_ref().is_some_and(|ctes| !ctes.is_empty()) && !self.profile.supports_cte {
            errors.push(self.feature_disabled("common_table_expressions"));
        }

        let join_count = plan.joins.as_ref().map_or(0, Vec::len);
        if let Some(max) = self.profile.max_joins
            && join_count > max
        {
            errors.push(VlorQLError::validation(
                ValidationErrorKind::TooManyJoins {
                    actual: join_count,
                    max,
                },
                json!({
                    "actual": join_count,
                    "max": max,
                    "dialect": self.profile.dialect,
                }),
            ));
        }

        if let Some(joins) = &plan.joins {
            for join in joins {
                if !self.profile.allowed_join_types.is_empty()
                    && !self.profile.allowed_join_types.contains(&join.join_type)
                {
                    errors.push(self.feature_disabled(format!(
                        "join_type:{}",
                        format!("{:?}", join.join_type).to_ascii_lowercase()
                    )));
                }
                self.validate_predicate(&join.on, errors);
            }
        }

        if !self.profile.supports_offset && plan.offset.is_some() {
            errors.push(self.feature_disabled("offset"));
        }

        if let Some(max) = self.profile.max_group_by_columns {
            let actual = plan.group_by.as_ref().map_or(0, Vec::len);
            if actual > max {
                errors.push(VlorQLError::validation(
                    ValidationErrorKind::AggregationMismatch {
                        message: format!(
                            "query groups by {actual} columns, but the maximum is {max}"
                        ),
                    },
                    json!({"actual": actual, "max": max}),
                ));
            }
        }

        if plan.distinct && !self.profile.allow_select_distinct {
            errors.push(self.feature_disabled("select_distinct"));
        }

        self.validate_select_group_by(plan, errors);

        for projection in &plan.select {
            if let Projection::Expr { expression, .. } = projection {
                self.validate_expression(expression, errors);
            }
        }
        if let Some(predicate) = &plan.r#where {
            self.validate_predicate(predicate, errors);
        }
        if let Some(expressions) = &plan.group_by {
            for expression in expressions {
                self.validate_expression(expression, errors);
            }
        }
        if let Some(predicate) = &plan.having {
            self.validate_predicate(predicate, errors);
        }
        if let Some(terms) = &plan.order_by {
            for term in terms {
                self.validate_expression(&term.expr, errors);
            }
        }
        if let Some(ctes) = &plan.ctes {
            for cte in ctes {
                self.validate_inner(&cte.query, errors);
            }
        }
    }

    fn validate_select_group_by(&self, plan: &QueryPlan, errors: &mut Vec<VlorQLError>) {
        let Some(group_by) = plan.group_by.as_ref().filter(|g| !g.is_empty()) else {
            return;
        };

        for projection in &plan.select {
            match projection {
                Projection::Star { .. } => {
                    errors.push(self.group_by_error(
                        "SELECT * is not allowed with GROUP BY: every column must be explicitly listed in GROUP BY or wrapped in an aggregate function",
                    ));
                }
                Projection::Column { table, column, .. } => {
                    if !is_column_in_group_by(group_by, table, column) {
                        errors.push(self.group_by_error(format!(
                            "column `{}.{}` must appear in GROUP BY or be wrapped in an aggregate function",
                            table.as_deref().unwrap_or("?"),
                            column,
                        )));
                    }
                }
                Projection::Expr { expression, .. } => {
                    match expression {
                        Expression::ColumnRef { table, column } => {
                            if !is_column_in_group_by(group_by, table, column) {
                                errors.push(self.group_by_error(format!(
                                    "column `{}.{}` must appear in GROUP BY or be wrapped in an aggregate function",
                                    table.as_deref().unwrap_or("?"),
                                    column,
                                )));
                            }
                        }
                        Expression::Star => {
                            errors.push(
                                self.group_by_error(
                                    "bare `*` is not allowed in SELECT with GROUP BY",
                                ),
                            );
                        }
                        // Aggregate functions (SUM, COUNT, etc.) are OK even if their
                        // arguments are not in GROUP BY.
                        Expression::FunctionCall { .. } => {}
                        // Other expression types (BinaryOp, Literal, SubQuery) are
                        // assumed valid — they don't introduce bare column references.
                        _ => {}
                    }
                }
            }
        }
    }

    fn group_by_error(&self, message: impl Into<String>) -> VlorQLError {
        VlorQLError::validation(
            ValidationErrorKind::AggregationMismatch {
                message: message.into(),
            },
            json!({"type": "select_group_by_mismatch"}),
        )
    }

    fn validate_expression(&self, expression: &Expression, errors: &mut Vec<VlorQLError>) {
        match expression {
            Expression::Literal { .. } | Expression::ColumnRef { .. } => {}
            Expression::FunctionCall {
                name,
                args,
                distinct,
            } => {
                let denied = self
                    .profile
                    .denied_functions
                    .iter()
                    .any(|function| function.eq_ignore_ascii_case(name));
                let allowed = self.profile.allowed_functions.is_empty()
                    || self
                        .profile
                        .allowed_functions
                        .iter()
                        .any(|function| function.eq_ignore_ascii_case(name));
                if denied || !allowed {
                    errors.push(VlorQLError::validation(
                        ValidationErrorKind::InvalidFunction {
                            function: name.clone(),
                            allowed_functions: self.profile.allowed_functions.clone(),
                        },
                        json!({
                            "function": name,
                            "denied": denied,
                            "allowed_functions": self.profile.allowed_functions,
                        }),
                    ));
                }
                if *distinct && !self.profile.allow_distinct {
                    errors.push(self.feature_disabled("distinct"));
                }
                for argument in args {
                    self.validate_expression(argument, errors);
                }
            }
            Expression::BinaryOp { left, right, .. } => {
                self.validate_expression(left, errors);
                self.validate_expression(right, errors);
            }
            Expression::Star => {} // `*` is always valid in function calls.
            Expression::SubQuery { query } => {
                self.validate_inner(query, errors);
            }
            Expression::Case {
                operand,
                when_thens,
                else_result,
            } => {
                if let Some(op) = operand {
                    self.validate_expression(op, errors);
                }
                for wt in when_thens {
                    self.validate_expression(&wt.when, errors);
                    self.validate_expression(&wt.then, errors);
                }
                if let Some(el) = else_result {
                    self.validate_expression(el, errors);
                }
            }
            Expression::WindowFunction {
                name,
                args,
                distinct,
                over,
            } => {
                if !self.profile.supports_window_functions {
                    errors.push(self.feature_disabled("window_functions"));
                }
                let denied = self
                    .profile
                    .denied_functions
                    .iter()
                    .any(|function| function.eq_ignore_ascii_case(name));
                let allowed = self.profile.allowed_functions.is_empty()
                    || self
                        .profile
                        .allowed_functions
                        .iter()
                        .any(|function| function.eq_ignore_ascii_case(name));
                if denied || !allowed {
                    errors.push(VlorQLError::validation(
                        ValidationErrorKind::InvalidFunction {
                            function: name.clone(),
                            allowed_functions: self.profile.allowed_functions.clone(),
                        },
                        json!({
                            "function": name,
                            "denied": denied,
                            "allowed_functions": self.profile.allowed_functions,
                        }),
                    ));
                }
                if *distinct && !self.profile.allow_distinct {
                    errors.push(self.feature_disabled("distinct"));
                }
                for argument in args {
                    self.validate_expression(argument, errors);
                }
                // Validate expressions inside the window spec.
                if let Some(partition_by) = &over.partition_by {
                    for expr in partition_by {
                        self.validate_expression(expr, errors);
                    }
                }
                if let Some(order_by) = &over.order_by {
                    for term in order_by {
                        self.validate_expression(&term.expr, errors);
                    }
                }
                if let Some(frame) = &over.frame {
                    self.validate_window_frame_bounds(frame, errors);
                }
            }
        }
    }

    fn validate_predicate(&self, predicate: &Predicate, errors: &mut Vec<VlorQLError>) {
        match predicate {
            Predicate::Comparison { left, right, .. } => {
                self.validate_expression(left, errors);
                self.validate_expression(right, errors);
            }
            Predicate::And { left, right } | Predicate::Or { left, right } => {
                self.validate_predicate(left, errors);
                self.validate_predicate(right, errors);
            }
            Predicate::Not { child } => self.validate_predicate(child, errors),
            Predicate::Between { expr, low, high } => {
                self.validate_expression(expr, errors);
                self.validate_expression(low, errors);
                self.validate_expression(high, errors);
            }
            Predicate::In { expr, target } => {
                self.validate_expression(expr, errors);
                match target {
                    InTarget::Values(values) => {
                        for value in values {
                            self.validate_expression(value, errors);
                        }
                    }
                    InTarget::SubQuery(query) => {
                        self.validate_inner(query, errors);
                    }
                }
            }
            Predicate::Exists { query } => {
                self.validate_inner(query, errors);
            }
            Predicate::Like { expr, .. } | Predicate::IsNull { expr } => {
                self.validate_expression(expr, errors);
            }
        }
    }

    fn feature_disabled(&self, feature: impl Into<String>) -> VlorQLError {
        let feature = feature.into();
        VlorQLError::validation(
            ValidationErrorKind::DialectFeatureDisabled {
                feature: feature.clone(),
            },
            json!({
                "feature": feature,
                "dialect": self.profile.dialect,
            }),
        )
    }

    fn validate_window_frame_bounds(
        &self,
        frame: &WindowFrame,
        errors: &mut Vec<VlorQLError>,
    ) {
        // Validate frame boundary expressions (PRECEDING / FOLLOWING values).
        Self::validate_frame_bound(&frame.start, errors);
        if let Some(end) = &frame.end {
            Self::validate_frame_bound(end, errors);
        }
    }

    fn validate_frame_bound(bound: &WindowFrameBound, errors: &mut Vec<VlorQLError>) {
        match bound {
            WindowFrameBound::Preceding(expr) | WindowFrameBound::Following(expr) => {
                // The expression must be a literal or column ref (no subqueries).
                if matches!(expr.as_ref(), Expression::SubQuery { .. }) {
                    errors.push(VlorQLError::validation(
                        ValidationErrorKind::TypeMismatch {
                            expected: "literal or column".to_owned(),
                            found: "subquery".to_owned(),
                            expr: "window_frame_bound".to_owned(),
                        },
                        json!({
                            "expected": "literal or column",
                            "found": "subquery",
                            "bound": format!("{bound:?}"),
                        }),
                    ));
                }
            }
            _ => {}
        }
    }
}

/// Checks whether a `(table, column)` reference is covered by the GROUP BY expressions.
///
/// A column is considered covered if any GROUP BY expression is a `ColumnRef`
/// whose `(table, column)` pair matches (comparing qualified names if both are
/// qualified, or just column names if either side is unqualified).
fn is_column_in_group_by(group_by: &[Expression], table: &Option<String>, column: &str) -> bool {
    group_by.iter().any(|expr| match expr {
        Expression::ColumnRef {
            table: gb_table,
            column: gb_column,
        } => match (gb_table, table) {
            (Some(t1), Some(t2)) => t1 == t2 && gb_column == column,
            _ => gb_column == column,
        },
        _ => false,
    })
}