Skip to main content

rypipe_core/
plan.rs

1use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet};
2
3/// The storage type for a column.
4#[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/// A compiled execution plan that controls field renaming, dropping,
28/// type assignment, dictionary encoding, row filtering, and column ordering.
29/// Default (empty) is a no-op.
30#[derive(Clone, Debug, Default)]
31pub struct ExecutionPlan {
32    /// Map from raw field name to output column name.
33    pub field_map: HashMap<String, String>,
34    /// Set of raw field names to drop entirely.
35    pub drop_fields: HashSet<String>,
36    /// Explicit type overrides per output column name.
37    pub field_types: HashMap<String, FieldType>,
38    /// Set of output column names to dict-encode.
39    pub dictionary_columns: HashSet<String>,
40    /// Optional row filter predicate.
41    pub filter: Option<FilterPredicate>,
42    /// Desired output column order (names in order).  Columns not listed here
43    /// appear after all listed columns in first-appearance order.  If empty,
44    /// first-appearance order is used.
45    pub schema_order: Vec<String>,
46    /// When true, string columns with low cardinality are automatically
47    /// upgraded to dictionary encoding during parse.
48    pub auto_dict: bool,
49}
50
51impl ExecutionPlan {
52    pub fn new() -> Self {
53        Self::default()
54    }
55
56    /// Rename a raw field to an output column name.
57    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    /// Drop a single raw/output field.
63    pub fn drop(mut self, field: impl Into<String>) -> Self {
64        self.drop_fields.insert(field.into());
65        self
66    }
67
68    /// Drop many raw/output fields.
69    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    /// Set the storage type for an output column.
79    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    /// Dict-encode an output column.
85    pub fn dictionary(mut self, field: impl Into<String>) -> Self {
86        self.dictionary_columns.insert(field.into());
87        self
88    }
89
90    /// Keep only rows where `field == value` (string comparison, per-row).
91    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    /// Keep only rows where `field != value` (string comparison, per-row).
100    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    /// Keep only rows where `field_a op field_b` (post-reduce via Arrow compute).
109    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    /// Set the output column order. Columns not listed appear after the listed
124    /// ones in first-appearance order.
125    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    /// Enable or disable automatic dictionary upgrade for low-cardinality
135    /// string columns.
136    pub fn with_auto_dict(mut self, yes: bool) -> Self {
137        self.auto_dict = yes;
138        self
139    }
140
141    /// Determine the storage type for an output column name.
142    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    /// Resolve a raw field name to its output column name.
153    /// Returns `None` if the field should be dropped.
154    ///
155    /// Application order: rename first, then drop, matching left-to-right
156    /// pipeline semantics.
157    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/// Comparison operator for column-to-column filters (evaluated post-reduce).
167#[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/// A filter predicate evaluated per-row during parsing.
193#[derive(Clone, Debug, PartialEq)]
194pub enum FilterPredicate {
195    /// Keep row if `field_value != value` (string comparison, per-row).
196    NotEqual { field: String, value: String },
197    /// Keep row if `field_value == value` (string comparison, per-row).
198    Equal { field: String, value: String },
199    /// Column-to-column comparison evaluated post-reduce via Arrow compute.
200    Compare {
201        field_a: String,
202        op: CompareOp,
203        field_b: String,
204    },
205}
206
207impl FilterPredicate {
208    /// Check whether a partial row passes the per-row part of the filter.
209    /// `columns` contains all builders; `row_index` is the current row number.
210    /// Returns true to keep the row.  Compare filters always return true here
211    /// because they are evaluated after all rows have been assembled.
212    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        // Resolve the filter field name: if renamed, use the new name.
224        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}