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