1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#[cfg(feature = "futures03")]
use crate::RowOwned;
use crate::row_metadata::RowMetadata;
use crate::{
RowRead,
bytes_ext::BytesExt,
cursors::RawCursor,
error::{Error, Result},
query_summary::QuerySummary,
response::Response,
rowbinary,
};
use bytes::Buf;
use clickhouse_types::error::TypesError;
use clickhouse_types::parse_rbwnat_columns_header;
use polonius_the_crab::prelude::*;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll, ready};
/// A cursor that emits rows deserialized as structures from RowBinary.
#[must_use]
pub struct RowCursor<T> {
raw: RawCursor,
bytes: BytesExt,
validation: bool,
/// [`None`] until the first call to [`RowCursor::next()`],
/// as [`RowCursor::new`] is not `async`, so it loads lazily.
row_metadata: Option<RowMetadata>,
span: tracing::Span,
returned_rows: u64,
_marker: PhantomData<fn() -> T>,
}
impl<T> RowCursor<T> {
pub(crate) fn new(response: Response, validation: bool, span: tracing::Span) -> Self {
Self {
_marker: PhantomData,
raw: RawCursor::new(response),
bytes: BytesExt::default(),
row_metadata: None,
validation,
span,
returned_rows: 0,
}
}
#[cold]
#[inline(never)]
fn poll_read_columns(&mut self, cx: &mut Context<'_>) -> Poll<Result<()>>
where
T: RowRead,
{
let _span = self.span.enter();
loop {
if self.bytes.remaining() > 0 {
let mut slice = self.bytes.slice();
// Can't pass `&mut self.bytes` because the parsing may partially consume the buffer
match parse_rbwnat_columns_header(&mut slice) {
Ok(columns) if !columns.is_empty() => {
self.bytes.set_remaining(slice.len());
let row_metadata = RowMetadata::new_for_cursor::<T>(columns)?;
self.row_metadata = Some(row_metadata);
return Poll::Ready(Ok(()));
}
Ok(_) => {
// This does not panic, as it could be a network issue
// or a malformed response from the server or LB,
// and a simple retry might help in certain cases.
return Poll::Ready(Err(Error::BadResponse(
"Expected at least one column in the header".to_string(),
)));
}
Err(TypesError::NotEnoughData(_)) => {}
Err(err) => {
return Poll::Ready(Err(Error::InvalidColumnsHeader(err.into())));
}
}
}
match ready!(self.raw.poll_next(cx))? {
Some(chunk) => self.bytes.extend(chunk),
None if self.row_metadata.is_none() => {
// Similar to the other BadResponse branch above
return Poll::Ready(Err(Error::BadResponse(
"Could not read columns header".to_string(),
)));
}
// if the result set is empty, there is only the columns header
None => return Poll::Ready(Ok(())),
}
}
}
/// Emits the next row.
///
/// The result is unspecified if it's called after `Err` is returned.
///
/// # Cancel safety
///
/// This method is cancellation safe.
pub async fn next(&mut self) -> Result<Option<T::Value<'_>>>
where
T: RowRead,
{
Next::new(self).await
}
#[inline]
fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Result<Option<T::Value<'_>>>>
where
T: RowRead,
{
if self.validation && self.row_metadata.is_none() {
ready!(self.poll_read_columns(cx))?;
debug_assert!(self.row_metadata.is_some());
}
let _span = self.span.enter();
let mut bytes = &mut self.bytes;
loop {
polonius!(|bytes| -> Poll<Result<Option<T::Value<'polonius>>>> {
if bytes.remaining() > 0 {
let mut slice = bytes.slice();
let result = rowbinary::deserialize_row::<T::Value<'_>>(
&mut slice,
self.row_metadata.as_ref(),
);
match result {
Ok(value) => {
self.returned_rows += 1;
bytes.set_remaining(slice.len());
polonius_return!(Poll::Ready(Ok(Some(value))))
}
Err(Error::NotEnoughData) => {}
Err(err) => {
tracing::debug!(error=?err, "error deserializing row");
polonius_return!(Poll::Ready(Err(err)))
}
}
}
});
match ready!(self.raw.poll_next(cx)) {
Ok(Some(chunk)) => bytes.extend(chunk),
Ok(None) => {
return if bytes.remaining() > 0 {
// If some data is left, we have an incomplete row in the buffer.
// This is usually a schema mismatch on the client side.
tracing::warn!(
bytes_remaining = bytes.remaining(),
"incomplete read from cursor"
);
Poll::Ready(Err(Error::NotEnoughData))
} else {
Poll::Ready(Ok(None))
};
}
Err(e) => {
tracing::debug!(error=?e, "error from raw cursor");
return Poll::Ready(Err(e));
}
}
}
}
/// Returns the total size in bytes received from the CH server since
/// the cursor was created.
///
/// This method counts only size without HTTP headers for now.
/// It can be changed in the future without notice.
#[inline]
pub fn received_bytes(&self) -> u64 {
self.raw.received_bytes()
}
/// Returns the total size in bytes decompressed since the cursor was created.
#[inline]
pub fn decoded_bytes(&self) -> u64 {
self.raw.decoded_bytes()
}
/// Returns the total number of rows that have been decoded so far.
#[inline]
pub fn returned_rows(&self) -> u64 {
self.returned_rows
}
/// Returns the parsed `X-ClickHouse-Summary` response header, if
/// present. Available once the response headers have been received.
///
/// Note: the summary values may be incomplete unless the query was
/// executed with `wait_end_of_query=1`.
#[inline]
pub fn summary(&self) -> Option<&QuerySummary> {
self.raw.summary()
}
}
impl<T> Drop for RowCursor<T> {
fn drop(&mut self) {
let _span = self.span.enter();
tracing::record_all!(
self.span,
db.response.returned_rows = self.returned_rows,
clickhouse.response.received_bytes = self.received_bytes(),
clickhouse.response.decoded_bytes = self.decoded_bytes(),
);
tracing::debug!("finished typed query");
}
}
#[cfg(feature = "futures03")]
impl<T> futures_util::stream::Stream for RowCursor<T>
where
T: RowOwned + RowRead,
{
type Item = Result<T>;
fn poll_next(
self: std::pin::Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<Self::Item>> {
Self::poll_next(self.get_mut(), cx).map(Result::transpose)
}
}
struct Next<'a, T> {
cursor: Option<&'a mut RowCursor<T>>,
}
impl<'a, T> Next<'a, T> {
fn new(cursor: &'a mut RowCursor<T>) -> Self {
Self {
cursor: Some(cursor),
}
}
}
impl<'a, T> std::future::Future for Next<'a, T>
where
T: RowRead,
{
type Output = Result<Option<T::Value<'a>>>;
#[inline]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// Temporarily take the cursor out in order for `cursor.poll_next` to return a value with
// the correct lifetime `'a` rather than the unnamed lifetime of `&mut self`.
let mut cursor = self.cursor.take().expect("Future polled after completion");
polonius!(|cursor| -> Poll<Result<Option<T::Value<'polonius>>>> {
match cursor.poll_next(cx) {
Poll::Ready(value) => polonius_return!(Poll::Ready(value)),
Poll::Pending => {}
}
});
self.cursor = Some(cursor);
Poll::Pending
}
}