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(Ord, PartialOrd, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash, MzReflect)]
26pub enum UnmaterializableFunc {
27    CurrentDatabase,
28    CurrentSchema,
29    CurrentSchemasWithSystem,
30    CurrentSchemasWithoutSystem,
31    CurrentTimestamp,
32    CurrentUser,
33    IsRbacEnabled,
34    MzEnvironmentId,
35    MzIsSuperuser,
36    MzNow,
37    MzRoleOidMemberships,
38    MzSessionId,
39    MzUptime,
40    MzVersion,
41    MzVersionNum,
42    PgBackendPid,
43    PgPostmasterStartTime,
44    SessionUser,
45    Version,
46    ViewableVariables,
47}
48
49impl UnmaterializableFunc {
50    pub fn output_type(&self) -> SqlColumnType {
51        match self {
52            UnmaterializableFunc::CurrentDatabase => SqlScalarType::String.nullable(false),
53            // TODO: The `CurrentSchema` function should return `name`. This is
54            // tricky in Materialize because `name` truncates to 63 characters
55            // but Materialize does not have a limit on identifier length.
56            UnmaterializableFunc::CurrentSchema => SqlScalarType::String.nullable(true),
57            // TODO: The `CurrentSchemas` function should return `name[]`. This
58            // is tricky in Materialize because `name` truncates to 63
59            // characters but Materialize does not have a limit on identifier
60            // length.
61            UnmaterializableFunc::CurrentSchemasWithSystem => {
62                SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(false)
63            }
64            UnmaterializableFunc::CurrentSchemasWithoutSystem => {
65                SqlScalarType::Array(Box::new(SqlScalarType::String)).nullable(false)
66            }
67            UnmaterializableFunc::CurrentTimestamp => {
68                SqlScalarType::TimestampTz { precision: None }.nullable(false)
69            }
70            UnmaterializableFunc::CurrentUser => SqlScalarType::String.nullable(false),
71            UnmaterializableFunc::IsRbacEnabled => SqlScalarType::Bool.nullable(false),
72            UnmaterializableFunc::MzEnvironmentId => SqlScalarType::String.nullable(false),
73            UnmaterializableFunc::MzIsSuperuser => SqlScalarType::Bool.nullable(false),
74            UnmaterializableFunc::MzNow => SqlScalarType::MzTimestamp.nullable(false),
75            UnmaterializableFunc::MzRoleOidMemberships => SqlScalarType::Map {
76                value_type: Box::new(SqlScalarType::Array(Box::new(SqlScalarType::String))),
77                custom_id: None,
78            }
79            .nullable(false),
80            UnmaterializableFunc::MzSessionId => SqlScalarType::Uuid.nullable(false),
81            UnmaterializableFunc::MzUptime => SqlScalarType::Interval.nullable(true),
82            UnmaterializableFunc::MzVersion => SqlScalarType::String.nullable(false),
83            UnmaterializableFunc::MzVersionNum => SqlScalarType::Int32.nullable(false),
84            UnmaterializableFunc::PgBackendPid => SqlScalarType::Int32.nullable(false),
85            UnmaterializableFunc::PgPostmasterStartTime => {
86                SqlScalarType::TimestampTz { precision: None }.nullable(false)
87            }
88            UnmaterializableFunc::SessionUser => SqlScalarType::String.nullable(false),
89            UnmaterializableFunc::Version => SqlScalarType::String.nullable(false),
90            UnmaterializableFunc::ViewableVariables => SqlScalarType::Map {
91                value_type: Box::new(SqlScalarType::String),
92                custom_id: None,
93            }
94            .nullable(false),
95        }
96    }
97}
98
99impl fmt::Display for UnmaterializableFunc {
100    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
101        match self {
102            UnmaterializableFunc::CurrentDatabase => f.write_str("current_database"),
103            UnmaterializableFunc::CurrentSchema => f.write_str("current_schema"),
104            UnmaterializableFunc::CurrentSchemasWithSystem => f.write_str("current_schemas(true)"),
105            UnmaterializableFunc::CurrentSchemasWithoutSystem => {
106                f.write_str("current_schemas(false)")
107            }
108            UnmaterializableFunc::CurrentTimestamp => f.write_str("current_timestamp"),
109            UnmaterializableFunc::CurrentUser => f.write_str("current_user"),
110            UnmaterializableFunc::IsRbacEnabled => f.write_str("is_rbac_enabled"),
111            UnmaterializableFunc::MzEnvironmentId => f.write_str("mz_environment_id"),
112            UnmaterializableFunc::MzIsSuperuser => f.write_str("mz_is_superuser"),
113            UnmaterializableFunc::MzNow => f.write_str("mz_now"),
114            UnmaterializableFunc::MzRoleOidMemberships => f.write_str("mz_role_oid_memberships"),
115            UnmaterializableFunc::MzSessionId => f.write_str("mz_session_id"),
116            UnmaterializableFunc::MzUptime => f.write_str("mz_uptime"),
117            UnmaterializableFunc::MzVersion => f.write_str("mz_version"),
118            UnmaterializableFunc::MzVersionNum => f.write_str("mz_version_num"),
119            UnmaterializableFunc::PgBackendPid => f.write_str("pg_backend_pid"),
120            UnmaterializableFunc::PgPostmasterStartTime => f.write_str("pg_postmaster_start_time"),
121            UnmaterializableFunc::SessionUser => f.write_str("session_user"),
122            UnmaterializableFunc::Version => f.write_str("version"),
123            UnmaterializableFunc::ViewableVariables => f.write_str("viewable_variables"),
124        }
125    }
126}