mz_expr/scalar/func/impls/
datum.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::{Datum, DatumList};
12
13use crate::EvalError;
14
15#[sqlfunc(sqlname = "isnull", is_monotone = true)]
16fn is_null<'a>(a: Datum<'a>) -> bool {
17    a.is_null()
18}
19
20#[sqlfunc(sqlname = "istrue")]
21fn is_true<'a>(a: Datum<'a>) -> bool {
22    a == Datum::True
23}
24
25#[sqlfunc(sqlname = "isfalse")]
26fn is_false<'a>(a: Datum<'a>) -> bool {
27    a == Datum::False
28}
29
30#[sqlfunc]
31fn pg_column_size<'a>(a: Datum<'a>) -> Result<Option<i32>, EvalError> {
32    match a {
33        Datum::Null => Ok(None),
34        datum => {
35            let sz = mz_repr::datum_size(&datum);
36            i32::try_from(sz)
37                .map(Some)
38                .or_else(|_| Err(EvalError::Int32OutOfRange(sz.to_string().into())))
39        }
40    }
41}
42
43#[sqlfunc]
44// TODO[btv] - if we plan to keep changing row format,
45// should we make this unmaterializable?
46fn mz_row_size<'a>(a: DatumList<'a>) -> Result<i32, EvalError> {
47    let sz = mz_repr::row_size(a.iter());
48    i32::try_from(sz).or_else(|_| Err(EvalError::Int32OutOfRange(sz.to_string().into())))
49}