Skip to main content

mz_expr/scalar/func/impls/
uint16.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 mz_expr_derive::sqlfunc;
11use mz_repr::adt::numeric::{self, Numeric, NumericMaxScale};
12use mz_repr::{SqlScalarType, strconv};
13use serde::{Deserialize, Serialize};
14
15use crate::EvalError;
16
17#[sqlfunc(
18    sqlname = "~",
19    preserves_uniqueness = true,
20    inverse = super::BitNotUint16
21)]
22fn bit_not_uint16(a: u16) -> u16 {
23    !a
24}
25
26#[sqlfunc(
27    sqlname = "uint2_to_real",
28    preserves_uniqueness = true,
29    inverse = super::CastFloat32ToUint16,
30    is_monotone = true
31)]
32fn cast_uint16_to_float32(a: u16) -> f32 {
33    f32::from(a)
34}
35
36#[sqlfunc(
37    sqlname = "uint2_to_double",
38    preserves_uniqueness = true,
39    inverse = super::CastFloat64ToUint16,
40    is_monotone = true
41)]
42fn cast_uint16_to_float64(a: u16) -> f64 {
43    f64::from(a)
44}
45
46#[sqlfunc(
47    sqlname = "uint2_to_uint4",
48    preserves_uniqueness = true,
49    inverse = super::CastUint32ToUint16,
50    is_monotone = true
51)]
52fn cast_uint16_to_uint32(a: u16) -> u32 {
53    u32::from(a)
54}
55
56#[sqlfunc(
57    sqlname = "uint2_to_uint8",
58    preserves_uniqueness = true,
59    inverse = super::CastUint64ToUint16,
60    is_monotone = true
61)]
62fn cast_uint16_to_uint64(a: u16) -> u64 {
63    u64::from(a)
64}
65
66#[sqlfunc(
67    sqlname = "uint2_to_smallint",
68    preserves_uniqueness = true,
69    inverse = super::CastInt16ToUint16,
70    is_monotone = true
71)]
72fn cast_uint16_to_int16(a: u16) -> Result<i16, EvalError> {
73    i16::try_from(a).or_else(|_| Err(EvalError::Int16OutOfRange(a.to_string().into())))
74}
75
76#[sqlfunc(
77    sqlname = "uint2_to_integer",
78    preserves_uniqueness = true,
79    inverse = super::CastInt32ToUint16,
80    is_monotone = true
81)]
82fn cast_uint16_to_int32(a: u16) -> i32 {
83    i32::from(a)
84}
85#[sqlfunc(
86    sqlname = "uint2_to_bigint",
87    preserves_uniqueness = true,
88    inverse = super::CastInt64ToUint16,
89    is_monotone = true
90)]
91fn cast_uint16_to_int64(a: u16) -> i64 {
92    i64::from(a)
93}
94
95#[sqlfunc(
96    sqlname = "uint2_to_text",
97    preserves_uniqueness = true,
98    inverse = super::CastStringToUint16
99)]
100fn cast_uint16_to_string(a: u16) -> String {
101    let mut buf = String::new();
102    strconv::format_uint16(&mut buf, a);
103    buf
104}
105
106#[derive(
107    Ord,
108    PartialOrd,
109    Clone,
110    Debug,
111    Eq,
112    PartialEq,
113    Serialize,
114    Deserialize,
115    Hash
116)]
117pub struct CastUint16ToNumeric(pub Option<NumericMaxScale>);
118
119#[sqlfunc(
120    CastUint16ToNumeric,
121    sqlname = "uint2_to_numeric",
122    could_error = self.0.is_some(),
123    inverse = super::CastNumericToUint16,
124    is_monotone = true,
125    output_type_expr = SqlScalarType::Numeric { max_scale: self.0 }
126        .nullable(input_type.nullable)
127)]
128fn cast_uint16_to_numeric(&self, a: u16) -> Result<Numeric, EvalError> {
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}