polars_plan/frame/
opt_state.rs

1use bitflags::bitflags;
2
3bitflags! {
4#[derive(Copy, Clone, Debug)]
5    /// Allowed optimizations.
6    pub struct OptFlags: u32 {
7        /// Only read columns that are used later in the query.
8        const PROJECTION_PUSHDOWN = 1;
9        /// Apply predicates/filters as early as possible.
10        const PREDICATE_PUSHDOWN = 1 << 2;
11        /// Cluster sequential `with_columns` calls to independent calls.
12        const CLUSTER_WITH_COLUMNS = 1 << 3;
13        /// Run many type coercion optimization rules until fixed point.
14        const TYPE_COERCION = 1 << 4;
15        /// Run many expression optimization rules until fixed point.
16        const SIMPLIFY_EXPR = 1 << 5;
17        /// Cache file reads.
18        const FILE_CACHING = 1 << 6;
19        /// Pushdown slices/limits.
20        const SLICE_PUSHDOWN = 1 << 7;
21        /// Run common-subplan-elimination. This elides duplicate plans and caches their
22        /// outputs.
23        const COMM_SUBPLAN_ELIM = 1 << 8;
24        /// Run common-subexpression-elimination. This elides duplicate expressions and caches their
25        /// outputs.
26        const COMM_SUBEXPR_ELIM = 1 << 9;
27        /// Run nodes that are capably of doing so on the streaming engine.
28        const STREAMING = 1 << 10;
29        const NEW_STREAMING = 1 << 11;
30        /// Run every node eagerly. This turns off multi-node optimizations.
31        const EAGER = 1 << 12;
32        /// Try to estimate the number of rows so that joins can determine which side to keep in memory.
33        const ROW_ESTIMATE = 1 << 13;
34        /// Replace simple projections with a faster inlined projection that skips the expression engine.
35        const FAST_PROJECTION = 1 << 14;
36        /// Collapse slower joins with filters into faster joins.
37        const COLLAPSE_JOINS = 1 << 15;
38        /// Check if operations are order dependent and unset maintaining_order if
39        /// the order would not be observed.
40        const CHECK_ORDER_OBSERVE = 1 << 16;
41        /// Do type checking of the IR.
42        const TYPE_CHECK = 1 << 17;
43    }
44}
45
46impl OptFlags {
47    pub fn schema_only() -> Self {
48        Self::TYPE_COERCION | Self::TYPE_CHECK
49    }
50
51    pub fn eager(&self) -> bool {
52        self.contains(OptFlags::EAGER)
53    }
54
55    pub fn cluster_with_columns(&self) -> bool {
56        self.contains(OptFlags::CLUSTER_WITH_COLUMNS)
57    }
58
59    pub fn collapse_joins(&self) -> bool {
60        self.contains(OptFlags::COLLAPSE_JOINS)
61    }
62
63    pub fn predicate_pushdown(&self) -> bool {
64        self.contains(OptFlags::PREDICATE_PUSHDOWN)
65    }
66
67    pub fn projection_pushdown(&self) -> bool {
68        self.contains(OptFlags::PROJECTION_PUSHDOWN)
69    }
70    pub fn simplify_expr(&self) -> bool {
71        self.contains(OptFlags::SIMPLIFY_EXPR)
72    }
73    pub fn slice_pushdown(&self) -> bool {
74        self.contains(OptFlags::SLICE_PUSHDOWN)
75    }
76    pub fn streaming(&self) -> bool {
77        self.contains(OptFlags::STREAMING)
78    }
79    pub fn new_streaming(&self) -> bool {
80        self.contains(OptFlags::NEW_STREAMING)
81    }
82    pub fn fast_projection(&self) -> bool {
83        self.contains(OptFlags::FAST_PROJECTION)
84    }
85}
86
87impl Default for OptFlags {
88    fn default() -> Self {
89        Self::from_bits_truncate(u32::MAX) & !Self::NEW_STREAMING & !Self::STREAMING & !Self::EAGER
90            // will be toggled by a scan operation such as csv scan or parquet scan
91            & !Self::FILE_CACHING
92    }
93}
94
95/// AllowedOptimizations
96pub type AllowedOptimizations = OptFlags;