risingwave_common/array/
chrono_array.rs1use super::{PrimitiveArray, PrimitiveArrayBuilder};
16use crate::types::{Date, Time, Timestamp, Timestamptz};
17
18pub type DateArray = PrimitiveArray<Date>;
19pub type TimeArray = PrimitiveArray<Time>;
20pub type TimestampArray = PrimitiveArray<Timestamp>;
21pub type TimestamptzArray = PrimitiveArray<Timestamptz>;
22
23pub type DateArrayBuilder = PrimitiveArrayBuilder<Date>;
24pub type TimeArrayBuilder = PrimitiveArrayBuilder<Time>;
25pub type TimestampArrayBuilder = PrimitiveArrayBuilder<Timestamp>;
26pub type TimestamptzArrayBuilder = PrimitiveArrayBuilder<Timestamptz>;
27
28#[cfg(test)]
29mod tests {
30 use itertools::Itertools;
31
32 use super::*;
33 use crate::array::{Array, ArrayBuilder};
34 use crate::util::iter_util::ZipEqFast;
35
36 #[test]
37 fn test_date_builder() {
38 let v = (0..1000)
39 .map(Date::with_days_since_ce)
40 .map(|x| x.ok())
41 .collect_vec();
42 let mut builder = DateArrayBuilder::new(0);
43 for i in &v {
44 builder.append(*i);
45 }
46 let a = builder.finish();
47 let res = v.iter().zip_eq_fast(a.iter()).all(|(a, b)| *a == b);
48 assert!(res)
49 }
50
51 #[test]
52 fn test_date_array_to_protobuf() {
53 let input = vec![
54 Date::with_days_since_ce(12345).ok(),
55 None,
56 Date::with_days_since_ce(67890).ok(),
57 ];
58
59 let array = DateArray::from_iter(&input);
60 let buffers = array.to_protobuf().values;
61
62 assert_eq!(buffers.len(), 1);
63
64 let output_buffer = input.iter().fold(Vec::new(), |mut v, d| match d {
65 Some(d) => {
66 d.to_protobuf(&mut v).unwrap();
67 v
68 }
69 None => v,
70 });
71
72 assert_eq!(buffers[0].get_body(), &output_buffer);
73 }
74}