databend_client/
response.rs

1// Copyright 2021 Datafuse Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::error_code::ErrorCode;
16use crate::session::SessionState;
17use serde::{Deserialize, Serialize};
18
19#[derive(Deserialize, Debug, Default)]
20pub struct QueryStats {
21    #[serde(flatten)]
22    pub progresses: Progresses,
23    pub running_time_ms: f64,
24}
25
26#[derive(Deserialize, Debug, Default)]
27pub struct Progresses {
28    pub scan_progress: ProgressValues,
29    pub write_progress: ProgressValues,
30    pub result_progress: ProgressValues,
31    // make it optional for backward compatibility
32    pub total_scan: Option<ProgressValues>,
33    #[serde(default)]
34    pub spill_progress: SpillProgress,
35}
36
37impl Progresses {
38    pub fn has_progress(&self) -> bool {
39        self.scan_progress.bytes > 0
40            || self.scan_progress.rows > 0
41            || self.write_progress.bytes > 0
42            || self.write_progress.rows > 0
43            || self.result_progress.bytes > 0
44            || self.result_progress.rows > 0
45            || self
46                .total_scan
47                .as_ref()
48                .is_some_and(|v| v.bytes > 0 || v.rows > 0)
49    }
50}
51
52#[derive(Debug, Deserialize, Default)]
53pub struct ProgressValues {
54    pub rows: usize,
55    pub bytes: usize,
56}
57
58#[derive(Debug, Clone, Deserialize, Serialize, Default)]
59pub struct SpillProgress {
60    pub file_nums: usize,
61    pub bytes: usize,
62}
63
64#[derive(Serialize, Deserialize, Debug, Clone)]
65pub struct SchemaField {
66    pub name: String,
67    #[serde(rename = "type")]
68    pub data_type: String,
69}
70
71#[derive(Deserialize, Debug)]
72pub struct QueryResponse {
73    pub id: String,
74    pub node_id: Option<String>,
75    pub session_id: Option<String>,
76    pub session: Option<SessionState>,
77    pub schema: Vec<SchemaField>,
78    pub data: Vec<Vec<Option<String>>>,
79    pub state: String,
80    pub error: Option<ErrorCode>,
81    // make it optional for backward compatibility
82    pub warnings: Option<Vec<String>>,
83    pub stats: QueryStats,
84    // pub affect: Option<QueryAffect>,
85    pub stats_uri: Option<String>,
86    pub final_uri: Option<String>,
87    pub next_uri: Option<String>,
88    pub kill_uri: Option<String>,
89}
90
91#[derive(Deserialize, Debug)]
92pub struct LoadResponse {
93    pub id: String,
94    pub stats: ProgressValues,
95}
96
97#[cfg(test)]
98mod test {
99    use std::collections::BTreeMap;
100
101    use super::*;
102
103    #[test]
104    fn deserialize_session_config() {
105        let session_json = r#"{"database":"default","settings":{}}"#;
106        let session_config: SessionState = serde_json::from_str(session_json).unwrap();
107        assert_eq!(session_config.database, Some("default".to_string()));
108        assert_eq!(session_config.settings, Some(BTreeMap::default()));
109        assert_eq!(session_config.role, None);
110        assert_eq!(session_config.secondary_roles, None);
111
112        let session_json = r#"{"database":"default","settings":{},"role": "role1", "secondary_roles": [], "unknown_field": 1}"#;
113        let session_config: SessionState = serde_json::from_str(session_json).unwrap();
114        assert_eq!(session_config.database, Some("default".to_string()));
115        assert_eq!(session_config.settings, Some(BTreeMap::default()));
116        assert_eq!(session_config.role, Some("role1".to_string()));
117        assert_eq!(session_config.secondary_roles, Some(vec![]));
118    }
119}