reifydb_routine/function/duration/
millis.rs1use reifydb_core::value::column::{ColumnWithName, buffer::ColumnBuffer, columns::Columns};
5use reifydb_type::value::{container::temporal::TemporalContainer, duration::Duration, r#type::Type};
6
7use crate::routine::{Function, FunctionKind, Routine, RoutineInfo, context::FunctionContext, error::RoutineError};
8
9pub struct DurationMillis {
10 info: RoutineInfo,
11}
12
13impl Default for DurationMillis {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl DurationMillis {
20 pub fn new() -> Self {
21 Self {
22 info: RoutineInfo::new("duration::millis"),
23 }
24 }
25}
26
27fn extract_i64(data: &ColumnBuffer, i: usize) -> Option<i64> {
28 match data {
29 ColumnBuffer::Int1(c) => c.get(i).map(|&v| v as i64),
30 ColumnBuffer::Int2(c) => c.get(i).map(|&v| v as i64),
31 ColumnBuffer::Int4(c) => c.get(i).map(|&v| v as i64),
32 ColumnBuffer::Int8(c) => c.get(i).copied(),
33 ColumnBuffer::Int16(c) => c.get(i).map(|&v| v as i64),
34 ColumnBuffer::Uint1(c) => c.get(i).map(|&v| v as i64),
35 ColumnBuffer::Uint2(c) => c.get(i).map(|&v| v as i64),
36 ColumnBuffer::Uint4(c) => c.get(i).map(|&v| v as i64),
37 ColumnBuffer::Uint8(c) => c.get(i).map(|&v| v as i64),
38 ColumnBuffer::Uint16(c) => c.get(i).map(|&v| v as i64),
39 _ => None,
40 }
41}
42
43fn is_integer_type(data: &ColumnBuffer) -> bool {
44 matches!(
45 data,
46 ColumnBuffer::Int1(_)
47 | ColumnBuffer::Int2(_)
48 | ColumnBuffer::Int4(_)
49 | ColumnBuffer::Int8(_)
50 | ColumnBuffer::Int16(_)
51 | ColumnBuffer::Uint1(_)
52 | ColumnBuffer::Uint2(_)
53 | ColumnBuffer::Uint4(_)
54 | ColumnBuffer::Uint8(_)
55 | ColumnBuffer::Uint16(_)
56 )
57}
58
59impl<'a> Routine<FunctionContext<'a>> for DurationMillis {
60 fn info(&self) -> &RoutineInfo {
61 &self.info
62 }
63
64 fn return_type(&self, _input_types: &[Type]) -> Type {
65 Type::Duration
66 }
67
68 fn execute(&self, ctx: &mut FunctionContext<'a>, args: &Columns) -> Result<Columns, RoutineError> {
69 if args.len() != 1 {
70 return Err(RoutineError::FunctionArityMismatch {
71 function: ctx.fragment.clone(),
72 expected: 1,
73 actual: args.len(),
74 });
75 }
76
77 let column = &args[0];
78 let (data, bitvec) = column.unwrap_option();
79 let row_count = data.len();
80
81 if !is_integer_type(data) {
82 return Err(RoutineError::FunctionInvalidArgumentType {
83 function: ctx.fragment.clone(),
84 argument_index: 0,
85 expected: vec![
86 Type::Int1,
87 Type::Int2,
88 Type::Int4,
89 Type::Int8,
90 Type::Int16,
91 Type::Uint1,
92 Type::Uint2,
93 Type::Uint4,
94 Type::Uint8,
95 Type::Uint16,
96 ],
97 actual: data.get_type(),
98 });
99 }
100
101 let mut container = TemporalContainer::with_capacity(row_count);
102
103 for i in 0..row_count {
104 if let Some(val) = extract_i64(data, i) {
105 container.push(Duration::from_milliseconds(val)?);
106 } else {
107 container.push_default();
108 }
109 }
110
111 let mut result_data = ColumnBuffer::Duration(container);
112 if let Some(bv) = bitvec {
113 result_data = ColumnBuffer::Option {
114 inner: Box::new(result_data),
115 bitvec: bv.clone(),
116 };
117 }
118 Ok(Columns::new(vec![ColumnWithName::new(ctx.fragment.clone(), result_data)]))
119 }
120}
121
122impl Function for DurationMillis {
123 fn kinds(&self) -> &[FunctionKind] {
124 &[FunctionKind::Scalar]
125 }
126}