clickhouse_native_client/column/
date.rs1use super::{
32 Column,
33 ColumnRef,
34};
35use crate::{
36 types::Type,
37 Error,
38 Result,
39};
40use bytes::BytesMut;
41use std::sync::Arc;
42
43const SECONDS_PER_DAY: i64 = 86400;
44
45pub struct ColumnDate {
56 type_: Type,
57 data: Arc<super::ColumnUInt16>, }
60
61impl ColumnDate {
62 pub fn new(type_: Type) -> Self {
64 Self { type_, data: Arc::new(super::ColumnUInt16::new()) }
65 }
66
67 pub fn with_data(mut self, data: Vec<u16>) -> Self {
69 self.data =
70 Arc::new(super::ColumnUInt16::from_vec(Type::uint16(), data));
71 self
72 }
73
74 pub fn append(&mut self, days: u16) {
76 Arc::get_mut(&mut self.data)
77 .expect("Cannot append to shared column")
78 .append(days);
79 }
80
81 pub fn append_timestamp(&mut self, timestamp: i64) {
83 let days = (timestamp / SECONDS_PER_DAY) as u16;
84 self.append(days);
85 }
86
87 pub fn at(&self, index: usize) -> u16 {
89 self.data.at(index)
90 }
91
92 pub fn timestamp_at(&self, index: usize) -> i64 {
94 self.data.at(index) as i64 * SECONDS_PER_DAY
95 }
96
97 pub fn len(&self) -> usize {
99 self.data.len()
100 }
101
102 pub fn is_empty(&self) -> bool {
104 self.data.is_empty()
105 }
106
107 pub fn data(&self) -> &super::ColumnUInt16 {
109 &self.data
110 }
111}
112
113impl Column for ColumnDate {
114 fn column_type(&self) -> &Type {
115 &self.type_
116 }
117
118 fn size(&self) -> usize {
119 self.data.size()
120 }
121
122 fn clear(&mut self) {
123 Arc::get_mut(&mut self.data)
124 .expect("Cannot clear shared column")
125 .clear();
126 }
127
128 fn reserve(&mut self, new_cap: usize) {
129 Arc::get_mut(&mut self.data)
130 .expect("Cannot reserve on shared column")
131 .reserve(new_cap);
132 }
133
134 fn append_column(&mut self, other: ColumnRef) -> Result<()> {
135 let other =
136 other.as_any().downcast_ref::<ColumnDate>().ok_or_else(|| {
137 Error::TypeMismatch {
138 expected: self.type_.name(),
139 actual: other.column_type().name(),
140 }
141 })?;
142
143 Arc::get_mut(&mut self.data)
145 .expect("Cannot append to shared column")
146 .append_column(other.data.clone() as ColumnRef)?;
147 Ok(())
148 }
149
150 fn load_from_buffer(
151 &mut self,
152 buffer: &mut &[u8],
153 rows: usize,
154 ) -> Result<()> {
155 Arc::get_mut(&mut self.data)
157 .expect("Cannot load into shared column")
158 .load_from_buffer(buffer, rows)
159 }
160
161 fn save_to_buffer(&self, buffer: &mut BytesMut) -> Result<()> {
162 self.data.save_to_buffer(buffer)
164 }
165
166 fn clone_empty(&self) -> ColumnRef {
167 Arc::new(ColumnDate::new(self.type_.clone()))
168 }
169
170 fn slice(&self, begin: usize, len: usize) -> Result<ColumnRef> {
171 let sliced_data = self.data.slice(begin, len)?;
173
174 Ok(Arc::new(ColumnDate {
175 type_: self.type_.clone(),
176 data: sliced_data
177 .as_any()
178 .downcast_ref::<super::ColumnUInt16>()
179 .map(|col| {
180 Arc::new(super::ColumnUInt16::from_vec(
182 Type::uint16(),
183 col.data().to_vec(),
184 ))
185 })
186 .expect("Slice should return ColumnUInt16"),
187 }))
188 }
189
190 fn as_any(&self) -> &dyn std::any::Any {
191 self
192 }
193
194 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
195 self
196 }
197}
198
199pub struct ColumnDate32 {
207 type_: Type,
208 data: Arc<super::ColumnInt32>, }
211
212impl ColumnDate32 {
213 pub fn new(type_: Type) -> Self {
215 Self { type_, data: Arc::new(super::ColumnInt32::new()) }
216 }
217
218 pub fn with_data(mut self, data: Vec<i32>) -> Self {
220 self.data =
221 Arc::new(super::ColumnInt32::from_vec(Type::int32(), data));
222 self
223 }
224
225 pub fn append(&mut self, days: i32) {
227 Arc::get_mut(&mut self.data)
228 .expect("Cannot append to shared column")
229 .append(days);
230 }
231
232 pub fn append_timestamp(&mut self, timestamp: i64) {
234 let days = (timestamp / SECONDS_PER_DAY) as i32;
235 self.append(days);
236 }
237
238 pub fn at(&self, index: usize) -> i32 {
240 self.data.at(index)
241 }
242
243 pub fn timestamp_at(&self, index: usize) -> i64 {
245 self.data.at(index) as i64 * SECONDS_PER_DAY
246 }
247
248 pub fn len(&self) -> usize {
250 self.data.len()
251 }
252
253 pub fn is_empty(&self) -> bool {
255 self.data.is_empty()
256 }
257
258 pub fn data(&self) -> &super::ColumnInt32 {
260 &self.data
261 }
262}
263
264impl Column for ColumnDate32 {
265 fn column_type(&self) -> &Type {
266 &self.type_
267 }
268
269 fn size(&self) -> usize {
270 self.data.size()
271 }
272
273 fn clear(&mut self) {
274 Arc::get_mut(&mut self.data)
275 .expect("Cannot clear shared column")
276 .clear();
277 }
278
279 fn reserve(&mut self, new_cap: usize) {
280 Arc::get_mut(&mut self.data)
281 .expect("Cannot reserve on shared column")
282 .reserve(new_cap);
283 }
284
285 fn append_column(&mut self, other: ColumnRef) -> Result<()> {
286 let other = other.as_any().downcast_ref::<ColumnDate32>().ok_or_else(
287 || Error::TypeMismatch {
288 expected: self.type_.name(),
289 actual: other.column_type().name(),
290 },
291 )?;
292
293 Arc::get_mut(&mut self.data)
295 .expect("Cannot append to shared column")
296 .append_column(other.data.clone() as ColumnRef)?;
297 Ok(())
298 }
299
300 fn load_from_buffer(
301 &mut self,
302 buffer: &mut &[u8],
303 rows: usize,
304 ) -> Result<()> {
305 Arc::get_mut(&mut self.data)
307 .expect("Cannot load into shared column")
308 .load_from_buffer(buffer, rows)
309 }
310
311 fn save_to_buffer(&self, buffer: &mut BytesMut) -> Result<()> {
312 self.data.save_to_buffer(buffer)
314 }
315
316 fn clone_empty(&self) -> ColumnRef {
317 Arc::new(ColumnDate32::new(self.type_.clone()))
318 }
319
320 fn slice(&self, begin: usize, len: usize) -> Result<ColumnRef> {
321 let sliced_data = self.data.slice(begin, len)?;
323
324 Ok(Arc::new(ColumnDate32 {
325 type_: self.type_.clone(),
326 data: sliced_data
327 .as_any()
328 .downcast_ref::<super::ColumnInt32>()
329 .map(|col| {
330 Arc::new(super::ColumnInt32::from_vec(
332 Type::int32(),
333 col.data().to_vec(),
334 ))
335 })
336 .expect("Slice should return ColumnInt32"),
337 }))
338 }
339
340 fn as_any(&self) -> &dyn std::any::Any {
341 self
342 }
343
344 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
345 self
346 }
347}
348
349pub struct ColumnDateTime {
357 type_: Type,
358 data: Arc<super::ColumnUInt32>, timezone: Option<String>,
361}
362
363impl ColumnDateTime {
364 pub fn new(type_: Type) -> Self {
366 let timezone = match &type_ {
367 Type::DateTime { timezone } => timezone.clone(),
368 _ => None,
369 };
370
371 Self { type_, data: Arc::new(super::ColumnUInt32::new()), timezone }
372 }
373
374 pub fn with_data(mut self, data: Vec<u32>) -> Self {
376 self.data =
377 Arc::new(super::ColumnUInt32::from_vec(Type::uint32(), data));
378 self
379 }
380
381 pub fn append(&mut self, timestamp: u32) {
383 Arc::get_mut(&mut self.data)
384 .expect("Cannot append to shared column")
385 .append(timestamp);
386 }
387
388 pub fn at(&self, index: usize) -> u32 {
390 self.data.at(index)
391 }
392
393 pub fn timezone(&self) -> Option<&str> {
395 self.timezone.as_deref()
396 }
397
398 pub fn len(&self) -> usize {
400 self.data.len()
401 }
402
403 pub fn is_empty(&self) -> bool {
405 self.data.is_empty()
406 }
407
408 pub fn data(&self) -> &super::ColumnUInt32 {
410 &self.data
411 }
412}
413
414impl Column for ColumnDateTime {
415 fn column_type(&self) -> &Type {
416 &self.type_
417 }
418
419 fn size(&self) -> usize {
420 self.data.size()
421 }
422
423 fn clear(&mut self) {
424 Arc::get_mut(&mut self.data)
425 .expect("Cannot clear shared column")
426 .clear();
427 }
428
429 fn reserve(&mut self, new_cap: usize) {
430 Arc::get_mut(&mut self.data)
431 .expect("Cannot reserve on shared column")
432 .reserve(new_cap);
433 }
434
435 fn append_column(&mut self, other: ColumnRef) -> Result<()> {
436 let other = other
437 .as_any()
438 .downcast_ref::<ColumnDateTime>()
439 .ok_or_else(|| Error::TypeMismatch {
440 expected: self.type_.name(),
441 actual: other.column_type().name(),
442 })?;
443
444 Arc::get_mut(&mut self.data)
446 .expect("Cannot append to shared column")
447 .append_column(other.data.clone() as ColumnRef)?;
448 Ok(())
449 }
450
451 fn load_from_buffer(
452 &mut self,
453 buffer: &mut &[u8],
454 rows: usize,
455 ) -> Result<()> {
456 Arc::get_mut(&mut self.data)
458 .expect("Cannot load into shared column")
459 .load_from_buffer(buffer, rows)
460 }
461
462 fn save_to_buffer(&self, buffer: &mut BytesMut) -> Result<()> {
463 self.data.save_to_buffer(buffer)
465 }
466
467 fn clone_empty(&self) -> ColumnRef {
468 Arc::new(ColumnDateTime::new(self.type_.clone()))
469 }
470
471 fn slice(&self, begin: usize, len: usize) -> Result<ColumnRef> {
472 let sliced_data = self.data.slice(begin, len)?;
474
475 Ok(Arc::new(ColumnDateTime {
476 type_: self.type_.clone(),
477 timezone: self.timezone.clone(),
478 data: sliced_data
479 .as_any()
480 .downcast_ref::<super::ColumnUInt32>()
481 .map(|col| {
482 Arc::new(super::ColumnUInt32::from_vec(
484 Type::uint32(),
485 col.data().to_vec(),
486 ))
487 })
488 .expect("Slice should return ColumnUInt32"),
489 }))
490 }
491
492 fn as_any(&self) -> &dyn std::any::Any {
493 self
494 }
495
496 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
497 self
498 }
499}
500
501pub struct ColumnDateTime64 {
508 type_: Type,
509 data: Arc<super::ColumnInt64>, precision: usize,
511 timezone: Option<String>,
512}
513
514impl ColumnDateTime64 {
515 pub fn new(type_: Type) -> Self {
518 let (precision, timezone) = match &type_ {
519 Type::DateTime64 { precision, timezone } => {
520 (*precision, timezone.clone())
521 }
522 _ => panic!("ColumnDateTime64 requires DateTime64 type"),
523 };
524
525 Self {
526 type_,
527 data: Arc::new(super::ColumnInt64::new()),
528 precision,
529 timezone,
530 }
531 }
532
533 pub fn with_data(mut self, data: Vec<i64>) -> Self {
536 self.data =
537 Arc::new(super::ColumnInt64::from_vec(Type::int64(), data));
538 self
539 }
540
541 pub fn append(&mut self, value: i64) {
544 Arc::get_mut(&mut self.data)
545 .expect("Cannot append to shared column")
546 .append(value);
547 }
548
549 pub fn at(&self, index: usize) -> i64 {
551 self.data.at(index)
552 }
553
554 pub fn precision(&self) -> usize {
556 self.precision
557 }
558
559 pub fn timezone(&self) -> Option<&str> {
561 self.timezone.as_deref()
562 }
563
564 pub fn len(&self) -> usize {
566 self.data.len()
567 }
568
569 pub fn is_empty(&self) -> bool {
571 self.data.is_empty()
572 }
573
574 pub fn data(&self) -> &super::ColumnInt64 {
576 &self.data
577 }
578}
579
580impl Column for ColumnDateTime64 {
581 fn column_type(&self) -> &Type {
582 &self.type_
583 }
584
585 fn size(&self) -> usize {
586 self.data.size()
587 }
588
589 fn clear(&mut self) {
590 Arc::get_mut(&mut self.data)
591 .expect("Cannot clear shared column")
592 .clear();
593 }
594
595 fn reserve(&mut self, new_cap: usize) {
596 Arc::get_mut(&mut self.data)
597 .expect("Cannot reserve on shared column")
598 .reserve(new_cap);
599 }
600
601 fn append_column(&mut self, other: ColumnRef) -> Result<()> {
602 let other = other
603 .as_any()
604 .downcast_ref::<ColumnDateTime64>()
605 .ok_or_else(|| Error::TypeMismatch {
606 expected: self.type_.name(),
607 actual: other.column_type().name(),
608 })?;
609
610 if self.precision != other.precision {
611 return Err(Error::TypeMismatch {
612 expected: format!("DateTime64({})", self.precision),
613 actual: format!("DateTime64({})", other.precision),
614 });
615 }
616
617 Arc::get_mut(&mut self.data)
619 .expect("Cannot append to shared column")
620 .append_column(other.data.clone() as ColumnRef)?;
621 Ok(())
622 }
623
624 fn load_from_buffer(
625 &mut self,
626 buffer: &mut &[u8],
627 rows: usize,
628 ) -> Result<()> {
629 Arc::get_mut(&mut self.data)
631 .expect("Cannot load into shared column")
632 .load_from_buffer(buffer, rows)
633 }
634
635 fn save_to_buffer(&self, buffer: &mut BytesMut) -> Result<()> {
636 self.data.save_to_buffer(buffer)
638 }
639
640 fn clone_empty(&self) -> ColumnRef {
641 Arc::new(ColumnDateTime64::new(self.type_.clone()))
642 }
643
644 fn slice(&self, begin: usize, len: usize) -> Result<ColumnRef> {
645 let sliced_data = self.data.slice(begin, len)?;
647
648 Ok(Arc::new(ColumnDateTime64 {
649 type_: self.type_.clone(),
650 precision: self.precision,
651 timezone: self.timezone.clone(),
652 data: sliced_data
653 .as_any()
654 .downcast_ref::<super::ColumnInt64>()
655 .map(|col| {
656 Arc::new(super::ColumnInt64::from_vec(
658 Type::int64(),
659 col.data().to_vec(),
660 ))
661 })
662 .expect("Slice should return ColumnInt64"),
663 }))
664 }
665
666 fn as_any(&self) -> &dyn std::any::Any {
667 self
668 }
669
670 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
671 self
672 }
673}
674
675#[cfg(test)]
676#[cfg_attr(coverage_nightly, coverage(off))]
677mod tests {
678 use super::*;
679
680 #[test]
681 fn test_date_append_and_retrieve() {
682 let mut col = ColumnDate::new(Type::date());
683 col.append(19000); col.append(19001);
685
686 assert_eq!(col.len(), 2);
687 assert_eq!(col.at(0), 19000);
688 assert_eq!(col.at(1), 19001);
689 }
690
691 #[test]
692 fn test_date_timestamp() {
693 let mut col = ColumnDate::new(Type::date());
694 col.append_timestamp(1640995200); assert_eq!(col.len(), 1);
697 let days = col.at(0);
698 assert_eq!(days, 18993);
699 }
700
701 #[test]
702 fn test_date32() {
703 let mut col = ColumnDate32::new(Type::date32());
704 col.append(-25567); col.append(0); col.append(100000); assert_eq!(col.len(), 3);
709 assert_eq!(col.at(0), -25567);
710 assert_eq!(col.at(1), 0);
711 assert_eq!(col.at(2), 100000);
712 }
713
714 #[test]
715 fn test_datetime() {
716 let mut col = ColumnDateTime::new(Type::datetime(None));
717 col.append(1640995200); col.append(1640995201);
719
720 assert_eq!(col.len(), 2);
721 assert_eq!(col.at(0), 1640995200);
722 assert_eq!(col.at(1), 1640995201);
723 }
724
725 #[test]
726 fn test_datetime_with_timezone() {
727 let mut col =
728 ColumnDateTime::new(Type::datetime(Some("UTC".to_string())));
729 col.append(1640995200);
730
731 assert_eq!(col.timezone(), Some("UTC"));
732 assert_eq!(col.at(0), 1640995200);
733 }
734
735 #[test]
736 fn test_datetime64() {
737 let mut col = ColumnDateTime64::new(Type::datetime64(3, None)); col.append(1640995200000); col.append(1640995200123);
740
741 assert_eq!(col.len(), 2);
742 assert_eq!(col.precision(), 3);
743 assert_eq!(col.at(0), 1640995200000);
744 assert_eq!(col.at(1), 1640995200123);
745 }
746}