Skip to main content

clickhouse_native_client/
protocol.rs

1/// Types of packets received from server
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3#[repr(u64)]
4pub enum ServerCode {
5    /// Server handshake response containing name, version, and revision.
6    Hello = 0,
7    /// Block of data, may be compressed.
8    Data = 1,
9    /// Exception that occurred during query execution.
10    Exception = 2,
11    /// Query execution progress: rows and bytes read.
12    Progress = 3,
13    /// Response to a client Ping request.
14    Pong = 4,
15    /// Signals that all packets for the current operation have been sent.
16    EndOfStream = 5,
17    /// Profiling data for query execution.
18    ProfileInfo = 6,
19    /// Block of totals, may be compressed.
20    Totals = 7,
21    /// Block of extremes (mins and maxs), may be compressed.
22    Extremes = 8,
23    /// Response to a TableStatus request.
24    TablesStatusResponse = 9,
25    /// Query execution log (always uncompressed).
26    Log = 10,
27    /// Columns description for default values calculation.
28    TableColumns = 11,
29    /// List of unique part UUIDs.
30    PartUUIDs = 12,
31    /// Request for the next distributed read task.
32    ReadTaskRequest = 13,
33    /// Profile events from the server (always uncompressed).
34    ProfileEvents = 14,
35}
36
37impl TryFrom<u64> for ServerCode {
38    type Error = crate::Error;
39
40    fn try_from(value: u64) -> Result<Self, Self::Error> {
41        match value {
42            0 => Ok(ServerCode::Hello),
43            1 => Ok(ServerCode::Data),
44            2 => Ok(ServerCode::Exception),
45            3 => Ok(ServerCode::Progress),
46            4 => Ok(ServerCode::Pong),
47            5 => Ok(ServerCode::EndOfStream),
48            6 => Ok(ServerCode::ProfileInfo),
49            7 => Ok(ServerCode::Totals),
50            8 => Ok(ServerCode::Extremes),
51            9 => Ok(ServerCode::TablesStatusResponse),
52            10 => Ok(ServerCode::Log),
53            11 => Ok(ServerCode::TableColumns),
54            12 => Ok(ServerCode::PartUUIDs),
55            13 => Ok(ServerCode::ReadTaskRequest),
56            14 => Ok(ServerCode::ProfileEvents),
57            _ => Err(crate::Error::Protocol(format!(
58                "Unknown server code: {}",
59                value
60            ))),
61        }
62    }
63}
64
65/// Types of packets sent by client
66#[derive(Debug, Clone, Copy, PartialEq, Eq)]
67#[repr(u64)]
68pub enum ClientCode {
69    /// Client handshake containing name, version, and default database.
70    Hello = 0,
71    /// Query packet with query id, settings, stage, compression, and query
72    /// text.
73    Query = 1,
74    /// Data block (e.g. INSERT data), may be compressed.
75    Data = 2,
76    /// Cancel the currently running query.
77    Cancel = 3,
78    /// Ping the server to check the connection is alive.
79    Ping = 4,
80}
81
82/// Should we compress Blocks of data
83#[derive(Debug, Clone, Copy, PartialEq, Eq)]
84#[repr(u64)]
85pub enum CompressionState {
86    /// Block compression is disabled.
87    Disable = 0,
88    /// Block compression is enabled.
89    Enable = 1,
90}
91
92/// Query processing stage
93#[derive(Debug, Clone, Copy, PartialEq, Eq)]
94#[repr(u64)]
95pub enum Stage {
96    /// Fully process the query and return the final result.
97    Complete = 2,
98}
99
100/// Methods of block compression
101#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
102pub enum CompressionMethod {
103    /// No compression (default).
104    #[default]
105    None = -1,
106    /// LZ4 compression -- fast with good compression ratio.
107    Lz4 = 1,
108    /// ZSTD compression -- better ratio but slower than LZ4.
109    Zstd = 2,
110}
111
112#[cfg(test)]
113#[cfg_attr(coverage_nightly, coverage(off))]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn test_server_code_conversion() {
119        assert_eq!(ServerCode::try_from(0).unwrap(), ServerCode::Hello);
120        assert_eq!(ServerCode::try_from(1).unwrap(), ServerCode::Data);
121        assert_eq!(
122            ServerCode::try_from(14).unwrap(),
123            ServerCode::ProfileEvents
124        );
125        assert!(ServerCode::try_from(99).is_err());
126    }
127
128    #[test]
129    fn test_compression_method_default() {
130        assert_eq!(CompressionMethod::default(), CompressionMethod::None);
131    }
132}