Skip to main content

reifydb_routine/function/datetime/
new.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2025 ReifyDB
3
4use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
5use reifydb_type::value::{container::temporal::TemporalContainer, datetime::DateTime, r#type::Type};
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct DateTimeNew {
10	info: FunctionInfo,
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: FunctionInfo::new("datetime::new"),
23		}
24	}
25}
26
27impl Function for DateTimeNew {
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::DateTime
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 date_col = &args[0];
50		let time_col = &args[1];
51		let (date_data, date_bitvec) = date_col.data().unwrap_option();
52		let (time_data, time_bitvec) = time_col.data().unwrap_option();
53		let row_count = date_data.len();
54
55		let result_data = match (date_data, time_data) {
56			(ColumnData::Date(date_container), ColumnData::Time(time_container)) => {
57				let mut container = TemporalContainer::with_capacity(row_count);
58
59				for i in 0..row_count {
60					match (date_container.get(i), time_container.get(i)) {
61						(Some(date), Some(time)) => {
62							match DateTime::new(
63								date.year(),
64								date.month(),
65								date.day(),
66								time.hour(),
67								time.minute(),
68								time.second(),
69								time.nanosecond(),
70							) {
71								Some(dt) => container.push(dt),
72								None => container.push_default(),
73							}
74						}
75						_ => container.push_default(),
76					}
77				}
78
79				ColumnData::DateTime(container)
80			}
81			(ColumnData::Date(_), other) => {
82				return Err(FunctionError::InvalidArgumentType {
83					function: ctx.fragment.clone(),
84					argument_index: 1,
85					expected: vec![Type::Time],
86					actual: other.get_type(),
87				});
88			}
89			(other, _) => {
90				return Err(FunctionError::InvalidArgumentType {
91					function: ctx.fragment.clone(),
92					argument_index: 0,
93					expected: vec![Type::Date],
94					actual: other.get_type(),
95				});
96			}
97		};
98
99		let final_data = match (date_bitvec, time_bitvec) {
100			(Some(bv), _) | (_, Some(bv)) => ColumnData::Option {
101				inner: Box::new(result_data),
102				bitvec: bv.clone(),
103			},
104			_ => result_data,
105		};
106
107		Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
108	}
109}