mz_expr/scalar/func/impls/
oid.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_ore::cast::ReinterpretCast;
11use mz_pgrepr::Type;
12use mz_repr::adt::system::{Oid, RegClass, RegProc, RegType};
13use mz_repr::strconv;
14
15sqlfunc!(
16    #[sqlname = "oid_to_text"]
17    #[preserves_uniqueness = true]
18    #[inverse = to_unary!(super::CastStringToOid)]
19    fn cast_oid_to_string(a: Oid) -> String {
20        let mut buf = String::new();
21        strconv::format_uint32(&mut buf, a.0);
22        buf
23    }
24);
25
26sqlfunc!(
27    #[sqlname = "oid_to_integer"]
28    #[preserves_uniqueness = true]
29    #[inverse = to_unary!(super::CastInt32ToOid)]
30    fn cast_oid_to_int32(a: Oid) -> i32 {
31        // For historical reasons in PostgreSQL, the bytes of the `u32` are
32        // reinterpreted as an `i32` without bounds checks, so very large
33        // positive OIDs become negative `i32`s.
34        //
35        // Do not use this as a model for behavior in other contexts. OIDs
36        // should not in general be thought of as freely convertible to `i32`s.
37        i32::reinterpret_cast(a.0)
38    }
39);
40
41sqlfunc!(
42    #[sqlname = "oid_to_bigint"]
43    #[preserves_uniqueness = true]
44    #[inverse = to_unary!(super::CastInt64ToOid)]
45    fn cast_oid_to_int64(a: Oid) -> i64 {
46        i64::from(a.0)
47    }
48);
49
50sqlfunc!(
51    #[sqlname = "oidtoregclass"]
52    #[preserves_uniqueness = true]
53    #[inverse = to_unary!(super::CastRegClassToOid)]
54    fn cast_oid_to_reg_class(a: Oid) -> RegClass {
55        RegClass(a.0)
56    }
57);
58
59sqlfunc!(
60    #[sqlname = "oidtoregproc"]
61    #[preserves_uniqueness = true]
62    #[inverse = to_unary!(super::CastRegProcToOid)]
63    fn cast_oid_to_reg_proc(a: Oid) -> RegProc {
64        RegProc(a.0)
65    }
66);
67
68sqlfunc!(
69    #[sqlname = "oidtoregtype"]
70    #[preserves_uniqueness = true]
71    #[inverse = to_unary!(super::CastRegTypeToOid)]
72    fn cast_oid_to_reg_type(a: Oid) -> RegType {
73        RegType(a.0)
74    }
75);
76
77sqlfunc!(
78    fn mz_type_name<'a>(oid: Oid) -> Option<String> {
79        if let Ok(t) = Type::from_oid(oid.0) {
80            Some(t.name().to_string())
81        } else {
82            None
83        }
84    }
85);