risingwave_common/session_config/
query_mode.rs1use std::fmt::Formatter;
18use std::str::FromStr;
19
20#[derive(Copy, Default, Debug, Clone, PartialEq, Eq)]
21pub enum QueryMode {
22    #[default]
23    Auto,
24
25    Local,
26
27    Distributed,
28}
29
30impl FromStr for QueryMode {
31    type Err = &'static str;
32
33    fn from_str(s: &str) -> Result<Self, Self::Err> {
34        if s.eq_ignore_ascii_case("local") {
35            Ok(Self::Local)
36        } else if s.eq_ignore_ascii_case("distributed") {
37            Ok(Self::Distributed)
38        } else if s.eq_ignore_ascii_case("auto") {
39            Ok(Self::Auto)
40        } else {
41            Err("expect one of [local, distributed, auto]")
42        }
43    }
44}
45
46impl std::fmt::Display for QueryMode {
47    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
48        match self {
49            Self::Auto => write!(f, "auto"),
50            Self::Local => write!(f, "local"),
51            Self::Distributed => write!(f, "distributed"),
52        }
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[test]
61    fn parse_query_mode() {
62        assert_eq!(QueryMode::from_str("auto").unwrap(), QueryMode::Auto);
63        assert_eq!(QueryMode::from_str("Auto").unwrap(), QueryMode::Auto);
64        assert_eq!(QueryMode::from_str("local").unwrap(), QueryMode::Local);
65        assert_eq!(QueryMode::from_str("Local").unwrap(), QueryMode::Local);
66        assert_eq!(
67            QueryMode::from_str("distributed").unwrap(),
68            QueryMode::Distributed
69        );
70        assert_eq!(
71            QueryMode::from_str("diStributed").unwrap(),
72            QueryMode::Distributed
73        );
74        assert!(QueryMode::from_str("ab").is_err());
75    }
76}