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