reifydb_routine/function/datetime/
age.rs1use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
5use reifydb_type::value::{container::temporal::TemporalContainer, date::Date, duration::Duration, r#type::Type};
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct DateTimeAge {
10 info: FunctionInfo,
11}
12
13impl Default for DateTimeAge {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl DateTimeAge {
20 pub fn new() -> Self {
21 Self {
22 info: FunctionInfo::new("datetime::age"),
23 }
24 }
25}
26
27impl Function for DateTimeAge {
28 fn info(&self) -> &FunctionInfo {
29 &self.info
30 }
31
32 fn capabilities(&self) -> &[FunctionCapability] {
33 &[FunctionCapability::Scalar]
34 }
35
36 fn return_type(&self, _input_types: &[Type]) -> Type {
37 Type::Duration
38 }
39
40 fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
41 if args.len() != 2 {
42 return Err(FunctionError::ArityMismatch {
43 function: ctx.fragment.clone(),
44 expected: 2,
45 actual: args.len(),
46 });
47 }
48
49 let col1 = &args[0];
50 let col2 = &args[1];
51 let (data1, bitvec1) = col1.data().unwrap_option();
52 let (data2, bitvec2) = col2.data().unwrap_option();
53 let row_count = data1.len();
54
55 let result_data = match (data1, data2) {
56 (ColumnData::DateTime(container1), ColumnData::DateTime(container2)) => {
57 let mut container = TemporalContainer::with_capacity(row_count);
58
59 for i in 0..row_count {
60 match (container1.get(i), container2.get(i)) {
61 (Some(dt1), Some(dt2)) => {
62 let nanos1 = dt1.time().to_nanos_since_midnight() as i64;
64 let nanos2 = dt2.time().to_nanos_since_midnight() as i64;
65 let mut nanos_diff = nanos1 - nanos2;
66 let mut days_borrow: i32 = 0;
67
68 if nanos_diff < 0 {
69 days_borrow = 1;
70 nanos_diff += 86_400_000_000_000;
71 }
72
73 let date1 = dt1.date();
75 let date2 = dt2.date();
76
77 let y1 = date1.year();
78 let m1 = date1.month() as i32;
79 let day1 = date1.day() as i32;
80
81 let y2 = date2.year();
82 let m2 = date2.month() as i32;
83 let day2 = date2.day() as i32;
84
85 let mut years = y1 - y2;
86 let mut months = m1 - m2;
87 let mut days = day1 - day2 - days_borrow;
88
89 if days < 0 {
90 months -= 1;
91 let borrow_month = if m1 - 1 < 1 {
92 12
93 } else {
94 m1 - 1
95 };
96 let borrow_year = if m1 - 1 < 1 {
97 y1 - 1
98 } else {
99 y1
100 };
101 days += Date::days_in_month(
102 borrow_year,
103 borrow_month as u32,
104 ) as i32;
105 }
106
107 if months < 0 {
108 years -= 1;
109 months += 12;
110 }
111
112 let total_months = years * 12 + months;
113 container.push(Duration::new(total_months, days, nanos_diff)?);
114 }
115 _ => container.push_default(),
116 }
117 }
118
119 ColumnData::Duration(container)
120 }
121 (ColumnData::DateTime(_), other) => {
122 return Err(FunctionError::InvalidArgumentType {
123 function: ctx.fragment.clone(),
124 argument_index: 1,
125 expected: vec![Type::DateTime],
126 actual: other.get_type(),
127 });
128 }
129 (other, _) => {
130 return Err(FunctionError::InvalidArgumentType {
131 function: ctx.fragment.clone(),
132 argument_index: 0,
133 expected: vec![Type::DateTime],
134 actual: other.get_type(),
135 });
136 }
137 };
138
139 let final_data = match (bitvec1, bitvec2) {
140 (Some(bv), _) | (_, Some(bv)) => ColumnData::Option {
141 inner: Box::new(result_data),
142 bitvec: bv.clone(),
143 },
144 _ => result_data,
145 };
146
147 Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
148 }
149}