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.
910use mz_ore::cast::ReinterpretCast;
11use mz_pgrepr::Type;
12use mz_repr::adt::system::{Oid, RegClass, RegProc, RegType};
13use mz_repr::strconv;
1415sqlfunc!(
16#[sqlname = "oid_to_text"]
17 #[preserves_uniqueness = true]
18 #[inverse = to_unary!(super::CastStringToOid)]
19fn cast_oid_to_string(a: Oid) -> String {
20let mut buf = String::new();
21 strconv::format_uint32(&mut buf, a.0);
22 buf
23 }
24);
2526sqlfunc!(
27#[sqlname = "oid_to_integer"]
28 #[preserves_uniqueness = true]
29 #[inverse = to_unary!(super::CastInt32ToOid)]
30fn 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.
37i32::reinterpret_cast(a.0)
38 }
39);
4041sqlfunc!(
42#[sqlname = "oid_to_bigint"]
43 #[preserves_uniqueness = true]
44 #[inverse = to_unary!(super::CastInt64ToOid)]
45fn cast_oid_to_int64(a: Oid) -> i64 {
46 i64::from(a.0)
47 }
48);
4950sqlfunc!(
51#[sqlname = "oidtoregclass"]
52 #[preserves_uniqueness = true]
53 #[inverse = to_unary!(super::CastRegClassToOid)]
54fn cast_oid_to_reg_class(a: Oid) -> RegClass {
55 RegClass(a.0)
56 }
57);
5859sqlfunc!(
60#[sqlname = "oidtoregproc"]
61 #[preserves_uniqueness = true]
62 #[inverse = to_unary!(super::CastRegProcToOid)]
63fn cast_oid_to_reg_proc(a: Oid) -> RegProc {
64 RegProc(a.0)
65 }
66);
6768sqlfunc!(
69#[sqlname = "oidtoregtype"]
70 #[preserves_uniqueness = true]
71 #[inverse = to_unary!(super::CastRegTypeToOid)]
72fn cast_oid_to_reg_type(a: Oid) -> RegType {
73 RegType(a.0)
74 }
75);
7677sqlfunc!(
78fn mz_type_name<'a>(oid: Oid) -> Option<String> {
79if let Ok(t) = Type::from_oid(oid.0) {
80Some(t.name().to_string())
81 } else {
82None
83}
84 }
85);