1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

//! "math" DataFusion functions

use datafusion_expr::ScalarUDF;
use std::sync::Arc;

pub mod abs;
pub mod cot;
pub mod factorial;
pub mod gcd;
pub mod iszero;
pub mod lcm;
pub mod log;
pub mod nans;
pub mod nanvl;
pub mod pi;
pub mod power;
pub mod random;
pub mod round;
pub mod trunc;

// Create UDFs
make_udf_function!(abs::AbsFunc, ABS, abs);
make_math_unary_udf!(AcosFunc, ACOS, acos, acos, None);
make_math_unary_udf!(AcoshFunc, ACOSH, acosh, acosh, Some(vec![Some(true)]));
make_math_unary_udf!(AsinFunc, ASIN, asin, asin, None);
make_math_unary_udf!(AsinhFunc, ASINH, asinh, asinh, Some(vec![Some(true)]));
make_math_unary_udf!(AtanFunc, ATAN, atan, atan, Some(vec![Some(true)]));
make_math_unary_udf!(AtanhFunc, ATANH, atanh, atanh, Some(vec![Some(true)]));
make_math_binary_udf!(Atan2, ATAN2, atan2, atan2, Some(vec![Some(true)]));
make_math_unary_udf!(CbrtFunc, CBRT, cbrt, cbrt, None);
make_math_unary_udf!(CeilFunc, CEIL, ceil, ceil, Some(vec![Some(true)]));
make_math_unary_udf!(CosFunc, COS, cos, cos, None);
make_math_unary_udf!(CoshFunc, COSH, cosh, cosh, None);
make_udf_function!(cot::CotFunc, COT, cot);
make_math_unary_udf!(DegreesFunc, DEGREES, degrees, to_degrees, None);
make_math_unary_udf!(ExpFunc, EXP, exp, exp, Some(vec![Some(true)]));
make_udf_function!(factorial::FactorialFunc, FACTORIAL, factorial);
make_math_unary_udf!(FloorFunc, FLOOR, floor, floor, Some(vec![Some(true)]));
make_udf_function!(log::LogFunc, LOG, log);
make_udf_function!(gcd::GcdFunc, GCD, gcd);
make_udf_function!(nans::IsNanFunc, ISNAN, isnan);
make_udf_function!(iszero::IsZeroFunc, ISZERO, iszero);
make_udf_function!(lcm::LcmFunc, LCM, lcm);
make_math_unary_udf!(LnFunc, LN, ln, ln, Some(vec![Some(true)]));
make_math_unary_udf!(Log2Func, LOG2, log2, log2, Some(vec![Some(true)]));
make_math_unary_udf!(Log10Func, LOG10, log10, log10, Some(vec![Some(true)]));
make_udf_function!(nanvl::NanvlFunc, NANVL, nanvl);
make_udf_function!(pi::PiFunc, PI, pi);
make_udf_function!(power::PowerFunc, POWER, power);
make_math_unary_udf!(RadiansFunc, RADIANS, radians, to_radians, None);
make_udf_function!(random::RandomFunc, RANDOM, random);
make_udf_function!(round::RoundFunc, ROUND, round);
make_math_unary_udf!(SignumFunc, SIGNUM, signum, signum, None);
make_math_unary_udf!(SinFunc, SIN, sin, sin, None);
make_math_unary_udf!(SinhFunc, SINH, sinh, sinh, None);
make_math_unary_udf!(SqrtFunc, SQRT, sqrt, sqrt, None);
make_math_unary_udf!(TanFunc, TAN, tan, tan, None);
make_math_unary_udf!(TanhFunc, TANH, tanh, tanh, None);
make_udf_function!(trunc::TruncFunc, TRUNC, trunc);

pub mod expr_fn {
    use datafusion_expr::Expr;

    #[doc = "returns the absolute value of a given number"]
    pub fn abs(num: Expr) -> Expr {
        super::abs().call(vec![num])
    }

    #[doc = "returns the arc cosine or inverse cosine of a number"]
    pub fn acos(num: Expr) -> Expr {
        super::acos().call(vec![num])
    }

    #[doc = "returns inverse hyperbolic cosine"]
    pub fn acosh(num: Expr) -> Expr {
        super::acosh().call(vec![num])
    }

    #[doc = "returns the arc sine or inverse sine of a number"]
    pub fn asin(num: Expr) -> Expr {
        super::asin().call(vec![num])
    }

    #[doc = "returns inverse hyperbolic sine"]
    pub fn asinh(num: Expr) -> Expr {
        super::asinh().call(vec![num])
    }

    #[doc = "returns inverse tangent"]
    pub fn atan(num: Expr) -> Expr {
        super::atan().call(vec![num])
    }

    #[doc = "returns inverse tangent of a division given in the argument"]
    pub fn atan2(y: Expr, x: Expr) -> Expr {
        super::atan2().call(vec![y, x])
    }

    #[doc = "returns inverse hyperbolic tangent"]
    pub fn atanh(num: Expr) -> Expr {
        super::atanh().call(vec![num])
    }

    #[doc = "cube root of a number"]
    pub fn cbrt(num: Expr) -> Expr {
        super::cbrt().call(vec![num])
    }

    #[doc = "nearest integer greater than or equal to argument"]
    pub fn ceil(num: Expr) -> Expr {
        super::ceil().call(vec![num])
    }

    #[doc = "cosine"]
    pub fn cos(num: Expr) -> Expr {
        super::cos().call(vec![num])
    }

    #[doc = "hyperbolic cosine"]
    pub fn cosh(num: Expr) -> Expr {
        super::cosh().call(vec![num])
    }

    #[doc = "cotangent of a number"]
    pub fn cot(num: Expr) -> Expr {
        super::cot().call(vec![num])
    }

    #[doc = "converts radians to degrees"]
    pub fn degrees(num: Expr) -> Expr {
        super::degrees().call(vec![num])
    }

    #[doc = "exponential"]
    pub fn exp(num: Expr) -> Expr {
        super::exp().call(vec![num])
    }

    #[doc = "factorial"]
    pub fn factorial(num: Expr) -> Expr {
        super::factorial().call(vec![num])
    }

    #[doc = "nearest integer less than or equal to argument"]
    pub fn floor(num: Expr) -> Expr {
        super::floor().call(vec![num])
    }

    #[doc = "greatest common divisor"]
    pub fn gcd(x: Expr, y: Expr) -> Expr {
        super::gcd().call(vec![x, y])
    }

    #[doc = "returns true if a given number is +NaN or -NaN otherwise returns false"]
    pub fn isnan(num: Expr) -> Expr {
        super::isnan().call(vec![num])
    }

    #[doc = "returns true if a given number is +0.0 or -0.0 otherwise returns false"]
    pub fn iszero(num: Expr) -> Expr {
        super::iszero().call(vec![num])
    }

    #[doc = "least common multiple"]
    pub fn lcm(x: Expr, y: Expr) -> Expr {
        super::lcm().call(vec![x, y])
    }

    #[doc = "natural logarithm (base e) of a number"]
    pub fn ln(num: Expr) -> Expr {
        super::ln().call(vec![num])
    }

    #[doc = "logarithm of a number for a particular `base`"]
    pub fn log(base: Expr, num: Expr) -> Expr {
        super::log().call(vec![base, num])
    }

    #[doc = "base 2 logarithm of a number"]
    pub fn log2(num: Expr) -> Expr {
        super::log2().call(vec![num])
    }

    #[doc = "base 10 logarithm of a number"]
    pub fn log10(num: Expr) -> Expr {
        super::log10().call(vec![num])
    }

    #[doc = "returns x if x is not NaN otherwise returns y"]
    pub fn nanvl(x: Expr, y: Expr) -> Expr {
        super::nanvl().call(vec![x, y])
    }

    #[doc = "Returns an approximate value of π"]
    pub fn pi() -> Expr {
        super::pi().call(vec![])
    }

    #[doc = "`base` raised to the power of `exponent`"]
    pub fn power(base: Expr, exponent: Expr) -> Expr {
        super::power().call(vec![base, exponent])
    }

    #[doc = "converts degrees to radians"]
    pub fn radians(num: Expr) -> Expr {
        super::radians().call(vec![num])
    }

    #[doc = "Returns a random value in the range 0.0 <= x < 1.0"]
    pub fn random() -> Expr {
        super::random().call(vec![])
    }

    #[doc = "round to nearest integer"]
    pub fn round(args: Vec<Expr>) -> Expr {
        super::round().call(args)
    }

    #[doc = "sign of the argument (-1, 0, +1)"]
    pub fn signum(num: Expr) -> Expr {
        super::signum().call(vec![num])
    }

    #[doc = "sine"]
    pub fn sin(num: Expr) -> Expr {
        super::sin().call(vec![num])
    }

    #[doc = "hyperbolic sine"]
    pub fn sinh(num: Expr) -> Expr {
        super::sinh().call(vec![num])
    }

    #[doc = "square root of a number"]
    pub fn sqrt(num: Expr) -> Expr {
        super::sqrt().call(vec![num])
    }

    #[doc = "returns the tangent of a number"]
    pub fn tan(num: Expr) -> Expr {
        super::tan().call(vec![num])
    }

    #[doc = "returns the hyperbolic tangent of a number"]
    pub fn tanh(num: Expr) -> Expr {
        super::tanh().call(vec![num])
    }

    #[doc = "truncate toward zero, with optional precision"]
    pub fn trunc(args: Vec<Expr>) -> Expr {
        super::trunc().call(args)
    }
}

///   Return a list of all functions in this package
pub fn functions() -> Vec<Arc<ScalarUDF>> {
    vec![
        abs(),
        acos(),
        acosh(),
        asin(),
        asinh(),
        atan(),
        atan2(),
        atanh(),
        cbrt(),
        ceil(),
        cos(),
        cosh(),
        cot(),
        degrees(),
        exp(),
        factorial(),
        floor(),
        gcd(),
        isnan(),
        iszero(),
        lcm(),
        ln(),
        log(),
        log2(),
        log10(),
        nanvl(),
        pi(),
        power(),
        radians(),
        random(),
        round(),
        signum(),
        sin(),
        sinh(),
        sqrt(),
        tan(),
        tanh(),
        trunc(),
    ]
}