databend_driver_core/
raw_rows.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 std::pin::Pin;
16use std::task::Context;
17use std::task::Poll;
18
19use tokio_stream::{Stream, StreamExt};
20
21use crate::error::Error;
22use crate::error::Result;
23use crate::rows::Row;
24use crate::rows::ServerStats;
25use crate::schema::SchemaRef;
26use crate::value::Value;
27
28#[derive(Clone, Debug)]
29pub enum RawRowWithStats {
30    Row(RawRow),
31    Stats(ServerStats),
32}
33
34#[derive(Clone, Debug, Default)]
35pub struct RawRow {
36    pub row: Row,
37    pub raw_row: Vec<Option<String>>,
38}
39
40impl RawRow {
41    pub fn new(row: Row, raw_row: Vec<Option<String>>) -> Self {
42        Self { row, raw_row }
43    }
44
45    pub fn len(&self) -> usize {
46        self.raw_row.len()
47    }
48
49    pub fn is_empty(&self) -> bool {
50        self.raw_row.is_empty()
51    }
52
53    pub fn values(&self) -> &[Option<String>] {
54        &self.raw_row
55    }
56
57    pub fn schema(&self) -> SchemaRef {
58        self.row.schema()
59    }
60}
61
62impl TryFrom<(SchemaRef, Vec<Option<String>>)> for RawRow {
63    type Error = Error;
64
65    fn try_from((schema, data): (SchemaRef, Vec<Option<String>>)) -> Result<Self> {
66        let mut values: Vec<Value> = Vec::new();
67        for (i, field) in schema.fields().iter().enumerate() {
68            let val: Option<&str> = data.get(i).and_then(|v| v.as_deref());
69            values.push(Value::try_from((&field.data_type, val))?);
70        }
71
72        let row = Row::new(schema, values);
73        Ok(RawRow::new(row, data))
74    }
75}
76
77impl IntoIterator for RawRow {
78    type Item = Option<String>;
79    type IntoIter = std::vec::IntoIter<Self::Item>;
80
81    fn into_iter(self) -> Self::IntoIter {
82        self.raw_row.into_iter()
83    }
84}
85
86#[derive(Clone, Debug)]
87pub struct RawRows {
88    rows: Vec<RawRow>,
89}
90
91impl RawRows {
92    pub fn new(rows: Vec<RawRow>) -> Self {
93        Self { rows }
94    }
95
96    pub fn rows(&self) -> &[RawRow] {
97        &self.rows
98    }
99
100    pub fn len(&self) -> usize {
101        self.rows.len()
102    }
103
104    pub fn is_empty(&self) -> bool {
105        self.rows.is_empty()
106    }
107}
108
109impl IntoIterator for RawRows {
110    type Item = RawRow;
111    type IntoIter = std::vec::IntoIter<Self::Item>;
112
113    fn into_iter(self) -> Self::IntoIter {
114        self.rows.into_iter()
115    }
116}
117
118pub struct RawRowIterator {
119    schema: SchemaRef,
120    it: Pin<Box<dyn Stream<Item = Result<RawRow>> + Send>>,
121}
122
123impl RawRowIterator {
124    pub fn new(
125        schema: SchemaRef,
126        it: Pin<Box<dyn Stream<Item = Result<RawRowWithStats>> + Send>>,
127    ) -> Self {
128        let it = it.filter_map(|r| match r {
129            Ok(RawRowWithStats::Row(r)) => Some(Ok(r)),
130            Ok(_) => None,
131            Err(err) => Some(Err(err)),
132        });
133        Self {
134            schema,
135            it: Box::pin(it),
136        }
137    }
138
139    pub fn schema(&self) -> SchemaRef {
140        self.schema.clone()
141    }
142}
143
144impl Stream for RawRowIterator {
145    type Item = Result<RawRow>;
146
147    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
148        Pin::new(&mut self.it).poll_next(cx)
149    }
150}