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