reifydb_routine/function/datetime/
new.rs1use reifydb_core::value::column::{ColumnWithName, buffer::ColumnBuffer, columns::Columns};
5use reifydb_type::value::{container::temporal::TemporalContainer, datetime::DateTime, r#type::Type};
6
7use crate::routine::{Function, FunctionKind, Routine, RoutineInfo, context::FunctionContext, error::RoutineError};
8
9pub struct DateTimeNew {
10 info: RoutineInfo,
11}
12
13impl Default for DateTimeNew {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl DateTimeNew {
20 pub fn new() -> Self {
21 Self {
22 info: RoutineInfo::new("datetime::new"),
23 }
24 }
25}
26
27impl<'a> Routine<FunctionContext<'a>> for DateTimeNew {
28 fn info(&self) -> &RoutineInfo {
29 &self.info
30 }
31
32 fn return_type(&self, _input_types: &[Type]) -> Type {
33 Type::DateTime
34 }
35
36 fn execute(&self, ctx: &mut FunctionContext<'a>, args: &Columns) -> Result<Columns, RoutineError> {
37 if args.len() != 2 {
38 return Err(RoutineError::FunctionArityMismatch {
39 function: ctx.fragment.clone(),
40 expected: 2,
41 actual: args.len(),
42 });
43 }
44
45 let date_col = &args[0];
46 let time_col = &args[1];
47 let (date_data, date_bitvec) = date_col.unwrap_option();
48 let (time_data, time_bitvec) = time_col.unwrap_option();
49 let row_count = date_data.len();
50
51 let result_data = match (date_data, time_data) {
52 (ColumnBuffer::Date(date_container), ColumnBuffer::Time(time_container)) => {
53 let mut container = TemporalContainer::with_capacity(row_count);
54
55 for i in 0..row_count {
56 match (date_container.get(i), time_container.get(i)) {
57 (Some(date), Some(time)) => {
58 match DateTime::new(
59 date.year(),
60 date.month(),
61 date.day(),
62 time.hour(),
63 time.minute(),
64 time.second(),
65 time.nanosecond(),
66 ) {
67 Some(dt) => container.push(dt),
68 None => container.push_default(),
69 }
70 }
71 _ => container.push_default(),
72 }
73 }
74
75 ColumnBuffer::DateTime(container)
76 }
77 (ColumnBuffer::Date(_), other) => {
78 return Err(RoutineError::FunctionInvalidArgumentType {
79 function: ctx.fragment.clone(),
80 argument_index: 1,
81 expected: vec![Type::Time],
82 actual: other.get_type(),
83 });
84 }
85 (other, _) => {
86 return Err(RoutineError::FunctionInvalidArgumentType {
87 function: ctx.fragment.clone(),
88 argument_index: 0,
89 expected: vec![Type::Date],
90 actual: other.get_type(),
91 });
92 }
93 };
94
95 let final_data = match (date_bitvec, time_bitvec) {
96 (Some(bv), _) | (_, Some(bv)) => ColumnBuffer::Option {
97 inner: Box::new(result_data),
98 bitvec: bv.clone(),
99 },
100 _ => result_data,
101 };
102
103 Ok(Columns::new(vec![ColumnWithName::new(ctx.fragment.clone(), final_data)]))
104 }
105}
106
107impl Function for DateTimeNew {
108 fn kinds(&self) -> &[FunctionKind] {
109 &[FunctionKind::Scalar]
110 }
111}