reifydb_function/math/scalar/
atan2.rs1use num_traits::ToPrimitive;
5use reifydb_core::value::column::data::ColumnData;
6use reifydb_type::value::r#type::Type;
7
8use crate::{ScalarFunction, ScalarFunctionContext, error::ScalarFunctionError, propagate_options};
9
10pub struct Atan2;
11
12impl Atan2 {
13 pub fn new() -> Self {
14 Self
15 }
16}
17
18fn numeric_to_f64(data: &ColumnData, i: usize) -> Option<f64> {
19 match data {
20 ColumnData::Int1(c) => c.get(i).map(|&v| v as f64),
21 ColumnData::Int2(c) => c.get(i).map(|&v| v as f64),
22 ColumnData::Int4(c) => c.get(i).map(|&v| v as f64),
23 ColumnData::Int8(c) => c.get(i).map(|&v| v as f64),
24 ColumnData::Int16(c) => c.get(i).map(|&v| v as f64),
25 ColumnData::Uint1(c) => c.get(i).map(|&v| v as f64),
26 ColumnData::Uint2(c) => c.get(i).map(|&v| v as f64),
27 ColumnData::Uint4(c) => c.get(i).map(|&v| v as f64),
28 ColumnData::Uint8(c) => c.get(i).map(|&v| v as f64),
29 ColumnData::Uint16(c) => c.get(i).map(|&v| v as f64),
30 ColumnData::Float4(c) => c.get(i).map(|&v| v as f64),
31 ColumnData::Float8(c) => c.get(i).copied(),
32 ColumnData::Int {
33 container,
34 ..
35 } => container.get(i).map(|v| v.0.to_f64().unwrap_or(0.0)),
36 ColumnData::Uint {
37 container,
38 ..
39 } => container.get(i).map(|v| v.0.to_f64().unwrap_or(0.0)),
40 ColumnData::Decimal {
41 container,
42 ..
43 } => container.get(i).map(|v| v.0.to_f64().unwrap_or(0.0)),
44 _ => None,
45 }
46}
47
48impl ScalarFunction for Atan2 {
49 fn scalar(&self, ctx: ScalarFunctionContext) -> crate::error::ScalarFunctionResult<ColumnData> {
50 if let Some(result) = propagate_options(self, &ctx) {
51 return result;
52 }
53 let columns = ctx.columns;
54 let row_count = ctx.row_count;
55
56 if columns.len() != 2 {
57 return Err(ScalarFunctionError::ArityMismatch {
58 function: ctx.fragment.clone(),
59 expected: 2,
60 actual: columns.len(),
61 });
62 }
63
64 let y_col = columns.get(0).unwrap();
65 let x_col = columns.get(1).unwrap();
66
67 if !y_col.data().get_type().is_number() {
68 return Err(ScalarFunctionError::InvalidArgumentType {
69 function: ctx.fragment.clone(),
70 argument_index: 0,
71 expected: vec![
72 Type::Int1,
73 Type::Int2,
74 Type::Int4,
75 Type::Int8,
76 Type::Int16,
77 Type::Uint1,
78 Type::Uint2,
79 Type::Uint4,
80 Type::Uint8,
81 Type::Uint16,
82 Type::Float4,
83 Type::Float8,
84 Type::Int,
85 Type::Uint,
86 Type::Decimal,
87 ],
88 actual: y_col.data().get_type(),
89 });
90 }
91
92 if !x_col.data().get_type().is_number() {
93 return Err(ScalarFunctionError::InvalidArgumentType {
94 function: ctx.fragment.clone(),
95 argument_index: 1,
96 expected: vec![
97 Type::Int1,
98 Type::Int2,
99 Type::Int4,
100 Type::Int8,
101 Type::Int16,
102 Type::Uint1,
103 Type::Uint2,
104 Type::Uint4,
105 Type::Uint8,
106 Type::Uint16,
107 Type::Float4,
108 Type::Float8,
109 Type::Int,
110 Type::Uint,
111 Type::Decimal,
112 ],
113 actual: x_col.data().get_type(),
114 });
115 }
116
117 let mut result = Vec::with_capacity(row_count);
118 let mut bitvec = Vec::with_capacity(row_count);
119
120 for i in 0..row_count {
121 match (numeric_to_f64(y_col.data(), i), numeric_to_f64(x_col.data(), i)) {
122 (Some(y), Some(x)) => {
123 result.push(y.atan2(x));
124 bitvec.push(true);
125 }
126 _ => {
127 result.push(0.0);
128 bitvec.push(false);
129 }
130 }
131 }
132
133 Ok(ColumnData::float8_with_bitvec(result, bitvec))
134 }
135
136 fn return_type(&self, _input_types: &[Type]) -> Type {
137 Type::Float8
138 }
139}