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(Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect)]
111pub struct CastUint16ToNumeric(pub Option<NumericMaxScale>);
112
113impl<'a> EagerUnaryFunc<'a> for CastUint16ToNumeric {
114 type Input = u16;
115 type Output = Result<Numeric, EvalError>;
116
117 fn call(&self, a: u16) -> Result<Numeric, EvalError> {
118 let mut a = Numeric::from(i32::from(a));
119 if let Some(scale) = self.0 {
120 if numeric::rescale(&mut a, scale.into_u8()).is_err() {
121 return Err(EvalError::NumericFieldOverflow);
122 }
123 }
124 Ok(a)
125 }
126
127 fn output_type(&self, input: SqlColumnType) -> SqlColumnType {
128 SqlScalarType::Numeric { max_scale: self.0 }.nullable(input.nullable)
129 }
130
131 fn could_error(&self) -> bool {
132 self.0.is_some()
133 }
134
135 fn inverse(&self) -> Option<crate::UnaryFunc> {
136 to_unary!(super::CastNumericToUint16)
137 }
138
139 fn is_monotone(&self) -> bool {
140 true
141 }
142}
143
144impl fmt::Display for CastUint16ToNumeric {
145 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
146 f.write_str("uint2_to_numeric")
147 }
148}