Skip to main content

mz_expr/scalar/func/
unmaterializable.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//
10// Portions of this file are derived from the PostgreSQL project. The original
11// source code is subject to the terms of the PostgreSQL license, a copy of
12// which can be found in the LICENSE file at the root of this repository.
13
14//! Unmaterializable functions.
15//!
16//! The definitions are placeholders and cannot be evaluated directly.
17//! Evaluation is handled directly within the `mz-adapter` crate.
18
19use std::fmt;
20
21use mz_lowertest::MzReflect;
22use mz_repr::{SqlColumnType, SqlScalarType};
23use serde::{Deserialize, Serialize};
24
25#[derive(
26    Ord,
27    PartialOrd,
28    Clone,
29    Debug,
30    Eq,
31    PartialEq,
32    Serialize,
33    Deserialize,
34    Hash,
35    MzReflect
36)]
37pub enum UnmaterializableFunc {
38    CurrentDatabase,
39    CurrentSchema,
40    CurrentSchemasWithSystem,
41    CurrentSchemasWithoutSystem,
42    CurrentTimestamp,
43    CurrentUser,
44    IsRbacEnabled,
45    MzEnvironmentId,
46    MzIsSuperuser,
47    MzNow,
48    MzRoleOidMemberships,
49    MzSessionId,
50    MzUptime,
51    MzVersion,
52    MzVersionNum,
53    PgBackendPid,
54    PgPostmasterStartTime,
55    SessionUser,
56    Version,
57    ViewableVariables,
58}
59
60impl UnmaterializableFunc {
61    pub fn output_type(&self) -> SqlColumnType {
62        match self {
63            UnmaterializableFunc::CurrentDatabase => SqlScalarType::String.nullable(false),
64            // TODO: The `CurrentSchema` function should return `name`. This is
65            // tricky in Materialize because `name` truncates to 63 characters
66            // but Materialize does not have a limit on identifier length.
67            UnmaterializableFunc::CurrentSchema => SqlScalarType::String.nullable(true),
68            // TODO: The `CurrentSchemas` function should return `name[]`. This
69            // is tricky in Materialize because `name` truncates to 63
70            // characters but Materialize does not have a limit on identifier
71            // length.
72            UnmaterializableFunc::CurrentSchemasWithSystem => {
73                SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(false)
74            }
75            UnmaterializableFunc::CurrentSchemasWithoutSystem => {
76                SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(false)
77            }
78            UnmaterializableFunc::CurrentTimestamp => {
79                SqlScalarType::TimestampTz { precision: None }.nullable(false)
80            }
81            UnmaterializableFunc::CurrentUser => SqlScalarType::String.nullable(false),
82            UnmaterializableFunc::IsRbacEnabled => SqlScalarType::Bool.nullable(false),
83            UnmaterializableFunc::MzEnvironmentId => SqlScalarType::String.nullable(false),
84            UnmaterializableFunc::MzIsSuperuser => SqlScalarType::Bool.nullable(false),
85            UnmaterializableFunc::MzNow => SqlScalarType::MzTimestamp.nullable(false),
86            UnmaterializableFunc::MzRoleOidMemberships => SqlScalarType::Map {
87                value_type: Box::new(SqlScalarType::Array(Box::new(SqlScalarType::String))),
88                custom_id: None,
89            }
90            .nullable(false),
91            UnmaterializableFunc::MzSessionId => SqlScalarType::Uuid.nullable(false),
92            UnmaterializableFunc::MzUptime => SqlScalarType::Interval.nullable(true),
93            UnmaterializableFunc::MzVersion => SqlScalarType::String.nullable(false),
94            UnmaterializableFunc::MzVersionNum => SqlScalarType::Int32.nullable(false),
95            UnmaterializableFunc::PgBackendPid => SqlScalarType::Int32.nullable(false),
96            UnmaterializableFunc::PgPostmasterStartTime => {
97                SqlScalarType::TimestampTz { precision: None }.nullable(false)
98            }
99            UnmaterializableFunc::SessionUser => SqlScalarType::String.nullable(false),
100            UnmaterializableFunc::Version => SqlScalarType::String.nullable(false),
101            UnmaterializableFunc::ViewableVariables => SqlScalarType::Map {
102                value_type: Box::new(SqlScalarType::String),
103                custom_id: None,
104            }
105            .nullable(false),
106        }
107    }
108}
109
110impl fmt::Display for UnmaterializableFunc {
111    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
112        match self {
113            UnmaterializableFunc::CurrentDatabase => f.write_str("current_database"),
114            UnmaterializableFunc::CurrentSchema => f.write_str("current_schema"),
115            UnmaterializableFunc::CurrentSchemasWithSystem => f.write_str("current_schemas(true)"),
116            UnmaterializableFunc::CurrentSchemasWithoutSystem => {
117                f.write_str("current_schemas(false)")
118            }
119            UnmaterializableFunc::CurrentTimestamp => f.write_str("current_timestamp"),
120            UnmaterializableFunc::CurrentUser => f.write_str("current_user"),
121            UnmaterializableFunc::IsRbacEnabled => f.write_str("is_rbac_enabled"),
122            UnmaterializableFunc::MzEnvironmentId => f.write_str("mz_environment_id"),
123            UnmaterializableFunc::MzIsSuperuser => f.write_str("mz_is_superuser"),
124            UnmaterializableFunc::MzNow => f.write_str("mz_now"),
125            UnmaterializableFunc::MzRoleOidMemberships => f.write_str("mz_role_oid_memberships"),
126            UnmaterializableFunc::MzSessionId => f.write_str("mz_session_id"),
127            UnmaterializableFunc::MzUptime => f.write_str("mz_uptime"),
128            UnmaterializableFunc::MzVersion => f.write_str("mz_version"),
129            UnmaterializableFunc::MzVersionNum => f.write_str("mz_version_num"),
130            UnmaterializableFunc::PgBackendPid => f.write_str("pg_backend_pid"),
131            UnmaterializableFunc::PgPostmasterStartTime => f.write_str("pg_postmaster_start_time"),
132            UnmaterializableFunc::SessionUser => f.write_str("session_user"),
133            UnmaterializableFunc::Version => f.write_str("version"),
134            UnmaterializableFunc::ViewableVariables => f.write_str("viewable_variables"),
135        }
136    }
137}