use super::query_plan::{OrderByTerm, QueryPlan};
use super::types::{BinaryOperator, ComparisonOperator, DataType};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
pub enum Expression {
Literal {
value: serde_json::Value,
data_type: DataType,
},
ColumnRef {
table: Option<String>,
column: String,
},
FunctionCall {
name: String,
args: Vec<Expression>,
distinct: bool,
},
BinaryOp {
left: Box<Expression>,
op: BinaryOperator,
right: Box<Expression>,
},
Star,
SubQuery {
query: Box<QueryPlan>,
},
Case {
operand: Option<Box<Expression>>,
when_thens: Vec<WhenThen>,
else_result: Option<Box<Expression>>,
},
WindowFunction {
name: String,
args: Vec<Expression>,
distinct: bool,
over: WindowSpec,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct WhenThen {
pub when: Expression,
pub then: Expression,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct WindowSpec {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub partition_by: Option<Vec<Expression>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub order_by: Option<Vec<OrderByTerm>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub frame: Option<WindowFrame>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct WindowFrame {
pub kind: WindowFrameKind,
pub start: WindowFrameBound,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub end: Option<WindowFrameBound>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum WindowFrameKind {
Rows,
Range,
Groups,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum WindowFrameBound {
UnboundedPreceding,
Preceding(Box<Expression>),
CurrentRow,
Following(Box<Expression>),
UnboundedFollowing,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
pub enum InTarget {
Values(Vec<Expression>),
SubQuery(Box<QueryPlan>),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case", deny_unknown_fields)]
pub enum Predicate {
Comparison {
left: Expression,
op: ComparisonOperator,
right: Expression,
},
And {
left: Box<Predicate>,
right: Box<Predicate>,
},
Or {
left: Box<Predicate>,
right: Box<Predicate>,
},
Not {
child: Box<Predicate>,
},
Between {
expr: Expression,
low: Expression,
high: Expression,
},
In {
expr: Expression,
target: InTarget,
},
Like {
expr: Expression,
pattern: String,
},
IsNull {
expr: Expression,
},
Exists {
query: Box<QueryPlan>,
},
}