aedb 0.2.3

Embedded Rust storage engine with transactional commits, WAL durability, and snapshot-consistent reads
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
443
444
445
446
447
448
449
450
451
use super::{
    CursorToken, QueryResult, aggregate_col_idx, aggregate_output_name,
    compute_remaining_limit_after_page, compute_split_recommended, encode_cursor, extract_pk_key,
    extract_sort_key, row_after_cursor,
};
use crate::catalog::Catalog;
use crate::catalog::namespace_key;
use crate::catalog::types::{Row, Value};
use crate::query::error::QueryError;
use crate::query::operators::{AggregateOperator, Operator, ScanOperator, compile_expr};
use crate::query::plan::{JoinType, Query, QueryOptions};
use crate::storage::encoded_key::EncodedKey;
use crate::storage::keyspace::KeyspaceSnapshot;
use std::collections::HashMap;

#[allow(clippy::too_many_arguments)]
pub(super) fn execute_join_query(
    snapshot: &KeyspaceSnapshot,
    catalog: &Catalog,
    project_id: &str,
    scope_id: &str,
    query: Query,
    options: QueryOptions,
    snapshot_seq: u64,
    max_scan_rows: usize,
    cursor_state: Option<CursorToken>,
    cursor_signing_key: Option<&[u8; 32]>,
) -> Result<QueryResult, QueryError> {
    let (base_ns_project, base_ns_scope, base_table) =
        resolve_table_ref(project_id, scope_id, &query.table);
    let base_schema = catalog
        .tables
        .get(&(
            namespace_key(&base_ns_project, &base_ns_scope),
            base_table.clone(),
        ))
        .ok_or_else(|| QueryError::TableNotFound {
            project_id: base_ns_project.clone(),
            table: base_table.clone(),
        })?;
    let base_alias = query.table_alias.clone().unwrap_or(base_table.clone());
    let mut columns: Vec<String> = base_schema
        .columns
        .iter()
        .map(|c| format!("{base_alias}.{}", c.name))
        .collect();
    let base_count = snapshot
        .table(&base_ns_project, &base_ns_scope, &base_table)
        .map(|t| t.rows.len())
        .unwrap_or(0);
    if !options.allow_full_scan && base_count > max_scan_rows {
        return Err(QueryError::ScanBoundExceeded {
            estimated_rows: base_count as u64,
            max_scan_rows: max_scan_rows as u64,
        });
    }
    let mut rows: Vec<Row> = snapshot
        .table(&base_ns_project, &base_ns_scope, &base_table)
        .map(|t| t.rows.values().cloned().collect())
        .unwrap_or_default();

    for join in &query.joins {
        let (jp, js, jt) = resolve_table_ref(project_id, scope_id, &join.table);
        let join_schema = catalog
            .tables
            .get(&(namespace_key(&jp, &js), jt.clone()))
            .ok_or_else(|| QueryError::TableNotFound {
                project_id: jp.clone(),
                table: jt.clone(),
            })?;
        let join_alias = join.alias.clone().unwrap_or(jt.clone());
        let join_table = snapshot.table(&jp, &js, &jt);
        let join_col_offset = columns.len();
        let mut next_columns = columns.clone();
        next_columns.extend(
            join_schema
                .columns
                .iter()
                .map(|c| format!("{join_alias}.{}", c.name)),
        );
        let (left_idx, right_idx) = match join.join_type {
            JoinType::Cross => (None, None),
            _ => {
                let left = join
                    .left_column
                    .as_ref()
                    .ok_or_else(|| QueryError::InvalidQuery {
                        reason: "join requires left_column".into(),
                    })?;
                let right = join
                    .right_column
                    .as_ref()
                    .ok_or_else(|| QueryError::InvalidQuery {
                        reason: "join requires right_column".into(),
                    })?;
                let left_idx = columns.iter().position(|c| c == left).ok_or_else(|| {
                    QueryError::ColumnNotFound {
                        table: query.table.clone(),
                        column: left.clone(),
                    }
                })?;
                let right_idx = join_schema
                    .columns
                    .iter()
                    .position(|c| format!("{join_alias}.{}", c.name) == *right || c.name == *right)
                    .ok_or_else(|| QueryError::ColumnNotFound {
                        table: join.table.clone(),
                        column: right.clone(),
                    })?;
                (Some(left_idx), Some(right_idx))
            }
        };
        let can_probe_right_primary_key =
            matches!(join.join_type, JoinType::Inner | JoinType::Left)
                && right_idx
                    .map(|idx| is_single_column_primary_key_join(join_schema, idx))
                    .unwrap_or(false);
        let estimated_join_rows = if matches!(join.join_type, JoinType::Cross) {
            rows.len()
                .saturating_mul(join_table.map(|table| table.rows.len()).unwrap_or(0).max(1))
        } else if can_probe_right_primary_key {
            rows.len()
        } else {
            rows.len()
                .saturating_mul(join_table.map(|table| table.rows.len()).unwrap_or(0).max(1))
        };
        if !options.allow_full_scan && estimated_join_rows > max_scan_rows {
            return Err(QueryError::ScanBoundExceeded {
                estimated_rows: estimated_join_rows as u64,
                max_scan_rows: max_scan_rows as u64,
            });
        }

        let mut joined = Vec::new();
        match join.join_type {
            JoinType::Cross => {
                let join_rows: Vec<&Row> = join_table
                    .map(|table| table.rows.values().collect())
                    .unwrap_or_default();
                for left in &rows {
                    for right in &join_rows {
                        let mut values = left.values.clone();
                        values.extend(right.values.clone());
                        joined.push(Row { values });
                    }
                }
            }
            JoinType::Inner | JoinType::Left => {
                let right_idx = right_idx.ok_or_else(|| QueryError::InvalidQuery {
                    reason: "join requires right join key".into(),
                })?;
                let left_idx = left_idx.ok_or_else(|| QueryError::InvalidQuery {
                    reason: "join requires left join key".into(),
                })?;
                if can_probe_right_primary_key {
                    for left in &rows {
                        let key = left.values[left_idx].clone();
                        let matched = join_table.and_then(|table| {
                            let encoded = EncodedKey::from_values(&[key]);
                            table.rows.get(&encoded)
                        });
                        if let Some(right) = matched {
                            let mut values = left.values.clone();
                            values.extend(right.values.clone());
                            joined.push(Row { values });
                        } else if matches!(join.join_type, JoinType::Left) {
                            let mut values = left.values.clone();
                            values.extend(std::iter::repeat_n(
                                Value::Null,
                                join_schema.columns.len(),
                            ));
                            joined.push(Row { values });
                        }
                    }
                } else {
                    let join_rows: Vec<&Row> = join_table
                        .map(|table| table.rows.values().collect())
                        .unwrap_or_default();
                    // Hash join for non-PK equality predicates.
                    let mut right_map: HashMap<Value, Vec<&Row>> = HashMap::new();
                    for right in &join_rows {
                        right_map
                            .entry(right.values[right_idx].clone())
                            .or_default()
                            .push(right);
                    }
                    for left in &rows {
                        let key = left.values[left_idx].clone();
                        if let Some(matches) = right_map.get(&key) {
                            for right in matches {
                                let mut values = left.values.clone();
                                values.extend(right.values.clone());
                                joined.push(Row { values });
                            }
                        } else if matches!(join.join_type, JoinType::Left) {
                            let mut values = left.values.clone();
                            values.extend(std::iter::repeat_n(
                                Value::Null,
                                join_schema.columns.len(),
                            ));
                            joined.push(Row { values });
                        }
                    }
                }
            }
            JoinType::Right => {
                let left_idx = left_idx.ok_or_else(|| QueryError::InvalidQuery {
                    reason: "join requires left join key".into(),
                })?;
                let right_idx = right_idx.ok_or_else(|| QueryError::InvalidQuery {
                    reason: "join requires right join key".into(),
                })?;
                let join_rows: Vec<&Row> = join_table
                    .map(|table| table.rows.values().collect())
                    .unwrap_or_default();
                let mut left_map: HashMap<Value, Vec<&Row>> = HashMap::new();
                for left in &rows {
                    left_map
                        .entry(left.values[left_idx].clone())
                        .or_default()
                        .push(left);
                }
                for right in &join_rows {
                    let key = right.values[right_idx].clone();
                    if let Some(matches) = left_map.get(&key) {
                        for left in matches {
                            let mut values = left.values.clone();
                            values.extend(right.values.clone());
                            joined.push(Row { values });
                        }
                    } else {
                        let mut values =
                            std::iter::repeat_n(Value::Null, join_col_offset).collect::<Vec<_>>();
                        values.extend(right.values.clone());
                        joined.push(Row { values });
                    }
                }
            }
        }
        rows = joined;
        if !options.allow_full_scan && rows.len() > max_scan_rows {
            return Err(QueryError::ScanBoundExceeded {
                estimated_rows: rows.len() as u64,
                max_scan_rows: max_scan_rows as u64,
            });
        }
        columns = next_columns;
    }

    if let Some(predicate) = &query.predicate {
        let compiled = compile_expr(predicate, &columns, "join")?;
        rows.retain(|r| crate::query::operators::eval_compiled_expr_public(&compiled, r));
    }

    if !query.aggregates.is_empty() {
        let group_by_idx = query
            .group_by
            .iter()
            .map(|name| {
                columns
                    .iter()
                    .position(|c| c == name)
                    .ok_or_else(|| QueryError::ColumnNotFound {
                        table: "join".into(),
                        column: name.clone(),
                    })
            })
            .collect::<Result<Vec<_>, _>>()?;
        let agg_col_idx = query
            .aggregates
            .iter()
            .map(|agg| aggregate_col_idx(agg, &columns))
            .collect::<Result<Vec<_>, _>>()?;

        let mut aggregate = AggregateOperator::new(
            Box::new(ScanOperator::new(rows)),
            query.aggregates.clone(),
            group_by_idx,
            agg_col_idx,
        );
        let mut aggregated_rows = Vec::new();
        while let Some(row) = aggregate.next() {
            aggregated_rows.push(row);
        }
        rows = aggregated_rows;
        columns = query.group_by.clone();
        columns.extend(query.aggregates.iter().map(aggregate_output_name));
    }

    if let Some(having) = &query.having {
        if query.aggregates.is_empty() {
            return Err(QueryError::InvalidQuery {
                reason: "having requires aggregate or group_by".into(),
            });
        }
        let compiled = compile_expr(having, &columns, "join")?;
        rows.retain(|r| crate::query::operators::eval_compiled_expr_public(&compiled, r));
    }

    if !query.order_by.is_empty() {
        let order_pairs: Vec<(usize, crate::query::plan::Order)> = query
            .order_by
            .iter()
            .map(|(col, ord)| {
                columns
                    .iter()
                    .position(|c| c == col)
                    .map(|idx| (idx, *ord))
                    .ok_or_else(|| QueryError::ColumnNotFound {
                        table: "join".into(),
                        column: col.clone(),
                    })
            })
            .collect::<Result<_, _>>()?;
        rows.sort_by(|a, b| {
            for (idx, ord) in &order_pairs {
                let cmp = a.values[*idx].cmp(&b.values[*idx]);
                let ord_cmp = match ord {
                    crate::query::plan::Order::Asc => cmp,
                    crate::query::plan::Order::Desc => cmp.reverse(),
                };
                if !ord_cmp.is_eq() {
                    return ord_cmp;
                }
            }
            std::cmp::Ordering::Equal
        });
    }

    let rows_examined = rows.len();
    let page_size = query.limit.unwrap_or_else(|| {
        cursor_state
            .as_ref()
            .map(|c| c.page_size)
            .unwrap_or(max_scan_rows.min(100))
    });
    let effective_page_size = page_size.min(max_scan_rows);
    let sort_indices: Vec<(usize, crate::query::plan::Order)> = if !query.order_by.is_empty() {
        query
            .order_by
            .iter()
            .filter_map(|(name, ord)| columns.iter().position(|c| c == name).map(|i| (i, *ord)))
            .collect()
    } else {
        Vec::new()
    };
    let pk_indices: Vec<usize> = (0..columns.len()).collect();
    let mut sliced = Vec::new();
    for row in rows {
        if let Some(cursor) = &cursor_state
            && !row_after_cursor(&row, cursor, &sort_indices, &pk_indices)
        {
            continue;
        }
        sliced.push(row);
        if sliced.len() > effective_page_size {
            break;
        }
    }
    let has_more = sliced.len() > effective_page_size;
    if has_more {
        sliced.truncate(effective_page_size);
    }
    let cursor_last_row = sliced.last().cloned();

    if !query.select.is_empty() && query.select[0] != "*" {
        let idxs: Vec<usize> = query
            .select
            .iter()
            .map(|col| {
                columns
                    .iter()
                    .position(|c| c == col)
                    .ok_or_else(|| QueryError::ColumnNotFound {
                        table: "join".into(),
                        column: col.clone(),
                    })
            })
            .collect::<Result<_, _>>()?;
        sliced = sliced
            .into_iter()
            .map(|r| Row {
                values: idxs.iter().map(|i| r.values[*i].clone()).collect(),
            })
            .collect();
    }

    let returned_rows = sliced.len();
    let remaining_limit_after_page = compute_remaining_limit_after_page(
        query.limit,
        cursor_state.as_ref().and_then(|c| c.remaining_limit),
        returned_rows,
    );
    let cursor = if has_more {
        let last_row = cursor_last_row.ok_or_else(|| QueryError::InvalidQuery {
            reason: "invalid cursor state".into(),
        })?;
        Some(encode_cursor(
            &CursorToken {
                snapshot_seq,
                last_sort_key: extract_sort_key(&last_row, &sort_indices),
                last_pk: extract_pk_key(&last_row, &pk_indices),
                page_size,
                remaining_limit: remaining_limit_after_page,
            },
            cursor_signing_key,
        )?)
    } else {
        None
    };
    let split_budget = query.limit.unwrap_or(max_scan_rows);
    let split_recommended = compute_split_recommended(rows_examined, split_budget);
    Ok(QueryResult {
        rows_examined,
        rows: sliced,
        truncated: cursor.is_some(),
        cursor,
        snapshot_seq,
        materialized_seq: None,
        split_recommended,
    })
}

pub(super) fn resolve_table_ref(
    project_id: &str,
    scope_id: &str,
    table_ref: &str,
) -> (String, String, String) {
    if let Some(name) = table_ref.strip_prefix("_global.") {
        return ("_global".to_string(), "app".to_string(), name.to_string());
    }
    (
        project_id.to_string(),
        scope_id.to_string(),
        table_ref.to_string(),
    )
}

fn is_single_column_primary_key_join(
    join_schema: &crate::catalog::schema::TableSchema,
    right_idx: usize,
) -> bool {
    if join_schema.primary_key.len() != 1 {
        return false;
    }
    join_schema
        .columns
        .get(right_idx)
        .map(|column| column.name == join_schema.primary_key[0])
        .unwrap_or(false)
}