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
//! Relational Query
//!
//! Strictly typed AST for descibing relational queries.

mod expr;
mod fold;
mod ids;
mod transform;

pub use expr::{Expr, ExprKind, UnOp};
pub use fold::*;
pub use ids::*;
pub use transform::*;

use enum_as_inner::EnumAsInner;
use serde::{Deserialize, Serialize};

use super::pl::{ColumnSort, QueryDef, Range, WindowFrame};
use super::pl::{InterpolateItem, TableExternRef};

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct Query {
    pub def: QueryDef,

    pub tables: Vec<TableDecl>,
    pub relation: Relation,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, EnumAsInner)]
pub enum Relation {
    ExternRef(TableExternRef, Vec<ColumnDecl>),
    Pipeline(Vec<Transform>),
    Literal(RelationLiteral, Vec<ColumnDecl>),
    SString(Vec<InterpolateItem<Expr>>, Vec<ColumnDecl>),
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct TableDecl {
    pub id: TId,

    /// Given name of this table (name of the CTE)
    pub name: Option<String>,

    pub relation: Relation,
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub struct TableRef {
    // referenced table
    pub source: TId,

    // new column definitions are required because there may be multiple instances
    // of this table in the same query
    pub columns: Vec<ColumnDecl>,

    /// Given name of this table (table alias)
    pub name: Option<String>,
}

#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
pub struct RelationLiteral {
    /// Column names
    pub columns: Vec<String>,
    /// Row-oriented data
    // TODO: this should be generic, so it can contain any type (but at least
    // numbers)
    pub rows: Vec<Vec<String>>,
}