feldera_ir/
hir.rs

1use std::collections::HashMap;
2
3use serde::Deserialize;
4use serde_json::Value;
5
6/// The Calcite plan representation of a dataflow graph.
7#[derive(Debug, Deserialize)]
8pub struct CalcitePlan {
9    pub rels: Vec<Rel>,
10}
11
12#[derive(Debug, Deserialize)]
13pub struct Rel {
14    pub id: usize,
15    #[serde(default)]
16    pub inputs: Vec<usize>,
17    #[serde(rename = "relOp")]
18    pub rel_op: String,
19
20    /// This is a vector where the elements concatenated form a fully qualified table name.
21    ///
22    /// e.g., usually is of the form `[$namespace, $table] / [schema, table]`
23    #[serde(default)]
24    pub table: Option<Vec<String>>,
25
26    #[serde(default)]
27    pub condition: Option<Condition>,
28
29    #[serde(default)]
30    #[serde(rename = "joinType")]
31    pub join_type: Option<String>,
32
33    #[serde(default)]
34    pub exprs: Option<Vec<Operand>>,
35
36    #[serde(default)]
37    pub fields: Option<Vec<String>>,
38
39    #[serde(default)]
40    pub all: Option<bool>,
41
42    #[serde(default)]
43    pub aggs: Option<Vec<Value>>,
44
45    #[serde(default)]
46    pub group: Option<Vec<usize>>,
47
48    #[serde(flatten)]
49    pub extra: HashMap<String, Value>,
50}
51
52#[derive(Debug, Deserialize)]
53pub struct Condition {
54    pub op: Option<Op>,
55    pub operands: Option<Vec<Operand>>,
56    #[serde(default)]
57    pub literal: bool,
58
59    #[serde(flatten)]
60    pub extra: HashMap<String, Value>,
61}
62
63#[derive(Debug, Deserialize)]
64pub struct Op {
65    pub kind: String,
66    pub name: String,
67    pub syntax: String,
68
69    #[serde(flatten)]
70    pub extra: HashMap<String, Value>,
71}
72
73#[derive(Debug, Deserialize)]
74pub struct Operand {
75    pub input: Option<usize>,
76    pub name: Option<String>,
77
78    #[serde(flatten)]
79    pub extra: HashMap<String, Value>,
80}
81
82#[derive(Debug, Clone, Deserialize)]
83#[serde(untagged)]
84pub enum CalciteId {
85    Partial {
86        partial: usize,
87    },
88    Final {
89        #[serde(rename = "final")]
90        final_: usize,
91    },
92    And {
93        and: Vec<CalciteId>,
94    },
95    Seq {
96        seq: Vec<CalciteId>,
97    },
98    Null,
99}
100
101impl CalciteId {
102    #[allow(unused)]
103    fn contains(&self, id: usize) -> bool {
104        match self {
105            CalciteId::Partial { partial } => *partial == id,
106            CalciteId::Final { final_ } => *final_ == id,
107            CalciteId::And { and } => and.iter().any(|cid| cid.contains(id)),
108            CalciteId::Seq { seq } => seq.iter().any(|cid| cid.contains(id)),
109            CalciteId::Null => false,
110        }
111    }
112}
113
114impl From<CalciteId> for Vec<usize> {
115    fn from(val: CalciteId) -> Self {
116        match val {
117            CalciteId::Partial { partial } => vec![partial],
118            CalciteId::Final { final_ } => vec![final_],
119            CalciteId::And { and } => and.into_iter().flat_map(Into::<Vec<usize>>::into).collect(),
120            CalciteId::Seq { seq } => seq.into_iter().flat_map(Into::<Vec<usize>>::into).collect(),
121            CalciteId::Null => vec![],
122        }
123    }
124}