Skip to main content

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 mz_expr_derive::sqlfunc;
11use mz_repr::adt::array::Array;
12use mz_repr::{Datum, Int2Vector, SqlScalarType};
13
14use crate::EvalError;
15use crate::scalar::func::stringify_datum;
16
17#[sqlfunc(
18    sqlname = "int2vectortoarray",
19    is_monotone = true,
20    introduces_nulls = false,
21    is_eliminable_cast = true,
22    output_type_expr = SqlScalarType::Array(Box::from(SqlScalarType::Int16))
23        .nullable(input_type.nullable)
24)]
25fn cast_int2_vector_to_array<'a>(a: Int2Vector<'a>) -> Array<'a> {
26    a.0
27}
28
29#[sqlfunc(
30    sqlname = "int2vectortostr",
31    preserves_uniqueness = true,
32    inverse = to_unary!(super::CastStringToInt2Vector)
33)]
34fn cast_int2_vector_to_string<'a>(a: Int2Vector<'a>) -> Result<String, EvalError> {
35    let mut buf = String::new();
36    stringify_datum(&mut buf, Datum::Array(a.0), &SqlScalarType::Int2Vector)?;
37    Ok(buf)
38}