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