mz_expr/scalar/func/impls/
boolean.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::strconv;
11
12sqlfunc!(
13    #[sqlname = "NOT"]
14    #[preserves_uniqueness = true]
15    #[inverse = to_unary!(Not)]
16    #[is_monotone = true]
17    fn not(a: bool) -> bool {
18        !a
19    }
20);
21
22sqlfunc!(
23    #[sqlname = "boolean_to_text"]
24    #[preserves_uniqueness = true]
25    #[inverse = to_unary!(super::CastStringToBool)]
26    #[is_monotone = true]
27    fn cast_bool_to_string<'a>(a: bool) -> &'a str {
28        match a {
29            true => "true",
30            false => "false",
31        }
32    }
33);
34
35sqlfunc!(
36    #[sqlname = "boolean_to_nonstandard_text"]
37    #[preserves_uniqueness = true]
38    #[inverse = to_unary!(super::CastStringToBool)]
39    #[is_monotone = true]
40    fn cast_bool_to_string_nonstandard<'a>(a: bool) -> &'a str {
41        // N.B. this function differs from `cast_bool_to_string` because
42        // the SQL specification requires `true` and `false` to be spelled out in
43        // explicit casts, while PostgreSQL prefers its more concise `t` and `f`
44        // representation in some contexts, for historical reasons.
45        strconv::format_bool_static(a)
46    }
47);
48
49sqlfunc!(
50    #[sqlname = "boolean_to_integer"]
51    #[preserves_uniqueness = true]
52    #[inverse = to_unary!(super::CastInt32ToBool)]
53    #[is_monotone = true]
54    fn cast_bool_to_int32(a: bool) -> i32 {
55        match a {
56            true => 1,
57            false => 0,
58        }
59    }
60);
61
62sqlfunc!(
63    #[sqlname = "boolean_to_bigint"]
64    #[preserves_uniqueness = true]
65    #[inverse = to_unary!(super::CastInt64ToBool)]
66    #[is_monotone = true]
67    fn cast_bool_to_int64(a: bool) -> i64 {
68        match a {
69            true => 1,
70            false => 0,
71        }
72    }
73);