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::with_capacity(data.len());
67        for (field, val) in schema.fields().iter().zip(data.clone().into_iter()) {
68            values.push(Value::try_from((&field.data_type, val))?);
69        }
70
71        let row = Row::new(schema, values);
72        Ok(RawRow::new(row, data))
73    }
74}
75
76impl IntoIterator for RawRow {
77    type Item = Option<String>;
78    type IntoIter = std::vec::IntoIter<Self::Item>;
79
80    fn into_iter(self) -> Self::IntoIter {
81        self.raw_row.into_iter()
82    }
83}
84
85#[derive(Clone, Debug)]
86pub struct RawRows {
87    rows: Vec<RawRow>,
88}
89
90impl RawRows {
91    pub fn new(rows: Vec<RawRow>) -> Self {
92        Self { rows }
93    }
94
95    pub fn rows(&self) -> &[RawRow] {
96        &self.rows
97    }
98
99    pub fn len(&self) -> usize {
100        self.rows.len()
101    }
102
103    pub fn is_empty(&self) -> bool {
104        self.rows.is_empty()
105    }
106}
107
108impl IntoIterator for RawRows {
109    type Item = RawRow;
110    type IntoIter = std::vec::IntoIter<Self::Item>;
111
112    fn into_iter(self) -> Self::IntoIter {
113        self.rows.into_iter()
114    }
115}
116
117pub struct RawRowIterator {
118    schema: SchemaRef,
119    it: Pin<Box<dyn Stream<Item = Result<RawRow>> + Send>>,
120}
121
122impl RawRowIterator {
123    pub fn new(
124        schema: SchemaRef,
125        it: Pin<Box<dyn Stream<Item = Result<RawRowWithStats>> + Send>>,
126    ) -> Self {
127        let it = it.filter_map(|r| match r {
128            Ok(RawRowWithStats::Row(r)) => Some(Ok(r)),
129            Ok(_) => None,
130            Err(err) => Some(Err(err)),
131        });
132        Self {
133            schema,
134            it: Box::pin(it),
135        }
136    }
137
138    pub fn schema(&self) -> SchemaRef {
139        self.schema.clone()
140    }
141}
142
143impl Stream for RawRowIterator {
144    type Item = Result<RawRow>;
145
146    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
147        Pin::new(&mut self.it).poll_next(cx)
148    }
149}