Skip to main content

powdb_query/
result.rs

1use std::fmt;
2
3use powdb_storage::types::Value;
4
5/// The result of executing a query.
6#[derive(Debug)]
7pub enum QueryResult {
8    Rows {
9        columns: Vec<String>,
10        rows: Vec<Vec<Value>>,
11    },
12    Scalar(Value),   // count, avg, etc.
13    Modified(u64),   // insert/update/delete — number of rows affected
14    Created(String), // DDL — type name created
15    Executed {
16        message: String,
17    }, // DDL — alter/drop feedback
18}
19
20impl QueryResult {
21    pub fn row_count(&self) -> usize {
22        match self {
23            QueryResult::Rows { rows, .. } => rows.len(),
24            QueryResult::Scalar(_) => 1,
25            QueryResult::Modified(n) => *n as usize,
26            QueryResult::Created(_) => 0,
27            QueryResult::Executed { .. } => 0,
28        }
29    }
30}
31
32/// Typed error enum for query execution failures.
33///
34/// Replaces the previous `Result<QueryResult, String>` pattern with
35/// structured variants that callers can programmatically match on.
36/// The `From<String>` impl enables gradual migration — existing
37/// `Err(format!(...))` sites continue to compile via `?` propagation.
38#[derive(Debug, Clone, PartialEq)]
39pub enum QueryError {
40    /// Table does not exist.
41    TableNotFound(String),
42    /// Column does not exist on table.
43    ColumnNotFound { table: String, column: String },
44    /// Type mismatch in expression.
45    TypeError(String),
46    /// Join result exceeded MAX_JOIN_ROWS.
47    JoinLimitExceeded,
48    /// Sort exceeded MAX_SORT_ROWS.
49    SortLimitExceeded,
50    /// Per-query memory budget exceeded during materialization (sort buffer,
51    /// join build side, GROUP BY hash table, or IN-list). Returned cleanly so
52    /// the server process is never OOM-killed by a crafted query.
53    MemoryLimitExceeded {
54        limit_bytes: usize,
55        requested_bytes: usize,
56    },
57    /// Parse error (wraps parser error).
58    Parse(String),
59    /// Index-related error.
60    IndexError(String),
61    /// View-related error.
62    ViewError(String),
63    /// WAL or I/O error.
64    StorageError(String),
65    /// Readonly path needs write lock (internal sentinel).
66    ReadonlyNeedsWrite,
67    /// Generic execution error (catch-all for migration).
68    Execution(String),
69}
70
71impl fmt::Display for QueryError {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        match self {
74            QueryError::TableNotFound(t) => write!(f, "table '{t}' not found"),
75            QueryError::ColumnNotFound { table, column } => {
76                if table.is_empty() {
77                    write!(f, "column '{column}' not found")
78                } else {
79                    write!(f, "column '{column}' not found in table '{table}'")
80                }
81            }
82            QueryError::TypeError(msg) => write!(f, "type mismatch: {msg}"),
83            QueryError::JoinLimitExceeded => write!(f, "join result exceeds row limit"),
84            QueryError::SortLimitExceeded => {
85                write!(f, "sort input exceeds row limit — add a LIMIT clause")
86            }
87            QueryError::MemoryLimitExceeded {
88                limit_bytes,
89                requested_bytes,
90            } => write!(
91                f,
92                "query exceeded memory budget: requested {requested_bytes} bytes, limit {limit_bytes} bytes"
93            ),
94            QueryError::Parse(msg) => write!(f, "{msg}"),
95            QueryError::IndexError(msg) => write!(f, "{msg}"),
96            QueryError::ViewError(msg) => write!(f, "{msg}"),
97            QueryError::StorageError(msg) => write!(f, "{msg}"),
98            QueryError::ReadonlyNeedsWrite => {
99                write!(f, "__POWDB_READONLY_NEEDS_WRITE__")
100            }
101            QueryError::Execution(msg) => write!(f, "{msg}"),
102        }
103    }
104}
105
106impl std::error::Error for QueryError {}
107
108impl From<String> for QueryError {
109    fn from(s: String) -> Self {
110        QueryError::Execution(s)
111    }
112}
113
114impl From<&str> for QueryError {
115    fn from(s: &str) -> Self {
116        QueryError::Execution(s.to_string())
117    }
118}