1use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
2
3#[derive(Clone, Debug, PartialEq, Eq)]
5pub enum FieldType {
6 String,
7 Int64,
8 Float64,
9 Boolean,
10 Dictionary,
11}
12
13impl FieldType {
14 #[allow(clippy::should_implement_trait)]
15 pub fn from_str(s: &str) -> Option<Self> {
16 match s {
17 "string" => Some(FieldType::String),
18 "int64" => Some(FieldType::Int64),
19 "float64" => Some(FieldType::Float64),
20 "bool" | "boolean" => Some(FieldType::Boolean),
21 "dictionary" => Some(FieldType::Dictionary),
22 _ => None,
23 }
24 }
25}
26
27#[derive(Clone, Debug, Default)]
31pub struct ExecutionPlan {
32 pub field_map: HashMap<String, String>,
34 pub drop_fields: HashSet<String>,
36 pub field_types: HashMap<String, FieldType>,
38 pub dictionary_columns: HashSet<String>,
40 pub filter: Option<FilterPredicate>,
42 pub schema_order: Vec<String>,
46 pub auto_dict: bool,
49}
50
51impl ExecutionPlan {
52 pub fn new() -> Self {
53 Self::default()
54 }
55
56 pub fn rename(mut self, raw: impl Into<String>, output: impl Into<String>) -> Self {
58 self.field_map.insert(raw.into(), output.into());
59 self
60 }
61
62 pub fn drop(mut self, field: impl Into<String>) -> Self {
64 self.drop_fields.insert(field.into());
65 self
66 }
67
68 pub fn drop_many<I, S>(mut self, fields: I) -> Self
70 where
71 I: IntoIterator<Item = S>,
72 S: Into<String>,
73 {
74 self.drop_fields.extend(fields.into_iter().map(Into::into));
75 self
76 }
77
78 pub fn type_as(mut self, field: impl Into<String>, field_type: FieldType) -> Self {
80 self.field_types.insert(field.into(), field_type);
81 self
82 }
83
84 pub fn dictionary(mut self, field: impl Into<String>) -> Self {
86 self.dictionary_columns.insert(field.into());
87 self
88 }
89
90 pub fn filter_eq(mut self, field: impl Into<String>, value: impl Into<String>) -> Self {
92 self.filter = Some(FilterPredicate::Equal {
93 field: field.into(),
94 value: value.into(),
95 });
96 self
97 }
98
99 pub fn filter_ne(mut self, field: impl Into<String>, value: impl Into<String>) -> Self {
101 self.filter = Some(FilterPredicate::NotEqual {
102 field: field.into(),
103 value: value.into(),
104 });
105 self
106 }
107
108 pub fn filter_compare(
110 mut self,
111 field_a: impl Into<String>,
112 op: CompareOp,
113 field_b: impl Into<String>,
114 ) -> Self {
115 self.filter = Some(FilterPredicate::Compare {
116 field_a: field_a.into(),
117 op,
118 field_b: field_b.into(),
119 });
120 self
121 }
122
123 pub fn schema_order<I, S>(mut self, order: I) -> Self
126 where
127 I: IntoIterator<Item = S>,
128 S: Into<String>,
129 {
130 self.schema_order = order.into_iter().map(Into::into).collect();
131 self
132 }
133
134 pub fn with_auto_dict(mut self, yes: bool) -> Self {
137 self.auto_dict = yes;
138 self
139 }
140
141 pub fn column_type(&self, name: &str) -> FieldType {
143 if let Some(ft) = self.field_types.get(name) {
144 return ft.clone();
145 }
146 if self.dictionary_columns.contains(name) {
147 return FieldType::Dictionary;
148 }
149 FieldType::String
150 }
151
152 pub fn resolve_field<'a>(&'a self, raw: &'a str) -> Option<&'a str> {
158 let resolved = self.field_map.get(raw).map_or(raw, |s| s.as_str());
159 if self.drop_fields.contains(resolved) {
160 return None;
161 }
162 Some(resolved)
163 }
164}
165
166#[derive(Clone, Copy, Debug, PartialEq, Eq)]
168pub enum CompareOp {
169 Gt,
170 Lt,
171 Ge,
172 Le,
173 Eq,
174 Ne,
175}
176
177impl CompareOp {
178 #[allow(clippy::should_implement_trait)]
179 pub fn from_str(s: &str) -> Option<Self> {
180 match s {
181 ">" | "gt" => Some(CompareOp::Gt),
182 "<" | "lt" => Some(CompareOp::Lt),
183 ">=" | "ge" => Some(CompareOp::Ge),
184 "<=" | "le" => Some(CompareOp::Le),
185 "==" | "eq" => Some(CompareOp::Eq),
186 "!=" | "ne" => Some(CompareOp::Ne),
187 _ => None,
188 }
189 }
190}
191
192#[derive(Clone, Debug, PartialEq)]
194pub enum FilterPredicate {
195 NotEqual { field: String, value: String },
197 Equal { field: String, value: String },
199 Compare {
201 field_a: String,
202 op: CompareOp,
203 field_b: String,
204 },
205}
206
207impl FilterPredicate {
208 pub(crate) fn check(
213 &self,
214 columns: &HashMap<String, crate::columnar::ColumnBuilder>,
215 row_index: usize,
216 plan: &ExecutionPlan,
217 ) -> bool {
218 let (field, expected) = match self {
219 FilterPredicate::NotEqual { field, value } => (field, value),
220 FilterPredicate::Equal { field, value } => (field, value),
221 FilterPredicate::Compare { .. } => return true,
222 };
223 let resolved = plan.field_map.get(field).map_or(field, |s| s);
225
226 let actual = columns
227 .get(resolved)
228 .and_then(|b| b.get_filter_value(row_index));
229 let actual = actual.as_deref();
230 match self {
231 FilterPredicate::NotEqual { .. } => actual != Some(expected),
232 FilterPredicate::Equal { .. } => actual == Some(expected),
233 FilterPredicate::Compare { .. } => true,
234 }
235 }
236}