1mod bool_view;
2
3pub use bool_view::BoolView;
4
5mod tinyint_view;
6
7use itertools::Itertools;
9pub use tinyint_view::TinyIntView;
10
11mod smallint_view;
12pub use smallint_view::SmallIntView;
13
14mod int_view;
15pub use int_view::IntView;
16
17mod big_int_view;
18pub use big_int_view::BigIntView;
19
20mod float_view;
21pub use float_view::FloatView;
22
23mod double_view;
24pub use double_view::DoubleView;
25
26mod tinyint_unsigned_view;
27pub use tinyint_unsigned_view::UTinyIntView;
28
29mod small_int_unsigned_view;
30pub use small_int_unsigned_view::USmallIntView;
31
32mod int_unsigned_view;
33pub use int_unsigned_view::UIntView;
34
35mod big_int_unsigned_view;
36pub use big_int_unsigned_view::UBigIntView;
37
38mod timestamp_view;
39pub use timestamp_view::TimestampView;
40
41mod var_char_view;
42pub use var_char_view::VarCharView;
43
44mod n_char_view;
45pub use n_char_view::NCharView;
46
47mod json_view;
48pub use json_view::JsonView;
49
50mod varbinary_view;
51pub use varbinary_view::VarBinaryView;
52
53mod geometry_view;
54pub use geometry_view::GeometryView;
55
56mod schema;
57pub(crate) use schema::*;
58
59mod nulls;
60pub(crate) use nulls::*;
61
62mod offsets;
63pub(crate) use offsets::*;
64
65mod lengths;
66pub(crate) use lengths::*;
67
68mod from;
69
70use crate::{
71 common::{BorrowedValue, Ty, Value},
72 Precision,
73};
74
75use std::{
76 ffi::c_void,
77 fmt::{Debug, Display},
78 io::Write,
79 iter::FusedIterator,
80};
81
82pub(crate) trait IsColumnView: Sized {
83 #[allow(dead_code)]
84 fn ty(&self) -> Ty;
86
87 #[allow(dead_code)]
88 fn from_value_iter<'a>(iter: impl Iterator<Item = &'a Value>) -> Self {
89 Self::from_borrowed_value_iter::<'a>(iter.map(|v| v.to_borrowed_value()))
90 }
91
92 fn from_borrowed_value_iter<'b>(iter: impl Iterator<Item = BorrowedValue<'b>>) -> Self;
93}
94
95#[derive(Debug, Clone, Copy, PartialEq)]
97pub(crate) enum Version {
98 V2,
99 V3,
100}
101
102#[derive(Clone)]
103
104pub enum ColumnView {
105 Bool(BoolView), TinyInt(TinyIntView), SmallInt(SmallIntView), Int(IntView), BigInt(BigIntView), Float(FloatView), Double(DoubleView), VarChar(VarCharView), Timestamp(TimestampView), NChar(NCharView), UTinyInt(UTinyIntView), USmallInt(USmallIntView), UInt(UIntView), UBigInt(UBigIntView), Json(JsonView), VarBinary(VarBinaryView), Geometry(GeometryView), }
123unsafe impl Send for ColumnView {}
124unsafe impl Sync for ColumnView {}
125
126impl Debug for ColumnView {
127 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
128 match self {
129 Self::Bool(view) => f.debug_tuple("Bool").field(&view.to_vec()).finish(),
130 Self::TinyInt(view) => f.debug_tuple("TinyInt").field(&view.to_vec()).finish(),
131 Self::SmallInt(view) => f.debug_tuple("SmallInt").field(&view.to_vec()).finish(),
132 Self::Int(view) => f.debug_tuple("Int").field(&view.to_vec()).finish(),
133 Self::BigInt(view) => f.debug_tuple("BigInt").field(&view.to_vec()).finish(),
134 Self::Float(view) => f.debug_tuple("Float").field(&view.to_vec()).finish(),
135 Self::Double(view) => f.debug_tuple("Double").field(&view.to_vec()).finish(),
136 Self::VarChar(view) => f.debug_tuple("VarChar").field(&view.to_vec()).finish(),
137 Self::Timestamp(view) => f.debug_tuple("Timestamp").field(&view.to_vec()).finish(),
138 Self::NChar(view) => f.debug_tuple("NChar").field(&view.to_vec()).finish(),
139 Self::UTinyInt(view) => f.debug_tuple("UTinyInt").field(&view.to_vec()).finish(),
140 Self::USmallInt(view) => f.debug_tuple("USmallInt").field(&view.to_vec()).finish(),
141 Self::UInt(view) => f.debug_tuple("UInt").field(&view.to_vec()).finish(),
142 Self::UBigInt(view) => f.debug_tuple("UBigInt").field(&view.to_vec()).finish(),
143 Self::Json(view) => f.debug_tuple("Json").field(&view.to_vec()).finish(),
144 Self::VarBinary(view) => f.debug_tuple("VarBinary").field(&view.to_vec()).finish(),
145 Self::Geometry(view) => f.debug_tuple("Geometry").field(&view.to_vec()).finish(),
146 }
147 }
148}
149
150impl std::ops::Add for ColumnView {
151 type Output = ColumnView;
152
153 fn add(self, rhs: Self) -> Self::Output {
154 self.concat(&rhs)
155 }
156}
157
158#[derive(Debug)]
159pub struct CastError {
160 from: Ty,
161 to: Ty,
162 message: &'static str,
163}
164
165impl Display for CastError {
166 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
167 f.write_fmt(format_args!(
168 "Cast from {} to {} error: {}",
169 self.from, self.to, self.message
170 ))
171 }
172}
173
174impl std::error::Error for CastError {}
175
176pub struct ColumnViewIter<'c> {
177 view: &'c ColumnView,
178 row: usize,
179}
180
181impl<'c> Iterator for ColumnViewIter<'c> {
182 type Item = BorrowedValue<'c>;
183
184 fn next(&mut self) -> Option<Self::Item> {
185 if self.row >= self.view.len() {
186 return None;
187 }
188 let row = self.row;
189 self.row += 1;
190 Some(unsafe { self.view.get_ref_unchecked(row) })
191 }
192
193 fn size_hint(&self) -> (usize, Option<usize>) {
194 (self.row, Some(self.view.len() - self.row))
195 }
196
197 fn nth(&mut self, n: usize) -> Option<Self::Item> {
198 let row = self.row + n;
199 if row >= self.view.len() {
200 return None;
201 }
202 self.row += n;
203 Some(unsafe { self.view.get_ref_unchecked(row) })
204 }
205
206 #[inline]
207 fn count(self) -> usize {
208 self.view.len()
209 }
210}
211
212impl<'c> ExactSizeIterator for ColumnViewIter<'c> {
213 fn len(&self) -> usize {
214 self.view.len() - self.row
215 }
216}
217
218impl<'a> FusedIterator for ColumnViewIter<'a> {}
219
220impl<'a> IntoIterator for &'a ColumnView {
221 type Item = BorrowedValue<'a>;
222
223 type IntoIter = ColumnViewIter<'a>;
224
225 fn into_iter(self) -> Self::IntoIter {
226 self.iter()
227 }
228}
229
230impl ColumnView {
231 pub fn from_millis_timestamp(values: Vec<impl Into<Option<i64>>>) -> Self {
232 ColumnView::Timestamp(TimestampView::from_millis(values))
233 }
234 pub fn from_micros_timestamp(values: Vec<impl Into<Option<i64>>>) -> Self {
235 ColumnView::Timestamp(TimestampView::from_micros(values))
236 }
237 pub fn from_nanos_timestamp(values: Vec<impl Into<Option<i64>>>) -> Self {
238 ColumnView::Timestamp(TimestampView::from_nanos(values))
239 }
240 pub fn from_bools(values: Vec<impl Into<Option<bool>>>) -> Self {
241 ColumnView::Bool(BoolView::from_iter(values))
242 }
243 pub fn from_tiny_ints(values: Vec<impl Into<Option<i8>>>) -> Self {
244 ColumnView::TinyInt(TinyIntView::from_iter(values))
245 }
246 pub fn from_small_ints(values: Vec<impl Into<Option<i16>>>) -> Self {
247 ColumnView::SmallInt(SmallIntView::from_iter(values))
248 }
249 pub fn from_ints(values: Vec<impl Into<Option<i32>>>) -> Self {
250 ColumnView::Int(IntView::from_iter(values))
251 }
252 pub fn from_big_ints(values: Vec<impl Into<Option<i64>>>) -> Self {
253 ColumnView::BigInt(BigIntView::from_iter(values))
254 }
255 pub fn from_unsigned_tiny_ints(values: Vec<impl Into<Option<u8>>>) -> Self {
256 ColumnView::UTinyInt(UTinyIntView::from_iter(values))
257 }
258 pub fn from_unsigned_small_ints(values: Vec<impl Into<Option<u16>>>) -> Self {
259 ColumnView::USmallInt(USmallIntView::from_iter(values))
260 }
261 pub fn from_unsigned_ints(values: Vec<impl Into<Option<u32>>>) -> Self {
262 ColumnView::UInt(UIntView::from_iter(values))
263 }
264 pub fn from_unsigned_big_ints(values: Vec<impl Into<Option<u64>>>) -> Self {
265 ColumnView::UBigInt(UBigIntView::from_iter(values))
266 }
267 pub fn from_floats(values: Vec<impl Into<Option<f32>>>) -> Self {
268 ColumnView::Float(FloatView::from_iter(values))
269 }
270 pub fn from_doubles(values: Vec<impl Into<Option<f64>>>) -> Self {
271 ColumnView::Double(DoubleView::from_iter(values))
272 }
273 pub fn from_varchar<
274 S: AsRef<str>,
275 T: Into<Option<S>>,
276 I: ExactSizeIterator<Item = T>,
277 V: IntoIterator<Item = T, IntoIter = I>,
278 >(
279 iter: V,
280 ) -> Self {
281 ColumnView::VarChar(VarCharView::from_iter(iter))
282 }
283 pub fn from_nchar<
284 S: AsRef<str>,
285 T: Into<Option<S>>,
286 I: ExactSizeIterator<Item = T>,
287 V: IntoIterator<Item = T, IntoIter = I>,
288 >(
289 iter: V,
290 ) -> Self {
291 ColumnView::NChar(NCharView::from_iter(iter))
292 }
293 pub fn from_json<
294 S: AsRef<str>,
295 T: Into<Option<S>>,
296 I: ExactSizeIterator<Item = T>,
297 V: IntoIterator<Item = T, IntoIter = I>,
298 >(
299 iter: V,
300 ) -> Self {
301 ColumnView::Json(JsonView::from_iter(iter))
302 }
303
304 pub fn from_bytes<
305 S: AsRef<[u8]>,
306 T: Into<Option<S>>,
307 I: ExactSizeIterator<Item = T>,
308 V: IntoIterator<Item = T, IntoIter = I>,
309 >(
310 iter: V,
311 ) -> Self {
312 ColumnView::VarBinary(VarBinaryView::from_iter(iter))
313 }
314
315 pub fn from_geobytes<
316 S: AsRef<[u8]>,
317 T: Into<Option<S>>,
318 I: ExactSizeIterator<Item = T>,
319 V: IntoIterator<Item = T, IntoIter = I>,
320 >(
321 iter: V,
322 ) -> Self {
323 ColumnView::Geometry(GeometryView::from_iter(iter))
324 }
325
326 #[inline]
327 pub fn concat_iter<'b, 'a: 'b>(
328 &'a self,
329 rhs: impl Iterator<Item = BorrowedValue<'b>>,
330 ty: Ty,
331 ) -> ColumnView {
332 match ty {
333 Ty::Null => unreachable!(),
334 Ty::Bool => ColumnView::Bool(IsColumnView::from_borrowed_value_iter(
335 self.iter().chain(rhs),
336 )),
337 Ty::TinyInt => ColumnView::TinyInt(IsColumnView::from_borrowed_value_iter(
338 self.iter().chain(rhs),
339 )),
340 Ty::SmallInt => ColumnView::SmallInt(IsColumnView::from_borrowed_value_iter(
341 self.iter().chain(rhs),
342 )),
343 Ty::Int => ColumnView::Int(IsColumnView::from_borrowed_value_iter(
344 self.iter().chain(rhs),
345 )),
346 Ty::BigInt => ColumnView::BigInt(IsColumnView::from_borrowed_value_iter(
347 self.iter().chain(rhs),
348 )),
349 Ty::UTinyInt => ColumnView::UTinyInt(IsColumnView::from_borrowed_value_iter(
350 self.iter().chain(rhs),
351 )),
352 Ty::USmallInt => ColumnView::USmallInt(IsColumnView::from_borrowed_value_iter(
353 self.iter().chain(rhs),
354 )),
355 Ty::UInt => ColumnView::UInt(IsColumnView::from_borrowed_value_iter(
356 self.iter().chain(rhs),
357 )),
358 Ty::UBigInt => ColumnView::UBigInt(IsColumnView::from_borrowed_value_iter(
359 self.iter().chain(rhs),
360 )),
361 Ty::Float => ColumnView::Float(IsColumnView::from_borrowed_value_iter(
362 self.iter().chain(rhs),
363 )),
364 Ty::Double => ColumnView::Double(IsColumnView::from_borrowed_value_iter(
365 self.iter().chain(rhs),
366 )),
367 Ty::Timestamp => ColumnView::Timestamp(IsColumnView::from_borrowed_value_iter(
368 self.iter().chain(rhs),
369 )),
370 Ty::VarChar => ColumnView::VarChar(IsColumnView::from_borrowed_value_iter(
371 self.iter().chain(rhs),
372 )),
373 Ty::NChar => ColumnView::NChar(IsColumnView::from_borrowed_value_iter(
374 self.iter().chain(rhs),
375 )),
376 Ty::Json => ColumnView::Json(IsColumnView::from_borrowed_value_iter(
377 self.iter().chain(rhs),
378 )),
379 Ty::VarBinary => ColumnView::VarBinary(IsColumnView::from_borrowed_value_iter(
380 self.iter().chain(rhs),
381 )),
382 Ty::Decimal => todo!(),
383 Ty::Blob => todo!(),
384 Ty::MediumBlob => todo!(),
385 Ty::Geometry => ColumnView::Geometry(IsColumnView::from_borrowed_value_iter(
386 self.iter().chain(rhs),
387 )),
388 }
389 }
390
391 #[inline]
393 pub fn concat(&self, rhs: &ColumnView) -> ColumnView {
394 self.concat_as(rhs, self.as_ty())
395 }
396
397 #[inline]
403 pub fn concat_strictly(&self, rhs: &ColumnView) -> ColumnView {
404 match (self, rhs) {
405 (ColumnView::Timestamp(a), ColumnView::Timestamp(b)) => {
406 ColumnView::Timestamp(a.concat(b))
407 }
408 (ColumnView::Bool(a), ColumnView::Bool(b)) => ColumnView::Bool(a.concat(b)),
409 (ColumnView::TinyInt(a), ColumnView::TinyInt(b)) => ColumnView::TinyInt(a.concat(b)),
410 (ColumnView::UTinyInt(a), ColumnView::UTinyInt(b)) => ColumnView::UTinyInt(a.concat(b)),
411 (ColumnView::SmallInt(a), ColumnView::SmallInt(b)) => ColumnView::SmallInt(a.concat(b)),
412 (ColumnView::USmallInt(a), ColumnView::USmallInt(b)) => {
413 ColumnView::USmallInt(a.concat(b))
414 }
415 (ColumnView::Int(a), ColumnView::Int(b)) => ColumnView::Int(a.concat(b)),
416 (ColumnView::UInt(a), ColumnView::UInt(b)) => ColumnView::UInt(a.concat(b)),
417 (ColumnView::BigInt(a), ColumnView::BigInt(b)) => ColumnView::BigInt(a.concat(b)),
418 (ColumnView::UBigInt(a), ColumnView::UBigInt(b)) => ColumnView::UBigInt(a.concat(b)),
419 (ColumnView::Float(a), ColumnView::Float(b)) => ColumnView::Float(a.concat(b)),
420 (ColumnView::Double(a), ColumnView::Double(b)) => ColumnView::Double(a.concat(b)),
421 (ColumnView::VarChar(a), ColumnView::VarChar(b)) => ColumnView::VarChar(a.concat(b)),
422 (ColumnView::NChar(a), ColumnView::NChar(b)) => ColumnView::NChar(a.concat(b)),
423 (ColumnView::Json(a), ColumnView::Json(b)) => ColumnView::Json(a.concat(b)),
424 (ColumnView::VarBinary(_a), ColumnView::VarBinary(_b)) => todo!(), (ColumnView::Geometry(_a), ColumnView::Geometry(_b)) => todo!(), _ => panic!("strict concat needs same schema: {self:?}, {rhs:?}"),
427 }
428 }
429
430 #[inline]
432 pub fn concat_as(&self, rhs: &ColumnView, ty: Ty) -> ColumnView {
433 self.concat_iter(rhs.iter(), ty)
434 }
435
436 pub fn null(n: usize, ty: Ty) -> Self {
438 match ty {
439 Ty::Null => panic!("type should be known"),
440 Ty::Bool => Self::from_bools(vec![None; n]),
441 Ty::TinyInt => Self::from_tiny_ints(vec![None; n]),
442 Ty::SmallInt => Self::from_small_ints(vec![None; n]),
443 Ty::Int => Self::from_ints(vec![None; n]),
444 Ty::BigInt => Self::from_big_ints(vec![None; n]),
445 Ty::UTinyInt => Self::from_unsigned_tiny_ints(vec![None; n]),
446 Ty::USmallInt => Self::from_unsigned_small_ints(vec![None; n]),
447 Ty::UInt => Self::from_unsigned_ints(vec![None; n]),
448 Ty::UBigInt => Self::from_unsigned_big_ints(vec![None; n]),
449 Ty::Float => Self::from_floats(vec![None; n]),
450 Ty::Double => Self::from_doubles(vec![None; n]),
451 Ty::Timestamp => Self::from_millis_timestamp(vec![None; n]),
452 Ty::VarChar => Self::from_varchar::<&'static str, _, _, _>(vec![None; n]),
453 Ty::NChar => Self::from_nchar::<&'static str, _, _, _>(vec![None; n]),
454 Ty::Json => todo!(),
455 Ty::VarBinary => todo!(),
456 Ty::Decimal => todo!(),
457 Ty::Blob => todo!(),
458 Ty::MediumBlob => todo!(),
459 Ty::Geometry => todo!(),
460 }
461 }
462
463 pub fn len(&self) -> usize {
465 match self {
466 ColumnView::Bool(view) => view.len(),
467 ColumnView::TinyInt(view) => view.len(),
468 ColumnView::SmallInt(view) => view.len(),
469 ColumnView::Int(view) => view.len(),
470 ColumnView::BigInt(view) => view.len(),
471 ColumnView::Float(view) => view.len(),
472 ColumnView::Double(view) => view.len(),
473 ColumnView::VarChar(view) => view.len(),
474 ColumnView::Timestamp(view) => view.len(),
475 ColumnView::NChar(view) => view.len(),
476 ColumnView::UTinyInt(view) => view.len(),
477 ColumnView::USmallInt(view) => view.len(),
478 ColumnView::UInt(view) => view.len(),
479 ColumnView::UBigInt(view) => view.len(),
480 ColumnView::Json(view) => view.len(),
481 ColumnView::VarBinary(view) => view.len(),
482 ColumnView::Geometry(view) => view.len(),
483 }
484 }
485
486 pub fn max_variable_length(&self) -> usize {
487 match self {
488 ColumnView::Bool(_) => 1,
489 ColumnView::TinyInt(_) => 1,
490 ColumnView::SmallInt(_) => 2,
491 ColumnView::Int(_) => 4,
492 ColumnView::BigInt(_) => 8,
493 ColumnView::Float(_) => 4,
494 ColumnView::Double(_) => 8,
495 ColumnView::VarChar(view) => view.max_length(),
496 ColumnView::Timestamp(_) => 8,
497 ColumnView::NChar(view) => view.max_length(),
498 ColumnView::UTinyInt(_) => 1,
499 ColumnView::USmallInt(_) => 2,
500 ColumnView::UInt(_) => 4,
501 ColumnView::UBigInt(_) => 8,
502 ColumnView::Json(view) => view.max_length(),
503 ColumnView::VarBinary(view) => view.max_length(),
504 ColumnView::Geometry(view) => view.max_length(),
505 }
506 }
507
508 #[inline]
510 pub(super) unsafe fn is_null_unchecked(&self, row: usize) -> bool {
511 match self {
512 ColumnView::Bool(view) => view.is_null_unchecked(row),
513 ColumnView::TinyInt(view) => view.is_null_unchecked(row),
514 ColumnView::SmallInt(view) => view.is_null_unchecked(row),
515 ColumnView::Int(view) => view.is_null_unchecked(row),
516 ColumnView::BigInt(view) => view.is_null_unchecked(row),
517 ColumnView::Float(view) => view.is_null_unchecked(row),
518 ColumnView::Double(view) => view.is_null_unchecked(row),
519 ColumnView::VarChar(view) => view.is_null_unchecked(row),
520 ColumnView::Timestamp(view) => view.is_null_unchecked(row),
521 ColumnView::NChar(view) => view.is_null_unchecked(row),
522 ColumnView::UTinyInt(view) => view.is_null_unchecked(row),
523 ColumnView::USmallInt(view) => view.is_null_unchecked(row),
524 ColumnView::UInt(view) => view.is_null_unchecked(row),
525 ColumnView::UBigInt(view) => view.is_null_unchecked(row),
526 ColumnView::Json(view) => view.is_null_unchecked(row),
527 ColumnView::VarBinary(view) => view.is_null_unchecked(row),
528 ColumnView::Geometry(view) => view.is_null_unchecked(row),
529 }
530 }
531
532 pub fn get(&self, row: usize) -> Option<BorrowedValue> {
533 if row < self.len() {
534 Some(unsafe { self.get_ref_unchecked(row) })
535 } else {
536 None
537 }
538 }
539
540 #[inline]
542 pub(super) unsafe fn get_ref_unchecked(&self, row: usize) -> BorrowedValue {
543 match self {
544 ColumnView::Bool(view) => view.get_value_unchecked(row),
545 ColumnView::TinyInt(view) => view.get_value_unchecked(row),
546 ColumnView::SmallInt(view) => view.get_value_unchecked(row),
547 ColumnView::Int(view) => view.get_value_unchecked(row),
548 ColumnView::BigInt(view) => view.get_value_unchecked(row),
549 ColumnView::Float(view) => view.get_value_unchecked(row),
550 ColumnView::Double(view) => view.get_value_unchecked(row),
551 ColumnView::VarChar(view) => view.get_value_unchecked(row),
552 ColumnView::Timestamp(view) => view.get_value_unchecked(row),
553 ColumnView::NChar(view) => view.get_value_unchecked(row),
554 ColumnView::UTinyInt(view) => view.get_value_unchecked(row),
555 ColumnView::USmallInt(view) => view.get_value_unchecked(row),
556 ColumnView::UInt(view) => view.get_value_unchecked(row),
557 ColumnView::UBigInt(view) => view.get_value_unchecked(row),
558 ColumnView::Json(view) => view.get_value_unchecked(row),
559 ColumnView::VarBinary(view) => view.get_value_unchecked(row),
560 ColumnView::Geometry(view) => view.get_value_unchecked(row),
561 }
562 }
563
564 #[inline]
566 pub(super) unsafe fn get_raw_value_unchecked(&self, row: usize) -> (Ty, u32, *const c_void) {
567 match self {
568 ColumnView::Bool(view) => view.get_raw_value_unchecked(row),
569 ColumnView::TinyInt(view) => view.get_raw_value_unchecked(row),
570 ColumnView::SmallInt(view) => view.get_raw_value_unchecked(row),
571 ColumnView::Int(view) => view.get_raw_value_unchecked(row),
572 ColumnView::BigInt(view) => view.get_raw_value_unchecked(row),
573 ColumnView::Float(view) => view.get_raw_value_unchecked(row),
574 ColumnView::Double(view) => view.get_raw_value_unchecked(row),
575 ColumnView::VarChar(view) => view.get_raw_value_unchecked(row),
576 ColumnView::Timestamp(view) => view.get_raw_value_unchecked(row),
577 ColumnView::NChar(view) => view.get_raw_value_unchecked(row),
578 ColumnView::UTinyInt(view) => view.get_raw_value_unchecked(row),
579 ColumnView::USmallInt(view) => view.get_raw_value_unchecked(row),
580 ColumnView::UInt(view) => view.get_raw_value_unchecked(row),
581 ColumnView::UBigInt(view) => view.get_raw_value_unchecked(row),
582 ColumnView::Json(view) => view.get_raw_value_unchecked(row),
583 ColumnView::VarBinary(view) => view.get_raw_value_unchecked(row),
584 ColumnView::Geometry(view) => view.get_raw_value_unchecked(row),
585 }
586 }
587
588 pub fn iter(&self) -> ColumnViewIter {
589 ColumnViewIter { view: self, row: 0 }
590 }
591
592 pub fn slice(&self, range: std::ops::Range<usize>) -> Option<Self> {
593 match self {
594 ColumnView::Bool(view) => view.slice(range).map(ColumnView::Bool),
595 ColumnView::TinyInt(view) => view.slice(range).map(ColumnView::TinyInt),
596 ColumnView::SmallInt(view) => view.slice(range).map(ColumnView::SmallInt),
597 ColumnView::Int(view) => view.slice(range).map(ColumnView::Int),
598 ColumnView::BigInt(view) => view.slice(range).map(ColumnView::BigInt),
599 ColumnView::Float(view) => view.slice(range).map(ColumnView::Float),
600 ColumnView::Double(view) => view.slice(range).map(ColumnView::Double),
601 ColumnView::VarChar(view) => view.slice(range).map(ColumnView::VarChar),
602 ColumnView::Timestamp(view) => view.slice(range).map(ColumnView::Timestamp),
603 ColumnView::NChar(view) => view.slice(range).map(ColumnView::NChar),
604 ColumnView::UTinyInt(view) => view.slice(range).map(ColumnView::UTinyInt),
605 ColumnView::USmallInt(view) => view.slice(range).map(ColumnView::USmallInt),
606 ColumnView::UInt(view) => view.slice(range).map(ColumnView::UInt),
607 ColumnView::UBigInt(view) => view.slice(range).map(ColumnView::UBigInt),
608 ColumnView::Json(view) => view.slice(range).map(ColumnView::Json),
609 ColumnView::VarBinary(_view) => todo!(), ColumnView::Geometry(_view) => todo!(), }
612 }
613
614 pub(super) fn write_raw_into<W: Write>(&self, wtr: &mut W) -> std::io::Result<usize> {
615 match self {
616 ColumnView::Bool(view) => view.write_raw_into(wtr),
617 ColumnView::TinyInt(view) => view.write_raw_into(wtr),
618 ColumnView::SmallInt(view) => view.write_raw_into(wtr),
619 ColumnView::Int(view) => view.write_raw_into(wtr),
620 ColumnView::BigInt(view) => view.write_raw_into(wtr),
621 ColumnView::Float(view) => view.write_raw_into(wtr),
622 ColumnView::Double(view) => view.write_raw_into(wtr),
623 ColumnView::VarChar(view) => view.write_raw_into(wtr),
624 ColumnView::Timestamp(view) => view.write_raw_into(wtr),
625 ColumnView::NChar(view) => view.write_raw_into(wtr),
626 ColumnView::UTinyInt(view) => view.write_raw_into(wtr),
627 ColumnView::USmallInt(view) => view.write_raw_into(wtr),
628 ColumnView::UInt(view) => view.write_raw_into(wtr),
629 ColumnView::UBigInt(view) => view.write_raw_into(wtr),
630 ColumnView::Json(view) => view.write_raw_into(wtr),
631 ColumnView::VarBinary(view) => view.write_raw_into(wtr),
632 ColumnView::Geometry(view) => view.write_raw_into(wtr),
633 }
634 }
635
636 pub fn as_ty(&self) -> Ty {
637 match self {
638 ColumnView::Bool(_) => Ty::Bool,
639 ColumnView::TinyInt(_) => Ty::TinyInt,
640 ColumnView::SmallInt(_) => Ty::SmallInt,
641 ColumnView::Int(_) => Ty::Int,
642 ColumnView::BigInt(_) => Ty::BigInt,
643 ColumnView::Float(_) => Ty::Float,
644 ColumnView::Double(_) => Ty::Double,
645 ColumnView::VarChar(_) => Ty::VarChar,
646 ColumnView::Timestamp(_) => Ty::Timestamp,
647 ColumnView::NChar(_) => Ty::NChar,
648 ColumnView::UTinyInt(_) => Ty::UTinyInt,
649 ColumnView::USmallInt(_) => Ty::USmallInt,
650 ColumnView::UInt(_) => Ty::UInt,
651 ColumnView::UBigInt(_) => Ty::UBigInt,
652 ColumnView::Json(_) => Ty::Json,
653 ColumnView::VarBinary(_) => Ty::VarBinary,
654 ColumnView::Geometry(_) => Ty::Geometry,
655 }
656 }
657
658 pub fn cast(&self, ty: Ty) -> Result<ColumnView, CastError> {
668 let l_ty = self.as_ty();
669 if l_ty == ty {
670 return Ok(self.clone());
671 }
672 use Ty::*;
673 match self {
674 ColumnView::Bool(booleans) => {
676 macro_rules! _cast_bool_to {
677 ($ty:ty) => {
678 booleans
679 .iter()
680 .map(|v| v.map(|b| if b { 1 as $ty } else { 0 as $ty }))
681 .collect_vec()
682 };
683 }
684
685 let view = match ty {
686 TinyInt => Self::from_tiny_ints(_cast_bool_to!(i8)),
687 SmallInt => Self::from_small_ints(_cast_bool_to!(i16)),
688 Int => Self::from_ints(_cast_bool_to!(i32)),
689 BigInt => Self::from_big_ints(_cast_bool_to!(i64)),
690 UTinyInt => Self::from_unsigned_tiny_ints(_cast_bool_to!(u8)),
691 USmallInt => Self::from_unsigned_small_ints(_cast_bool_to!(u16)),
692 UInt => Self::from_unsigned_ints(_cast_bool_to!(u32)),
693 UBigInt => Self::from_unsigned_big_ints(_cast_bool_to!(u64)),
694 Float => Self::from_floats(_cast_bool_to!(f32)),
695 Double => Self::from_doubles(_cast_bool_to!(f64)),
696 VarChar => Self::from_varchar::<String, _, _, _>(
697 booleans.iter().map(|v| v.map(|b| b.to_string())),
698 ),
699 NChar => Self::from_nchar::<String, _, _, _>(
700 booleans.iter().map(|v| v.map(|b| b.to_string())),
701 ),
702 _ => {
703 return Err(CastError {
704 from: l_ty,
705 to: ty,
706 message: "booleans can be casted to primitive types only",
707 })
708 }
709 };
710 Ok(view)
711 }
712 ColumnView::TinyInt(booleans) => {
713 macro_rules! _cast_to {
714 ($ty:ty) => {
715 booleans.iter().map(|v| v.map(|b| b as $ty)).collect_vec()
716 };
717 }
718
719 let view = match ty {
720 Bool => {
721 Self::from_bools(booleans.iter().map(|v| v.map(|b| b > 0)).collect_vec())
722 }
723 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
724 SmallInt => Self::from_small_ints(_cast_to!(i16)),
725 Int => Self::from_ints(_cast_to!(i32)),
726 BigInt => Self::from_big_ints(_cast_to!(i64)),
727 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
728 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
729 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
730 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
731 Float => Self::from_floats(_cast_to!(f32)),
732 Double => Self::from_doubles(_cast_to!(f64)),
733 VarChar => Self::from_varchar::<String, _, _, _>(
734 booleans.iter().map(|v| v.map(|b| b.to_string())),
735 ),
736 NChar => Self::from_nchar::<String, _, _, _>(
737 booleans.iter().map(|v| v.map(|b| b.to_string())),
738 ),
739 _ => {
740 return Err(CastError {
741 from: l_ty,
742 to: ty,
743 message: "booleans can be casted to primitive types only",
744 })
745 }
746 };
747 Ok(view)
748 }
749 ColumnView::SmallInt(booleans) => {
750 macro_rules! _cast_to {
751 ($ty:ty) => {
752 booleans.iter().map(|v| v.map(|b| b as $ty)).collect_vec()
753 };
754 }
755
756 let view = match ty {
757 Bool => {
758 Self::from_bools(booleans.iter().map(|v| v.map(|b| b > 0)).collect_vec())
759 }
760 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
761 SmallInt => Self::from_small_ints(_cast_to!(i16)),
762 Int => Self::from_ints(_cast_to!(i32)),
763 BigInt => Self::from_big_ints(_cast_to!(i64)),
764 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
765 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
766 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
767 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
768 Float => Self::from_floats(_cast_to!(f32)),
769 Double => Self::from_doubles(_cast_to!(f64)),
770 VarChar => Self::from_varchar::<String, _, _, _>(
771 booleans.iter().map(|v| v.map(|b| b.to_string())),
772 ),
773 NChar => Self::from_nchar::<String, _, _, _>(
774 booleans.iter().map(|v| v.map(|b| b.to_string())),
775 ),
776 _ => {
777 return Err(CastError {
778 from: l_ty,
779 to: ty,
780 message: "booleans can be casted to primitive types only",
781 })
782 }
783 };
784 Ok(view)
785 }
786 ColumnView::Int(booleans) => {
787 macro_rules! _cast_to {
788 ($ty:ty) => {
789 booleans.iter().map(|v| v.map(|b| b as $ty)).collect_vec()
790 };
791 }
792
793 let view = match ty {
794 Bool => {
795 Self::from_bools(booleans.iter().map(|v| v.map(|b| b > 0)).collect_vec())
796 }
797 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
798 SmallInt => Self::from_small_ints(_cast_to!(i16)),
799 Int => Self::from_ints(_cast_to!(i32)),
800 BigInt => Self::from_big_ints(_cast_to!(i64)),
801 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
802 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
803 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
804 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
805 Float => Self::from_floats(_cast_to!(f32)),
806 Double => Self::from_doubles(_cast_to!(f64)),
807 VarChar => Self::from_varchar::<String, _, _, _>(
808 booleans.iter().map(|v| v.map(|b| b.to_string())),
809 ),
810 NChar => Self::from_nchar::<String, _, _, _>(
811 booleans.iter().map(|v| v.map(|b| b.to_string())),
812 ),
813 _ => {
814 return Err(CastError {
815 from: l_ty,
816 to: ty,
817 message: "booleans can be casted to primitive types only",
818 })
819 }
820 };
821 Ok(view)
822 }
823 ColumnView::BigInt(booleans) => {
824 macro_rules! _cast_to {
825 ($ty:ty) => {
826 booleans.iter().map(|v| v.map(|b| b as $ty)).collect_vec()
827 };
828 }
829
830 let view = match ty {
831 Bool => {
832 Self::from_bools(booleans.iter().map(|v| v.map(|b| b > 0)).collect_vec())
833 }
834 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
835 SmallInt => Self::from_small_ints(_cast_to!(i16)),
836 Int => Self::from_ints(_cast_to!(i32)),
837 BigInt => Self::from_big_ints(_cast_to!(i64)),
838 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
839 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
840 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
841 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
842 Float => Self::from_floats(_cast_to!(f32)),
843 Double => Self::from_doubles(_cast_to!(f64)),
844 VarChar => Self::from_varchar::<String, _, _, _>(
845 booleans.iter().map(|v| v.map(|b| b.to_string())),
846 ),
847 NChar => Self::from_nchar::<String, _, _, _>(
848 booleans.iter().map(|v| v.map(|b| b.to_string())),
849 ),
850 _ => {
851 return Err(CastError {
852 from: l_ty,
853 to: ty,
854 message: "booleans can be casted to primitive types only",
855 })
856 }
857 };
858 Ok(view)
859 }
860
861 ColumnView::UTinyInt(booleans) => {
862 macro_rules! _cast_to {
863 ($ty:ty) => {
864 booleans.iter().map(|v| v.map(|b| b as $ty)).collect_vec()
865 };
866 }
867
868 let view = match ty {
869 Bool => {
870 Self::from_bools(booleans.iter().map(|v| v.map(|b| b > 0)).collect_vec())
871 }
872 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
873 SmallInt => Self::from_small_ints(_cast_to!(i16)),
874 Int => Self::from_ints(_cast_to!(i32)),
875 BigInt => Self::from_big_ints(_cast_to!(i64)),
876 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
877 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
878 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
879 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
880 Float => Self::from_floats(_cast_to!(f32)),
881 Double => Self::from_doubles(_cast_to!(f64)),
882 VarChar => Self::from_varchar::<String, _, _, _>(
883 booleans.iter().map(|v| v.map(|b| b.to_string())),
884 ),
885 NChar => Self::from_nchar::<String, _, _, _>(
886 booleans.iter().map(|v| v.map(|b| b.to_string())),
887 ),
888 _ => {
889 return Err(CastError {
890 from: l_ty,
891 to: ty,
892 message: "booleans can be casted to primitive types only",
893 })
894 }
895 };
896 Ok(view)
897 }
898
899 ColumnView::USmallInt(booleans) => {
900 macro_rules! _cast_to {
901 ($ty:ty) => {
902 booleans.iter().map(|v| v.map(|b| b as $ty)).collect_vec()
903 };
904 }
905
906 let view = match ty {
907 Bool => {
908 Self::from_bools(booleans.iter().map(|v| v.map(|b| b > 0)).collect_vec())
909 }
910 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
911 SmallInt => Self::from_small_ints(_cast_to!(i16)),
912 Int => Self::from_ints(_cast_to!(i32)),
913 BigInt => Self::from_big_ints(_cast_to!(i64)),
914 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
915 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
916 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
917 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
918 Float => Self::from_floats(_cast_to!(f32)),
919 Double => Self::from_doubles(_cast_to!(f64)),
920 VarChar => Self::from_varchar::<String, _, _, _>(
921 booleans.iter().map(|v| v.map(|b| b.to_string())),
922 ),
923 NChar => Self::from_nchar::<String, _, _, _>(
924 booleans.iter().map(|v| v.map(|b| b.to_string())),
925 ),
926 _ => {
927 return Err(CastError {
928 from: l_ty,
929 to: ty,
930 message: "booleans can be casted to primitive types only",
931 })
932 }
933 };
934 Ok(view)
935 }
936
937 ColumnView::UInt(booleans) => {
938 macro_rules! _cast_to {
939 ($ty:ty) => {
940 booleans.iter().map(|v| v.map(|b| b as $ty)).collect_vec()
941 };
942 }
943
944 let view = match ty {
945 Bool => {
946 Self::from_bools(booleans.iter().map(|v| v.map(|b| b > 0)).collect_vec())
947 }
948 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
949 SmallInt => Self::from_small_ints(_cast_to!(i16)),
950 Int => Self::from_ints(_cast_to!(i32)),
951 BigInt => Self::from_big_ints(_cast_to!(i64)),
952 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
953 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
954 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
955 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
956 Float => Self::from_floats(_cast_to!(f32)),
957 Double => Self::from_doubles(_cast_to!(f64)),
958 VarChar => Self::from_varchar::<String, _, _, _>(
959 booleans.iter().map(|v| v.map(|b| b.to_string())),
960 ),
961 NChar => Self::from_nchar::<String, _, _, _>(
962 booleans.iter().map(|v| v.map(|b| b.to_string())),
963 ),
964 _ => {
965 return Err(CastError {
966 from: l_ty,
967 to: ty,
968 message: "booleans can be casted to primitive types only",
969 })
970 }
971 };
972 Ok(view)
973 }
974 ColumnView::UBigInt(booleans) => {
975 macro_rules! _cast_to {
976 ($ty:ty) => {
977 booleans.iter().map(|v| v.map(|b| b as $ty)).collect_vec()
978 };
979 }
980
981 let view = match ty {
982 Bool => {
983 Self::from_bools(booleans.iter().map(|v| v.map(|b| b > 0)).collect_vec())
984 }
985 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
986 SmallInt => Self::from_small_ints(_cast_to!(i16)),
987 Int => Self::from_ints(_cast_to!(i32)),
988 BigInt => Self::from_big_ints(_cast_to!(i64)),
989 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
990 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
991 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
992 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
993 Float => Self::from_floats(_cast_to!(f32)),
994 Double => Self::from_doubles(_cast_to!(f64)),
995 VarChar => Self::from_varchar::<String, _, _, _>(
996 booleans.iter().map(|v| v.map(|b| b.to_string())),
997 ),
998 NChar => Self::from_nchar::<String, _, _, _>(
999 booleans.iter().map(|v| v.map(|b| b.to_string())),
1000 ),
1001 _ => {
1002 return Err(CastError {
1003 from: l_ty,
1004 to: ty,
1005 message: "booleans can be casted to primitive types only",
1006 })
1007 }
1008 };
1009 Ok(view)
1010 }
1011
1012 ColumnView::Float(view) => {
1013 macro_rules! _cast_to {
1014 ($ty:ty) => {
1015 view.iter().map(|v| v.map(|b| b as $ty)).collect_vec()
1016 };
1017 }
1018
1019 let view = match ty {
1020 Bool => Self::from_bools(view.iter().map(|v| v.map(|b| b > 0.0)).collect_vec()),
1021 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
1022 SmallInt => Self::from_small_ints(_cast_to!(i16)),
1023 Int => Self::from_ints(_cast_to!(i32)),
1024 BigInt => Self::from_big_ints(_cast_to!(i64)),
1025 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
1026 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
1027 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
1028 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
1029 Float => Self::from_floats(_cast_to!(f32)),
1030 Double => Self::from_doubles(_cast_to!(f64)),
1031 VarChar => Self::from_varchar::<String, _, _, _>(
1032 view.iter().map(|v| v.map(|b| b.to_string())),
1033 ),
1034 NChar => Self::from_nchar::<String, _, _, _>(
1035 view.iter().map(|v| v.map(|b| b.to_string())),
1036 ),
1037 _ => {
1038 return Err(CastError {
1039 from: l_ty,
1040 to: ty,
1041 message: "booleans can be casted to primitive types only",
1042 })
1043 }
1044 };
1045 Ok(view)
1046 }
1047 ColumnView::Double(view) => {
1048 macro_rules! _cast_to {
1049 ($ty:ty) => {
1050 view.iter().map(|v| v.map(|b| b as $ty)).collect_vec()
1051 };
1052 }
1053
1054 let view = match ty {
1055 Bool => Self::from_bools(view.iter().map(|v| v.map(|b| b > 0.0)).collect_vec()),
1056 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
1057 SmallInt => Self::from_small_ints(_cast_to!(i16)),
1058 Int => Self::from_ints(_cast_to!(i32)),
1059 BigInt => Self::from_big_ints(_cast_to!(i64)),
1060 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
1061 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
1062 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
1063 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
1064 Float => Self::from_floats(_cast_to!(f32)),
1065 Double => Self::from_doubles(_cast_to!(f64)),
1066 VarChar => Self::from_varchar::<String, _, _, _>(
1067 view.iter().map(|v| v.map(|b| b.to_string())),
1068 ),
1069 NChar => Self::from_nchar::<String, _, _, _>(
1070 view.iter().map(|v| v.map(|b| b.to_string())),
1071 ),
1072 _ => {
1073 return Err(CastError {
1074 from: l_ty,
1075 to: ty,
1076 message: "booleans can be casted to primitive types only",
1077 })
1078 }
1079 };
1080 Ok(view)
1081 }
1082 ColumnView::VarChar(view) => {
1083 macro_rules! _cast_to {
1084 ($ty:ty) => {
1085 view.iter()
1086 .map(|v| v.and_then(|b| b.as_str().parse::<$ty>().ok()))
1087 .collect_vec()
1088 };
1089 }
1090
1091 let view = match ty {
1092 Bool => Self::from_bools(_cast_to!(bool)),
1093 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
1094 SmallInt => Self::from_small_ints(_cast_to!(i16)),
1095 Int => Self::from_ints(_cast_to!(i32)),
1096 BigInt => Self::from_big_ints(_cast_to!(i64)),
1097 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
1098 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
1099 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
1100 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
1101 Float => Self::from_floats(_cast_to!(f32)),
1102 Double => Self::from_doubles(_cast_to!(f64)),
1103 VarChar => Self::from_varchar::<String, _, _, _>(
1104 view.iter().map(|v| v.map(|b| b.to_string())),
1105 ),
1106 NChar => Self::from_nchar::<String, _, _, _>(
1107 view.iter().map(|v| v.map(|b| b.to_string())),
1108 ),
1109 _ => {
1110 return Err(CastError {
1111 from: l_ty,
1112 to: ty,
1113 message: "booleans can be casted to primitive types only",
1114 })
1115 }
1116 };
1117 Ok(view)
1118 }
1119 ColumnView::NChar(view) => {
1120 macro_rules! _cast_to {
1121 ($ty:ty) => {
1122 view.iter()
1123 .map(|v| v.and_then(|b| b.parse::<$ty>().ok()))
1124 .collect_vec()
1125 };
1126 }
1127
1128 let view = match ty {
1129 Bool => Self::from_bools(_cast_to!(bool)),
1130 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
1131 SmallInt => Self::from_small_ints(_cast_to!(i16)),
1132 Int => Self::from_ints(_cast_to!(i32)),
1133 BigInt => Self::from_big_ints(_cast_to!(i64)),
1134 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
1135 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
1136 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
1137 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
1138 Float => Self::from_floats(_cast_to!(f32)),
1139 Double => Self::from_doubles(_cast_to!(f64)),
1140 VarChar => Self::from_varchar::<String, _, _, _>(
1141 view.iter().map(|v| v.map(|b| b.to_string())),
1142 ),
1143 NChar => Self::from_nchar::<String, _, _, _>(
1144 view.iter().map(|v| v.map(|b| b.to_string())),
1145 ),
1146 _ => {
1147 return Err(CastError {
1148 from: l_ty,
1149 to: ty,
1150 message: "can be casted to primitive types only",
1151 })
1152 }
1153 };
1154 Ok(view)
1155 }
1156 ColumnView::Timestamp(view) => {
1157 macro_rules! _cast_to {
1158 ($ty:ty) => {
1159 view.iter()
1160 .map(|v| v.map(|b| b.as_raw_i64() as $ty))
1161 .collect_vec()
1162 };
1163 }
1164
1165 let view = match ty {
1166 Bool => Self::from_bools(
1167 view.iter()
1168 .map(|v| v.map(|b| b.as_raw_i64() > 0))
1169 .collect_vec(),
1170 ),
1171 TinyInt => Self::from_tiny_ints(_cast_to!(i8)),
1172 SmallInt => Self::from_small_ints(_cast_to!(i16)),
1173 Int => Self::from_ints(_cast_to!(i32)),
1174 BigInt => Self::from_big_ints(_cast_to!(i64)),
1175 UTinyInt => Self::from_unsigned_tiny_ints(_cast_to!(u8)),
1176 USmallInt => Self::from_unsigned_small_ints(_cast_to!(u16)),
1177 UInt => Self::from_unsigned_ints(_cast_to!(u32)),
1178 UBigInt => Self::from_unsigned_big_ints(_cast_to!(u64)),
1179 Float => Self::from_floats(_cast_to!(f32)),
1180 Double => Self::from_doubles(_cast_to!(f64)),
1181 VarChar => Self::from_varchar::<String, _, _, _>(
1182 view.iter()
1183 .map(|v| v.map(|b| b.to_datetime_with_tz().to_rfc3339())),
1184 ),
1185 NChar => Self::from_nchar::<String, _, _, _>(
1186 view.iter()
1187 .map(|v| v.map(|b| b.to_datetime_with_tz().to_rfc3339())),
1188 ),
1189 _ => {
1190 return Err(CastError {
1191 from: l_ty,
1192 to: ty,
1193
1194 message: "",
1195 })
1196 }
1197 };
1198 Ok(view)
1199 }
1200 _ => todo!(),
1201 }
1202 }
1203
1204 pub fn cast_precision(&self, precision: Precision) -> ColumnView {
1205 match self {
1206 ColumnView::Timestamp(view) => ColumnView::Timestamp(view.cast_precision(precision)),
1207 _ => self.clone(),
1208 }
1209 }
1210 pub unsafe fn as_timestamp_view(&self) -> &TimestampView {
1211 match self {
1212 ColumnView::Timestamp(view) => view,
1213 _ => unreachable!(),
1214 }
1215 }
1216
1217 pub(crate) fn _to_nulls_vec(&self) -> Vec<bool> {
1218 (0..self.len())
1219 .map(|i| unsafe { self.is_null_unchecked(i) })
1220 .collect()
1221 }
1222}
1223
1224pub fn views_to_raw_block(views: &[ColumnView]) -> Vec<u8> {
1225 let header = super::Header {
1226 nrows: views.first().map(|v| v.len()).unwrap_or(0) as _,
1227 ncols: views.len() as _,
1228 ..Default::default()
1229 };
1230
1231 let ncols = views.len();
1232
1233 let mut bytes = Vec::new();
1234 bytes.extend(header.as_bytes());
1235
1236 let schemas = views
1237 .iter()
1238 .map(|view| {
1239 let ty = view.as_ty();
1240 ColSchema {
1241 ty,
1242 len: ty.fixed_length() as _,
1243 }
1244 })
1245 .collect_vec();
1246 let schema_bytes = unsafe {
1247 std::slice::from_raw_parts(
1248 schemas.as_ptr() as *const u8,
1249 ncols * std::mem::size_of::<ColSchema>(),
1250 )
1251 };
1252 bytes.write_all(schema_bytes).unwrap();
1253
1254 let length_offset = bytes.len();
1255 bytes.resize(bytes.len() + ncols * std::mem::size_of::<u32>(), 0);
1256
1257 let mut lengths = vec![0; ncols];
1258 for (i, view) in views.iter().enumerate() {
1259 let cur = bytes.len();
1260 let n = view.write_raw_into(&mut bytes).unwrap();
1261 let len = bytes.len();
1262 debug_assert!(cur + n == len);
1263 if !view.as_ty().is_primitive() {
1264 lengths[i] = (n - header.nrows() * 4) as _;
1265 } else {
1266 lengths[i] = (header.nrows() * view.as_ty().fixed_length()) as _;
1267 }
1268 }
1269 unsafe {
1270 (*(bytes.as_mut_ptr() as *mut super::Header)).length = bytes.len() as _;
1271 std::ptr::copy_nonoverlapping(
1272 lengths.as_ptr() as *mut u8,
1273 bytes.as_mut_ptr().add(length_offset) as *mut u8,
1274 lengths.len() * std::mem::size_of::<u32>(),
1275 );
1276 }
1277 bytes
1278}
1279
1280impl From<Value> for ColumnView {
1281 fn from(value: Value) -> Self {
1282 match value {
1283 Value::Null(ty) => ColumnView::null(1, ty),
1284 Value::Bool(v) => vec![v].into(),
1285 Value::TinyInt(v) => vec![v].into(),
1286 Value::SmallInt(v) => vec![v].into(),
1287 Value::Int(v) => vec![v].into(),
1288 Value::BigInt(v) => vec![v].into(),
1289 Value::Float(v) => vec![v].into(),
1290 Value::Double(v) => vec![v].into(),
1291 Value::VarChar(v) => ColumnView::from_varchar::<String, _, _, _>(vec![v]),
1292 Value::Timestamp(v) => ColumnView::Timestamp(TimestampView::from_timestamp(vec![v])),
1293 Value::NChar(v) => ColumnView::from_nchar::<String, _, _, _>(vec![v]),
1294 Value::UTinyInt(v) => vec![v].into(),
1295 Value::USmallInt(v) => vec![v].into(),
1296 Value::UInt(v) => vec![v].into(),
1297 Value::UBigInt(v) => vec![v].into(),
1298 _ => todo!(),
1299 }
1300 }
1301}
1302
1303#[cfg(test)]
1304mod tests {
1305 use super::*;
1306
1307 #[test]
1308 fn test_column_view_null() {
1309 let null_value = Value::Null(Ty::Int);
1310 let null_column_view = ColumnView::from(null_value);
1311 assert_eq!(null_column_view.len(), 1);
1312 assert_eq!(null_column_view.as_ty(), Ty::Int);
1313 }
1314
1315 #[test]
1316 fn test_column_view_bool() {
1317 let bool_value = Value::Bool(true);
1318 let bool_column_view = ColumnView::from(bool_value);
1319 assert_eq!(bool_column_view.len(), 1);
1320 assert_eq!(bool_column_view.as_ty(), Ty::Bool);
1321 assert_eq!(ColumnView::null(1, Ty::Bool).len(), 1);
1322 assert_eq!(bool_column_view.max_variable_length(), 1);
1323 println!("{:?}", bool_column_view.slice(0..1));
1324 println!("{:?}", bool_column_view.cast(Ty::TinyInt).unwrap());
1325 println!("{:?}", bool_column_view.cast(Ty::SmallInt).unwrap());
1326 println!("{:?}", bool_column_view.cast(Ty::Int).unwrap());
1327 println!("{:?}", bool_column_view.cast(Ty::BigInt).unwrap());
1328 println!("{:?}", bool_column_view.cast(Ty::UTinyInt).unwrap());
1329 println!("{:?}", bool_column_view.cast(Ty::USmallInt).unwrap());
1330 println!("{:?}", bool_column_view.cast(Ty::UInt).unwrap());
1331 println!("{:?}", bool_column_view.cast(Ty::UBigInt).unwrap());
1332 println!("{:?}", bool_column_view.cast(Ty::Float).unwrap());
1333 println!("{:?}", bool_column_view.cast(Ty::Double).unwrap());
1334 println!("{:?}", bool_column_view.cast(Ty::VarChar).unwrap());
1335 println!("{:?}", bool_column_view.cast(Ty::NChar).unwrap());
1336 }
1337
1338 #[test]
1339 fn test_concat_iter() {
1340 let column_view_int = ColumnView::from(vec![1, 2, 3]);
1341
1342 let iterator_values = vec![
1343 BorrowedValue::Int(7),
1344 BorrowedValue::UInt(8),
1345 BorrowedValue::Int(9),
1346 ];
1347
1348 let result_column_int =
1349 column_view_int.concat_iter(iterator_values.iter().cloned(), Ty::Int);
1350 assert_eq!(result_column_int.len(), 6);
1351
1352 let result_column_uint =
1353 column_view_int.concat_iter(iterator_values.iter().cloned(), Ty::UInt);
1354 assert_eq!(result_column_uint.len(), 6);
1355
1356 let result_column_bigint =
1357 column_view_int.concat_iter(iterator_values.iter().cloned(), Ty::BigInt);
1358 assert_eq!(result_column_bigint.len(), 6);
1359
1360 let result_column_ubigint =
1361 column_view_int.concat_iter(iterator_values.iter().cloned(), Ty::UBigInt);
1362 assert_eq!(result_column_ubigint.len(), 6);
1363
1364 let result_column_float =
1365 column_view_int.concat_iter(iterator_values.iter().cloned(), Ty::Float);
1366 assert_eq!(result_column_float.len(), 6);
1367
1368 let result_column_double =
1369 column_view_int.concat_iter(iterator_values.iter().cloned(), Ty::Double);
1370 assert_eq!(result_column_double.len(), 6);
1371
1372 let result_column_varchar =
1373 column_view_int.concat_iter(iterator_values.iter().cloned(), Ty::VarChar);
1374 assert_eq!(result_column_varchar.len(), 6);
1375 }
1376}