vlorql_core/schema/expressions.rs
1//! Tagged expressions and predicates used in query plans.
2//!
3//! Both [`Expression`] and [`Predicate`] are serialized as
4//! internally-tagged JSON enums (`{"type": "...", ...}`). The
5//! validators and the SQL compiler inspect the tag to decide how to
6//! type-check or render the node.
7
8use super::query_plan::QueryPlan;
9use super::types::{BinaryOperator, ComparisonOperator, DataType};
10use schemars::JsonSchema;
11use serde::{Deserialize, Serialize};
12
13/// An expression that can be selected, filtered, grouped, or ordered.
14///
15/// The enum is serialized with `#[serde(tag = "type", rename_all = "snake_case")]`,
16/// so a literal payload looks like
17/// `{"type": "literal", "value": 42, "data_type": "int"}` and a column
18/// reference looks like
19/// `{"type": "column_ref", "table": "users", "column": "id"}`.
20///
21/// # Examples
22///
23/// ```
24/// use vlorql_core::schema::{Expression, DataType};
25///
26/// let lit = Expression::Literal {
27/// value: serde_json::json!(42),
28/// data_type: DataType::Int,
29/// };
30/// let col = Expression::ColumnRef {
31/// table: Some("users".to_owned()),
32/// column: "id".to_owned(),
33/// };
34/// assert_ne!(lit, col);
35/// ```
36#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
37#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
38pub enum Expression {
39 /// A literal value together with its SQL type.
40 Literal {
41 /// The literal value as a JSON-compatible scalar (string,
42 /// number, boolean, or `null`).
43 value: serde_json::Value,
44 /// The declared SQL type of the literal.
45 data_type: DataType,
46 },
47 /// A reference to a column, optionally qualified by a table.
48 ColumnRef {
49 /// Source-table qualifier (may be `None` for an unqualified
50 /// reference, in which case the validator resolves it
51 /// against the plan's `from` and joined tables).
52 table: Option<String>,
53 /// The column name as it appears in the schema.
54 column: String,
55 },
56 /// A scalar or aggregate function call.
57 FunctionCall {
58 /// Function name as emitted to the SQL backend (case is
59 /// preserved).
60 name: String,
61 /// Arguments passed positionally to the function.
62 args: Vec<Expression>,
63 /// When `true`, the call is rendered as `DISTINCT` (subject
64 /// to the dialect profile's `allow_distinct` setting).
65 distinct: bool,
66 },
67 /// A binary operation between two expressions.
68 BinaryOp {
69 /// Left-hand operand.
70 left: Box<Expression>,
71 /// The operator to apply.
72 op: BinaryOperator,
73 /// Right-hand operand.
74 right: Box<Expression>,
75 },
76 /// A literal `*` (asterisk) used inside aggregate function calls
77 /// such as `COUNT(*)` or `COUNT(DISTINCT *)`.
78 Star,
79 /// A scalar subquery expression.
80 SubQuery {
81 /// The inner query plan.
82 query: Box<QueryPlan>,
83 },
84}
85
86/// The target of an `IN` predicate: either a list of literal values
87/// or a subquery.
88#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
89#[serde(untagged)]
90pub enum InTarget {
91 /// A list of literal expressions.
92 Values(Vec<Expression>),
93 /// A subquery.
94 SubQuery(Box<QueryPlan>),
95}
96
97/// A boolean condition used by `WHERE`, `HAVING`, and join clauses.
98///
99/// Like [`Expression`], this is serialized with an internal `type`
100/// tag. The `In` and `Between` variants are the only way to express
101/// list membership and range checks; the bare comparison operators
102/// of the same name are reserved for the dedicated predicate shapes.
103///
104/// # Examples
105///
106/// ```
107/// use vlorql_core::schema::{Predicate, Expression, ComparisonOperator, DataType};
108///
109/// let pred = Predicate::Comparison {
110/// left: Expression::ColumnRef {
111/// table: Some("users".to_owned()),
112/// column: "age".to_owned(),
113/// },
114/// op: ComparisonOperator::Gte,
115/// right: Expression::Literal {
116/// value: serde_json::json!(18),
117/// data_type: DataType::Int,
118/// },
119/// };
120/// assert!(matches!(pred, Predicate::Comparison { .. }));
121/// ```
122#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
123#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
124pub enum Predicate {
125 /// A comparison between two expressions.
126 Comparison {
127 /// Left-hand operand.
128 left: Expression,
129 /// The comparison operator.
130 op: ComparisonOperator,
131 /// Right-hand operand.
132 right: Expression,
133 },
134 /// The conjunction of two predicates.
135 And {
136 /// Left sub-predicate.
137 left: Box<Predicate>,
138 /// Right sub-predicate.
139 right: Box<Predicate>,
140 },
141 /// The disjunction of two predicates.
142 Or {
143 /// Left sub-predicate.
144 left: Box<Predicate>,
145 /// Right sub-predicate.
146 right: Box<Predicate>,
147 },
148 /// The negation of a predicate.
149 Not {
150 /// The predicate to negate.
151 child: Box<Predicate>,
152 },
153 /// A value constrained to an inclusive range.
154 Between {
155 /// The value being tested.
156 expr: Expression,
157 /// Inclusive lower bound.
158 low: Expression,
159 /// Inclusive upper bound.
160 high: Expression,
161 },
162 /// A value constrained to a list of expressions or a subquery.
163 In {
164 /// The value being tested.
165 expr: Expression,
166 /// Allowed values or a subquery.
167 target: InTarget,
168 },
169 /// A string pattern match.
170 Like {
171 /// The value being tested.
172 expr: Expression,
173 /// Pattern string (using `LIKE`/`ILIKE` wildcards).
174 pattern: String,
175 },
176 /// A null check.
177 IsNull {
178 /// The value being tested.
179 expr: Expression,
180 },
181 /// Tests whether a subquery returns any rows.
182 Exists {
183 /// The subquery to check.
184 query: Box<QueryPlan>,
185 },
186}