clickhouse_readonly/block/
row.rs1use std::{marker, sync::Arc};
2
3use crate::{
4 column::{Column, ColumnType},
5 types::{FromSql, SqlType},
6};
7
8use crate::{
9 block::{Block, ColumnIdx},
10 error::Result,
11};
12
13pub struct Row<'a, K: ColumnType> {
15 pub(crate) row: usize,
16 pub(crate) block_ref: BlockRef<'a, K>,
17 pub(crate) kind: marker::PhantomData<K>,
18}
19
20impl<'a, K: ColumnType> Row<'a, K> {
21 pub fn get<T, I>(&'a self, col: I) -> Result<T>
23 where
24 T: FromSql<'a>,
25 I: ColumnIdx + Copy,
26 {
27 self.block_ref.get(self.row, col)
28 }
29
30 pub fn len(&self) -> usize {
32 self.block_ref.column_count()
33 }
34
35 pub fn is_empty(&self) -> bool {
37 self.len() == 0
38 }
39
40 pub fn name<I: ColumnIdx + Copy>(&self, col: I) -> Result<&str> {
42 Ok(self.block_ref.get_column(col)?.name())
43 }
44
45 pub fn sql_type<I: ColumnIdx + Copy>(&self, col: I) -> Result<SqlType> {
47 Ok(self.block_ref.get_column(col)?.sql_type())
48 }
49}
50
51pub(crate) enum BlockRef<'a, K: ColumnType> {
52 Borrowed(&'a Block<K>),
53 Owned(Arc<Block<K>>),
54}
55
56impl<'a, K: ColumnType> Clone for BlockRef<'a, K> {
57 fn clone(&self) -> Self {
58 match self {
59 BlockRef::Borrowed(block_ref) => BlockRef::Borrowed(*block_ref),
60 BlockRef::Owned(block_ref) => BlockRef::Owned(block_ref.clone()),
61 }
62 }
63}
64
65impl<'a, K: ColumnType> BlockRef<'a, K> {
66 fn row_count(&self) -> usize {
67 match self {
68 BlockRef::Borrowed(block) => block.row_count(),
69 BlockRef::Owned(block) => block.row_count(),
70 }
71 }
72
73 fn column_count(&self) -> usize {
74 match self {
75 BlockRef::Borrowed(block) => block.column_count(),
76 BlockRef::Owned(block) => block.column_count(),
77 }
78 }
79
80 fn get<'s, T, I>(&'s self, row: usize, col: I) -> Result<T>
81 where
82 T: FromSql<'s>,
83 I: ColumnIdx + Copy,
84 {
85 match self {
86 BlockRef::Borrowed(block) => block.get(row, col),
87 BlockRef::Owned(block) => block.get(row, col),
88 }
89 }
90
91 fn get_column<I: ColumnIdx + Copy>(&self, col: I) -> Result<&Column<K>> {
92 match self {
93 BlockRef::Borrowed(block) => {
94 let column_index = col.get_index(block.columns())?;
95 Ok(&block.columns[column_index])
96 }
97 BlockRef::Owned(block) => {
98 let column_index = col.get_index(block.columns())?;
99 Ok(&block.columns[column_index])
100 }
101 }
102 }
103}
104
105pub struct Rows<'a, K: ColumnType> {
107 pub(crate) row: usize,
108 pub(crate) block_ref: BlockRef<'a, K>,
109 pub(crate) kind: marker::PhantomData<K>,
110}
111
112impl<'a, K: ColumnType> Iterator for Rows<'a, K> {
113 type Item = Row<'a, K>;
114
115 fn next(&mut self) -> Option<Self::Item> {
116 if self.row >= self.block_ref.row_count() {
117 return None;
118 }
119 let result = Some(Row {
120 row: self.row,
121 block_ref: self.block_ref.clone(),
122 kind: marker::PhantomData,
123 });
124 self.row += 1;
125 result
126 }
127}