mz_expr/scalar/func/impls/
uint32.rs1use 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 #[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(Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect)]
116pub struct CastUint32ToNumeric(pub Option<NumericMaxScale>);
117
118impl<'a> EagerUnaryFunc<'a> for CastUint32ToNumeric {
119 type Input = u32;
120 type Output = Result<Numeric, EvalError>;
121
122 fn call(&self, a: u32) -> Result<Numeric, EvalError> {
123 let mut a = Numeric::from(a);
124 if let Some(scale) = self.0 {
125 if numeric::rescale(&mut a, scale.into_u8()).is_err() {
126 return Err(EvalError::NumericFieldOverflow);
127 }
128 }
129 Ok(a)
131 }
132
133 fn output_type(&self, input: SqlColumnType) -> SqlColumnType {
134 SqlScalarType::Numeric { max_scale: self.0 }.nullable(input.nullable)
135 }
136
137 fn could_error(&self) -> bool {
138 self.0.is_some()
139 }
140
141 fn inverse(&self) -> Option<crate::UnaryFunc> {
142 to_unary!(super::CastNumericToUint32)
143 }
144
145 fn is_monotone(&self) -> bool {
146 true
147 }
148}
149
150impl fmt::Display for CastUint32ToNumeric {
151 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
152 f.write_str("uint4_to_numeric")
153 }
154}