mz_expr/scalar/func/impls/
int2vector.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 std::fmt;
11
12use mz_lowertest::MzReflect;
13use mz_repr::{Datum, RowArena, SqlColumnType, SqlScalarType};
14use serde::{Deserialize, Serialize};
15
16use crate::scalar::func::{LazyUnaryFunc, stringify_datum};
17use crate::{EvalError, MirScalarExpr};
18
19#[derive(Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect)]
20pub struct CastInt2VectorToArray;
21
22// This could be simplified to an EagerUnaryFunc once we have
23// auto-parameterization of array built-in functions.
24impl LazyUnaryFunc for CastInt2VectorToArray {
25    fn eval<'a>(
26        &'a self,
27        datums: &[Datum<'a>],
28        temp_storage: &'a RowArena,
29        a: &'a MirScalarExpr,
30    ) -> Result<Datum<'a>, EvalError> {
31        a.eval(datums, temp_storage)
32    }
33
34    /// The output SqlColumnType of this function
35    fn output_type(&self, input_type: SqlColumnType) -> SqlColumnType {
36        SqlScalarType::Array(Box::from(SqlScalarType::Int16)).nullable(input_type.nullable)
37    }
38
39    /// Whether this function will produce NULL on NULL input
40    fn propagates_nulls(&self) -> bool {
41        true
42    }
43
44    /// Whether this function will produce NULL on non-NULL input
45    fn introduces_nulls(&self) -> bool {
46        false
47    }
48
49    /// Whether this function preserves uniqueness
50    fn preserves_uniqueness(&self) -> bool {
51        false
52    }
53
54    fn inverse(&self) -> Option<crate::UnaryFunc> {
55        None
56    }
57
58    fn is_monotone(&self) -> bool {
59        true // A noop is trivially monotone.
60    }
61}
62
63impl fmt::Display for CastInt2VectorToArray {
64    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
65        f.write_str("int2vectortoarray")
66    }
67}
68
69#[derive(Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect)]
70pub struct CastInt2VectorToString;
71
72impl LazyUnaryFunc for CastInt2VectorToString {
73    fn eval<'a>(
74        &'a self,
75        datums: &[Datum<'a>],
76        temp_storage: &'a RowArena,
77        a: &'a MirScalarExpr,
78    ) -> Result<Datum<'a>, EvalError> {
79        let a = a.eval(datums, temp_storage)?;
80        if a.is_null() {
81            return Ok(Datum::Null);
82        }
83        let mut buf = String::new();
84        stringify_datum(&mut buf, a, &SqlScalarType::Int2Vector)?;
85        Ok(Datum::String(temp_storage.push_string(buf)))
86    }
87
88    fn output_type(&self, input_type: SqlColumnType) -> SqlColumnType {
89        SqlScalarType::String.nullable(input_type.nullable)
90    }
91
92    fn propagates_nulls(&self) -> bool {
93        true
94    }
95
96    fn introduces_nulls(&self) -> bool {
97        false
98    }
99
100    fn preserves_uniqueness(&self) -> bool {
101        true
102    }
103
104    fn inverse(&self) -> Option<crate::UnaryFunc> {
105        to_unary!(super::CastStringToInt2Vector)
106    }
107
108    fn is_monotone(&self) -> bool {
109        false
110    }
111}
112
113impl fmt::Display for CastInt2VectorToString {
114    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
115        f.write_str("int2vectortostr")
116    }
117}