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

//! Defines QGM-related errors and traits for those errors.
//!
//! The public interface consists of the [`QGMError`] method and the
//! implemented traits.

use std::error::Error;
use std::fmt;

use crate::plan::{HirRelationExpr, HirScalarExpr};
use crate::query_model::model::{BoxScalarExpr, BoxType, QuantifierType};

/// Errors that can occur while handling a QGM model.
///
/// A bunch of the error types exist because our support for HIR ⇒ QGM
/// conversion and QGM ⇒ MIR conversion is currently incomplete. They will be
/// removed once these limitations are addressed.
#[derive(Debug, Clone)]
pub enum QGMError {
    /// Indicates HIR ⇒ QGM conversion failure due to unsupported [`HirRelationExpr`].
    UnsupportedHirRelationExpr(UnsupportedHirRelationExpr),
    /// Indicates HIR ⇒ QGM conversion failure due to unsupported [`HirScalarExpr`].
    UnsupportedHirScalarExpr(UnsupportedHirScalarExpr),
    /// Indicates QGM ⇒ MIR conversion failure due to unsupported box type.
    UnsupportedBoxType(UnsupportedBoxType),
    /// Indicates QGM ⇒ MIR conversion failure due to unsupported scalar expression.
    UnsupportedBoxScalarExpr(UnsupportedBoxScalarExpr),
    /// Indicates QGM ⇒ MIR conversion failure due to unsupported quantifier type.
    UnsupportedQuantifierType(UnsupportedQuantifierType),
    /// Indicates QGM ⇒ MIR conversion failure due to lack of support for decorrelation.
    UnsupportedDecorrelation(UnsupportedDecorrelation),
    /// An unstructured error.
    Internal(Internal),
}

impl Error for QGMError {}

impl fmt::Display for QGMError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            QGMError::UnsupportedHirRelationExpr(e) => write!(f, "{}", e),
            QGMError::UnsupportedHirScalarExpr(e) => write!(f, "{}", e),
            QGMError::UnsupportedBoxType(e) => write!(f, "{}", e),
            QGMError::UnsupportedBoxScalarExpr(e) => write!(f, "{}", e),
            QGMError::UnsupportedQuantifierType(e) => write!(f, "{}", e),
            QGMError::UnsupportedDecorrelation(e) => write!(f, "{}", e),
            QGMError::Internal(e) => write!(f, "{}", e),
        }
    }
}

impl From<QGMError> for String {
    fn from(error: QGMError) -> Self {
        format!("{}", error)
    }
}

#[derive(Debug, Clone)]
pub struct UnsupportedHirRelationExpr {
    pub(crate) expr: HirRelationExpr,
    pub(crate) explanation: Option<String>,
}

impl From<UnsupportedHirRelationExpr> for QGMError {
    fn from(inner: UnsupportedHirRelationExpr) -> Self {
        QGMError::UnsupportedHirRelationExpr(inner)
    }
}

impl fmt::Display for UnsupportedHirRelationExpr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Unsupported HirRelationExpr variant in QGM conversion: {}.",
            serde_json::to_string(&self.expr).unwrap()
        )?;
        if let Some(explanation) = &self.explanation {
            write!(f, " Explanation: {}", explanation)?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct UnsupportedHirScalarExpr {
    pub(crate) scalar: HirScalarExpr,
}

impl From<UnsupportedHirScalarExpr> for QGMError {
    fn from(inner: UnsupportedHirScalarExpr) -> Self {
        QGMError::UnsupportedHirScalarExpr(inner)
    }
}

impl fmt::Display for UnsupportedHirScalarExpr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Unsupported HirScalarExpr variant in QGM conversion: {}",
            serde_json::to_string(&self.scalar).unwrap()
        )
    }
}

#[derive(Debug, Clone)]
pub struct UnsupportedBoxType {
    pub(crate) box_type: BoxType,
    pub(crate) explanation: Option<String>,
}

impl From<UnsupportedBoxType> for QGMError {
    fn from(inner: UnsupportedBoxType) -> Self {
        QGMError::UnsupportedBoxType(inner)
    }
}

impl fmt::Display for UnsupportedBoxType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Unsupported box type in MIR conversion: {:?}.",
            self.box_type
        )?;
        if let Some(explanation) = &self.explanation {
            write!(f, " Explanation: {}", explanation)?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct UnsupportedQuantifierType {
    pub(crate) quantifier_type: QuantifierType,
    /// Context where error is being thrown.
    pub(crate) context: String,
}

impl From<UnsupportedQuantifierType> for QGMError {
    fn from(inner: UnsupportedQuantifierType) -> Self {
        QGMError::UnsupportedQuantifierType(inner)
    }
}

impl fmt::Display for UnsupportedQuantifierType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Unsupported quantifier type in {}: {}.",
            self.context, self.quantifier_type
        )
    }
}

#[derive(Debug, Clone)]
pub struct UnsupportedBoxScalarExpr {
    pub(crate) scalar: BoxScalarExpr,
    /// Context where error is being thrown.
    pub(crate) context: String,
    pub(crate) explanation: Option<String>,
}

impl From<UnsupportedBoxScalarExpr> for QGMError {
    fn from(inner: UnsupportedBoxScalarExpr) -> Self {
        QGMError::UnsupportedBoxScalarExpr(inner)
    }
}

impl fmt::Display for UnsupportedBoxScalarExpr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Unsupported QGM scalar expression in {}: {}.",
            self.context, self.scalar
        )?;
        if let Some(explanation) = &self.explanation {
            write!(f, " Explanation: {}", explanation)?;
        }
        Ok(())
    }
}

#[derive(Debug, Clone)]
pub struct UnsupportedDecorrelation {
    pub(crate) msg: String,
}

impl From<UnsupportedDecorrelation> for QGMError {
    fn from(inner: UnsupportedDecorrelation) -> Self {
        QGMError::UnsupportedDecorrelation(inner)
    }
}

impl fmt::Display for UnsupportedDecorrelation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}

#[derive(Debug, Clone)]
pub struct Internal {
    pub(crate) msg: String,
}

impl From<Internal> for QGMError {
    fn from(inner: Internal) -> Self {
        QGMError::Internal(inner)
    }
}

impl fmt::Display for Internal {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.msg)
    }
}