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