mz_expr/scalar/func/impls/
char.rs1use std::fmt;
11
12use mz_expr_derive::sqlfunc;
13use mz_repr::adt::char::{Char, CharLength, format_str_pad};
14use mz_repr::{SqlColumnType, SqlScalarType};
15use serde::{Deserialize, Serialize};
16
17use crate::scalar::func::EagerUnaryFunc;
18
19#[derive(
23 Ord,
24 PartialOrd,
25 Clone,
26 Debug,
27 Eq,
28 PartialEq,
29 Serialize,
30 Deserialize,
31 Hash
32)]
33pub struct PadChar {
34 pub length: Option<CharLength>,
35}
36
37impl EagerUnaryFunc for PadChar {
38 type Input<'a> = &'a str;
39 type Output<'a> = Char<String>;
40
41 fn call<'a>(&self, a: Self::Input<'a>) -> Self::Output<'a> {
42 Char(format_str_pad(a, self.length))
43 }
44
45 fn output_sql_type(&self, input: SqlColumnType) -> SqlColumnType {
46 SqlScalarType::Char {
47 length: self.length,
48 }
49 .nullable(input.nullable)
50 }
51}
52
53impl fmt::Display for PadChar {
54 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55 f.write_str("padchar")
56 }
57}
58
59#[sqlfunc(
62 sqlname = "char_to_text",
63 preserves_uniqueness = true,
64 is_eliminable_cast = true,
65 inverse = to_unary!(super::CastStringToChar{
66 length: None,
67 fail_on_len: false,
68 })
69)]
70fn cast_char_to_string<'a>(a: Char<&'a str>) -> &'a str {
71 a.0
72}