1use std::{collections::HashSet, ffi::c_void};
2
3use odbc_sys::{CDataType, Date, Time, Timestamp};
4
5use crate::{
6 Bit, DataType, Error,
7 columnar_bulk_inserter::BoundInputSlice,
8 error::TooLargeBufferSize,
9 handles::{CData, CDataMut, HasDataType, StatementRef},
10};
11
12use super::{
13 BinColumn, BinColumnView, BufferDesc, CharColumn, ColumnarBuffer, Indicator, Item,
14 NullableSlice, NullableSliceMut, TextColumn, TextColumnView, WCharColumn,
15 bin_column::BinColumnSliceMut,
16 column_with_indicator::{
17 OptBitColumn, OptDateColumn, OptF32Column, OptF64Column, OptI8Column, OptI16Column,
18 OptI32Column, OptI64Column, OptTimeColumn, OptTimestampColumn, OptU8Column,
19 },
20 columnar::ColumnBuffer,
21 text_column::TextColumnSliceMut,
22};
23
24const DEFAULT_TIME_PRECISION: i16 = 7;
29
30#[derive(Debug)]
32pub enum AnyBuffer {
33 Binary(BinColumn),
35 Text(CharColumn),
38 WText(WCharColumn),
40 Date(Vec<Date>),
41 Time(Vec<Time>),
42 Timestamp(Vec<Timestamp>),
43 F64(Vec<f64>),
44 F32(Vec<f32>),
45 I8(Vec<i8>),
46 I16(Vec<i16>),
47 I32(Vec<i32>),
48 I64(Vec<i64>),
49 U8(Vec<u8>),
50 Bit(Vec<Bit>),
51 NullableDate(OptDateColumn),
52 NullableTime(OptTimeColumn),
53 NullableTimestamp(OptTimestampColumn),
54 NullableF64(OptF64Column),
55 NullableF32(OptF32Column),
56 NullableI8(OptI8Column),
57 NullableI16(OptI16Column),
58 NullableI32(OptI32Column),
59 NullableI64(OptI64Column),
60 NullableU8(OptU8Column),
61 NullableBit(OptBitColumn),
62}
63
64impl AnyBuffer {
65 pub fn try_from_desc(max_rows: usize, desc: BufferDesc) -> Result<Self, TooLargeBufferSize> {
67 let fallible_allocations = true;
68 Self::impl_from_desc(max_rows, desc, fallible_allocations)
69 }
70
71 pub fn from_desc(max_rows: usize, desc: BufferDesc) -> Self {
73 let fallible_allocations = false;
74 Self::impl_from_desc(max_rows, desc, fallible_allocations).unwrap()
75 }
76
77 fn impl_from_desc(
79 max_rows: usize,
80 desc: BufferDesc,
81 fallible_allocations: bool,
82 ) -> Result<Self, TooLargeBufferSize> {
83 let buffer = match desc {
84 BufferDesc::Binary { length } => {
85 if fallible_allocations {
86 AnyBuffer::Binary(BinColumn::try_new(max_rows, length)?)
87 } else {
88 AnyBuffer::Binary(BinColumn::new(max_rows, length))
89 }
90 }
91 BufferDesc::Text { max_str_len } => {
92 if fallible_allocations {
93 AnyBuffer::Text(TextColumn::try_new(max_rows, max_str_len)?)
94 } else {
95 AnyBuffer::Text(TextColumn::new(max_rows, max_str_len))
96 }
97 }
98 BufferDesc::WText { max_str_len } => {
99 if fallible_allocations {
100 AnyBuffer::WText(TextColumn::try_new(max_rows, max_str_len)?)
101 } else {
102 AnyBuffer::WText(TextColumn::new(max_rows, max_str_len))
103 }
104 }
105 BufferDesc::Date { nullable: false } => {
106 AnyBuffer::Date(vec![Date::default(); max_rows])
107 }
108 BufferDesc::Time { nullable: false } => {
109 AnyBuffer::Time(vec![Time::default(); max_rows])
110 }
111 BufferDesc::Timestamp { nullable: false } => {
112 AnyBuffer::Timestamp(vec![Timestamp::default(); max_rows])
113 }
114 BufferDesc::F64 { nullable: false } => AnyBuffer::F64(vec![f64::default(); max_rows]),
115 BufferDesc::F32 { nullable: false } => AnyBuffer::F32(vec![f32::default(); max_rows]),
116 BufferDesc::I8 { nullable: false } => AnyBuffer::I8(vec![i8::default(); max_rows]),
117 BufferDesc::I16 { nullable: false } => AnyBuffer::I16(vec![i16::default(); max_rows]),
118 BufferDesc::I32 { nullable: false } => AnyBuffer::I32(vec![i32::default(); max_rows]),
119 BufferDesc::I64 { nullable: false } => AnyBuffer::I64(vec![i64::default(); max_rows]),
120 BufferDesc::U8 { nullable: false } => AnyBuffer::U8(vec![u8::default(); max_rows]),
121 BufferDesc::Bit { nullable: false } => AnyBuffer::Bit(vec![Bit::default(); max_rows]),
122 BufferDesc::Date { nullable: true } => {
123 AnyBuffer::NullableDate(OptDateColumn::new(max_rows))
124 }
125 BufferDesc::Time { nullable: true } => {
126 AnyBuffer::NullableTime(OptTimeColumn::new(max_rows))
127 }
128 BufferDesc::Timestamp { nullable: true } => {
129 AnyBuffer::NullableTimestamp(OptTimestampColumn::new(max_rows))
130 }
131 BufferDesc::F64 { nullable: true } => {
132 AnyBuffer::NullableF64(OptF64Column::new(max_rows))
133 }
134 BufferDesc::F32 { nullable: true } => {
135 AnyBuffer::NullableF32(OptF32Column::new(max_rows))
136 }
137 BufferDesc::I8 { nullable: true } => AnyBuffer::NullableI8(OptI8Column::new(max_rows)),
138 BufferDesc::I16 { nullable: true } => {
139 AnyBuffer::NullableI16(OptI16Column::new(max_rows))
140 }
141 BufferDesc::I32 { nullable: true } => {
142 AnyBuffer::NullableI32(OptI32Column::new(max_rows))
143 }
144 BufferDesc::I64 { nullable: true } => {
145 AnyBuffer::NullableI64(OptI64Column::new(max_rows))
146 }
147 BufferDesc::U8 { nullable: true } => AnyBuffer::NullableU8(OptU8Column::new(max_rows)),
148 BufferDesc::Bit { nullable: true } => {
149 AnyBuffer::NullableBit(OptBitColumn::new(max_rows))
150 }
151 };
152 Ok(buffer)
153 }
154
155 fn fill_default_slice<T: Default + Copy>(col: &mut [T]) {
156 let element = T::default();
157 for item in col {
158 *item = element;
159 }
160 }
161
162 fn inner_cdata(&self) -> &dyn CData {
163 match self {
164 AnyBuffer::Binary(col) => col,
165 AnyBuffer::Text(col) => col,
166 AnyBuffer::WText(col) => col,
167 AnyBuffer::F64(col) => col,
168 AnyBuffer::F32(col) => col,
169 AnyBuffer::Date(col) => col,
170 AnyBuffer::Time(col) => col,
171 AnyBuffer::Timestamp(col) => col,
172 AnyBuffer::I8(col) => col,
173 AnyBuffer::I16(col) => col,
174 AnyBuffer::I32(col) => col,
175 AnyBuffer::I64(col) => col,
176 AnyBuffer::Bit(col) => col,
177 AnyBuffer::U8(col) => col,
178 AnyBuffer::NullableF64(col) => col,
179 AnyBuffer::NullableF32(col) => col,
180 AnyBuffer::NullableDate(col) => col,
181 AnyBuffer::NullableTime(col) => col,
182 AnyBuffer::NullableTimestamp(col) => col,
183 AnyBuffer::NullableI8(col) => col,
184 AnyBuffer::NullableI16(col) => col,
185 AnyBuffer::NullableI32(col) => col,
186 AnyBuffer::NullableI64(col) => col,
187 AnyBuffer::NullableBit(col) => col,
188 AnyBuffer::NullableU8(col) => col,
189 }
190 }
191
192 fn inner_cdata_mut(&mut self) -> &mut dyn CDataMut {
193 match self {
194 AnyBuffer::Binary(col) => col,
195 AnyBuffer::Text(col) => col,
196 AnyBuffer::WText(col) => col,
197 AnyBuffer::F64(col) => col,
198 AnyBuffer::F32(col) => col,
199 AnyBuffer::Date(col) => col,
200 AnyBuffer::Time(col) => col,
201 AnyBuffer::Timestamp(col) => col,
202 AnyBuffer::I8(col) => col,
203 AnyBuffer::I16(col) => col,
204 AnyBuffer::I32(col) => col,
205 AnyBuffer::I64(col) => col,
206 AnyBuffer::Bit(col) => col,
207 AnyBuffer::U8(col) => col,
208 AnyBuffer::NullableF64(col) => col,
209 AnyBuffer::NullableF32(col) => col,
210 AnyBuffer::NullableDate(col) => col,
211 AnyBuffer::NullableTime(col) => col,
212 AnyBuffer::NullableTimestamp(col) => col,
213 AnyBuffer::NullableI8(col) => col,
214 AnyBuffer::NullableI16(col) => col,
215 AnyBuffer::NullableI32(col) => col,
216 AnyBuffer::NullableI64(col) => col,
217 AnyBuffer::NullableBit(col) => col,
218 AnyBuffer::NullableU8(col) => col,
219 }
220 }
221}
222
223unsafe impl CData for AnyBuffer {
224 fn cdata_type(&self) -> CDataType {
225 self.inner_cdata().cdata_type()
226 }
227
228 fn indicator_ptr(&self) -> *const isize {
229 self.inner_cdata().indicator_ptr()
230 }
231
232 fn value_ptr(&self) -> *const c_void {
233 self.inner_cdata().value_ptr()
234 }
235
236 fn buffer_length(&self) -> isize {
237 self.inner_cdata().buffer_length()
238 }
239}
240
241unsafe impl CDataMut for AnyBuffer {
242 fn mut_indicator_ptr(&mut self) -> *mut isize {
243 self.inner_cdata_mut().mut_indicator_ptr()
244 }
245
246 fn mut_value_ptr(&mut self) -> *mut c_void {
247 self.inner_cdata_mut().mut_value_ptr()
248 }
249}
250
251impl HasDataType for AnyBuffer {
252 fn data_type(&self) -> DataType {
253 match self {
254 AnyBuffer::Binary(col) => col.data_type(),
255 AnyBuffer::Text(col) => col.data_type(),
256 AnyBuffer::WText(col) => col.data_type(),
257 AnyBuffer::Date(_) | AnyBuffer::NullableDate(_) => DataType::Date,
258 AnyBuffer::Time(_) | AnyBuffer::NullableTime(_) => DataType::Time {
259 precision: DEFAULT_TIME_PRECISION,
260 },
261 AnyBuffer::Timestamp(_) | AnyBuffer::NullableTimestamp(_) => DataType::Timestamp {
262 precision: DEFAULT_TIME_PRECISION,
263 },
264 AnyBuffer::F64(_) | AnyBuffer::NullableF64(_) => DataType::Double,
265 AnyBuffer::F32(_) | AnyBuffer::NullableF32(_) => DataType::Real,
266 AnyBuffer::I8(_) | AnyBuffer::NullableI8(_) => DataType::TinyInt,
267 AnyBuffer::I16(_) | AnyBuffer::NullableI16(_) => DataType::SmallInt,
268 AnyBuffer::I32(_) | AnyBuffer::NullableI32(_) => DataType::Integer,
269 AnyBuffer::I64(_) | AnyBuffer::NullableI64(_) => DataType::BigInt,
270 AnyBuffer::U8(_) | AnyBuffer::NullableU8(_) => DataType::SmallInt,
274 AnyBuffer::Bit(_) | AnyBuffer::NullableBit(_) => DataType::Bit,
275 }
276 }
277}
278
279pub type ColumnarAnyBuffer = ColumnarBuffer<AnyBuffer>;
282
283impl ColumnarAnyBuffer {
284 pub fn from_descs(capacity: usize, descs: impl IntoIterator<Item = BufferDesc>) -> Self {
286 let mut column_index = 0;
287 let columns = descs
288 .into_iter()
289 .map(move |desc| {
290 let buffer = AnyBuffer::from_desc(capacity, desc);
291 column_index += 1;
292 (column_index, buffer)
293 })
294 .collect();
295 unsafe { ColumnarBuffer::new_unchecked(capacity, columns) }
296 }
297
298 pub fn try_from_descs(
303 capacity: usize,
304 descs: impl IntoIterator<Item = BufferDesc>,
305 ) -> Result<Self, Error> {
306 let mut column_index = 0;
307 let columns = descs
308 .into_iter()
309 .map(move |desc| {
310 let buffer = AnyBuffer::try_from_desc(capacity, desc)
311 .map_err(|source| source.add_context(column_index))?;
312 column_index += 1;
313 Ok::<_, Error>((column_index, buffer))
314 })
315 .collect::<Result<_, _>>()?;
316 Ok(unsafe { ColumnarBuffer::new_unchecked(capacity, columns) })
317 }
318
319 pub fn from_descs_and_indices(
324 max_rows: usize,
325 description: impl Iterator<Item = (u16, BufferDesc)>,
326 ) -> ColumnarBuffer<AnyBuffer> {
327 let columns: Vec<_> = description
328 .map(|(col_index, buffer_desc)| {
329 (col_index, AnyBuffer::from_desc(max_rows, buffer_desc))
330 })
331 .collect();
332
333 let mut indices = HashSet::new();
335 if columns
336 .iter()
337 .any(move |&(col_index, _)| !indices.insert(col_index))
338 {
339 panic!("Column indices must be unique.")
340 }
341
342 ColumnarBuffer::new(columns)
343 }
344}
345
346#[derive(Debug, Clone, Copy)]
352pub enum AnySlice<'a> {
353 Text(TextColumnView<'a, u8>),
355 WText(TextColumnView<'a, u16>),
357 Binary(BinColumnView<'a>),
358 Date(&'a [Date]),
359 Time(&'a [Time]),
360 Timestamp(&'a [Timestamp]),
361 F64(&'a [f64]),
362 F32(&'a [f32]),
363 I8(&'a [i8]),
364 I16(&'a [i16]),
365 I32(&'a [i32]),
366 I64(&'a [i64]),
367 U8(&'a [u8]),
368 Bit(&'a [Bit]),
369 NullableDate(NullableSlice<'a, Date>),
370 NullableTime(NullableSlice<'a, Time>),
371 NullableTimestamp(NullableSlice<'a, Timestamp>),
372 NullableF64(NullableSlice<'a, f64>),
373 NullableF32(NullableSlice<'a, f32>),
374 NullableI8(NullableSlice<'a, i8>),
375 NullableI16(NullableSlice<'a, i16>),
376 NullableI32(NullableSlice<'a, i32>),
377 NullableI64(NullableSlice<'a, i64>),
378 NullableU8(NullableSlice<'a, u8>),
379 NullableBit(NullableSlice<'a, Bit>),
380}
381
382impl<'a> AnySlice<'a> {
383 pub fn as_text_view(self) -> Option<TextColumnView<'a, u8>> {
386 if let Self::Text(view) = self {
387 Some(view)
388 } else {
389 None
390 }
391 }
392
393 pub fn as_w_text_view(self) -> Option<TextColumnView<'a, u16>> {
396 if let Self::WText(view) = self {
397 Some(view)
398 } else {
399 None
400 }
401 }
402
403 pub fn as_bin_view(self) -> Option<BinColumnView<'a>> {
406 if let Self::Binary(view) = self {
407 Some(view)
408 } else {
409 None
410 }
411 }
412
413 pub fn as_slice<I: Item>(self) -> Option<&'a [I]> {
415 I::as_slice(self)
416 }
417
418 pub fn as_nullable_slice<I: Item>(self) -> Option<NullableSlice<'a, I>> {
420 I::as_nullable_slice(self)
421 }
422}
423
424unsafe impl<'a> BoundInputSlice<'a> for AnyBuffer {
425 type SliceMut = AnySliceMut<'a>;
426
427 unsafe fn as_view_mut(
428 &'a mut self,
429 parameter_index: u16,
430 stmt: StatementRef<'a>,
431 ) -> Self::SliceMut {
432 let num_rows = self.capacity();
433 unsafe {
434 match self {
435 AnyBuffer::Binary(column) => {
436 AnySliceMut::Binary(column.as_view_mut(parameter_index, stmt))
437 }
438 AnyBuffer::Text(column) => {
439 AnySliceMut::Text(column.as_view_mut(parameter_index, stmt))
440 }
441 AnyBuffer::WText(column) => {
442 AnySliceMut::WText(column.as_view_mut(parameter_index, stmt))
443 }
444 AnyBuffer::Date(column) => AnySliceMut::Date(column),
445 AnyBuffer::Time(column) => AnySliceMut::Time(column),
446 AnyBuffer::Timestamp(column) => AnySliceMut::Timestamp(column),
447 AnyBuffer::F64(column) => AnySliceMut::F64(column),
448 AnyBuffer::F32(column) => AnySliceMut::F32(column),
449 AnyBuffer::I8(column) => AnySliceMut::I8(column),
450 AnyBuffer::I16(column) => AnySliceMut::I16(column),
451 AnyBuffer::I32(column) => AnySliceMut::I32(column),
452 AnyBuffer::I64(column) => AnySliceMut::I64(column),
453 AnyBuffer::U8(column) => AnySliceMut::U8(column),
454 AnyBuffer::Bit(column) => AnySliceMut::Bit(column),
455 AnyBuffer::NullableDate(column) => {
456 AnySliceMut::NullableDate(column.writer_n(num_rows))
457 }
458 AnyBuffer::NullableTime(column) => {
459 AnySliceMut::NullableTime(column.writer_n(num_rows))
460 }
461 AnyBuffer::NullableTimestamp(column) => {
462 AnySliceMut::NullableTimestamp(column.writer_n(num_rows))
463 }
464 AnyBuffer::NullableF64(column) => {
465 AnySliceMut::NullableF64(column.writer_n(num_rows))
466 }
467 AnyBuffer::NullableF32(column) => {
468 AnySliceMut::NullableF32(column.writer_n(num_rows))
469 }
470 AnyBuffer::NullableI8(column) => AnySliceMut::NullableI8(column.writer_n(num_rows)),
471 AnyBuffer::NullableI16(column) => {
472 AnySliceMut::NullableI16(column.writer_n(num_rows))
473 }
474 AnyBuffer::NullableI32(column) => {
475 AnySliceMut::NullableI32(column.writer_n(num_rows))
476 }
477 AnyBuffer::NullableI64(column) => {
478 AnySliceMut::NullableI64(column.writer_n(num_rows))
479 }
480 AnyBuffer::NullableU8(column) => AnySliceMut::NullableU8(column.writer_n(num_rows)),
481 AnyBuffer::NullableBit(column) => {
482 AnySliceMut::NullableBit(column.writer_n(num_rows))
483 }
484 }
485 }
486 }
487}
488
489pub enum AnySliceMut<'a> {
492 Text(TextColumnSliceMut<'a, u8>),
493 WText(TextColumnSliceMut<'a, u16>),
495 Binary(BinColumnSliceMut<'a>),
496 Date(&'a mut [Date]),
497 Time(&'a mut [Time]),
498 Timestamp(&'a mut [Timestamp]),
499 F64(&'a mut [f64]),
500 F32(&'a mut [f32]),
501 I8(&'a mut [i8]),
502 I16(&'a mut [i16]),
503 I32(&'a mut [i32]),
504 I64(&'a mut [i64]),
505 U8(&'a mut [u8]),
506 Bit(&'a mut [Bit]),
507 NullableDate(NullableSliceMut<'a, Date>),
508 NullableTime(NullableSliceMut<'a, Time>),
509 NullableTimestamp(NullableSliceMut<'a, Timestamp>),
510 NullableF64(NullableSliceMut<'a, f64>),
511 NullableF32(NullableSliceMut<'a, f32>),
512 NullableI8(NullableSliceMut<'a, i8>),
513 NullableI16(NullableSliceMut<'a, i16>),
514 NullableI32(NullableSliceMut<'a, i32>),
515 NullableI64(NullableSliceMut<'a, i64>),
516 NullableU8(NullableSliceMut<'a, u8>),
517 NullableBit(NullableSliceMut<'a, Bit>),
518}
519
520impl<'a> AnySliceMut<'a> {
521 pub fn as_bin_view(self) -> Option<BinColumnSliceMut<'a>> {
524 if let Self::Binary(view) = self {
525 Some(view)
526 } else {
527 None
528 }
529 }
530
531 pub fn as_text_view(self) -> Option<TextColumnSliceMut<'a, u8>> {
534 if let Self::Text(view) = self {
535 Some(view)
536 } else {
537 None
538 }
539 }
540
541 pub fn as_w_text_view(self) -> Option<TextColumnSliceMut<'a, u16>> {
544 if let Self::WText(view) = self {
545 Some(view)
546 } else {
547 None
548 }
549 }
550
551 pub fn as_slice<I: Item>(self) -> Option<&'a mut [I]> {
553 I::as_slice_mut(self)
554 }
555
556 pub fn as_nullable_slice<I: Item>(self) -> Option<NullableSliceMut<'a, I>> {
558 I::as_nullable_slice_mut(self)
559 }
560}
561
562unsafe impl ColumnBuffer for AnyBuffer {
563 type View<'a> = AnySlice<'a>;
564
565 fn capacity(&self) -> usize {
566 match self {
567 AnyBuffer::Binary(col) => col.capacity(),
568 AnyBuffer::Text(col) => col.capacity(),
569 AnyBuffer::WText(col) => col.capacity(),
570 AnyBuffer::Date(col) => col.capacity(),
571 AnyBuffer::Time(col) => col.capacity(),
572 AnyBuffer::Timestamp(col) => col.capacity(),
573 AnyBuffer::F64(col) => col.capacity(),
574 AnyBuffer::F32(col) => col.capacity(),
575 AnyBuffer::I8(col) => col.capacity(),
576 AnyBuffer::I16(col) => col.capacity(),
577 AnyBuffer::I32(col) => col.capacity(),
578 AnyBuffer::I64(col) => col.capacity(),
579 AnyBuffer::U8(col) => col.capacity(),
580 AnyBuffer::Bit(col) => col.capacity(),
581 AnyBuffer::NullableDate(col) => col.capacity(),
582 AnyBuffer::NullableTime(col) => col.capacity(),
583 AnyBuffer::NullableTimestamp(col) => col.capacity(),
584 AnyBuffer::NullableF64(col) => col.capacity(),
585 AnyBuffer::NullableF32(col) => col.capacity(),
586 AnyBuffer::NullableI8(col) => col.capacity(),
587 AnyBuffer::NullableI16(col) => col.capacity(),
588 AnyBuffer::NullableI32(col) => col.capacity(),
589 AnyBuffer::NullableI64(col) => col.capacity(),
590 AnyBuffer::NullableU8(col) => col.capacity(),
591 AnyBuffer::NullableBit(col) => col.capacity(),
592 }
593 }
594
595 fn view(&self, valid_rows: usize) -> AnySlice {
596 match self {
597 AnyBuffer::Binary(col) => AnySlice::Binary(col.view(valid_rows)),
598 AnyBuffer::Text(col) => AnySlice::Text(col.view(valid_rows)),
599 AnyBuffer::WText(col) => AnySlice::WText(col.view(valid_rows)),
600 AnyBuffer::Date(col) => AnySlice::Date(&col[0..valid_rows]),
601 AnyBuffer::Time(col) => AnySlice::Time(&col[0..valid_rows]),
602 AnyBuffer::Timestamp(col) => AnySlice::Timestamp(&col[0..valid_rows]),
603 AnyBuffer::F64(col) => AnySlice::F64(&col[0..valid_rows]),
604 AnyBuffer::F32(col) => AnySlice::F32(&col[0..valid_rows]),
605 AnyBuffer::I8(col) => AnySlice::I8(&col[0..valid_rows]),
606 AnyBuffer::I16(col) => AnySlice::I16(&col[0..valid_rows]),
607 AnyBuffer::I32(col) => AnySlice::I32(&col[0..valid_rows]),
608 AnyBuffer::I64(col) => AnySlice::I64(&col[0..valid_rows]),
609 AnyBuffer::U8(col) => AnySlice::U8(&col[0..valid_rows]),
610 AnyBuffer::Bit(col) => AnySlice::Bit(&col[0..valid_rows]),
611 AnyBuffer::NullableDate(col) => AnySlice::NullableDate(col.iter(valid_rows)),
612 AnyBuffer::NullableTime(col) => AnySlice::NullableTime(col.iter(valid_rows)),
613 AnyBuffer::NullableTimestamp(col) => AnySlice::NullableTimestamp(col.iter(valid_rows)),
614 AnyBuffer::NullableF64(col) => AnySlice::NullableF64(col.iter(valid_rows)),
615 AnyBuffer::NullableF32(col) => AnySlice::NullableF32(col.iter(valid_rows)),
616 AnyBuffer::NullableI8(col) => AnySlice::NullableI8(col.iter(valid_rows)),
617 AnyBuffer::NullableI16(col) => AnySlice::NullableI16(col.iter(valid_rows)),
618 AnyBuffer::NullableI32(col) => AnySlice::NullableI32(col.iter(valid_rows)),
619 AnyBuffer::NullableI64(col) => AnySlice::NullableI64(col.iter(valid_rows)),
620 AnyBuffer::NullableU8(col) => AnySlice::NullableU8(col.iter(valid_rows)),
621 AnyBuffer::NullableBit(col) => AnySlice::NullableBit(col.iter(valid_rows)),
622 }
623 }
624
625 fn fill_default(&mut self, from: usize, to: usize) {
627 match self {
628 AnyBuffer::Binary(col) => col.fill_null(from, to),
629 AnyBuffer::Text(col) => col.fill_null(from, to),
630 AnyBuffer::WText(col) => col.fill_null(from, to),
631 AnyBuffer::Date(col) => Self::fill_default_slice(&mut col[from..to]),
632 AnyBuffer::Time(col) => Self::fill_default_slice(&mut col[from..to]),
633 AnyBuffer::Timestamp(col) => Self::fill_default_slice(&mut col[from..to]),
634 AnyBuffer::F64(col) => Self::fill_default_slice(&mut col[from..to]),
635 AnyBuffer::F32(col) => Self::fill_default_slice(&mut col[from..to]),
636 AnyBuffer::I8(col) => Self::fill_default_slice(&mut col[from..to]),
637 AnyBuffer::I16(col) => Self::fill_default_slice(&mut col[from..to]),
638 AnyBuffer::I32(col) => Self::fill_default_slice(&mut col[from..to]),
639 AnyBuffer::I64(col) => Self::fill_default_slice(&mut col[from..to]),
640 AnyBuffer::U8(col) => Self::fill_default_slice(&mut col[from..to]),
641 AnyBuffer::Bit(col) => Self::fill_default_slice(&mut col[from..to]),
642 AnyBuffer::NullableDate(col) => col.fill_null(from, to),
643 AnyBuffer::NullableTime(col) => col.fill_null(from, to),
644 AnyBuffer::NullableTimestamp(col) => col.fill_null(from, to),
645 AnyBuffer::NullableF64(col) => col.fill_null(from, to),
646 AnyBuffer::NullableF32(col) => col.fill_null(from, to),
647 AnyBuffer::NullableI8(col) => col.fill_null(from, to),
648 AnyBuffer::NullableI16(col) => col.fill_null(from, to),
649 AnyBuffer::NullableI32(col) => col.fill_null(from, to),
650 AnyBuffer::NullableI64(col) => col.fill_null(from, to),
651 AnyBuffer::NullableU8(col) => col.fill_null(from, to),
652 AnyBuffer::NullableBit(col) => col.fill_null(from, to),
653 }
654 }
655
656 fn has_truncated_values(&self, num_rows: usize) -> Option<Indicator> {
657 match self {
658 AnyBuffer::Binary(col) => col.has_truncated_values(num_rows),
659 AnyBuffer::Text(col) => col.has_truncated_values(num_rows),
660 AnyBuffer::WText(col) => col.has_truncated_values(num_rows),
661 _ => None,
662 }
663 }
664}
665
666#[cfg(test)]
667mod tests {
668 use crate::buffers::{AnySlice, AnySliceMut, ColumnBuffer};
669
670 use super::AnyBuffer;
671
672 #[test]
673 fn slice_should_only_contain_part_of_the_buffer() {
674 let buffer = AnyBuffer::I32(vec![1, 2, 3]);
675
676 let view = buffer.view(2);
677
678 assert_eq!(Some([1, 2].as_slice()), view.as_slice::<i32>());
679 }
680
681 #[test]
682 fn slice_should_be_none_if_types_mismatch() {
683 let buffer = [1, 2, 3];
684 let view = AnySlice::I32(&buffer);
685 assert_eq!(None, view.as_slice::<i16>());
686 }
687
688 #[test]
689 fn slice_mut_should_be_none_if_types_mismatch() {
690 let mut buffer = [1, 2, 3];
691 let view = AnySliceMut::I32(&mut buffer);
692 assert_eq!(None, view.as_slice::<i16>());
693 }
694
695 #[test]
696 fn nullable_slice_should_be_none_if_buffer_is_non_nullable() {
697 let buffer = [1, 2, 3];
698 let view = AnySlice::I32(&buffer);
699 assert!(view.as_nullable_slice::<i32>().is_none());
700 }
701
702 #[test]
703 fn nullable_slice_mut_should_be_none_if_buffer_is_non_nullable() {
704 let mut buffer = [1, 2, 3];
705 let view = AnySliceMut::I32(&mut buffer);
706 assert!(view.as_nullable_slice::<i32>().is_none());
707 }
708}