reifydb_routine/function/math/
sqrt.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 Sqrt {
11 info: FunctionInfo,
12}
13
14impl Default for Sqrt {
15 fn default() -> Self {
16 Self::new()
17 }
18}
19
20impl Sqrt {
21 pub fn new() -> Self {
22 Self {
23 info: FunctionInfo::new("math::sqrt"),
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 Sqrt {
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::Float8
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 result.push(v.sqrt());
100 res_bitvec.push(true);
101 }
102 None => {
103 result.push(0.0);
104 res_bitvec.push(false);
105 }
106 }
107 }
108
109 let result_data = ColumnData::float8_with_bitvec(result, res_bitvec);
110 let final_data = if let Some(bv) = bitvec {
111 ColumnData::Option {
112 inner: Box::new(result_data),
113 bitvec: bv.clone(),
114 }
115 } else {
116 result_data
117 };
118
119 Ok(Columns::new(vec![Column::new(ctx.fragment.clone(), final_data)]))
120 }
121}