reifydb_routine/function/duration/
scale.rs1use reifydb_core::value::column::{Column, columns::Columns, data::ColumnData};
5use reifydb_type::value::{container::temporal::TemporalContainer, r#type::Type};
6
7use crate::function::{Function, FunctionCapability, FunctionContext, FunctionInfo, error::FunctionError};
8
9pub struct DurationScale {
10 info: FunctionInfo,
11}
12
13impl Default for DurationScale {
14 fn default() -> Self {
15 Self::new()
16 }
17}
18
19impl DurationScale {
20 pub fn new() -> Self {
21 Self {
22 info: FunctionInfo::new("duration::scale"),
23 }
24 }
25}
26
27fn extract_i64(data: &ColumnData, i: usize) -> Option<i64> {
28 match data {
29 ColumnData::Int1(c) => c.get(i).map(|&v| v as i64),
30 ColumnData::Int2(c) => c.get(i).map(|&v| v as i64),
31 ColumnData::Int4(c) => c.get(i).map(|&v| v as i64),
32 ColumnData::Int8(c) => c.get(i).copied(),
33 ColumnData::Int16(c) => c.get(i).map(|&v| v as i64),
34 ColumnData::Uint1(c) => c.get(i).map(|&v| v as i64),
35 ColumnData::Uint2(c) => c.get(i).map(|&v| v as i64),
36 ColumnData::Uint4(c) => c.get(i).map(|&v| v as i64),
37 ColumnData::Uint8(c) => c.get(i).map(|&v| v as i64),
38 ColumnData::Uint16(c) => c.get(i).map(|&v| v as i64),
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 DurationScale {
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::Duration
68 }
69
70 fn execute(&self, ctx: &FunctionContext, args: &Columns) -> Result<Columns, FunctionError> {
71 if args.len() != 2 {
72 return Err(FunctionError::ArityMismatch {
73 function: ctx.fragment.clone(),
74 expected: 2,
75 actual: args.len(),
76 });
77 }
78
79 let dur_col = &args[0];
80 let scalar_col = &args[1];
81
82 let (dur_data, dur_bv) = dur_col.data().unwrap_option();
83 let (scalar_data, scalar_bv) = scalar_col.data().unwrap_option();
84
85 match dur_data {
86 ColumnData::Duration(dur_container) => {
87 if !is_integer_type(scalar_data) {
88 return Err(FunctionError::InvalidArgumentType {
89 function: ctx.fragment.clone(),
90 argument_index: 1,
91 expected: vec![
92 Type::Int1,
93 Type::Int2,
94 Type::Int4,
95 Type::Int8,
96 Type::Int16,
97 Type::Uint1,
98 Type::Uint2,
99 Type::Uint4,
100 Type::Uint8,
101 Type::Uint16,
102 ],
103 actual: scalar_data.get_type(),
104 });
105 }
106
107 let row_count = dur_data.len();
108 let mut container = TemporalContainer::with_capacity(row_count);
109
110 for i in 0..row_count {
111 match (dur_container.get(i), extract_i64(scalar_data, i)) {
112 (Some(dur), Some(scalar)) => {
113 container.push(*dur * scalar);
114 }
115 _ => container.push_default(),
116 }
117 }
118
119 let mut result_data = ColumnData::Duration(container);
120 if let Some(bv) = dur_bv {
121 result_data = ColumnData::Option {
122 inner: Box::new(result_data),
123 bitvec: bv.clone(),
124 };
125 } else if let Some(bv) = scalar_bv {
126 result_data = ColumnData::Option {
127 inner: Box::new(result_data),
128 bitvec: bv.clone(),
129 };
130 }
131 Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), result_data)]))
132 }
133 other => Err(FunctionError::InvalidArgumentType {
134 function: ctx.fragment.clone(),
135 argument_index: 0,
136 expected: vec![Type::Duration],
137 actual: other.get_type(),
138 }),
139 }
140 }
141}