mz_expr/scalar/func/impls/
uint16.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::BitNotUint16)
25)]
26fn bit_not_uint16(a: u16) -> u16 {
27 !a
28}
29
30#[sqlfunc(
31 sqlname = "uint2_to_real",
32 preserves_uniqueness = true,
33 inverse = to_unary!(super::CastFloat32ToUint16),
34 is_monotone = true
35)]
36fn cast_uint16_to_float32(a: u16) -> f32 {
37 f32::from(a)
38}
39
40#[sqlfunc(
41 sqlname = "uint2_to_double",
42 preserves_uniqueness = true,
43 inverse = to_unary!(super::CastFloat64ToUint16),
44 is_monotone = true
45)]
46fn cast_uint16_to_float64(a: u16) -> f64 {
47 f64::from(a)
48}
49
50#[sqlfunc(
51 sqlname = "uint2_to_uint4",
52 preserves_uniqueness = true,
53 inverse = to_unary!(super::CastUint32ToUint16),
54 is_monotone = true
55)]
56fn cast_uint16_to_uint32(a: u16) -> u32 {
57 u32::from(a)
58}
59
60#[sqlfunc(
61 sqlname = "uint2_to_uint8",
62 preserves_uniqueness = true,
63 inverse = to_unary!(super::CastUint64ToUint16),
64 is_monotone = true
65)]
66fn cast_uint16_to_uint64(a: u16) -> u64 {
67 u64::from(a)
68}
69
70#[sqlfunc(
71 sqlname = "uint2_to_smallint",
72 preserves_uniqueness = true,
73 inverse = to_unary!(super::CastInt16ToUint16),
74 is_monotone = true
75)]
76fn cast_uint16_to_int16(a: u16) -> Result<i16, EvalError> {
77 i16::try_from(a).or_else(|_| Err(EvalError::Int16OutOfRange(a.to_string().into())))
78}
79
80#[sqlfunc(
81 sqlname = "uint2_to_integer",
82 preserves_uniqueness = true,
83 inverse = to_unary!(super::CastInt32ToUint16),
84 is_monotone = true
85)]
86fn cast_uint16_to_int32(a: u16) -> i32 {
87 i32::from(a)
88}
89#[sqlfunc(
90 sqlname = "uint2_to_bigint",
91 preserves_uniqueness = true,
92 inverse = to_unary!(super::CastInt64ToUint16),
93 is_monotone = true
94)]
95fn cast_uint16_to_int64(a: u16) -> i64 {
96 i64::from(a)
97}
98
99#[sqlfunc(
100 sqlname = "uint2_to_text",
101 preserves_uniqueness = true,
102 inverse = to_unary!(super::CastStringToUint16)
103)]
104fn cast_uint16_to_string(a: u16) -> String {
105 let mut buf = String::new();
106 strconv::format_uint16(&mut buf, a);
107 buf
108}
109
110#[derive(
111 Ord,
112 PartialOrd,
113 Clone,
114 Debug,
115 Eq,
116 PartialEq,
117 Serialize,
118 Deserialize,
119 Hash,
120 MzReflect
121)]
122pub struct CastUint16ToNumeric(pub Option<NumericMaxScale>);
123
124impl EagerUnaryFunc for CastUint16ToNumeric {
125 type Input<'a> = u16;
126 type Output<'a> = Result<Numeric, EvalError>;
127
128 fn call<'a>(&self, a: Self::Input<'a>) -> Self::Output<'a> {
129 let mut a = Numeric::from(i32::from(a));
130 if let Some(scale) = self.0 {
131 if numeric::rescale(&mut a, scale.into_u8()).is_err() {
132 return Err(EvalError::NumericFieldOverflow);
133 }
134 }
135 Ok(a)
136 }
137
138 fn output_type(&self, input: SqlColumnType) -> SqlColumnType {
139 SqlScalarType::Numeric { max_scale: self.0 }.nullable(input.nullable)
140 }
141
142 fn could_error(&self) -> bool {
143 self.0.is_some()
144 }
145
146 fn inverse(&self) -> Option<crate::UnaryFunc> {
147 to_unary!(super::CastNumericToUint16)
148 }
149
150 fn is_monotone(&self) -> bool {
151 true
152 }
153}
154
155impl fmt::Display for CastUint16ToNumeric {
156 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
157 f.write_str("uint2_to_numeric")
158 }
159}