Skip to main content

mz_expr/scalar/func/impls/
uint32.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 mz_expr_derive::sqlfunc;
13use mz_lowertest::MzReflect;
14use mz_repr::adt::numeric::{self, Numeric, NumericMaxScale};
15use mz_repr::{SqlColumnType, SqlScalarType, strconv};
16use serde::{Deserialize, Serialize};
17
18use crate::EvalError;
19use crate::scalar::func::EagerUnaryFunc;
20
21#[sqlfunc(
22    sqlname = "~",
23    preserves_uniqueness = true,
24    inverse = to_unary!(super::BitNotUint32)
25)]
26fn bit_not_uint32(a: u32) -> u32 {
27    !a
28}
29
30#[sqlfunc(
31    sqlname = "uint4_to_real",
32    preserves_uniqueness = false,
33    inverse = to_unary!(super::CastFloat32ToUint32),
34    is_monotone = true
35)]
36fn cast_uint32_to_float32(a: u32) -> f32 {
37    // TODO(benesch): remove potentially dangerous usage of `as`.
38    #[allow(clippy::as_conversions)]
39    {
40        a as f32
41    }
42}
43
44#[sqlfunc(
45    sqlname = "uint4_to_double",
46    preserves_uniqueness = true,
47    inverse = to_unary!(super::CastFloat64ToUint32),
48    is_monotone = true
49)]
50fn cast_uint32_to_float64(a: u32) -> f64 {
51    f64::from(a)
52}
53
54#[sqlfunc(
55    sqlname = "uint4_to_uint2",
56    preserves_uniqueness = true,
57    inverse = to_unary!(super::CastUint16ToUint32),
58    is_monotone = true
59)]
60fn cast_uint32_to_uint16(a: u32) -> Result<u16, EvalError> {
61    u16::try_from(a).or_else(|_| Err(EvalError::UInt16OutOfRange(a.to_string().into())))
62}
63
64#[sqlfunc(
65    sqlname = "uint4_to_uint8",
66    preserves_uniqueness = true,
67    inverse = to_unary!(super::CastUint64ToUint32),
68    is_monotone = true
69)]
70fn cast_uint32_to_uint64(a: u32) -> u64 {
71    u64::from(a)
72}
73
74#[sqlfunc(
75    sqlname = "uint4_to_smallint",
76    preserves_uniqueness = true,
77    inverse = to_unary!(super::CastInt16ToUint32),
78    is_monotone = true
79)]
80fn cast_uint32_to_int16(a: u32) -> Result<i16, EvalError> {
81    i16::try_from(a).or_else(|_| Err(EvalError::Int16OutOfRange(a.to_string().into())))
82}
83
84#[sqlfunc(
85    sqlname = "uint4_to_integer",
86    preserves_uniqueness = true,
87    inverse = to_unary!(super::CastInt32ToUint32),
88    is_monotone = true
89)]
90fn cast_uint32_to_int32(a: u32) -> Result<i32, EvalError> {
91    i32::try_from(a).or_else(|_| Err(EvalError::Int32OutOfRange(a.to_string().into())))
92}
93
94#[sqlfunc(
95    sqlname = "uint4_to_bigint",
96    preserves_uniqueness = true,
97    inverse = to_unary!(super::CastInt64ToUint32),
98    is_monotone = true
99)]
100fn cast_uint32_to_int64(a: u32) -> i64 {
101    i64::from(a)
102}
103
104#[sqlfunc(
105    sqlname = "uint4_to_text",
106    preserves_uniqueness = true,
107    inverse = to_unary!(super::CastStringToUint32)
108)]
109fn cast_uint32_to_string(a: u32) -> String {
110    let mut buf = String::new();
111    strconv::format_uint32(&mut buf, a);
112    buf
113}
114
115#[derive(
116    Ord,
117    PartialOrd,
118    Clone,
119    Debug,
120    Eq,
121    PartialEq,
122    Serialize,
123    Deserialize,
124    Hash,
125    MzReflect
126)]
127pub struct CastUint32ToNumeric(pub Option<NumericMaxScale>);
128
129impl EagerUnaryFunc for CastUint32ToNumeric {
130    type Input<'a> = u32;
131    type Output<'a> = Result<Numeric, EvalError>;
132
133    fn call<'a>(&self, a: Self::Input<'a>) -> Self::Output<'a> {
134        let mut a = Numeric::from(a);
135        if let Some(scale) = self.0 {
136            if numeric::rescale(&mut a, scale.into_u8()).is_err() {
137                return Err(EvalError::NumericFieldOverflow);
138            }
139        }
140        // Besides `rescale`, cast is infallible.
141        Ok(a)
142    }
143
144    fn output_type(&self, input: SqlColumnType) -> SqlColumnType {
145        SqlScalarType::Numeric { max_scale: self.0 }.nullable(input.nullable)
146    }
147
148    fn could_error(&self) -> bool {
149        self.0.is_some()
150    }
151
152    fn inverse(&self) -> Option<crate::UnaryFunc> {
153        to_unary!(super::CastNumericToUint32)
154    }
155
156    fn is_monotone(&self) -> bool {
157        true
158    }
159}
160
161impl fmt::Display for CastUint32ToNumeric {
162    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
163        f.write_str("uint4_to_numeric")
164    }
165}