• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

joaoh82 / rust_sqlite / 26781911684

01 Jun 2026 09:03PM UTC coverage: 69.235% (+0.3%) from 68.985%
26781911684

Pull #158

github

web-flow
Merge 8b575ec8f into d508336e0
Pull Request #158: feat(sql): JOIN ... USING / NATURAL / CROSS (SQLR-5)

142 of 146 new or added lines in 2 files covered. (97.26%)

11484 of 16587 relevant lines covered (69.23%)

1.25 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.3
/src/sql/parser/select.rs
1
use sqlparser::ast::{
2
    DuplicateTreatment, Expr, FunctionArg, FunctionArgExpr, FunctionArguments, JoinConstraint,
3
    JoinOperator, LimitClause, ObjectName, ObjectNamePart, OrderByKind, Query, Select, SelectItem,
4
    SetExpr, Statement, TableFactor, TableWithJoins, Value,
5
};
6

7
use crate::error::{Result, SQLRiteError};
8

9
/// Aggregate function name. v1 covers the SQLite-classic five.
10
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11
pub enum AggregateFn {
12
    Count,
13
    Sum,
14
    Avg,
15
    Min,
16
    Max,
17
}
18

19
impl AggregateFn {
20
    pub fn as_str(self) -> &'static str {
1✔
21
        match self {
1✔
22
            AggregateFn::Count => "COUNT",
1✔
23
            AggregateFn::Sum => "SUM",
1✔
24
            AggregateFn::Avg => "AVG",
1✔
25
            AggregateFn::Min => "MIN",
1✔
26
            AggregateFn::Max => "MAX",
1✔
27
        }
28
    }
29

30
    fn from_name(name: &str) -> Option<Self> {
1✔
31
        match name.to_ascii_lowercase().as_str() {
2✔
32
            "count" => Some(AggregateFn::Count),
2✔
33
            "sum" => Some(AggregateFn::Sum),
3✔
34
            "avg" => Some(AggregateFn::Avg),
3✔
35
            "min" => Some(AggregateFn::Min),
3✔
36
            "max" => Some(AggregateFn::Max),
3✔
37
            _ => None,
×
38
        }
39
    }
40
}
41

42
/// What the aggregate is fed: `*` (only valid for COUNT) or a bare column.
43
#[derive(Debug, Clone, PartialEq, Eq)]
44
pub enum AggregateArg {
45
    Star,
46
    Column(String),
47
}
48

49
/// A parsed aggregate call like `COUNT(*)`, `SUM(salary)`, `COUNT(DISTINCT dept)`.
50
#[derive(Debug, Clone, PartialEq, Eq)]
51
pub struct AggregateCall {
52
    pub func: AggregateFn,
53
    pub arg: AggregateArg,
54
    /// `DISTINCT` inside the parens. v1 only allows it on COUNT.
55
    pub distinct: bool,
56
}
57

58
impl AggregateCall {
59
    /// Canonical display form used to match ORDER BY expressions against
60
    /// aggregate output columns when the user didn't supply an alias.
61
    /// Mirrors the output-header convention.
62
    pub fn display_name(&self) -> String {
1✔
63
        let inner = match &self.arg {
1✔
64
            AggregateArg::Star => "*".to_string(),
1✔
65
            AggregateArg::Column(c) => {
1✔
66
                if self.distinct {
1✔
67
                    format!("DISTINCT {c}")
1✔
68
                } else {
69
                    c.clone()
1✔
70
                }
71
            }
72
        };
73
        format!("{}({inner})", self.func.as_str())
2✔
74
    }
75
}
76

77
/// One entry in the projection list.
78
#[derive(Debug, Clone)]
79
pub struct ProjectionItem {
80
    pub kind: ProjectionKind,
81
    /// `AS alias` if explicitly supplied.
82
    pub alias: Option<String>,
83
}
84

85
impl ProjectionItem {
86
    /// Resolve the user-visible column header for this projection item.
87
    /// Alias if supplied, else the bare column name or aggregate display.
88
    /// For qualified `t.col` shapes the header is just `col` — this
89
    /// matches SQLite, where qualifiers don't propagate to output
90
    /// column names.
91
    pub fn output_name(&self) -> String {
1✔
92
        if let Some(a) = &self.alias {
1✔
93
            return a.clone();
1✔
94
        }
95
        match &self.kind {
1✔
96
            ProjectionKind::Column { name, .. } => name.clone(),
1✔
97
            ProjectionKind::Aggregate(a) => a.display_name(),
1✔
98
        }
99
    }
100
}
101

102
/// What an individual projection item produces.
103
#[derive(Debug, Clone)]
104
pub enum ProjectionKind {
105
    /// Column reference. `qualifier` is `Some` for `t.col` shapes
106
    /// (SQLR-5 — needed so JOIN execution can disambiguate
107
    /// same-named columns across tables); `None` for bare `col`.
108
    /// The single-table path ignores the qualifier and looks up the
109
    /// name directly, preserving legacy behavior.
110
    Column {
111
        qualifier: Option<String>,
112
        name: String,
113
    },
114
    /// Aggregate function call: `COUNT(*)`, `SUM(col)`, etc.
115
    Aggregate(AggregateCall),
116
}
117

118
/// What columns to project from a SELECT.
119
#[derive(Debug, Clone)]
120
pub enum Projection {
121
    /// `SELECT *` — every column in the table, in declaration order.
122
    All,
123
    /// Explicit, ordered projection list — possibly mixing bare columns
124
    /// with aggregate calls (`SELECT dept, COUNT(*) FROM t`).
125
    Items(Vec<ProjectionItem>),
126
}
127

128
/// A parsed `ORDER BY` clause: a single sort key (expression), ascending
129
/// by default. Phase 7b widened this from "bare column name" to
130
/// "arbitrary expression" so KNN queries of the form
131
/// `ORDER BY vec_distance_l2(col, [...]) LIMIT k` work end-to-end. The
132
/// expression is evaluated per-row at execution time via `eval_expr`;
133
/// the simple `ORDER BY col` form still works because that's just an
134
/// `Expr::Identifier` taking the same path.
135
#[derive(Debug, Clone)]
136
pub struct OrderByClause {
137
    pub expr: Expr,
138
    pub ascending: bool,
139
}
140

141
/// SQLR-5 — flavor of join. SQLite ships INNER and LEFT OUTER; we
142
/// implement the full quartet on top of a single nested-loop driver
143
/// because the per-flavor differences are small (NULL-padding policy
144
/// for unmatched left/right rows). RIGHT OUTER and FULL OUTER aren't
145
/// in SQLite — see `docs/design-decisions.md` for the rationale.
146
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
147
pub enum JoinType {
148
    Inner,
149
    LeftOuter,
150
    RightOuter,
151
    FullOuter,
152
}
153

154
impl JoinType {
155
    pub fn as_str(self) -> &'static str {
×
156
        match self {
×
157
            JoinType::Inner => "INNER",
×
158
            JoinType::LeftOuter => "LEFT OUTER",
×
159
            JoinType::RightOuter => "RIGHT OUTER",
×
160
            JoinType::FullOuter => "FULL OUTER",
×
161
        }
162
    }
163
}
164

165
/// How a JOIN matches rows. SQLR-5 originally shipped `ON` only; the
166
/// USING / NATURAL increment adds the two name-based constraints.
167
/// `ON` carries its predicate straight from the parser. `USING` and
168
/// `NATURAL` defer their equality synthesis to the executor because
169
/// they need table schemas (which column names exist, and — for
170
/// `NATURAL` — which are shared) that the parser doesn't have. The
171
/// executor turns both into the same `left.col = right.col [AND …]`
172
/// predicate the `ON` path already evaluates. `CROSS JOIN` is rewritten
173
/// to `ON true` at parse time (no schema needed) and so reuses the
174
/// `On` variant directly.
175
#[derive(Debug, Clone)]
176
pub enum JoinConstraintKind {
177
    /// `ON <expr>` (and the parse-time rewrite of `CROSS JOIN` to
178
    /// `ON true`). Evaluated per-row over the multi-table scope. Boxed
179
    /// to keep this enum small — `Expr` dwarfs the other variants.
180
    On(Box<Expr>),
181
    /// `USING (col[, col…])` — equality on each named column, plus the
182
    /// SQLite convention that each named column appears once in
183
    /// `SELECT *`. Columns are validated and the predicate is
184
    /// synthesized at execution time.
185
    Using(Vec<String>),
186
    /// `NATURAL` — the shared column names of the two sides are
187
    /// discovered at execution time, then treated exactly like
188
    /// `USING (<shared cols>)`. No shared columns ⇒ a cross product.
189
    Natural,
190
}
191

192
/// One JOIN clause from the FROM list. Multi-join queries
193
/// (`A JOIN B ... JOIN C ...`) become a `Vec<JoinClause>` evaluated
194
/// left-to-right against the accumulator. The match condition is one
195
/// of `ON` / `USING` / `NATURAL` (see [`JoinConstraintKind`]);
196
/// `CROSS JOIN` arrives here already rewritten to `ON true`.
197
#[derive(Debug, Clone)]
198
pub struct JoinClause {
199
    pub join_type: JoinType,
200
    pub right_table: String,
201
    /// `AS alias` if the right table introduced one. Stored separately
202
    /// from `right_table` so the executor can normalize on
203
    /// `alias.unwrap_or(right_table)` for qualifier matching.
204
    pub right_alias: Option<String>,
205
    /// What the join matches on. See [`JoinConstraintKind`].
206
    pub constraint: JoinConstraintKind,
207
}
208

209
/// A parsed, simplified SELECT query.
210
#[derive(Debug, Clone)]
211
pub struct SelectQuery {
212
    pub table_name: String,
213
    /// Optional `AS alias` on the leading FROM table. The executor's
214
    /// scope resolver treats `alias.unwrap_or(table_name)` as the
215
    /// qualifier name.
216
    pub table_alias: Option<String>,
217
    /// SQLR-5 — JOIN clauses in source order. Empty = single-table
218
    /// SELECT, the existing fast path.
219
    pub joins: Vec<JoinClause>,
220
    pub projection: Projection,
221
    /// Raw sqlparser WHERE expression, evaluated by the executor at run time.
222
    pub selection: Option<Expr>,
223
    pub order_by: Option<OrderByClause>,
224
    pub limit: Option<usize>,
225
    /// `SELECT DISTINCT`.
226
    pub distinct: bool,
227
    /// `GROUP BY a, b` — bare column names. Empty = no GROUP BY.
228
    pub group_by: Vec<String>,
229
}
230

231
impl SelectQuery {
232
    pub fn new(statement: &Statement) -> Result<Self> {
1✔
233
        let Statement::Query(query) = statement else {
1✔
234
            return Err(SQLRiteError::Internal(
×
235
                "Error parsing SELECT: expected a Query statement".to_string(),
×
236
            ));
237
        };
238

239
        let Query {
1✔
240
            body,
1✔
241
            order_by,
1✔
242
            limit_clause,
1✔
243
            ..
×
244
        } = query.as_ref();
×
245

246
        let SetExpr::Select(select) = body.as_ref() else {
2✔
247
            return Err(SQLRiteError::NotImplemented(
×
248
                "Only simple SELECT queries are supported (no UNION / VALUES / CTEs yet)"
×
249
                    .to_string(),
×
250
            ));
251
        };
252
        let Select {
1✔
253
            projection,
1✔
254
            from,
1✔
255
            selection,
1✔
256
            distinct,
1✔
257
            group_by,
1✔
258
            having,
1✔
259
            ..
×
260
        } = select.as_ref();
×
261

262
        // SQLR-3: read DISTINCT instead of rejecting it. Postgres's
263
        // `DISTINCT ON (...)` stays unsupported — it's a per-group
264
        // tie-breaker that isn't part of the SQLite surface we mirror.
265
        let distinct_flag = match distinct {
2✔
266
            None => false,
1✔
267
            Some(sqlparser::ast::Distinct::Distinct) => true,
1✔
268
            Some(sqlparser::ast::Distinct::All) => false,
×
269
            Some(sqlparser::ast::Distinct::On(_)) => {
×
270
                return Err(SQLRiteError::NotImplemented(
×
271
                    "SELECT DISTINCT ON (...) is not supported".to_string(),
×
272
                ));
273
            }
274
        };
275
        if having.is_some() {
1✔
276
            return Err(SQLRiteError::NotImplemented(
×
277
                "HAVING is not supported yet".to_string(),
×
278
            ));
279
        }
280
        // SQLR-3: parse GROUP BY into a list of bare column names.
281
        // GroupByExpr::Expressions(v, _) with an empty v is the "no
282
        // GROUP BY" shape; non-empty means we've got grouping. Reject
283
        // GROUP BY ALL and GROUP BY on non-bare expressions for v1.
284
        let group_by_cols: Vec<String> = match group_by {
1✔
285
            sqlparser::ast::GroupByExpr::Expressions(exprs, _) => {
1✔
286
                let mut out = Vec::with_capacity(exprs.len());
1✔
287
                for e in exprs {
2✔
288
                    let col = match e {
1✔
289
                        Expr::Identifier(ident) => ident.value.clone(),
2✔
290
                        Expr::CompoundIdentifier(parts) => {
×
291
                            parts.last().map(|p| p.value.clone()).ok_or_else(|| {
×
292
                                SQLRiteError::Internal("empty compound identifier".to_string())
×
293
                            })?
294
                        }
295
                        other => {
×
296
                            return Err(SQLRiteError::NotImplemented(format!(
×
297
                                "GROUP BY only supports bare column references for now, got {other:?}"
×
298
                            )));
299
                        }
300
                    };
301
                    out.push(col);
2✔
302
                }
303
                out
1✔
304
            }
305
            _ => {
×
306
                return Err(SQLRiteError::NotImplemented(
×
307
                    "GROUP BY ALL is not supported".to_string(),
×
308
                ));
309
            }
310
        };
311

312
        let (table_name, table_alias, joins) = extract_from_clause(from)?;
2✔
313
        let projection = parse_projection(projection)?;
3✔
314
        let order_by = parse_order_by(order_by.as_ref())?;
2✔
315
        let limit = parse_limit(limit_clause.as_ref())?;
2✔
316

317
        // SQLR-3 validation: when GROUP BY is present, every bare-column
318
        // entry in the projection must appear in the GROUP BY list. Bare
319
        // columns in the SELECT are otherwise undefined per group.
320
        if !group_by_cols.is_empty()
1✔
321
            && let Projection::Items(items) = &projection
2✔
322
        {
323
            for item in items {
1✔
324
                if let ProjectionKind::Column { name: c, .. } = &item.kind
1✔
325
                    && !group_by_cols.contains(c)
1✔
326
                {
327
                    return Err(SQLRiteError::Internal(format!(
1✔
328
                        "column '{c}' must appear in GROUP BY or be used in an aggregate function"
×
329
                    )));
330
                }
331
            }
332
        }
333

334
        // SQLR-5 — aggregations across joined results aren't covered
335
        // by the current single-table grouping pipeline. Reject GROUP
336
        // BY / aggregates over a join up front so the user gets a clear
337
        // message rather than wrong results.
338
        if !joins.is_empty() {
2✔
339
            let has_agg = matches!(
3✔
340
                &projection,
1✔
341
                Projection::Items(items)
1✔
342
                    if items.iter().any(|i| matches!(i.kind, ProjectionKind::Aggregate(_)))
4✔
343
            );
344
            if has_agg || !group_by_cols.is_empty() {
2✔
345
                return Err(SQLRiteError::NotImplemented(
1✔
346
                    "GROUP BY / aggregate functions over JOIN results are not supported yet"
×
347
                        .to_string(),
1✔
348
                ));
349
            }
350
            if distinct_flag {
1✔
351
                return Err(SQLRiteError::NotImplemented(
×
352
                    "SELECT DISTINCT over JOIN results is not supported yet".to_string(),
×
353
                ));
354
            }
355
        }
356

357
        Ok(SelectQuery {
1✔
358
            table_name,
1✔
359
            table_alias,
1✔
360
            joins,
1✔
361
            projection,
1✔
362
            selection: selection.clone(),
1✔
363
            order_by,
1✔
364
            limit,
×
365
            distinct: distinct_flag,
1✔
366
            group_by: group_by_cols,
1✔
367
        })
368
    }
369
}
370

371
/// Pull the leading FROM table (with optional alias) and any JOIN
372
/// clauses out of the parsed FROM list. Supports a single base table
373
/// plus zero or more INNER / LEFT / RIGHT / FULL OUTER joins with an
374
/// `ON`, `USING (...)`, or `NATURAL` constraint, and `CROSS JOIN`
375
/// (rewritten to `INNER ... ON true`). Comma-separated FROM lists and
376
/// SEMI / ANTI / ASOF / APPLY joins surface as `NotImplemented`.
377
fn extract_from_clause(
1✔
378
    from: &[TableWithJoins],
379
) -> Result<(String, Option<String>, Vec<JoinClause>)> {
380
    if from.is_empty() {
1✔
381
        return Err(SQLRiteError::Internal(
×
382
            "SELECT requires a FROM clause".to_string(),
×
383
        ));
384
    }
385
    if from.len() != 1 {
1✔
386
        return Err(SQLRiteError::NotImplemented(
×
387
            "comma-separated FROM lists are not supported — use explicit JOIN syntax".to_string(),
×
388
        ));
389
    }
390
    let twj = &from[0];
2✔
391
    let (table_name, table_alias) = extract_table_factor(&twj.relation)?;
1✔
392

393
    let mut joins = Vec::with_capacity(twj.joins.len());
2✔
394
    for j in &twj.joins {
3✔
395
        let (right_table, right_alias) = extract_table_factor(&j.relation)?;
2✔
396
        let (join_type, constraint) = match &j.join_operator {
6✔
397
            // Bare `JOIN` defaults to INNER per SQL standard.
398
            JoinOperator::Join(c) | JoinOperator::Inner(c) => {
2✔
399
                (JoinType::Inner, convert_constraint(c)?)
2✔
400
            }
401
            JoinOperator::Left(c) | JoinOperator::LeftOuter(c) => {
1✔
402
                (JoinType::LeftOuter, convert_constraint(c)?)
1✔
403
            }
404
            JoinOperator::Right(c) | JoinOperator::RightOuter(c) => {
1✔
405
                (JoinType::RightOuter, convert_constraint(c)?)
1✔
406
            }
407
            JoinOperator::FullOuter(c) => (JoinType::FullOuter, convert_constraint(c)?),
2✔
408
            // `CROSS JOIN` is the cross product: INNER with an always-true
409
            // ON. A constraint on a CROSS JOIN is non-standard, but if the
410
            // parser handed us `USING` / `NATURAL` / `ON` we honor it
411
            // rather than silently dropping it.
412
            JoinOperator::CrossJoin(c) => (JoinType::Inner, convert_cross_constraint(c)?),
2✔
413
            other => {
×
414
                return Err(SQLRiteError::NotImplemented(format!(
×
415
                    "join flavor {other:?} is not supported \
416
                     (only INNER / LEFT OUTER / RIGHT OUTER / FULL OUTER / CROSS, \
417
                     with ON / USING / NATURAL)"
418
                )));
419
            }
420
        };
421
        joins.push(JoinClause {
1✔
422
            join_type,
423
            right_table,
1✔
424
            right_alias,
1✔
425
            constraint,
1✔
426
        });
427
    }
428

429
    Ok((table_name, table_alias, joins))
1✔
430
}
431

432
fn extract_table_factor(tf: &TableFactor) -> Result<(String, Option<String>)> {
1✔
433
    match tf {
1✔
434
        TableFactor::Table { name, alias, .. } => {
1✔
435
            let table_name = name.to_string();
1✔
436
            let alias_name = alias.as_ref().map(|a| a.name.value.clone());
4✔
437
            // We don't yet support alias column lists like `(c1, c2)` —
438
            // they only matter for table-valued functions / derived
439
            // tables, which we don't have either.
440
            if let Some(a) = alias.as_ref()
2✔
441
                && !a.columns.is_empty()
2✔
442
            {
443
                return Err(SQLRiteError::NotImplemented(
×
444
                    "table alias column lists are not supported".to_string(),
×
445
                ));
446
            }
447
            Ok((table_name, alias_name))
1✔
448
        }
449
        _ => Err(SQLRiteError::NotImplemented(
×
450
            "only plain table references are supported in FROM / JOIN".to_string(),
×
451
        )),
452
    }
453
}
454

455
/// Lower a `sqlparser` join constraint into our [`JoinConstraintKind`].
456
/// `ON` passes through; `USING` is narrowed to a list of bare column
457
/// names; `NATURAL` defers to the executor. A constraint-less join
458
/// (`A JOIN B` with no `ON` / `USING`) is rejected — `CROSS JOIN` is
459
/// the supported way to ask for a cross product and is handled by
460
/// [`convert_cross_constraint`].
461
fn convert_constraint(constraint: &JoinConstraint) -> Result<JoinConstraintKind> {
1✔
462
    match constraint {
1✔
463
        JoinConstraint::On(expr) => Ok(JoinConstraintKind::On(Box::new(expr.clone()))),
1✔
464
        JoinConstraint::Using(cols) => {
1✔
465
            let names = cols
2✔
466
                .iter()
1✔
467
                .map(extract_using_column)
1✔
468
                .collect::<Result<Vec<String>>>()?;
1✔
469
            Ok(JoinConstraintKind::Using(names))
1✔
470
        }
471
        JoinConstraint::Natural => Ok(JoinConstraintKind::Natural),
1✔
472
        JoinConstraint::None => Err(SQLRiteError::NotImplemented(
×
473
            "JOIN without an ON / USING / NATURAL condition is not supported \
474
             (use `... ON ...`, `... USING (...)`, `NATURAL JOIN`, or `CROSS JOIN`)"
NEW
475
                .to_string(),
×
476
        )),
477
    }
478
}
479

480
/// Constraint handling for `CROSS JOIN`. The standard form carries no
481
/// constraint and means "cross product", which we express as `ON true`
482
/// so it flows through the same executor path as any other join.
483
fn convert_cross_constraint(constraint: &JoinConstraint) -> Result<JoinConstraintKind> {
1✔
484
    match constraint {
1✔
485
        JoinConstraint::None => Ok(JoinConstraintKind::On(Box::new(true_literal()))),
1✔
486
        // Non-standard, but if a constraint was attached to a CROSS JOIN,
487
        // honor it instead of dropping it on the floor.
NEW
488
        other => convert_constraint(other),
×
489
    }
490
}
491

492
/// Pull a bare column name out of a `USING (...)` entry. `USING`
493
/// columns are always simple identifiers; anything qualified or
494
/// multi-part is rejected.
495
fn extract_using_column(name: &ObjectName) -> Result<String> {
1✔
496
    match name.0.as_slice() {
2✔
497
        [ObjectNamePart::Identifier(ident)] => Ok(ident.value.clone()),
2✔
NEW
498
        _ => Err(SQLRiteError::NotImplemented(format!(
×
499
            "USING column must be a simple column name, got {name}"
500
        ))),
501
    }
502
}
503

504
/// An always-true boolean literal expression, used to rewrite
505
/// `CROSS JOIN` into `INNER JOIN ... ON true`.
506
fn true_literal() -> Expr {
1✔
507
    Expr::Value(Value::Boolean(true).with_empty_span())
1✔
508
}
509

510
fn parse_projection(items: &[SelectItem]) -> Result<Projection> {
1✔
511
    // Special-case `SELECT *`.
512
    if items.len() == 1
1✔
513
        && let SelectItem::Wildcard(_) = &items[0]
1✔
514
    {
515
        return Ok(Projection::All);
1✔
516
    }
517
    let mut out = Vec::with_capacity(items.len());
1✔
518
    for item in items {
2✔
519
        out.push(parse_select_item(item)?);
2✔
520
    }
521
    Ok(Projection::Items(out))
1✔
522
}
523

524
fn parse_select_item(item: &SelectItem) -> Result<ProjectionItem> {
1✔
525
    match item {
1✔
526
        SelectItem::UnnamedExpr(expr) => parse_projection_expr(expr, None),
1✔
527
        SelectItem::ExprWithAlias { expr, alias } => {
1✔
528
            parse_projection_expr(expr, Some(alias.value.clone()))
1✔
529
        }
530
        SelectItem::Wildcard(_) | SelectItem::QualifiedWildcard(_, _) => {
531
            Err(SQLRiteError::NotImplemented(
×
532
                "Wildcard mixed with other columns is not supported".to_string(),
×
533
            ))
534
        }
535
    }
536
}
537

538
fn parse_projection_expr(expr: &Expr, alias: Option<String>) -> Result<ProjectionItem> {
1✔
539
    match expr {
1✔
540
        Expr::Identifier(ident) => Ok(ProjectionItem {
2✔
541
            kind: ProjectionKind::Column {
1✔
542
                qualifier: None,
1✔
543
                name: ident.value.clone(),
1✔
544
            },
545
            alias,
1✔
546
        }),
547
        Expr::CompoundIdentifier(parts) => match parts.as_slice() {
1✔
548
            [only] => Ok(ProjectionItem {
1✔
549
                kind: ProjectionKind::Column {
×
550
                    qualifier: None,
×
551
                    name: only.value.clone(),
×
552
                },
553
                alias,
×
554
            }),
555
            [q, c] => Ok(ProjectionItem {
3✔
556
                kind: ProjectionKind::Column {
1✔
557
                    qualifier: Some(q.value.clone()),
2✔
558
                    name: c.value.clone(),
1✔
559
                },
560
                alias,
1✔
561
            }),
562
            _ => Err(SQLRiteError::NotImplemented(format!(
×
563
                "compound identifier with {} parts is not supported in projection",
564
                parts.len()
×
565
            ))),
566
        },
567
        Expr::Function(func) => {
1✔
568
            let call = parse_aggregate_call(func)?;
2✔
569
            Ok(ProjectionItem {
1✔
570
                kind: ProjectionKind::Aggregate(call),
1✔
571
                alias,
1✔
572
            })
573
        }
574
        other => Err(SQLRiteError::NotImplemented(format!(
2✔
575
            "Only bare column references and aggregate functions are supported in the projection list (got {other:?})"
576
        ))),
577
    }
578
}
579

580
fn parse_aggregate_call(func: &sqlparser::ast::Function) -> Result<AggregateCall> {
1✔
581
    // Function name: only unqualified names like COUNT(...). Qualified
582
    // names like `pkg.fn(...)` are out of scope.
583
    let name = match func.name.0.as_slice() {
2✔
584
        [sqlparser::ast::ObjectNamePart::Identifier(ident)] => ident.value.clone(),
2✔
585
        _ => {
586
            return Err(SQLRiteError::NotImplemented(format!(
×
587
                "qualified function names not supported: {:?}",
588
                func.name
589
            )));
590
        }
591
    };
592
    let agg_fn = AggregateFn::from_name(&name).ok_or_else(|| {
2✔
593
        SQLRiteError::NotImplemented(format!(
×
594
            "function '{name}' is not supported in the projection list (only aggregate functions are: COUNT, SUM, AVG, MIN, MAX)"
595
        ))
596
    })?;
597

598
    // Aggregates only accept the basic List form. None / Subquery forms
599
    // (CURRENT_TIMESTAMP, scalar subqueries) don't apply here.
600
    let arg_list = match &func.args {
1✔
601
        FunctionArguments::List(l) => l,
1✔
602
        _ => {
603
            return Err(SQLRiteError::NotImplemented(format!(
×
604
                "{name}(...) — unsupported argument shape"
605
            )));
606
        }
607
    };
608

609
    let distinct = matches!(
2✔
610
        arg_list.duplicate_treatment,
2✔
611
        Some(DuplicateTreatment::Distinct)
612
    );
613

614
    if !arg_list.clauses.is_empty() {
1✔
615
        return Err(SQLRiteError::NotImplemented(format!(
×
616
            "{name}(...) — extra argument clauses (ORDER BY / LIMIT inside the call) are not supported"
617
        )));
618
    }
619
    if func.over.is_some() {
2✔
620
        return Err(SQLRiteError::NotImplemented(
×
621
            "window functions (OVER (...)) are not supported".to_string(),
×
622
        ));
623
    }
624
    if func.filter.is_some() {
2✔
625
        return Err(SQLRiteError::NotImplemented(
×
626
            "FILTER (WHERE ...) on aggregates is not supported".to_string(),
×
627
        ));
628
    }
629
    if !func.within_group.is_empty() {
2✔
630
        return Err(SQLRiteError::NotImplemented(
×
631
            "WITHIN GROUP on aggregates is not supported".to_string(),
×
632
        ));
633
    }
634

635
    if arg_list.args.len() != 1 {
2✔
636
        return Err(SQLRiteError::NotImplemented(format!(
×
637
            "{name}(...) expects exactly one argument, got {}",
638
            arg_list.args.len()
×
639
        )));
640
    }
641

642
    let arg = match &arg_list.args[0] {
3✔
643
        FunctionArg::Unnamed(FunctionArgExpr::Wildcard) => AggregateArg::Star,
1✔
644
        FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Identifier(ident))) => {
1✔
645
            AggregateArg::Column(ident.value.clone())
2✔
646
        }
647
        FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::CompoundIdentifier(parts))) => {
×
648
            let c = parts
×
649
                .last()
×
650
                .map(|p| p.value.clone())
×
651
                .ok_or_else(|| SQLRiteError::Internal("empty compound identifier".to_string()))?;
×
652
            AggregateArg::Column(c)
×
653
        }
654
        other => {
×
655
            return Err(SQLRiteError::NotImplemented(format!(
×
656
                "{name}(...) — argument must be `*` or a bare column reference (got {other:?})"
657
            )));
658
        }
659
    };
660

661
    // v1: only COUNT(DISTINCT col) is supported. SUM/AVG/MIN/MAX with
662
    // DISTINCT are valid SQL but uncommon and add accumulator complexity
663
    // we don't yet need.
664
    if distinct && agg_fn != AggregateFn::Count {
3✔
665
        return Err(SQLRiteError::NotImplemented(format!(
×
666
            "DISTINCT is only supported on COUNT(...) for now, not {}",
667
            agg_fn.as_str()
×
668
        )));
669
    }
670
    if matches!(arg, AggregateArg::Star) && agg_fn != AggregateFn::Count {
3✔
671
        return Err(SQLRiteError::NotImplemented(format!(
×
672
            "{}(*) is not supported; use {}(<column>)",
673
            agg_fn.as_str(),
×
674
            agg_fn.as_str()
×
675
        )));
676
    }
677

678
    Ok(AggregateCall {
1✔
679
        func: agg_fn,
680
        arg,
1✔
681
        distinct,
1✔
682
    })
683
}
684

685
fn parse_order_by(order_by: Option<&sqlparser::ast::OrderBy>) -> Result<Option<OrderByClause>> {
1✔
686
    let Some(ob) = order_by else {
1✔
687
        return Ok(None);
1✔
688
    };
689
    let exprs = match &ob.kind {
1✔
690
        OrderByKind::Expressions(v) => v,
1✔
691
        OrderByKind::All(_) => {
692
            return Err(SQLRiteError::NotImplemented(
×
693
                "ORDER BY ALL is not supported".to_string(),
×
694
            ));
695
        }
696
    };
697
    if exprs.len() != 1 {
1✔
698
        return Err(SQLRiteError::NotImplemented(
×
699
            "ORDER BY must have exactly one column for now".to_string(),
×
700
        ));
701
    }
702
    let obe = &exprs[0];
1✔
703
    // Phase 7b: accept arbitrary expressions, not just bare column refs.
704
    // The executor's `sort_rowids` evaluates this expression per row via
705
    // `eval_expr`, which handles Identifier (column lookup), Function
706
    // (vec_distance_*), arithmetic, etc. uniformly. The previous
707
    // column-name-only restriction has been lifted.
708
    let expr = obe.expr.clone();
1✔
709
    // `asc == None` is the dialect default (ASC).
710
    let ascending = obe.options.asc.unwrap_or(true);
2✔
711
    Ok(Some(OrderByClause { expr, ascending }))
1✔
712
}
713

714
fn parse_limit(limit: Option<&LimitClause>) -> Result<Option<usize>> {
1✔
715
    let Some(lc) = limit else {
1✔
716
        return Ok(None);
1✔
717
    };
718
    let limit_expr = match lc {
1✔
719
        LimitClause::LimitOffset { limit, offset, .. } => {
1✔
720
            if offset.is_some() {
1✔
721
                return Err(SQLRiteError::NotImplemented(
×
722
                    "OFFSET is not supported yet".to_string(),
×
723
                ));
724
            }
725
            limit.as_ref()
1✔
726
        }
727
        LimitClause::OffsetCommaLimit { .. } => {
728
            return Err(SQLRiteError::NotImplemented(
×
729
                "`LIMIT <offset>, <limit>` syntax is not supported yet".to_string(),
×
730
            ));
731
        }
732
    };
733
    let Some(expr) = limit_expr else {
2✔
734
        return Ok(None);
×
735
    };
736
    let n = eval_const_usize(expr)?;
1✔
737
    Ok(Some(n))
1✔
738
}
739

740
fn eval_const_usize(expr: &Expr) -> Result<usize> {
1✔
741
    match expr {
1✔
742
        Expr::Value(v) => match &v.value {
1✔
743
            sqlparser::ast::Value::Number(n, _) => n.parse::<usize>().map_err(|e| {
1✔
744
                SQLRiteError::Internal(format!("LIMIT must be a non-negative integer: {e}"))
×
745
            }),
746
            _ => Err(SQLRiteError::Internal(
×
747
                "LIMIT must be an integer literal".to_string(),
×
748
            )),
749
        },
750
        _ => Err(SQLRiteError::NotImplemented(
×
751
            "LIMIT expression must be a literal number".to_string(),
×
752
        )),
753
    }
754
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc