1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use mz_pgrepr::oid;
use mz_repr::namespaces::MZ_INTERNAL_SCHEMA;
use mz_repr::{RelationDesc, ScalarType};
use mz_sql::catalog::NameReference;
use std::sync::LazyLock;

use crate::builtin::{Builtin, BuiltinIndex, BuiltinTable, BuiltinView, MONITOR_SELECT};

use super::{MONITOR_REDACTED_SELECT, SUPPORT_SELECT};

pub static MZ_OPTIMIZER_NOTICES: LazyLock<BuiltinTable> = LazyLock::new(|| {
    use ScalarType::{List, String, TimestampTz};

    BuiltinTable {
        name: "mz_optimizer_notices",
        schema: MZ_INTERNAL_SCHEMA,
        oid: oid::TABLE_MZ_OPTIMIZER_NOTICES_OID,
        desc: RelationDesc::builder()
            .with_column("id", String.nullable(false))
            .with_column("notice_type", String.nullable(false))
            .with_column("message", String.nullable(false))
            .with_column("hint", String.nullable(false))
            .with_column("action", String.nullable(true))
            .with_column("redacted_message", String.nullable(true))
            .with_column("redacted_hint", String.nullable(true))
            .with_column("redacted_action", String.nullable(true))
            .with_column("action_type", String.nullable(true))
            .with_column("object_id", String.nullable(true))
            .with_column(
                "dependency_ids",
                List {
                    element_type: Box::new(String),
                    custom_id: None,
                }
                .nullable(false),
            )
            .with_column(
                "created_at",
                TimestampTz { precision: None }.nullable(false),
            )
            .with_key(vec![0])
            .finish(),
        is_retained_metrics_object: false,
        access: vec![MONITOR_SELECT],
    }
});

/// An [`MZ_NOTICES`] that is made safe to be viewed by Materialize staff
/// because it binds the `redacted_~` from [`MZ_NOTICES`] as `~`.
///
/// This view is provisioned to accomodate the union of notice types (optimizer,
/// sources and sinks, etc). Even though at the moment is only hosts optimizer
/// notices, the idea is to evolve it over time as sketched in the design doc[^1].
///
/// [^1] <https://github.com/MaterializeInc/materialize/blob/main/doc/developer/design/20231113_optimizer_notice_catalog.md>
pub static MZ_NOTICES: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
    name: "mz_notices",
    schema: MZ_INTERNAL_SCHEMA,
    oid: oid::VIEW_MZ_NOTICES_OID,
    column_defs: None,
    sql: "SELECT
    n.id,
    n.notice_type,
    n.message,
    n.hint,
    n.action,
    n.redacted_message,
    n.redacted_hint,
    n.redacted_action,
    n.action_type,
    n.object_id,
    n.created_at
FROM
    mz_internal.mz_optimizer_notices n
",
    access: vec![MONITOR_SELECT],
});

/// A redacted version of [`MZ_NOTICES`] that is made safe to be viewed by
/// Materialize staff because it binds the `redacted_~` from [`MZ_NOTICES`] as
/// `~`.
pub static MZ_NOTICES_REDACTED: LazyLock<BuiltinView> = LazyLock::new(|| BuiltinView {
    name: "mz_notices_redacted",
    schema: MZ_INTERNAL_SCHEMA,
    oid: oid::VIEW_MZ_NOTICES_REDACTED_OID,
    column_defs: None,
    sql: "SELECT
    id,
    notice_type,
    coalesce(redacted_message, message) as message,
    coalesce(redacted_hint, hint) as hint,
    coalesce(redacted_action, action) as action,
    action_type,
    object_id,
    created_at
FROM
    mz_internal.mz_notices
",
    access: vec![SUPPORT_SELECT, MONITOR_REDACTED_SELECT, MONITOR_SELECT],
});

pub const MZ_NOTICES_IND: BuiltinIndex = BuiltinIndex {
    name: "mz_notices_ind",
    schema: MZ_INTERNAL_SCHEMA,
    oid: oid::INDEX_MZ_NOTICES_IND_OID,
    sql: "IN CLUSTER mz_catalog_server ON mz_internal.mz_notices(id)",
    is_retained_metrics_object: false,
};

/// An iterator over [`Builtin`] objects for optimization hints.
///
/// Used in the [`super::BUILTINS_STATIC`] initializer.
pub(super) fn builtins() -> impl Iterator<Item = Builtin<NameReference>> {
    [
        Builtin::Table(&MZ_OPTIMIZER_NOTICES),
        Builtin::View(&MZ_NOTICES),
        Builtin::View(&MZ_NOTICES_REDACTED),
        Builtin::Index(&MZ_NOTICES_IND),
    ]
    .into_iter()
}