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