use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::types::*;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "op")]
pub enum LogicalOp {
NodeScan {
label: Label,
as_: String,
#[serde(skip_serializing_if = "Option::is_none")]
props: Option<Properties>,
},
IndexScan {
label: Label,
as_: String,
index: String,
value: Value,
},
Filter {
pred: Predicate,
input: Box<LogicalOp>,
},
Expand {
edge: EdgePattern,
to_as: String,
from: Box<LogicalOp>,
},
Join {
left: Box<LogicalOp>,
right: Box<LogicalOp>,
on: Vec<String>, },
Project {
cols: Vec<String>,
input: Box<LogicalOp>,
},
Group {
keys: Vec<String>,
aggregations: Vec<Aggregation>,
input: Box<LogicalOp>,
},
Sort {
keys: Vec<SortKey>,
input: Box<LogicalOp>,
},
Limit {
count: usize,
input: Box<LogicalOp>,
},
Distinct {
input: Box<LogicalOp>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EdgePattern {
pub label: Label,
pub dir: Direction,
#[serde(skip_serializing_if = "Option::is_none")]
pub props: Option<Properties>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Direction {
#[serde(rename = "out")]
Out,
#[serde(rename = "in")]
In,
#[serde(rename = "both")]
Both,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Predicate {
Eq { eq: [Expr; 2] },
Ne { ne: [Expr; 2] },
Lt { lt: [Expr; 2] },
Le { le: [Expr; 2] },
Gt { gt: [Expr; 2] },
Ge { ge: [Expr; 2] },
And { and: Vec<Predicate> },
Or { or: Vec<Predicate> },
Not { not: Box<Predicate> },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Expr {
Var(String),
Const(Value),
Fn { fn_: String, args: Vec<Expr> },
}
impl std::fmt::Display for Expr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expr::Var(v) => write!(f, "{}", v),
Expr::Const(val) => write!(f, "{:?}", val),
Expr::Fn { fn_, args } => {
write!(f, "{}({})", fn_, args.len())
}
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Aggregation {
pub fn_: String,
pub args: Vec<String>,
pub as_: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SortKey {
pub expr: Expr,
pub asc: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlanIR {
pub plan: LogicalOp,
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<usize>,
}
#[derive(Debug, Clone)]
pub struct Row {
pub values: HashMap<String, Value>,
}
pub type RowStream = Vec<Row>;