Skip to main content

mz_expr/scalar/func/impls/
char.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::SqlScalarType;
12use mz_repr::adt::char::{Char, CharLength, format_str_pad};
13use serde::{Deserialize, Serialize};
14
15/// All Char data is stored in Datum::String with its blank padding removed
16/// (i.e. trimmed), so this function provides a means of restoring any
17/// removed padding.
18#[derive(
19    Ord,
20    PartialOrd,
21    Clone,
22    Debug,
23    Eq,
24    PartialEq,
25    Serialize,
26    Deserialize,
27    Hash
28)]
29pub struct PadChar {
30    pub length: Option<CharLength>,
31}
32
33#[sqlfunc(
34    PadChar,
35    sqlname = "padchar",
36    output_type_expr = SqlScalarType::Char { length: self.length }.nullable(input_type.nullable)
37)]
38fn pad_char<'a>(&self, a: &'a str) -> Char<String> {
39    Char(format_str_pad(a, self.length))
40}
41
42// This function simply allows the expression of changing a's type from char to
43// string
44#[sqlfunc(
45    sqlname = "char_to_text",
46    preserves_uniqueness = true,
47    is_eliminable_cast = true,
48    inverse = super::CastStringToChar{
49        length: None,
50        fail_on_len: false,
51    }
52)]
53fn cast_char_to_string<'a>(a: Char<&'a str>) -> &'a str {
54    a.0
55}