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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// 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.

//! Notices that the optimizer wants to show to users.
//!
//! The top-level notice types are [`RawOptimizerNotice`] (for notices emitted
//! by optimizer pipelines) and [`OptimizerNotice`] (for notices stored in the
//! catalog memory). The `adapter` module contains code for converting the
//! former to the latter.
//!
//! The [`RawOptimizerNotice`] type is an enum generated by the
//! `raw_optimizer_notices` macro. Each notice type lives in its own submodule
//! and implements the [`OptimizerNoticeApi`] trait.
//!
//! To add a new notice do the following:
//!
//! 1. Create a new submodule.
//! 2. Define a struct for the new notice in that submodule.
//! 3. Implement [`OptimizerNoticeApi`] for that struct.
//! 4. Re-export the notice type in this module.
//! 5. Add the notice type to the `raw_optimizer_notices` macro which generates
//!    the [`RawOptimizerNotice`] enum and other boilerplate code.

// Modules (one for each notice type).
mod index_already_exists;
mod index_key_empty;
mod index_too_wide_for_literal_constraints;

pub use index_already_exists::IndexAlreadyExists;
pub use index_key_empty::IndexKeyEmpty;
pub use index_too_wide_for_literal_constraints::IndexTooWideForLiteralConstraints;

use std::collections::BTreeSet;
use std::fmt::{self, Error, Formatter, Write};
use std::sync::Arc;
use std::{concat, stringify};

use enum_kinds::EnumKind;
use mz_repr::explain::ExprHumanizer;
use mz_repr::GlobalId;
use proptest_derive::Arbitrary;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Arbitrary)]
/// An long lived in-memory representation of a [`RawOptimizerNotice`] that is
/// meant to be kept as part of the hydrated catalog state.
pub struct OptimizerNotice {
    /// An `id` that uniquely identifies this notice in the `mz_notices` relation.
    pub id: GlobalId,
    /// The notice kind.
    pub kind: OptimizerNoticeKind,
    /// The ID of the catalog item associated with this notice.
    ///
    /// This is `None` if the notice is scoped to the entire catalog.
    pub item_id: Option<GlobalId>,
    /// A set of ids that need to exist for this notice to be considered valid.
    /// Removing any of the IDs in this set will result in the notice being
    /// asynchronously removed from the catalog state.
    pub dependencies: BTreeSet<GlobalId>,
    /// A brief description of what concretely went wrong.
    ///
    /// Details and context about situations in which this notice kind would be
    /// emitted should be reserved for the documentation page for this notice
    /// kind.
    pub message: String,
    /// A high-level hint that tells the user what can be improved.
    pub hint: String,
    /// A recommended action. This is a more concrete version of the hint.
    pub action: Action,
    /// A redacted version of the `message` field.
    pub message_redacted: Option<String>,
    /// A redacted version of the `hint` field.
    pub hint_redacted: Option<String>,
    /// A redacted version of the `action` field.
    pub action_redacted: Option<Action>,
    /// The date at which this notice was last created.
    pub created_at: u64,
}

impl OptimizerNotice {
    /// Turns a vector of notices into a vector of strings that can be used in
    /// EXPLAIN.
    ///
    /// This method should be consistent with [`RawOptimizerNotice::explain`].
    pub fn explain(
        notices: &Vec<Arc<Self>>,
        humanizer: &dyn ExprHumanizer,
        redacted: bool,
    ) -> Result<Vec<String>, Error> {
        let mut notice_strings = Vec::new();
        for notice in notices {
            if notice.is_valid(humanizer) {
                let mut s = String::new();
                let message = match notice.message_redacted.as_deref() {
                    Some(message_redacted) if redacted => message_redacted,
                    _ => notice.message.as_str(),
                };
                let hint = match notice.hint_redacted.as_deref() {
                    Some(hint_redacted) if redacted => hint_redacted,
                    _ => notice.hint.as_str(),
                };
                write!(s, "  - Notice: {}\n", message)?;
                write!(s, "    Hint: {}", hint)?;
                notice_strings.push(s);
            }
        }
        Ok(notice_strings)
    }

    /// Returns `true` iff both the dependencies and the associated item for
    /// this notice still exist.
    ///
    /// This method should be consistent with [`RawOptimizerNotice::is_valid`].
    fn is_valid(&self, humanizer: &dyn ExprHumanizer) -> bool {
        // All dependencies exist.
        self.dependencies.iter().all(|id| humanizer.id_exists(*id))
    }
}

#[derive(
    EnumKind, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize, Arbitrary,
)]
#[enum_kind(ActionKind)]
/// An action attached to an [`OptimizerNotice`]
pub enum Action {
    /// No action.
    None,
    /// An action that cannot be defined as a program.
    PlainText(String),
    /// One or more SQL statements
    ///
    /// The statements should be formatted and fully-qualified names, meaning
    /// that this field can be rendered in the console with a button that
    /// executes this as a valid SQL statement.
    SqlStatements(String),
}

impl Action {
    /// Return the kind of this notice.
    pub fn kind(&self) -> ActionKind {
        ActionKind::from(self)
    }
}

impl ActionKind {
    /// Return a string representation for this action kind.
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::None => "",
            Self::PlainText => "plain_text",
            Self::SqlStatements => "sql_statements",
        }
    }
}

/// An API structs [`RawOptimizerNotice`] wrapped by structs
pub trait OptimizerNoticeApi: Sized {
    /// See [`OptimizerNoticeApi::dependencies`].
    fn dependencies(&self) -> BTreeSet<GlobalId>;

    /// Format the text for the optionally redacted [`OptimizerNotice::message`]
    /// value for this notice.
    fn fmt_message(
        &self,
        f: &mut Formatter<'_>,
        humanizer: &dyn ExprHumanizer,
        redacted: bool,
    ) -> fmt::Result;

    /// Format the text for the optionally redacted [`OptimizerNotice::hint`]
    /// value for this notice.
    fn fmt_hint(
        &self,
        f: &mut Formatter<'_>,
        humanizer: &dyn ExprHumanizer,
        redacted: bool,
    ) -> fmt::Result;

    /// Format the text for the optionally redacted [`OptimizerNotice::action`]
    /// value for this notice.
    fn fmt_action(
        &self,
        f: &mut Formatter<'_>,
        humanizer: &dyn ExprHumanizer,
        redacted: bool,
    ) -> fmt::Result;

    /// The kind of action suggested by this notice.
    fn action_kind(&self, humanizer: &dyn ExprHumanizer) -> ActionKind;

    /// Return a thunk that will render the optionally redacted
    /// [`OptimizerNotice::message`] value for this notice.
    fn message<'a>(
        &'a self,
        humanizer: &'a dyn ExprHumanizer,
        redacted: bool,
    ) -> HumanizedMessage<'a, Self> {
        HumanizedMessage {
            notice: self,
            humanizer,
            redacted,
        }
    }

    /// Return a thunk that will render the optionally redacted
    /// [`OptimizerNotice::hint`] value for
    /// this notice.
    fn hint<'a>(
        &'a self,
        humanizer: &'a dyn ExprHumanizer,
        redacted: bool,
    ) -> HumanizedHint<'a, Self> {
        HumanizedHint {
            notice: self,
            humanizer,
            redacted,
        }
    }

    /// Return a thunk that will render the optionally redacted
    /// [`OptimizerNotice::action`] value for this notice.
    fn action<'a>(
        &'a self,
        humanizer: &'a dyn ExprHumanizer,
        redacted: bool,
    ) -> HumanizedAction<'a, Self> {
        HumanizedAction {
            notice: self,
            humanizer,
            redacted,
        }
    }
}

/// A wrapper for the [`OptimizerNoticeApi::fmt_message`] that implements
/// [`fmt::Display`].
#[allow(missing_debug_implementations)]
pub struct HumanizedMessage<'a, T> {
    notice: &'a T,
    humanizer: &'a dyn ExprHumanizer,
    redacted: bool,
}
impl<'a, T: OptimizerNoticeApi> fmt::Display for HumanizedMessage<'a, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.notice.fmt_message(f, self.humanizer, self.redacted)
    }
}

/// A wrapper for the [`OptimizerNoticeApi::fmt_hint`] that implements [`fmt::Display`].
#[allow(missing_debug_implementations)]
pub struct HumanizedHint<'a, T> {
    notice: &'a T,
    humanizer: &'a dyn ExprHumanizer,
    redacted: bool,
}

impl<'a, T: OptimizerNoticeApi> fmt::Display for HumanizedHint<'a, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.notice.fmt_hint(f, self.humanizer, self.redacted)
    }
}

/// A wrapper for the [`OptimizerNoticeApi::fmt_action`] that implements
/// [`fmt::Display`].
#[allow(missing_debug_implementations)]
pub struct HumanizedAction<'a, T> {
    notice: &'a T,
    humanizer: &'a dyn ExprHumanizer,
    redacted: bool,
}

impl<'a, T: OptimizerNoticeApi> fmt::Display for HumanizedAction<'a, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        self.notice.fmt_action(f, self.humanizer, self.redacted)
    }
}

macro_rules! raw_optimizer_notices {
    ($($ty:ident => $name:literal,)+) => {
        paste::paste!{
            /// Notices that the optimizer wants to show to users.
            #[derive(EnumKind, Clone, Debug, Eq, PartialEq, Hash)]
            #[enum_kind(OptimizerNoticeKind, derive(PartialOrd, Ord, Hash, Serialize, Deserialize,Arbitrary))]
            pub enum RawOptimizerNotice {
                $(
                    #[doc = concat!("See [`", stringify!($ty), "`].")]
                    $ty($ty),
                )+
            }

            impl OptimizerNoticeApi for RawOptimizerNotice {
                fn dependencies(&self) -> BTreeSet<GlobalId> {
                    match self {
                        $(Self::$ty(notice) => notice.dependencies(),)+
                    }
                }

                fn fmt_message(&self, f: &mut Formatter<'_>, humanizer: &dyn ExprHumanizer, redacted: bool) -> fmt::Result {
                    match self {
                        $(Self::$ty(notice) => notice.fmt_message(f, humanizer, redacted),)+
                    }
                }

                fn fmt_hint(&self, f: &mut Formatter<'_>, humanizer: &dyn ExprHumanizer, redacted: bool) -> fmt::Result {
                    match self {
                        $(Self::$ty(notice) => notice.fmt_hint(f, humanizer, redacted),)+
                    }
                }

                fn fmt_action(&self, f: &mut Formatter<'_>, humanizer: &dyn ExprHumanizer, redacted: bool) -> fmt::Result {
                    match self {
                        $(Self::$ty(notice) => notice.fmt_action(f, humanizer, redacted),)+
                    }
                }

                fn action_kind(&self, humanizer: &dyn ExprHumanizer) -> ActionKind {
                    match self {
                        $(Self::$ty(notice) => notice.action_kind(humanizer),)+
                    }
                }
            }

            impl OptimizerNoticeKind {
                /// Return a string representation for this optimizer notice
                /// kind.
                pub fn as_str(&self) -> &'static str {
                    match self {
                        $(Self::$ty => $name,)+
                    }
                }

                /// A notice name, which will be applied as the label on the
                /// metric that is counting notices labelled by notice kind.
                pub fn metric_label(&self) -> &str {
                    match self {
                        $(Self::$ty => stringify!($ty),)+
                    }
                }
            }

            $(
                impl From<$ty> for RawOptimizerNotice {
                    fn from(value: $ty) -> Self {
                        RawOptimizerNotice::$ty(value)
                    }
                }
            )+
        }
    };
}

raw_optimizer_notices![
    IndexAlreadyExists => "An identical index already exists",
    IndexTooWideForLiteralConstraints => "Index too wide for literal constraints",
    IndexKeyEmpty => "Empty index key",
];

impl RawOptimizerNotice {
    /// Turns a vector of notices into a vector of strings that can be used in
    /// EXPLAIN.
    ///
    /// This method should be consistent with [`OptimizerNotice::explain`].
    pub fn explain(
        notices: &Vec<RawOptimizerNotice>,
        humanizer: &dyn ExprHumanizer,
        redacted: bool,
    ) -> Result<Vec<String>, Error> {
        let mut notice_strings = Vec::new();
        for notice in notices {
            if notice.is_valid(humanizer) {
                let mut s = String::new();
                write!(s, "  - Notice: {}\n", notice.message(humanizer, redacted))?;
                write!(s, "    Hint: {}", notice.hint(humanizer, redacted))?;
                notice_strings.push(s);
            }
        }
        Ok(notice_strings)
    }

    /// Returns `true` iff all dependencies for this notice still exist.
    ///
    /// This method should be consistent with [`OptimizerNotice::is_valid`].
    fn is_valid(&self, humanizer: &dyn ExprHumanizer) -> bool {
        self.dependencies()
            .iter()
            .all(|id| humanizer.id_exists(*id))
    }

    /// A notice name, which will be applied as the label on the metric that is
    /// counting notices labelled by notice type.
    pub fn metric_label(&self) -> &str {
        OptimizerNoticeKind::from(self).as_str()
    }
}