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
// 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.

//! Hosts [`IndexAlreadyExists`].

use std::collections::BTreeSet;
use std::fmt;

use mz_expr::explain::{HumanizedNotice, HumanizerMode};
use mz_expr::MirScalarExpr;
use mz_ore::str::separated;
use mz_repr::explain::ExprHumanizer;
use mz_repr::GlobalId;

use crate::notice::{ActionKind, OptimizerNoticeApi};

/// Trying to re-create an index that already exists.
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct IndexAlreadyExists {
    /// The id of the identical index.
    pub index_id: GlobalId,
    /// The key of the index.
    pub index_key: Vec<MirScalarExpr>,
    /// The id of the object that the index is on.
    pub index_on_id: GlobalId,
    /// The id the index that duplicates existing indexes.
    pub exported_index_id: GlobalId,
}

impl OptimizerNoticeApi for IndexAlreadyExists {
    fn dependencies(&self) -> BTreeSet<GlobalId> {
        BTreeSet::from([self.index_id, self.index_on_id])
    }

    fn fmt_message(
        &self,
        f: &mut fmt::Formatter<'_>,
        humanizer: &dyn ExprHumanizer,
        redacted: bool,
    ) -> fmt::Result {
        let exported_index_name = humanizer
            .humanize_id(self.exported_index_id)
            .unwrap_or_else(|| self.exported_index_id.to_string());
        let index_name = humanizer
            .humanize_id(self.index_id)
            .unwrap_or_else(|| self.index_id.to_string());
        let index_on_id_name = humanizer
            .humanize_id_unqualified(self.index_on_id)
            .unwrap_or_else(|| self.index_on_id.to_string());

        let mode = HumanizedNotice::new(redacted);
        let col_names = humanizer.column_names_for_id(self.index_on_id);
        let col_names = col_names.as_ref();
        let index_key = separated(", ", mode.seq(&self.index_key, col_names));

        write!(
            f,
            "Index {exported_index_name} is identical to {index_name}, which \
             is also defined on {index_on_id_name}({index_key})."
        )
    }

    fn fmt_hint(
        &self,
        f: &mut fmt::Formatter<'_>,
        humanizer: &dyn ExprHumanizer,
        redacted: bool,
    ) -> fmt::Result {
        let index_on_id_name = humanizer
            .humanize_id_unqualified(self.index_on_id)
            .unwrap_or_else(|| self.index_on_id.to_string());

        let mode = HumanizedNotice::new(redacted);
        let col_names = humanizer.column_names_for_id(self.index_on_id);
        let col_names = col_names.as_ref();
        let index_key = separated(", ", mode.seq(&self.index_key, col_names));

        write!(
            f,
            "Please drop all indexes except the first index created on \
             {index_on_id_name}({index_key}) and recreate all dependent objects."
        )
    }

    fn fmt_action(
        &self,
        _f: &mut fmt::Formatter<'_>,
        _humanizer: &dyn ExprHumanizer,
        _redacted: bool,
    ) -> fmt::Result {
        Ok(())
    }

    fn action_kind(&self, _humanizer: &dyn ExprHumanizer) -> ActionKind {
        ActionKind::None
    }
}