Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/plans/aexpr/hash.rs
8424 views
1
use std::hash::{Hash, Hasher};
2
3
use polars_utils::arena::{Arena, Node};
4
5
use crate::prelude::AExpr;
6
7
impl Hash for AExpr {
8
// This hashes the variant, not the whole expression
9
// IMPORTANT: This is also used for equality in some cases with blake3.
10
// Make sure that all attributes that are important for equality are hashed. Nodes don't have
11
// to be hashed.
12
fn hash<H: Hasher>(&self, state: &mut H) {
13
std::mem::discriminant(self).hash(state);
14
15
match self {
16
AExpr::Column(name) => name.hash(state),
17
AExpr::StructField(name) => name.hash(state),
18
AExpr::Literal(lv) => lv.hash(state),
19
AExpr::Function {
20
options,
21
function,
22
input: _,
23
} => {
24
options.hash(state);
25
function.hash(state)
26
},
27
AExpr::AnonymousFunction {
28
options,
29
fmt_str,
30
function,
31
input: _,
32
} => {
33
fmt_str.hash(state);
34
options.hash(state);
35
function.hash(state);
36
},
37
AExpr::Agg(agg) => agg.hash(state),
38
AExpr::SortBy { sort_options, .. } => sort_options.hash(state),
39
AExpr::Cast {
40
options,
41
dtype,
42
expr: _,
43
} => {
44
options.hash(state);
45
dtype.hash(state);
46
},
47
#[cfg(feature = "dynamic_group_by")]
48
AExpr::Rolling {
49
function: _,
50
index_column: _,
51
period,
52
offset,
53
closed_window,
54
} => {
55
period.hash(state);
56
offset.hash(state);
57
closed_window.hash(state);
58
},
59
AExpr::Over {
60
mapping,
61
order_by,
62
function: _,
63
partition_by: _,
64
} => {
65
mapping.hash(state);
66
if let Some(o) = order_by {
67
o.1.hash(state);
68
}
69
},
70
AExpr::BinaryExpr {
71
op,
72
left: _,
73
right: _,
74
} => op.hash(state),
75
AExpr::Element => {},
76
AExpr::Explode { expr: _, options } => options.hash(state),
77
AExpr::Sort { expr: _, options } => options.hash(state),
78
AExpr::Gather {
79
expr: _,
80
idx: _,
81
returns_scalar,
82
null_on_oob: _,
83
} => returns_scalar.hash(state),
84
AExpr::Filter { input: _, by: _ } => {},
85
AExpr::Ternary {
86
predicate: _,
87
truthy: _,
88
falsy: _,
89
} => {},
90
AExpr::AnonymousAgg {
91
input: _,
92
fmt_str,
93
function,
94
} => {
95
function.hash(state);
96
fmt_str.hash(state);
97
},
98
AExpr::Eval {
99
expr: _,
100
evaluation: _,
101
variant,
102
} => variant.hash(state),
103
AExpr::StructEval {
104
expr: _,
105
evaluation: _,
106
} => {},
107
AExpr::Slice {
108
input: _,
109
offset: _,
110
length: _,
111
} => {},
112
AExpr::Len => {},
113
}
114
}
115
}
116
117
pub(crate) fn traverse_and_hash_aexpr<H: Hasher>(
118
node: Node,
119
expr_arena: &Arena<AExpr>,
120
state: &mut H,
121
) {
122
let mut scratch = vec![node];
123
124
while let Some(node) = scratch.pop() {
125
let ae = expr_arena.get(node);
126
ae.hash(state);
127
ae.children_rev(&mut scratch);
128
}
129
}
130
131