Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/crates/polars-plan/src/plans/functions/hint.rs
7887 views
1
use std::fmt;
2
use std::sync::Arc;
3
4
use polars_core::prelude::PlHashSet;
5
use polars_utils::pl_str::PlSmallStr;
6
7
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
9
#[derive(Debug, Clone, Hash)]
10
pub struct Sorted {
11
pub column: PlSmallStr,
12
/// None -> either way / unsure
13
/// Some(false) -> ascending
14
/// Some(true) -> descending
15
pub descending: Option<bool>,
16
/// None -> either way / unsure
17
/// Some(false) -> nulls (if any) at start
18
/// Some(true) -> nulls (if any) at end
19
pub nulls_last: Option<bool>,
20
}
21
22
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23
#[cfg_attr(feature = "dsl-schema", derive(schemars::JsonSchema))]
24
#[derive(Clone, Hash, strum_macros::IntoStaticStr)]
25
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
26
pub enum HintIR {
27
Sorted(Arc<[Sorted]>),
28
}
29
30
impl HintIR {
31
pub fn project(&self, projected_names: &PlHashSet<PlSmallStr>) -> Option<HintIR> {
32
match self {
33
Self::Sorted(s) => {
34
let num_matches = s
35
.iter()
36
.filter(|i| projected_names.contains(&i.column))
37
.count();
38
39
if num_matches == s.len() {
40
return Some(Self::Sorted(s.clone()));
41
} else if num_matches == 0 {
42
return None;
43
}
44
45
let mut sorted = Vec::with_capacity(num_matches);
46
sorted.extend(
47
s.iter()
48
.filter(|i| projected_names.contains(&i.column))
49
.cloned(),
50
);
51
Some(Self::Sorted(sorted.into()))
52
},
53
}
54
}
55
}
56
57
impl fmt::Display for Sorted {
58
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59
let descending = match self.descending {
60
None => "?",
61
Some(false) => "false",
62
Some(true) => "true",
63
};
64
let nulls_last = match self.nulls_last {
65
None => "?",
66
Some(false) => "false",
67
Some(true) => "true",
68
};
69
70
write!(
71
f,
72
"'{}': {{ descending: {descending}, nulls_last: {nulls_last} }}",
73
self.column,
74
)
75
}
76
}
77
78
impl fmt::Debug for HintIR {
79
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80
write!(f, "{self}")
81
}
82
}
83
84
impl fmt::Display for HintIR {
85
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86
match self {
87
HintIR::Sorted(s) => {
88
write!(f, "sorted(")?;
89
if let Some(fst) = s.first() {
90
fst.fmt(f)?;
91
for si in &s[1..] {
92
f.write_str(", ")?;
93
si.fmt(f)?;
94
}
95
}
96
write!(f, ")")
97
},
98
}
99
}
100
}
101
102