Skip to main content

reifydb_routine/function/time/
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, time::Time, r#type::Type};
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct TimeNew {
10	info: FunctionInfo,
11}
12
13impl Default for TimeNew {
14	fn default() -> Self {
15		Self::new()
16	}
17}
18
19impl TimeNew {
20	pub fn new() -> Self {
21		Self {
22			info: FunctionInfo::new("time::new"),
23		}
24	}
25}
26
27fn extract_i32(data: &ColumnData, i: usize) -> Option<i32> {
28	match data {
29		ColumnData::Int1(c) => c.get(i).map(|&v| v as i32),
30		ColumnData::Int2(c) => c.get(i).map(|&v| v as i32),
31		ColumnData::Int4(c) => c.get(i).copied(),
32		ColumnData::Int8(c) => c.get(i).map(|&v| v as i32),
33		ColumnData::Int16(c) => c.get(i).map(|&v| v as i32),
34		ColumnData::Uint1(c) => c.get(i).map(|&v| v as i32),
35		ColumnData::Uint2(c) => c.get(i).map(|&v| v as i32),
36		ColumnData::Uint4(c) => c.get(i).map(|&v| v as i32),
37		ColumnData::Uint8(c) => c.get(i).map(|&v| v as i32),
38		ColumnData::Uint16(c) => c.get(i).map(|&v| v as i32),
39		_ => None,
40	}
41}
42
43fn is_integer_type(data: &ColumnData) -> bool {
44	matches!(
45		data,
46		ColumnData::Int1(_)
47			| ColumnData::Int2(_) | ColumnData::Int4(_)
48			| ColumnData::Int8(_) | ColumnData::Int16(_)
49			| ColumnData::Uint1(_)
50			| ColumnData::Uint2(_)
51			| ColumnData::Uint4(_)
52			| ColumnData::Uint8(_)
53			| ColumnData::Uint16(_)
54	)
55}
56
57impl Function for TimeNew {
58	fn info(&self) -> &FunctionInfo {
59		&self.info
60	}
61
62	fn capabilities(&self) -> &[FunctionCapability] {
63		&[FunctionCapability::Scalar]
64	}
65
66	fn return_type(&self, _input_types: &[Type]) -> Type {
67		Type::Time
68	}
69
70	fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
71		if args.len() != 3 && args.len() != 4 {
72			return Err(FunctionError::ArityMismatch {
73				function: ctx.fragment.clone(),
74				expected: 3,
75				actual: args.len(),
76			});
77		}
78
79		let hour_col = &args[0];
80		let min_col = &args[1];
81		let sec_col = &args[2];
82		let nano_col = if args.len() == 4 {
83			Some(&args[3])
84		} else {
85			None
86		};
87
88		let (hour_data, _) = hour_col.data().unwrap_option();
89		let (min_data, _) = min_col.data().unwrap_option();
90		let (sec_data, _) = sec_col.data().unwrap_option();
91		let nano_data = nano_col.map(|c| c.data().unwrap_option());
92
93		if !is_integer_type(hour_data) {
94			return Err(FunctionError::InvalidArgumentType {
95				function: ctx.fragment.clone(),
96				argument_index: 0,
97				expected: vec![
98					Type::Int1,
99					Type::Int2,
100					Type::Int4,
101					Type::Int8,
102					Type::Int16,
103					Type::Uint1,
104					Type::Uint2,
105					Type::Uint4,
106					Type::Uint8,
107					Type::Uint16,
108				],
109				actual: hour_data.get_type(),
110			});
111		}
112		if !is_integer_type(min_data) {
113			return Err(FunctionError::InvalidArgumentType {
114				function: ctx.fragment.clone(),
115				argument_index: 1,
116				expected: vec![
117					Type::Int1,
118					Type::Int2,
119					Type::Int4,
120					Type::Int8,
121					Type::Int16,
122					Type::Uint1,
123					Type::Uint2,
124					Type::Uint4,
125					Type::Uint8,
126					Type::Uint16,
127				],
128				actual: min_data.get_type(),
129			});
130		}
131		if !is_integer_type(sec_data) {
132			return Err(FunctionError::InvalidArgumentType {
133				function: ctx.fragment.clone(),
134				argument_index: 2,
135				expected: vec![
136					Type::Int1,
137					Type::Int2,
138					Type::Int4,
139					Type::Int8,
140					Type::Int16,
141					Type::Uint1,
142					Type::Uint2,
143					Type::Uint4,
144					Type::Uint8,
145					Type::Uint16,
146				],
147				actual: sec_data.get_type(),
148			});
149		}
150		if let Some((nd, _)) = &nano_data
151			&& !is_integer_type(nd)
152		{
153			return Err(FunctionError::InvalidArgumentType {
154				function: ctx.fragment.clone(),
155				argument_index: 3,
156				expected: vec![
157					Type::Int1,
158					Type::Int2,
159					Type::Int4,
160					Type::Int8,
161					Type::Uint1,
162					Type::Uint2,
163					Type::Uint4,
164				],
165				actual: nd.get_type(),
166			});
167		}
168
169		let row_count = hour_data.len();
170		let mut container = TemporalContainer::with_capacity(row_count);
171
172		for i in 0..row_count {
173			let hour = extract_i32(hour_data, i);
174			let min = extract_i32(min_data, i);
175			let sec = extract_i32(sec_data, i);
176			let nano = if let Some((nd, _)) = &nano_data {
177				extract_i32(nd, i)
178			} else {
179				Some(0)
180			};
181
182			match (hour, min, sec, nano) {
183				(Some(h), Some(m), Some(s), Some(n)) => {
184					if h >= 0 && m >= 0 && s >= 0 && n >= 0 {
185						match Time::new(h as u32, m as u32, s as u32, n as u32) {
186							Some(time) => container.push(time),
187							None => container.push_default(),
188						}
189					} else {
190						container.push_default();
191					}
192				}
193				_ => container.push_default(),
194			}
195		}
196
197		let result_data = ColumnData::Time(container);
198		Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), result_data)]))
199	}
200}