mz_expr/scalar/func/impls/
float64.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use std::fmt;
11
12use chrono::{DateTime, Utc};
13use mz_expr_derive::sqlfunc;
14use mz_lowertest::MzReflect;
15use mz_ore::cast::TryCastFrom;
16use mz_repr::adt::numeric::{self, Numeric, NumericMaxScale};
17use mz_repr::adt::timestamp::CheckedTimestamp;
18use mz_repr::{SqlColumnType, SqlScalarType, strconv};
19use serde::{Deserialize, Serialize};
20
21use crate::EvalError;
22use crate::scalar::DomainLimit;
23use crate::scalar::func::EagerUnaryFunc;
24
25#[sqlfunc(
26    sqlname = "-",
27    preserves_uniqueness = false,
28    inverse = to_unary!(NegFloat64),
29    is_monotone = true
30)]
31fn neg_float64(a: f64) -> f64 {
32    -a
33}
34
35#[sqlfunc(sqlname = "abs")]
36fn abs_float64(a: f64) -> f64 {
37    a.abs()
38}
39
40#[sqlfunc(sqlname = "roundf64")]
41fn round_float64(a: f64) -> f64 {
42    a.round_ties_even()
43}
44
45#[sqlfunc(sqlname = "truncf64")]
46fn trunc_float64(a: f64) -> f64 {
47    a.trunc()
48}
49
50#[sqlfunc(sqlname = "ceilf64")]
51fn ceil_float64(a: f64) -> f64 {
52    a.ceil()
53}
54
55#[sqlfunc(sqlname = "floorf64")]
56fn floor_float64(a: f64) -> f64 {
57    a.floor()
58}
59
60#[sqlfunc(
61    sqlname = "double_to_smallint",
62    preserves_uniqueness = false,
63    inverse = to_unary!(super::CastInt16ToFloat64),
64    is_monotone = true
65)]
66fn cast_float64_to_int16(a: f64) -> Result<i16, EvalError> {
67    let f = round_float64(a);
68    // TODO(benesch): remove potentially dangerous usage of `as`.
69    #[allow(clippy::as_conversions)]
70    if (f >= (i16::MIN as f64)) && (f < -(i16::MIN as f64)) {
71        Ok(f as i16)
72    } else {
73        Err(EvalError::Int16OutOfRange(f.to_string().into()))
74    }
75}
76
77#[sqlfunc(
78    sqlname = "double_to_integer",
79    preserves_uniqueness = false,
80    inverse = to_unary!(super::CastInt32ToFloat64),
81    is_monotone = true
82)]
83fn cast_float64_to_int32(a: f64) -> Result<i32, EvalError> {
84    let f = round_float64(a);
85    // This condition is delicate because i32::MIN can be represented exactly by
86    // an f64 but not i32::MAX. We follow PostgreSQL's approach here.
87    //
88    // See: https://github.com/postgres/postgres/blob/ca3b37487/src/include/c.h#L1074-L1096
89    // TODO(benesch): remove potentially dangerous usage of `as`.
90    #[allow(clippy::as_conversions)]
91    if (f >= (i32::MIN as f64)) && (f < -(i32::MIN as f64)) {
92        Ok(f as i32)
93    } else {
94        Err(EvalError::Int32OutOfRange(f.to_string().into()))
95    }
96}
97
98#[sqlfunc(
99    sqlname = "f64toi64",
100    preserves_uniqueness = false,
101    inverse = to_unary!(super::CastInt64ToFloat64),
102    is_monotone = true
103)]
104fn cast_float64_to_int64(a: f64) -> Result<i64, EvalError> {
105    let f = round_float64(a);
106    // This condition is delicate because i64::MIN can be represented exactly by
107    // an f64 but not i64::MAX. We follow PostgreSQL's approach here.
108    //
109    // See: https://github.com/postgres/postgres/blob/ca3b37487/src/include/c.h#L1074-L1096
110    // TODO(benesch): remove potentially dangerous usage of `as`.
111    #[allow(clippy::as_conversions)]
112    if (f >= (i64::MIN as f64)) && (f < -(i64::MIN as f64)) {
113        Ok(f as i64)
114    } else {
115        Err(EvalError::Int64OutOfRange(f.to_string().into()))
116    }
117}
118
119#[sqlfunc(
120    sqlname = "double_to_real",
121    preserves_uniqueness = false,
122    inverse = to_unary!(super::CastFloat32ToFloat64),
123    is_monotone = true
124)]
125fn cast_float64_to_float32(a: f64) -> Result<f32, EvalError> {
126    // TODO(benesch): remove potentially dangerous usage of `as`.
127    #[allow(clippy::as_conversions)]
128    let result = a as f32;
129    if result.is_infinite() && !a.is_infinite() {
130        Err(EvalError::FloatOverflow)
131    } else if result == 0.0 && a != 0.0 {
132        Err(EvalError::FloatUnderflow)
133    } else {
134        Ok(result)
135    }
136}
137
138#[sqlfunc(
139    sqlname = "double_to_text",
140    preserves_uniqueness = false,
141    inverse = to_unary!(super::CastStringToFloat64)
142)]
143fn cast_float64_to_string(a: f64) -> String {
144    let mut s = String::new();
145    strconv::format_float64(&mut s, a);
146    s
147}
148
149#[sqlfunc(
150    sqlname = "double_to_uint2",
151    preserves_uniqueness = false,
152    inverse = to_unary!(super::CastUint16ToFloat64),
153    is_monotone = true
154)]
155fn cast_float64_to_uint16(a: f64) -> Result<u16, EvalError> {
156    let f = round_float64(a);
157    // TODO(benesch): remove potentially dangerous usage of `as`.
158    #[allow(clippy::as_conversions)]
159    if (f >= 0.0) && (f <= (u16::MAX as f64)) {
160        Ok(f as u16)
161    } else {
162        Err(EvalError::UInt16OutOfRange(f.to_string().into()))
163    }
164}
165
166#[sqlfunc(
167    sqlname = "double_to_uint4",
168    preserves_uniqueness = false,
169    inverse = to_unary!(super::CastUint32ToFloat64),
170    is_monotone = true
171)]
172fn cast_float64_to_uint32(a: f64) -> Result<u32, EvalError> {
173    let f = round_float64(a);
174    // TODO(benesch): remove potentially dangerous usage of `as`.
175    #[allow(clippy::as_conversions)]
176    if (f >= 0.0) && (f <= (u32::MAX as f64)) {
177        Ok(f as u32)
178    } else {
179        Err(EvalError::UInt32OutOfRange(f.to_string().into()))
180    }
181}
182
183#[sqlfunc(
184    sqlname = "double_to_uint8",
185    preserves_uniqueness = false,
186    inverse = to_unary!(super::CastUint64ToFloat64),
187    is_monotone = true
188)]
189fn cast_float64_to_uint64(a: f64) -> Result<u64, EvalError> {
190    let f = round_float64(a);
191    // TODO(benesch): remove potentially dangerous usage of `as`.
192    #[allow(clippy::as_conversions)]
193    if (f >= 0.0) && (f <= (u64::MAX as f64)) {
194        Ok(f as u64)
195    } else {
196        Err(EvalError::UInt64OutOfRange(f.to_string().into()))
197    }
198}
199
200#[derive(Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect)]
201pub struct CastFloat64ToNumeric(pub Option<NumericMaxScale>);
202
203impl<'a> EagerUnaryFunc<'a> for CastFloat64ToNumeric {
204    type Input = f64;
205    type Output = Result<Numeric, EvalError>;
206
207    fn call(&self, a: f64) -> Result<Numeric, EvalError> {
208        if a.is_infinite() {
209            return Err(EvalError::InfinityOutOfDomain(
210                "casting double precision to numeric".into(),
211            ));
212        }
213        let mut a = Numeric::from(a);
214        if let Some(scale) = self.0 {
215            if numeric::rescale(&mut a, scale.into_u8()).is_err() {
216                return Err(EvalError::NumericFieldOverflow);
217            }
218        }
219        match numeric::munge_numeric(&mut a) {
220            Ok(_) => Ok(a),
221            Err(_) => Err(EvalError::NumericFieldOverflow),
222        }
223    }
224
225    fn output_type(&self, input: SqlColumnType) -> SqlColumnType {
226        SqlScalarType::Numeric { max_scale: self.0 }.nullable(input.nullable)
227    }
228
229    fn inverse(&self) -> Option<crate::UnaryFunc> {
230        to_unary!(super::CastNumericToFloat64)
231    }
232
233    fn is_monotone(&self) -> bool {
234        true
235    }
236}
237
238impl fmt::Display for CastFloat64ToNumeric {
239    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
240        f.write_str("double_to_numeric")
241    }
242}
243
244#[sqlfunc(sqlname = "sqrtf64")]
245fn sqrt_float64(a: f64) -> Result<f64, EvalError> {
246    if a < 0.0 {
247        return Err(EvalError::NegSqrt);
248    }
249    Ok(a.sqrt())
250}
251
252#[sqlfunc(sqlname = "cbrtf64")]
253fn cbrt_float64(a: f64) -> f64 {
254    a.cbrt()
255}
256
257#[sqlfunc]
258fn cos(a: f64) -> Result<f64, EvalError> {
259    if a.is_infinite() {
260        return Err(EvalError::InfinityOutOfDomain("cos".into()));
261    }
262    Ok(a.cos())
263}
264
265#[sqlfunc]
266fn acos(a: f64) -> Result<f64, EvalError> {
267    if a < -1.0 || 1.0 < a {
268        return Err(EvalError::OutOfDomain(
269            DomainLimit::Inclusive(-1),
270            DomainLimit::Inclusive(1),
271            "acos".into(),
272        ));
273    }
274    Ok(a.acos())
275}
276
277#[sqlfunc]
278fn cosh(a: f64) -> f64 {
279    a.cosh()
280}
281
282#[sqlfunc]
283fn acosh(a: f64) -> Result<f64, EvalError> {
284    if a < 1.0 {
285        return Err(EvalError::OutOfDomain(
286            DomainLimit::Inclusive(1),
287            DomainLimit::None,
288            "acosh".into(),
289        ));
290    }
291    Ok(a.acosh())
292}
293
294#[sqlfunc]
295fn sin(a: f64) -> Result<f64, EvalError> {
296    if a.is_infinite() {
297        return Err(EvalError::InfinityOutOfDomain("sin".into()));
298    }
299    Ok(a.sin())
300}
301
302#[sqlfunc]
303fn asin(a: f64) -> Result<f64, EvalError> {
304    if a < -1.0 || 1.0 < a {
305        return Err(EvalError::OutOfDomain(
306            DomainLimit::Inclusive(-1),
307            DomainLimit::Inclusive(1),
308            "asin".into(),
309        ));
310    }
311    Ok(a.asin())
312}
313
314#[sqlfunc]
315fn sinh(a: f64) -> f64 {
316    a.sinh()
317}
318
319#[sqlfunc]
320fn asinh(a: f64) -> f64 {
321    a.asinh()
322}
323
324#[sqlfunc]
325fn tan(a: f64) -> Result<f64, EvalError> {
326    if a.is_infinite() {
327        return Err(EvalError::InfinityOutOfDomain("tan".into()));
328    }
329    Ok(a.tan())
330}
331
332#[sqlfunc]
333fn atan(a: f64) -> f64 {
334    a.atan()
335}
336
337#[sqlfunc]
338fn tanh(a: f64) -> f64 {
339    a.tanh()
340}
341
342#[sqlfunc]
343fn atanh(a: f64) -> Result<f64, EvalError> {
344    if a < -1.0 || 1.0 < a {
345        return Err(EvalError::OutOfDomain(
346            DomainLimit::Inclusive(-1),
347            DomainLimit::Inclusive(1),
348            "atanh".into(),
349        ));
350    }
351    Ok(a.atanh())
352}
353
354#[sqlfunc]
355fn cot(a: f64) -> Result<f64, EvalError> {
356    if a.is_infinite() {
357        return Err(EvalError::InfinityOutOfDomain("cot".into()));
358    }
359    Ok(1.0 / a.tan())
360}
361
362#[sqlfunc]
363fn radians(a: f64) -> f64 {
364    a.to_radians()
365}
366
367#[sqlfunc]
368fn degrees(a: f64) -> f64 {
369    a.to_degrees()
370}
371
372#[sqlfunc(sqlname = "log10f64")]
373fn log10(a: f64) -> Result<f64, EvalError> {
374    if a.is_sign_negative() {
375        return Err(EvalError::NegativeOutOfDomain("log10".into()));
376    }
377    if a == 0.0 {
378        return Err(EvalError::ZeroOutOfDomain("log10".into()));
379    }
380    Ok(a.log10())
381}
382
383#[sqlfunc(sqlname = "lnf64")]
384fn ln(a: f64) -> Result<f64, EvalError> {
385    if a.is_sign_negative() {
386        return Err(EvalError::NegativeOutOfDomain("ln".into()));
387    }
388    if a == 0.0 {
389        return Err(EvalError::ZeroOutOfDomain("ln".into()));
390    }
391    Ok(a.ln())
392}
393
394#[sqlfunc(sqlname = "expf64")]
395fn exp(a: f64) -> Result<f64, EvalError> {
396    let r = a.exp();
397    if r.is_infinite() {
398        return Err(EvalError::FloatOverflow);
399    }
400    if r == 0.0 {
401        return Err(EvalError::FloatUnderflow);
402    }
403    Ok(r)
404}
405
406#[sqlfunc(sqlname = "mz_sleep")]
407fn sleep(a: f64) -> Option<CheckedTimestamp<DateTime<Utc>>> {
408    let duration = std::time::Duration::from_secs_f64(a);
409    std::thread::sleep(duration);
410    None
411}
412
413#[sqlfunc(sqlname = "tots")]
414fn to_timestamp(f: f64) -> Result<CheckedTimestamp<DateTime<Utc>>, EvalError> {
415    const NANO_SECONDS_PER_SECOND: i64 = 1_000_000_000;
416    if f.is_nan() {
417        Err(EvalError::TimestampCannotBeNan)
418    } else if f.is_infinite() {
419        // TODO(jkosh44) implement infinite timestamps
420        Err(EvalError::TimestampOutOfRange)
421    } else {
422        let mut secs = i64::try_cast_from(f.trunc()).ok_or(EvalError::TimestampOutOfRange)?;
423        // NOTE(benesch): PostgreSQL has microsecond precision in its timestamps,
424        // while chrono has nanosecond precision. While we normally accept
425        // nanosecond precision, here we round to the nearest microsecond because
426        // f64s lose quite a bit of accuracy in the nanosecond digits when dealing
427        // with common Unix timestamp values (> 1 billion).
428        let microsecs = (f.fract() * 1_000_000.0).round();
429        let mut nanosecs =
430            i64::try_cast_from(microsecs * 1_000.0).ok_or(EvalError::TimestampOutOfRange)?;
431        if nanosecs < 0 {
432            secs = secs.checked_sub(1).ok_or(EvalError::TimestampOutOfRange)?;
433            nanosecs = NANO_SECONDS_PER_SECOND
434                .checked_add(nanosecs)
435                .ok_or(EvalError::TimestampOutOfRange)?;
436        }
437        // Ensure `nanosecs` is less than 1 second.
438        secs = secs
439            .checked_add(nanosecs / NANO_SECONDS_PER_SECOND)
440            .ok_or(EvalError::TimestampOutOfRange)?;
441        nanosecs %= NANO_SECONDS_PER_SECOND;
442        let nanosecs = u32::try_from(nanosecs).map_err(|_| EvalError::TimestampOutOfRange)?;
443        match DateTime::from_timestamp(secs, nanosecs) {
444            Some(dt) => {
445                CheckedTimestamp::from_timestamplike(dt).map_err(|_| EvalError::TimestampOutOfRange)
446            }
447            None => Err(EvalError::TimestampOutOfRange),
448        }
449    }
450}