Skip to main content

nodedb_sql/types/
query.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Query structure types: projections, sort keys, aggregates, windows, engine/join/spatial enums.
4
5use crate::types_expr::SqlExpr;
6
7/// Database engine type for a collection.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum EngineType {
10    DocumentSchemaless,
11    DocumentStrict,
12    KeyValue,
13    Columnar,
14    Timeseries,
15    Spatial,
16    /// ND sparse array engine — `CREATE ARRAY ...`. Routes through
17    /// `ArrayRules` for SQL-level validation, but most table-shaped
18    /// operations are unsupported on this engine (DML happens via
19    /// dedicated `INSERT INTO ARRAY` / `DELETE FROM ARRAY` syntax).
20    Array,
21}
22
23/// SQL join type.
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
25pub enum JoinType {
26    Inner,
27    Left,
28    Right,
29    Full,
30    Semi,
31    Anti,
32    Cross,
33}
34
35impl JoinType {
36    pub fn as_str(&self) -> &'static str {
37        match self {
38            Self::Inner => "inner",
39            Self::Left => "left",
40            Self::Right => "right",
41            Self::Full => "full",
42            Self::Semi => "semi",
43            Self::Anti => "anti",
44            Self::Cross => "cross",
45        }
46    }
47}
48
49/// Spatial predicate types.
50#[derive(Debug, Clone, Copy, PartialEq, Eq)]
51pub enum SpatialPredicate {
52    DWithin,
53    Contains,
54    Intersects,
55    Within,
56}
57
58/// Projection item in SELECT.
59#[derive(Debug, Clone)]
60pub enum Projection {
61    /// Simple column reference: `SELECT name`
62    Column(String),
63    /// All columns: `SELECT *`
64    Star,
65    /// Qualified star: `SELECT t.*`
66    QualifiedStar(String),
67    /// Computed expression: `SELECT price * qty AS total`
68    Computed { expr: SqlExpr, alias: String },
69}
70
71/// Sort key for ORDER BY.
72#[derive(Debug, Clone)]
73pub struct SortKey {
74    pub expr: SqlExpr,
75    pub ascending: bool,
76    pub nulls_first: bool,
77}
78
79/// Aggregate expression: `COUNT(*)`, `SUM(amount)`, etc.
80#[derive(Debug, Clone)]
81pub struct AggregateExpr {
82    pub function: String,
83    pub args: Vec<SqlExpr>,
84    pub alias: String,
85    pub distinct: bool,
86    /// For the synthetic `GROUPING(col)` pseudo-aggregate: the index of `col`
87    /// in the canonical group-by key list. The executor uses this index to read
88    /// the grouping-set bitmask and return 0 (present) or 1 (NULL-filled).
89    /// `None` for all ordinary aggregate functions.
90    pub grouping_col_index: Option<usize>,
91}
92
93/// Window function specification.
94#[derive(Debug, Clone)]
95pub struct WindowSpec {
96    pub function: String,
97    pub args: Vec<SqlExpr>,
98    pub partition_by: Vec<SqlExpr>,
99    pub order_by: Vec<SortKey>,
100    pub alias: String,
101    /// Frame specification (`ROWS`/`RANGE` BETWEEN ... AND ...).
102    /// Defaults to `RANGE UNBOUNDED PRECEDING TO CURRENT ROW`
103    /// when the SQL omits a frame clause.
104    pub frame: nodedb_query::WindowFrame,
105}