risingwave_common/session_config/
over_window.rs1use std::str::FromStr;
16
17use enum_as_inner::EnumAsInner;
18use parse_display::Display;
19use risingwave_pb::stream_plan::PbOverWindowCachePolicy;
20
21#[derive(Copy, Default, Debug, Clone, PartialEq, Eq, Display, EnumAsInner)]
22#[display(style = "snake_case")]
23pub enum OverWindowCachePolicy {
24 #[default]
26 Full,
27 Recent,
29 RecentFirstN,
31 RecentLastN,
33}
34
35impl FromStr for OverWindowCachePolicy {
36 type Err = &'static str;
37
38 fn from_str(s: &str) -> Result<Self, Self::Err> {
39 let s = s.to_ascii_lowercase().replace('-', "_");
40 match s.as_str() {
41 "full" => Ok(Self::Full),
42 "recent" => Ok(Self::Recent),
43 "recent_first_n" => Ok(Self::RecentFirstN),
44 "recent_last_n" => Ok(Self::RecentLastN),
45 _ => Err("expect one of [full, recent, recent_first_n, recent_last_n]"),
46 }
47 }
48}
49
50impl OverWindowCachePolicy {
51 pub fn to_protobuf(self) -> PbOverWindowCachePolicy {
52 match self {
53 Self::Full => PbOverWindowCachePolicy::Full,
54 Self::Recent => PbOverWindowCachePolicy::Recent,
55 Self::RecentFirstN => PbOverWindowCachePolicy::RecentFirstN,
56 Self::RecentLastN => PbOverWindowCachePolicy::RecentLastN,
57 }
58 }
59
60 pub fn from_protobuf(pb: PbOverWindowCachePolicy) -> Self {
61 match pb {
62 PbOverWindowCachePolicy::Unspecified => Self::default(),
63 PbOverWindowCachePolicy::Full => Self::Full,
64 PbOverWindowCachePolicy::Recent => Self::Recent,
65 PbOverWindowCachePolicy::RecentFirstN => Self::RecentFirstN,
66 PbOverWindowCachePolicy::RecentLastN => Self::RecentLastN,
67 }
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 #[test]
76 fn parse_over_window_cache_policy() {
77 assert_eq!(
78 OverWindowCachePolicy::from_str("full").unwrap(),
79 OverWindowCachePolicy::Full
80 );
81 assert_eq!(
82 OverWindowCachePolicy::from_str("recent").unwrap(),
83 OverWindowCachePolicy::Recent
84 );
85 assert_eq!(
86 OverWindowCachePolicy::from_str("RECENT").unwrap(),
87 OverWindowCachePolicy::Recent
88 );
89 assert_eq!(
90 OverWindowCachePolicy::from_str("recent_first_n").unwrap(),
91 OverWindowCachePolicy::RecentFirstN
92 );
93 assert_eq!(
94 OverWindowCachePolicy::from_str("recent_last_n").unwrap(),
95 OverWindowCachePolicy::RecentLastN
96 );
97 assert_eq!(
98 OverWindowCachePolicy::from_str("recent-last-n").unwrap(),
99 OverWindowCachePolicy::RecentLastN
100 );
101 assert_eq!(
102 OverWindowCachePolicy::from_str("recent_last_N").unwrap(),
103 OverWindowCachePolicy::RecentLastN
104 );
105 assert!(OverWindowCachePolicy::from_str("foo").is_err());
106 }
107}