rupg-func 0.1.84

The 3,414 built-in functions, the 805 operators, the 249 casts, the aggregates and window functions, the regular expression engine.
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! The trigonometric, hyperbolic, error and gamma functions of `float.c`, and `width_bucket` of `float8`.
//!
//! The functions in degrees use the method of PostgreSQL: they reduce the angle to the first quadrant and scale the result of the C library by constants such as `sin(30)`, so that `sind(30)` is exactly 0.5 and `tand(45)` is exactly 1.

use std::hint::black_box;
use std::sync::OnceLock;

use rupg_common::{Error, Result, SqlState};
use rupg_types::Value;

use crate::compare::float;
use crate::math::{overflow, underflow};
use crate::{Call, Kernel, bad_value, special};

/// `RADIANS_PER_DEGREE` of `float.h`.
const RADIANS_PER_DEGREE: f64 = 0.017_453_292_519_943_295;

/// The float argument at `at`.
fn arg(args: &[Value], at: usize) -> Result<f64> {
    args.get(at).and_then(float).ok_or_else(bad_value)
}

/// `22003` for an argument outside the domain of the function.
fn out_of_range() -> Error {
    Error::new(SqlState::NUMERIC_VALUE_OUT_OF_RANGE, "input is out of range")
}

/// A `float8` result that must be finite.
fn finite(r: f64) -> Result<Value> {
    if r.is_infinite() {
        return Err(overflow());
    }
    Ok(Value::Float8(r))
}

/// A function of one float that gives NaN for NaN and rejects arguments outside `[-1, 1]`.
fn unit_domain(args: &[Value], f: fn(f64) -> f64) -> Result<Value> {
    let x = arg(args, 0)?;
    if x.is_nan() {
        return Ok(Value::Float8(f64::NAN));
    }
    if !(-1.0..=1.0).contains(&x) {
        return Err(out_of_range());
    }
    finite(f(x))
}

/// A function of one float that gives NaN for NaN and rejects an infinite argument.
fn periodic(args: &[Value], f: fn(f64) -> f64) -> Result<f64> {
    let x = arg(args, 0)?;
    if x.is_nan() {
        return Ok(f64::NAN);
    }
    if x.is_infinite() {
        return Err(out_of_range());
    }
    Ok(f(x))
}

fn dacos(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    unit_domain(args, f64::acos)
}

fn dasin(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    unit_domain(args, f64::asin)
}

fn datan(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let x = arg(args, 0)?;
    if x.is_nan() {
        return Ok(Value::Float8(f64::NAN));
    }
    finite(x.atan())
}

fn datan2(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let (y, x) = (arg(args, 0)?, arg(args, 1)?);
    if y.is_nan() || x.is_nan() {
        return Ok(Value::Float8(f64::NAN));
    }
    finite(y.atan2(x))
}

fn dcos(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    finite(periodic(args, f64::cos)?)
}

fn dsin(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    finite(periodic(args, f64::sin)?)
}

/// `dtan`. `tan(pi/2)` can be infinite, so there is no check of overflow.
fn dtan(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    Ok(Value::Float8(periodic(args, f64::tan)?))
}

/// `dcot`: `1 / tan(x)`. `cot(0)` is infinite.
fn dcot(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    Ok(Value::Float8(1.0 / periodic(args, f64::tan)?))
}

/// The constants of `init_degree_constants`. The arguments go through `black_box`, so the compiler cannot compute them with a different `sin`.
struct Degrees {
    sin_30: f64,
    one_minus_cos_60: f64,
    asin_0_5: f64,
    acos_0_5: f64,
    atan_1_0: f64,
    tan_45: f64,
    cot_45: f64,
}

fn degrees() -> &'static Degrees {
    static DEGREES: OnceLock<Degrees> = OnceLock::new();
    DEGREES.get_or_init(|| {
        let mut d = Degrees {
            sin_30: (black_box(30.0) * RADIANS_PER_DEGREE).sin(),
            one_minus_cos_60: 1.0 - (black_box(60.0) * RADIANS_PER_DEGREE).cos(),
            asin_0_5: black_box(0.5f64).asin(),
            acos_0_5: black_box(0.5f64).acos(),
            atan_1_0: black_box(1.0f64).atan(),
            tan_45: 1.0,
            cot_45: 1.0,
        };
        let forty_five = black_box(45.0);
        d.tan_45 = sind_q1(&d, forty_five) / cosd_q1(&d, forty_five);
        d.cot_45 = cosd_q1(&d, forty_five) / sind_q1(&d, forty_five);
        d
    })
}

/// The arcsine in degrees of `x` in `[0, 1]`. 0, 0.5 and 1 give exactly 0, 30 and 90.
fn asind_q1(d: &Degrees, x: f64) -> f64 {
    if x <= 0.5 { (x.asin() / d.asin_0_5) * 30.0 } else { 90.0 - (x.acos() / d.acos_0_5) * 60.0 }
}

/// The arccosine in degrees of `x` in `[0, 1]`. 0, 0.5 and 1 give exactly 90, 60 and 0.
fn acosd_q1(d: &Degrees, x: f64) -> f64 {
    if x <= 0.5 { 90.0 - (x.asin() / d.asin_0_5) * 30.0 } else { (x.acos() / d.acos_0_5) * 60.0 }
}

/// The sine of an angle from 0 to 30 degrees.
fn sind_0_to_30(d: &Degrees, x: f64) -> f64 {
    ((x * RADIANS_PER_DEGREE).sin() / d.sin_30) / 2.0
}

/// The cosine of an angle from 0 to 60 degrees.
fn cosd_0_to_60(d: &Degrees, x: f64) -> f64 {
    let one_minus_cos_x = 1.0 - (x * RADIANS_PER_DEGREE).cos();
    1.0 - (one_minus_cos_x / d.one_minus_cos_60) / 2.0
}

/// The sine of an angle in the first quadrant.
fn sind_q1(d: &Degrees, x: f64) -> f64 {
    if x <= 30.0 { sind_0_to_30(d, x) } else { cosd_0_to_60(d, 90.0 - x) }
}

/// The cosine of an angle in the first quadrant.
fn cosd_q1(d: &Degrees, x: f64) -> f64 {
    if x <= 60.0 { cosd_0_to_60(d, x) } else { sind_0_to_30(d, 90.0 - x) }
}

fn dacosd(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let d = degrees();
    let x = arg(args, 0)?;
    if x.is_nan() {
        return Ok(Value::Float8(f64::NAN));
    }
    if !(-1.0..=1.0).contains(&x) {
        return Err(out_of_range());
    }
    finite(if x >= 0.0 { acosd_q1(d, x) } else { 90.0 + asind_q1(d, -x) })
}

fn dasind(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let d = degrees();
    let x = arg(args, 0)?;
    if x.is_nan() {
        return Ok(Value::Float8(f64::NAN));
    }
    if !(-1.0..=1.0).contains(&x) {
        return Err(out_of_range());
    }
    finite(if x >= 0.0 { asind_q1(d, x) } else { -asind_q1(d, -x) })
}

fn datand(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let x = arg(args, 0)?;
    if x.is_nan() {
        return Ok(Value::Float8(f64::NAN));
    }
    finite((x.atan() / degrees().atan_1_0) * 45.0)
}

fn datan2d(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let (y, x) = (arg(args, 0)?, arg(args, 1)?);
    if y.is_nan() || x.is_nan() {
        return Ok(Value::Float8(f64::NAN));
    }
    finite((y.atan2(x) / degrees().atan_1_0) * 45.0)
}

/// The angle in `[0, 90]` and the sign of the result for a function in degrees. `odd` tells if `f(-x) = -f(x)`, and `back` tells if `f(180 - x) = -f(x)`. Then `f(360 - x)` has the sign of `f(-x)`.
fn reduce(x: f64, odd: bool, back: bool) -> (f64, f64) {
    let mut x = x % 360.0;
    let mut sign = 1.0;
    if x < 0.0 {
        x = -x;
        if odd {
            sign = -sign;
        }
    }
    if x > 180.0 {
        x = 360.0 - x;
        if odd {
            sign = -sign;
        }
    }
    if x > 90.0 {
        x = 180.0 - x;
        if back {
            sign = -sign;
        }
    }
    (x, sign)
}

/// The argument of a function in degrees: NaN gives `None`, and infinity is an error.
fn degree_arg(args: &[Value]) -> Result<Option<f64>> {
    let x = arg(args, 0)?;
    if x.is_nan() {
        return Ok(None);
    }
    if x.is_infinite() {
        return Err(out_of_range());
    }
    Ok(Some(x))
}

fn dcosd(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let Some(x) = degree_arg(args)? else { return Ok(Value::Float8(f64::NAN)) };
    let (x, sign) = reduce(x, false, true);
    finite(sign * cosd_q1(degrees(), x))
}

fn dsind(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let Some(x) = degree_arg(args)? else { return Ok(Value::Float8(f64::NAN)) };
    let (x, sign) = reduce(x, true, false);
    finite(sign * sind_q1(degrees(), x))
}

/// `dtand`. Minus zero becomes zero, and `tand(90)` is infinite.
fn dtand(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let Some(x) = degree_arg(args)? else { return Ok(Value::Float8(f64::NAN)) };
    let d = degrees();
    let (x, sign) = reduce(x, true, true);
    let tan_x = sind_q1(d, x) / cosd_q1(d, x);
    let r = sign * (tan_x / d.tan_45);
    Ok(Value::Float8(if r == 0.0 { 0.0 } else { r }))
}

/// `dcotd`. Minus zero becomes zero, and `cotd(0)` is infinite.
fn dcotd(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let Some(x) = degree_arg(args)? else { return Ok(Value::Float8(f64::NAN)) };
    let d = degrees();
    let (x, sign) = reduce(x, true, true);
    let cot_x = cosd_q1(d, x) / sind_q1(d, x);
    let r = sign * (cot_x / d.cot_45);
    Ok(Value::Float8(if r == 0.0 { 0.0 } else { r }))
}

/// `degrees`: `float8_div(x, RADIANS_PER_DEGREE)`.
fn to_degrees(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let x = arg(args, 0)?;
    let r = x / RADIANS_PER_DEGREE;
    if r.is_infinite() && !x.is_infinite() {
        return Err(overflow());
    }
    if r == 0.0 && x != 0.0 {
        return Err(underflow());
    }
    Ok(Value::Float8(r))
}

/// `radians`: `float8_mul(x, RADIANS_PER_DEGREE)`.
fn to_radians(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let x = arg(args, 0)?;
    let r = x * RADIANS_PER_DEGREE;
    if r.is_infinite() && !x.is_infinite() {
        return Err(overflow());
    }
    if r == 0.0 && x != 0.0 {
        return Err(underflow());
    }
    Ok(Value::Float8(r))
}

/// `dsinh`. An overflow gives an infinity with the sign of the argument.
fn dsinh(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    Ok(Value::Float8(arg(args, 0)?.sinh()))
}

/// `dcosh`. An overflow gives infinity.
fn dcosh(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let r = arg(args, 0)?.cosh();
    if r == 0.0 {
        return Err(underflow());
    }
    Ok(Value::Float8(r))
}

fn dtanh(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    finite(arg(args, 0)?.tanh())
}

fn dasinh(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    Ok(Value::Float8(special::asinh(arg(args, 0)?)))
}

fn dacosh(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let x = arg(args, 0)?;
    if x < 1.0 {
        return Err(out_of_range());
    }
    Ok(Value::Float8(special::acosh(x)))
}

fn datanh(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let x = arg(args, 0)?;
    if x < -1.0 || x > 1.0 {
        return Err(out_of_range());
    }
    Ok(Value::Float8(if x == -1.0 {
        f64::NEG_INFINITY
    } else if x == 1.0 {
        f64::INFINITY
    } else {
        special::atanh(x)
    }))
}

fn derf(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    finite(special::erf(arg(args, 0)?))
}

fn derfc(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    finite(special::erfc(arg(args, 0)?))
}

/// `dgamma`. A pole, an overflow and minus infinity are errors of overflow. A result of zero is an error of underflow.
fn dgamma(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let x = arg(args, 0)?;
    if x.is_nan() || x == f64::INFINITY {
        return Ok(Value::Float8(x));
    }
    if x == f64::NEG_INFINITY {
        return Err(overflow());
    }
    let r = special::tgamma(x);
    if r.is_infinite() || r.is_nan() {
        return Err(overflow());
    }
    if r == 0.0 {
        return Err(underflow());
    }
    Ok(Value::Float8(r))
}

/// `dlgamma`. A pole and an overflow are errors.
fn dlgamma(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let x = arg(args, 0)?;
    let r = special::lgamma(x);
    if r.is_infinite() && !x.is_infinite() {
        return Err(overflow());
    }
    Ok(Value::Float8(r))
}

/// `2201G` for a bad argument of `width_bucket`.
fn bucket_error(message: &str) -> Error {
    Error::new(SqlState::INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION, message)
}

/// `width_bucket_float8`: the bucket of `operand` among `count` buckets of the same width from `bound1` to `bound2`.
#[allow(clippy::cast_possible_truncation)]
fn width_bucket_float8(_: &Call<'_>, args: &[Value]) -> Result<Value> {
    let (operand, bound1, bound2) = (arg(args, 0)?, arg(args, 1)?, arg(args, 2)?);
    let Some(Value::Int4(count)) = args.get(3) else { return Err(bad_value()) };
    let count = *count;
    if count <= 0 {
        return Err(bucket_error("count must be greater than zero"));
    }
    if bound1.is_nan() || bound2.is_nan() {
        return Err(bucket_error("lower and upper bounds cannot be NaN"));
    }
    if bound1.is_infinite() || bound2.is_infinite() {
        return Err(bucket_error("lower and upper bounds must be finite"));
    }
    let past = || {
        count
            .checked_add(1)
            .ok_or_else(|| Error::new(SqlState::NUMERIC_VALUE_OUT_OF_RANGE, "integer out of range"))
    };
    let inside = |low: f64, high: f64, x: f64, ascending: bool| {
        let fraction = if (high - low).is_infinite() {
            if ascending {
                (x / 2.0 - low / 2.0) / (high / 2.0 - low / 2.0)
            } else {
                (high / 2.0 - x / 2.0) / (high / 2.0 - low / 2.0)
            }
        } else if ascending {
            (x - low) / (high - low)
        } else {
            (high - x) / (high - low)
        };
        let r = (f64::from(count) * fraction) as i32;
        r.min(count - 1) + 1
    };
    let result = if bound1 < bound2 {
        if operand.is_nan() || operand >= bound2 {
            past()?
        } else if operand < bound1 {
            0
        } else {
            inside(bound1, bound2, operand, true)
        }
    } else if bound1 > bound2 {
        if operand.is_nan() || operand > bound1 {
            0
        } else if operand <= bound2 {
            past()?
        } else {
            inside(bound2, bound1, operand, false)
        }
    } else {
        return Err(bucket_error("lower bound cannot equal upper bound"));
    };
    Ok(Value::Int4(result))
}

/// The kernel of a function of this module by its `prosrc`.
pub(crate) fn by_src(src: &str) -> Option<Kernel> {
    Some(match src {
        "dacos" => dacos,
        "dasin" => dasin,
        "datan" => datan,
        "datan2" => datan2,
        "dcos" => dcos,
        "dsin" => dsin,
        "dtan" => dtan,
        "dcot" => dcot,
        "dacosd" => dacosd,
        "dasind" => dasind,
        "datand" => datand,
        "datan2d" => datan2d,
        "dcosd" => dcosd,
        "dsind" => dsind,
        "dtand" => dtand,
        "dcotd" => dcotd,
        "degrees" => to_degrees,
        "radians" => to_radians,
        "dsinh" => dsinh,
        "dcosh" => dcosh,
        "dtanh" => dtanh,
        "dasinh" => dasinh,
        "dacosh" => dacosh,
        "datanh" => datanh,
        "derf" => derf,
        "derfc" => derfc,
        "dgamma" => dgamma,
        "dlgamma" => dlgamma,
        "width_bucket_float8" => width_bucket_float8,
        _ => return None,
    })
}