vibesql-executor 0.2.0

Query execution engine for vibesql SQL database
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
//! DISTINCT elimination optimization
//!
//! This module implements the optimization that eliminates redundant DISTINCT
//! when the SELECT columns are guaranteed to be unique due to:
//! 1. A UNIQUE index covering all SELECT columns
//! 2. All columns in that unique index being NOT NULL
//!
//! Without the NOT NULL guarantee, multiple NULL values could exist in the result
//! set (since NULL != NULL in SQL), making DISTINCT necessary.
//!
//! References:
//! - SQLite's distinct.test (tests 1.1 vs 1.2, etc.)
//! - SQL standard semantics for UNIQUE constraints with NULLs

use std::collections::HashSet;

use vibesql_ast::{Expression, SelectItem, SelectStmt};
use vibesql_storage::Database;

/// Check if DISTINCT can be eliminated for a SELECT statement.
///
/// DISTINCT can be eliminated when:
/// 1. The query is a simple single-table query (no joins, subqueries)
/// 2. There exists a UNIQUE index (or PRIMARY KEY, or rowid) that covers the SELECT columns
/// 3. All columns in that unique index are NOT NULL
///
/// This follows SQLite's behavior as documented in distinct.test.
///
/// # Arguments
/// * `stmt` - The SELECT statement to analyze
/// * `database` - Database for looking up table schemas and indexes
///
/// # Returns
/// * `true` if DISTINCT is provably redundant and can be eliminated
/// * `false` if DISTINCT must be preserved
pub fn can_eliminate_distinct(stmt: &SelectStmt, database: &Database) -> bool {
    // Only handle simple single-table queries
    let table_name = match &stmt.from {
        Some(vibesql_ast::FromClause::Table { name, .. }) => name.as_str(),
        // Joins, subqueries, etc. - don't eliminate DISTINCT
        _ => return false,
    };

    // Get the table schema
    let Some(table) = database.get_table(table_name) else {
        return false;
    };
    let schema = &table.schema;

    // Extract columns from SELECT list
    let select_columns = match extract_select_columns(&stmt.select_list, schema) {
        Some(cols) => cols,
        None => return false, // Wildcard or expression - can't determine columns
    };

    if select_columns.is_empty() {
        return false;
    }

    // Check if rowid is in the select list - rowid is always unique
    if select_columns.iter().any(|c| c.eq_ignore_ascii_case("rowid")) {
        return true;
    }

    // Check PRIMARY KEY (rowid alias)
    // If we're selecting the INTEGER PRIMARY KEY column, it's always unique
    if let Some(rowid_idx) = schema.rowid_alias_column {
        let rowid_col = &schema.columns[rowid_idx].name;
        if select_columns.iter().any(|c| c.eq_ignore_ascii_case(rowid_col)) {
            return true;
        }
    }

    // Check if PRIMARY KEY covers select columns AND all PK columns are NOT NULL
    if let Some(ref pk_cols) = schema.primary_key {
        if covers_columns(pk_cols, &select_columns, &stmt.where_clause, schema) {
            // Check if all PK columns are NOT NULL
            if all_columns_not_null(pk_cols, schema) {
                return true;
            }
        }
    }

    // Check UNIQUE constraints from CREATE TABLE
    for unique_cols in &schema.unique_constraints {
        if covers_columns(unique_cols, &select_columns, &stmt.where_clause, schema)
            && all_columns_not_null(unique_cols, schema)
        {
            return true;
        }
    }

    // Check user-defined UNIQUE indexes
    for index_name in database.list_indexes() {
        let Some(index) = database.get_index(&index_name) else {
            continue;
        };

        // Skip if not for this table or not unique
        if !index.table_name.eq_ignore_ascii_case(table_name) || !index.unique {
            continue;
        }

        // Skip partial UNIQUE indexes: they only enforce uniqueness over the
        // subset of rows matching the WHERE predicate, so they do not prove
        // distinctness of the full table. The `is_partial` flag lives on the
        // catalog-side `IndexMetadata`.
        //
        // Intentionally stays conservative even though the planner now has
        // `optimizer::predicate_implication`: DISTINCT elimination needs a
        // uniqueness guarantee over every row the query can return, which
        // structural implication alone does not establish here (future work).
        if database.catalog.find_index_by_name(&index_name).map(|m| m.is_partial()).unwrap_or(false)
        {
            continue;
        }

        // Get column names from index (skip expression indexes)
        let index_cols: Vec<String> =
            index.columns.iter().filter_map(|c| c.column_name().map(|s| s.to_string())).collect();

        if index_cols.len() != index.columns.len() {
            // Has expression columns - skip for now
            continue;
        }

        if covers_columns(&index_cols, &select_columns, &stmt.where_clause, schema)
            && all_columns_not_null(&index_cols, schema)
        {
            return true;
        }
    }

    false
}

/// Extract column names from SELECT list.
/// Returns None if the select list contains wildcards or complex expressions.
fn extract_select_columns(
    select_list: &[SelectItem],
    schema: &vibesql_catalog::TableSchema,
) -> Option<Vec<String>> {
    let mut columns = Vec::new();

    for item in select_list {
        match item {
            SelectItem::Wildcard { .. } => {
                // SELECT * - return all columns
                for col in &schema.columns {
                    columns.push(col.name.clone());
                }
            }
            SelectItem::QualifiedWildcard { .. } => {
                // table.* - for single table, same as *
                for col in &schema.columns {
                    columns.push(col.name.clone());
                }
            }
            SelectItem::Expression { expr, .. } => {
                // Try to extract column reference
                if let Some(col_name) = extract_column_from_expr(expr) {
                    columns.push(col_name);
                } else {
                    // Complex expression - we can still continue
                    // The column might still be covered by an index
                    // But for safety, skip expressions that aren't simple column refs
                    return None;
                }
            }
        }
    }

    Some(columns)
}

/// Extract a column name from a simple column reference expression.
fn extract_column_from_expr(expr: &Expression) -> Option<String> {
    match expr {
        Expression::ColumnRef(col_id) => Some(col_id.column_canonical().to_string()),
        _ => None,
    }
}

/// Check if a set of unique columns covers the select columns.
///
/// A unique index "covers" the select columns if:
/// 1. All unique index columns are in the select columns, OR
/// 2. Some unique index columns are fixed by WHERE clause equality conditions
///
/// For example, with UNIQUE INDEX ON (a, b):
/// - SELECT a, b FROM t - covered (both columns in select)
/// - SELECT b FROM t WHERE a = 5 - covered (a is fixed, b in select)
fn covers_columns(
    unique_cols: &[String],
    select_cols: &[String],
    where_clause: &Option<Expression>,
    schema: &vibesql_catalog::TableSchema,
) -> bool {
    let select_set: HashSet<String> = select_cols.iter().map(|c| c.to_lowercase()).collect();

    // Get columns fixed by WHERE clause (col = const)
    let fixed_cols = get_fixed_columns(where_clause, schema);

    // Check if every unique column is either in select list or fixed by WHERE
    for unique_col in unique_cols {
        let unique_col_lower = unique_col.to_lowercase();
        if !select_set.contains(&unique_col_lower) && !fixed_cols.contains(&unique_col_lower) {
            return false;
        }
    }

    true
}

/// Get columns that are fixed to constant values by WHERE clause.
/// Only considers simple equality conditions (col = const).
fn get_fixed_columns(
    where_clause: &Option<Expression>,
    _schema: &vibesql_catalog::TableSchema,
) -> HashSet<String> {
    let mut fixed = HashSet::new();

    if let Some(expr) = where_clause {
        collect_fixed_columns(expr, &mut fixed);
    }

    fixed
}

/// Recursively collect columns that are fixed to constants.
fn collect_fixed_columns(expr: &Expression, fixed: &mut HashSet<String>) {
    use vibesql_ast::BinaryOperator;

    match expr {
        Expression::BinaryOp { op: BinaryOperator::Equal, left, right } => {
            // Check if one side is a column and the other is a constant
            if let Expression::ColumnRef(col_id) = left.as_ref() {
                if is_constant(right) {
                    fixed.insert(col_id.column_canonical().to_lowercase());
                }
            }
            if let Expression::ColumnRef(col_id) = right.as_ref() {
                if is_constant(left) {
                    fixed.insert(col_id.column_canonical().to_lowercase());
                }
            }
        }
        Expression::Conjunction(exprs) => {
            for e in exprs {
                collect_fixed_columns(e, fixed);
            }
        }
        Expression::BinaryOp { op: BinaryOperator::And, left, right } => {
            collect_fixed_columns(left, fixed);
            collect_fixed_columns(right, fixed);
        }
        _ => {}
    }
}

/// Check if an expression is a constant (literal, placeholder, etc.).
fn is_constant(expr: &Expression) -> bool {
    matches!(
        expr,
        Expression::Literal(_)
            | Expression::Placeholder(_)
            | Expression::NumberedPlaceholder(_)
            | Expression::NamedPlaceholder(_)
    )
}

/// Check if all columns in a list are NOT NULL.
fn all_columns_not_null(columns: &[String], schema: &vibesql_catalog::TableSchema) -> bool {
    for col_name in columns {
        let col_lower = col_name.to_lowercase();
        let col = schema.columns.iter().find(|c| c.name.to_lowercase() == col_lower);
        match col {
            Some(c) => {
                if c.nullable {
                    return false;
                }
            }
            None => {
                // Column not found - shouldn't happen, but be conservative
                return false;
            }
        }
    }
    true
}

#[cfg(test)]
mod tests {
    use vibesql_ast::{ColumnIdentifier, OrderDirection, SelectStmt};
    use vibesql_catalog::{ColumnSchema, TableSchema};
    use vibesql_storage::Database;
    use vibesql_types::DataType;

    use super::*;

    fn create_test_database() -> Database {
        let mut db = Database::new();

        // Create t1: nullable columns with unique index
        let t1_cols = vec![
            ColumnSchema::new("a".to_string(), DataType::Integer, true),
            ColumnSchema::new("b".to_string(), DataType::Integer, true),
            ColumnSchema::new("c".to_string(), DataType::Integer, true),
            ColumnSchema::new("d".to_string(), DataType::Integer, true),
        ];
        let t1_schema = TableSchema::new("t1".to_string(), t1_cols);
        db.create_table(t1_schema).unwrap();

        // Create t4: NOT NULL columns with unique index
        let t4_cols = vec![
            ColumnSchema::new("a".to_string(), DataType::Integer, true), // nullable
            ColumnSchema::new("b".to_string(), DataType::Integer, false), // NOT NULL
            ColumnSchema::new("c".to_string(), DataType::Integer, false), // NOT NULL
            ColumnSchema::new("d".to_string(), DataType::Integer, false), // NOT NULL
        ];
        let t4_schema = TableSchema::new("t4".to_string(), t4_cols);
        db.create_table(t4_schema).unwrap();

        // Create unique index on (b, c) for both tables
        db.create_index(
            "i1".to_string(),
            "t1".to_string(),
            true, // unique
            vec![
                vibesql_ast::IndexColumn::new_column("b".to_string(), OrderDirection::Asc),
                vibesql_ast::IndexColumn::new_column("c".to_string(), OrderDirection::Asc),
            ],
        )
        .unwrap();

        db.create_index(
            "t4i1".to_string(),
            "t4".to_string(),
            true, // unique
            vec![
                vibesql_ast::IndexColumn::new_column("b".to_string(), OrderDirection::Asc),
                vibesql_ast::IndexColumn::new_column("c".to_string(), OrderDirection::Asc),
            ],
        )
        .unwrap();

        db
    }

    fn make_select_stmt(table: &str, columns: &[&str]) -> SelectStmt {
        let select_list: Vec<SelectItem> = columns
            .iter()
            .map(|c| SelectItem::Expression {
                expr: Expression::ColumnRef(ColumnIdentifier::simple(c, false)),
                alias: None,
                source_text: None,
            })
            .collect();

        SelectStmt {
            with_clause: None,
            distinct: true,
            select_list,
            into_table: None,
            into_variables: None,
            from: Some(vibesql_ast::FromClause::Table {
                index_hint: None,
                name: table.to_string(),
                alias: None,
                column_aliases: None,
                quoted: false,
            }),
            where_clause: None,
            group_by: None,
            having: None,
            window_definitions: None,
            order_by: None,
            limit: None,
            offset: None,
            set_operation: None,
            values: None,
        }
    }

    #[test]
    fn test_distinct_not_eliminated_nullable_columns() {
        let db = create_test_database();

        // SELECT DISTINCT b, c FROM t1 - should NOT be eliminated (nullable columns)
        let stmt = make_select_stmt("t1", &["b", "c"]);
        assert!(!can_eliminate_distinct(&stmt, &db));
    }

    #[test]
    fn test_distinct_eliminated_not_null_columns() {
        let db = create_test_database();

        // SELECT DISTINCT b, c FROM t4 - SHOULD be eliminated (NOT NULL columns)
        let stmt = make_select_stmt("t4", &["b", "c"]);
        assert!(can_eliminate_distinct(&stmt, &db));
    }

    #[test]
    fn test_distinct_not_eliminated_partial_unique() {
        let db = create_test_database();

        // SELECT DISTINCT a, b FROM t4 - should NOT be eliminated (a not in unique index)
        let stmt = make_select_stmt("t4", &["a", "b"]);
        assert!(!can_eliminate_distinct(&stmt, &db));
    }
}