Skip to main content

uqa_sql/
error.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Error types surfaced by the SQL compiler and executor.
8
9#[derive(Debug, thiserror::Error)]
10pub enum SQLError {
11    #[error("parse error: {0}")]
12    Parse(String),
13    #[error("unsupported SQL feature: {0}")]
14    Unsupported(String),
15    #[error("unknown table: {0}")]
16    UnknownTable(String),
17    #[error("unknown column: {0}")]
18    UnknownColumn(String),
19    #[error("column reference \"{0}\" is ambiguous")]
20    AmbiguousColumn(String),
21    #[error("unknown function: {0}")]
22    UnknownFunction(String),
23    #[error("type mismatch: {0}")]
24    TypeMismatch(String),
25    #[error("invalid argument count for `{name}`: expected {expected}, got {actual}")]
26    BadArity {
27        name: String,
28        expected: String,
29        actual: usize,
30    },
31    #[error("No value supplied for parameter ${0}")]
32    MissingParam(usize),
33    #[error("vector dimension mismatch: expected {expected}, got {actual}")]
34    VectorDimMismatch { expected: usize, actual: usize },
35    #[error("{0}")]
36    Cancelled(#[from] uqa_core::QueryCancelled),
37    /// Error raised by (or on behalf of) a user-defined SQL /
38    /// `PL/pgSQL` routine. Carries an explicit `SQLSTATE` so
39    /// `EXCEPTION WHEN <condition>` handlers and `SQLSTATE` /
40    /// `SQLERRM` report the same code `PostgreSQL` would.
41    #[error("{message}")]
42    Routine { sqlstate: String, message: String },
43    #[error("internal error: {0}")]
44    Internal(String),
45}
46
47impl SQLError {
48    /// `PostgreSQL` `SQLSTATE` code for the error, mirroring the
49    /// the current exception-to-state mapping. `None` for
50    /// errors that do not carry a defined `SQLSTATE`.
51    pub fn sqlstate(&self) -> Option<&str> {
52        match self {
53            SQLError::Cancelled(_) => Some(uqa_core::SQLSTATE_QUERY_CANCELED),
54            SQLError::Parse(_) => Some("42601"), // syntax_error
55            SQLError::Unsupported(_) => Some("0A000"), // feature_not_supported
56            SQLError::UnknownTable(_) => Some("42P01"), // undefined_table
57            SQLError::UnknownColumn(_) => Some("42703"), // undefined_column
58            SQLError::AmbiguousColumn(_) => Some("42702"), // ambiguous_column
59            SQLError::UnknownFunction(_) => Some("42883"), // undefined_function
60            SQLError::TypeMismatch(_) => Some("42804"), // datatype_mismatch
61            SQLError::BadArity { .. } => Some("42883"), // undefined_function (PG)
62            SQLError::MissingParam(_) => Some("S1002"), // ERRCODE_INVALID_PARAMETER_VALUE
63            SQLError::VectorDimMismatch { .. } => Some("22023"), // invalid_parameter_value
64            SQLError::Routine { sqlstate, .. } => Some(sqlstate),
65            SQLError::Internal(_) => Some("XX000"), // internal_error
66        }
67    }
68}
69
70pub type Result<T> = std::result::Result<T, SQLError>;
71
72impl From<pg_query::Error> for SQLError {
73    fn from(value: pg_query::Error) -> Self {
74        SQLError::Parse(value.to_string())
75    }
76}