perspective_client/virtual_server/generic_sql_model.rs
1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
5// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors. ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13//! SQL query builder for virtual server operations.
14//!
15//! This module provides a stateless SQL query generator that produces
16//! generic SQL strings for perspective virtual server operations.
17
18// TODO(texodus): Missing these features
19//
20// - row expand/collapse in the datagrid needs datamodel support, this is likely
21// a "collapsed" boolean column in the temp table we `UPDATE`.
22//
23// - `on_update` real-time support will be method which takes sa view name and a
24// handler and calls the handler when the view needs to be recalculated.
25//
26// Nice to have:
27//
28// - Optional `view_change` method can be implemented for engine optimization,
29// defaulting to just delete & recreate (as Perspective engine does now).
30//
31// - Would like to add a metadata API so that e.g. Viewer debug panel could show
32// internal generated SQL.
33
34mod table_make_view;
35
36#[cfg(test)]
37mod tests;
38
39use std::fmt;
40
41use indexmap::IndexMap;
42use serde::Deserialize;
43
44use crate::config::{
45 FilterTerm, GroupRollupMode, Scalar, Sort, SortDir, SplitRollupMode, ViewConfig,
46};
47use crate::proto::{ColumnType, ViewPort};
48use crate::virtual_server::generic_sql_model::table_make_view::ViewQueryContext;
49
50/// Error type for SQL generation operations.
51#[derive(Debug, Clone)]
52pub enum GenericSQLError {
53 /// A required column was not found in the schema.
54 ColumnNotFound(String),
55 /// An invalid configuration was provided.
56 InvalidConfig(String),
57 /// An unsupported operation was requested.
58 UnsupportedOperation(String),
59}
60
61impl fmt::Display for GenericSQLError {
62 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63 match self {
64 Self::ColumnNotFound(col) => write!(f, "Column not found: {}", col),
65 Self::InvalidConfig(msg) => write!(f, "Invalid configuration: {}", msg),
66 Self::UnsupportedOperation(msg) => write!(f, "Unsupported operation: {}", msg),
67 }
68 }
69}
70
71impl std::error::Error for GenericSQLError {}
72
73/// Result type alias for SQL operations.
74pub type GenericSQLResult<T> = Result<T, GenericSQLError>;
75
76#[derive(Clone, Debug, Deserialize, Default)]
77pub struct GenericSQLVirtualServerModelArgs {
78 create_entity: Option<String>,
79 grouping_fn: Option<String>,
80
81 /// Separator joining `split_by` values and the column name in pivoted
82 /// view column names, e.g. `"CA|Sales"` for separator `"|"`. Perspective's
83 /// column-path separator is `"|"`, so any other value produces views the
84 /// client will not interpret as column paths.
85 column_separator: Option<String>,
86
87 /// Escape character emitted as an `ESCAPE` clause after generated
88 /// `ILIKE` patterns (Perspective's `begins with` / `contains` /
89 /// `ends with` filter ops and their negations). Dialects with no
90 /// default `LIKE` escape character (DuckDB) must pass `"\\"`; dialects
91 /// where backslash escaping is implicit and the `ESCAPE` clause is
92 /// unsupported (ClickHouse) must omit it.
93 like_escape_clause: Option<String>,
94
95 /// Whether the dialect's string literal parser consumes C-style
96 /// backslash escapes (ClickHouse), requiring backslashes in emitted
97 /// literals to be doubled. Dialects with standard-conforming literals
98 /// (DuckDB) omit it.
99 backslash_escaped_literals: Option<bool>,
100
101 /// Name of the dialect's partial-match regex function, emitted as
102 /// `{regex_fn}("col", 'pattern')` for the `matches` / `not matches`
103 /// filter ops — `"regexp_matches"` for DuckDB, `"match"` for
104 /// ClickHouse (both RE2, matching the engine's semantics). When
105 /// omitted, regex filter clauses are dropped.
106 regex_fn: Option<String>,
107}
108
109/// Recovers the source column of a pivoted view column name — the longest
110/// `config.columns` entry that is a strict suffix of `name` — with its index
111/// in `config.columns`. Requires no separator knowledge, so it works at
112/// protocol boundaries where the SQL model's `column_separator` is unknown.
113/// Returns `None` for non-path names (e.g. flat-view columns, which equal a
114/// `config.columns` entry exactly rather than strictly containing one).
115pub(crate) fn column_path_source<'a>(
116 name: &str,
117 config: &'a ViewConfig,
118) -> Option<(usize, &'a str)> {
119 if config.split_by.is_empty() {
120 return None;
121 }
122
123 let rollup = config.split_rollup_mode == SplitRollupMode::Rollup;
124
125 let mut best: Option<(usize, &'a str)> = None;
126 for (idx, col) in config.columns.iter().flatten().enumerate() {
127 if (name.len() > col.len() || rollup)
128 && name.ends_with(col.as_str())
129 && best.is_none_or(|(_, b)| col.len() > b.len())
130 {
131 best = Some((idx, col));
132 }
133 }
134
135 best
136}
137
138/// Sorts pivoted view column names into Perspective's column-path order:
139/// `split_by` value paths ascending, then `config.columns` order within each
140/// path (e.g. `CA|price, CA|qty, NY|price, NY|qty`).
141///
142/// The per-column `PIVOT` join in [`ViewQueryContext`] emits columns grouped
143/// by source column instead, so every egress of view column names re-sorts
144/// with this. Internal `__`-prefixed columns sort first, unmatched names
145/// last, both preserving relative order.
146pub(crate) fn sort_column_paths<T: AsRef<str>>(names: &mut [T], config: &ViewConfig) {
147 names.sort_by_cached_key(|name| {
148 let name = name.as_ref();
149 if name.starts_with("__") {
150 return (0u8, String::new(), 0usize);
151 }
152
153 match column_path_source(name, config) {
154 Some((idx, col)) => (1, name[..name.len() - col.len()].to_string(), idx),
155 None => (2, String::new(), 0),
156 }
157 });
158}
159
160/// A stateless SQL query builder virtual server operations.
161///
162/// This struct generates SQL query strings without executing them, allowing
163/// the caller to execute the queries against a SQL connection.
164#[derive(Debug, Default, Clone)]
165pub struct GenericSQLVirtualServerModel(GenericSQLVirtualServerModelArgs);
166
167impl GenericSQLVirtualServerModel {
168 /// Creates a new `GenericSQLVirtualServerModel` instance.
169 pub fn new(args: GenericSQLVirtualServerModelArgs) -> Self {
170 Self(args)
171 }
172
173 /// Returns the SQL query to list all hosted tables.
174 ///
175 /// # Returns
176 /// SQL: `SHOW ALL TABLES`
177 pub fn get_hosted_tables(&self) -> GenericSQLResult<String> {
178 Ok("SHOW ALL TABLES".to_string())
179 }
180
181 /// Returns the SQL query to describe a table's schema.
182 ///
183 /// # Arguments
184 /// * `table_id` - The identifier of the table to describe.
185 ///
186 /// # Returns
187 /// SQL: `DESCRIBE {table_id}`
188 pub fn table_schema(&self, table_id: &str) -> GenericSQLResult<String> {
189 Ok(format!("DESCRIBE {}", table_id))
190 }
191
192 /// Returns the SQL query to get the row count of a table.
193 ///
194 /// # Arguments
195 /// * `table_id` - The identifier of the table.
196 ///
197 /// # Returns
198 /// SQL: `SELECT COUNT(*) FROM {table_id}`
199 pub fn table_size(&self, table_id: &str) -> GenericSQLResult<String> {
200 Ok(format!("SELECT COUNT(*) FROM {}", table_id))
201 }
202
203 /// Returns the SQL query to get the column count of a view.
204 ///
205 /// # Arguments
206 /// * `view_id` - The identifier of the view.
207 ///
208 /// # Returns
209 /// SQL: `SELECT COUNT(*) FROM (DESCRIBE {view_id})`
210 pub fn view_column_size(&self, view_id: &str) -> GenericSQLResult<String> {
211 Ok(format!("SELECT COUNT(*) FROM (DESCRIBE {})", view_id))
212 }
213
214 /// Returns the SQL query to validate an expression against a table.
215 ///
216 /// # Arguments
217 /// * `table_id` - The identifier of the table.
218 /// * `expression` - The SQL expression to validate.
219 ///
220 /// # Returns
221 /// SQL: `DESCRIBE (SELECT {expression} FROM {table_id})`
222 pub fn table_validate_expression(
223 &self,
224 table_id: &str,
225 expression: &str,
226 ) -> GenericSQLResult<String> {
227 Ok(format!(
228 "DESCRIBE (SELECT {} FROM {})",
229 expression, table_id
230 ))
231 }
232
233 /// Returns the SQL query to delete a view.
234 ///
235 /// # Arguments
236 /// * `view_id` - The identifier of the view to delete.
237 ///
238 /// # Returns
239 /// SQL: `DROP TABLE IF EXISTS {view_id}`
240 pub fn view_delete(&self, view_id: &str) -> GenericSQLResult<String> {
241 Ok(format!("DROP TABLE IF EXISTS {}", view_id))
242 }
243
244 /// Returns the SQL query to create a view from a table with the given
245 /// configuration.
246 ///
247 /// # Arguments
248 /// * `table_id` - The identifier of the source table.
249 /// * `view_id` - The identifier for the new view.
250 /// * `config` - The view configuration specifying columns, group_by,
251 /// split_by, etc.
252 ///
253 /// # Returns
254 /// SQL: `CREATE TABLE {view_id} AS (...)`
255 pub fn table_make_view(
256 &self,
257 table_id: &str,
258 view_id: &str,
259 config: &ViewConfig,
260 ) -> GenericSQLResult<String> {
261 let ctx = ViewQueryContext::new(self, table_id, config)?;
262 let query = ctx.build_query();
263 let template = self.0.create_entity.as_deref().unwrap_or("TABLE");
264 Ok(format!("CREATE {} {} AS ({})", template, view_id, query))
265 }
266
267 /// Returns the SQL query to fetch data from a view with the given viewport.
268 ///
269 /// # Arguments
270 /// * `view_id` - The identifier of the view.
271 /// * `config` - The view configuration.
272 /// * `viewport` - The viewport specifying row/column ranges.
273 /// * `schema` - The schema of the view (column names to types).
274 ///
275 /// # Returns
276 /// SQL: `SELECT ... FROM {view_id} LIMIT ... OFFSET ...`
277 pub fn view_get_data(
278 &self,
279 view_id: &str,
280 config: &ViewConfig,
281 viewport: &ViewPort,
282 schema: &IndexMap<String, ColumnType>,
283 ) -> GenericSQLResult<String> {
284 let group_by = &config.group_by;
285 let sort = &config.sort;
286 let start_col = viewport.start_col.unwrap_or(0) as usize;
287 let end_col = viewport.end_col.map(|x| x as usize);
288 let start_row = viewport.start_row.unwrap_or(0);
289 let end_row = viewport.end_row;
290 let limit_clause = if let Some(end) = end_row {
291 format!("LIMIT {} OFFSET {}", end - start_row, start_row)
292 } else {
293 String::new()
294 };
295
296 let mut data_columns: Vec<&String> = schema
297 .keys()
298 .filter(|col_name| !col_name.starts_with("__"))
299 .collect();
300
301 let col_sort_dir = sort.iter().find_map(|Sort(_, dir)| match dir {
302 SortDir::ColAsc | SortDir::ColAscAbs => Some(true),
303 SortDir::ColDesc | SortDir::ColDescAbs => Some(false),
304 _ => None,
305 });
306
307 if let Some(ascending) = col_sort_dir {
308 if ascending {
309 data_columns.sort();
310 } else {
311 data_columns.sort_by(|a, b| b.cmp(a));
312 }
313 } else if !config.split_by.is_empty() {
314 sort_column_paths(&mut data_columns, config);
315 }
316
317 let data_columns: Vec<&String> = data_columns
318 .into_iter()
319 .skip(start_col)
320 .take(end_col.map(|e| e - start_col).unwrap_or(usize::MAX))
321 .collect();
322
323 let mut group_by_cols: Vec<String> = Vec::new();
324 if !group_by.is_empty() {
325 if config.group_rollup_mode != GroupRollupMode::Flat {
326 group_by_cols.push("\"__GROUPING_ID__\"".to_string());
327 }
328 for idx in 0..group_by.len() {
329 group_by_cols.push(format!("\"__ROW_PATH_{}__\"", idx));
330 }
331 }
332
333 let all_columns: Vec<String> = group_by_cols
334 .into_iter()
335 .chain(data_columns.iter().map(|col| format!("\"{}\"", col)))
336 .collect();
337
338 Ok(format!(
339 "SELECT {} FROM {} {}",
340 all_columns.join(", "),
341 view_id,
342 limit_clause
343 )
344 .trim()
345 .to_string())
346 }
347
348 /// Returns the SQL query to describe a view's schema.
349 ///
350 /// # Arguments
351 /// * `view_id` - The identifier of the view.
352 ///
353 /// # Returns
354 /// SQL: `DESCRIBE {view_id}`
355 pub fn view_schema(&self, view_id: &str) -> GenericSQLResult<String> {
356 Ok(format!("DESCRIBE {}", view_id))
357 }
358
359 /// Returns the SQL query to get the row count of a view.
360 ///
361 /// # Arguments
362 /// * `view_id` - The identifier of the view.
363 ///
364 /// # Returns
365 /// SQL: `SELECT COUNT(*) FROM {view_id}`
366 pub fn view_size(&self, view_id: &str) -> GenericSQLResult<String> {
367 Ok(format!("SELECT COUNT(*) FROM {}", view_id))
368 }
369
370 /// Returns the SQL query to get the min and max values of a column.
371 ///
372 /// # Arguments
373 /// * `view_id` - The identifier of the view.
374 /// * `column_name` - The name of the column.
375 /// * `config` - The view configuration.
376 ///
377 /// # Returns
378 /// SQL: `SELECT MIN("column_name"), MAX("column_name") FROM {view_id}`
379 /// When the view uses ROLLUP grouping (non-flat mode with group_by),
380 /// a `WHERE __GROUPING_ID__ = 0` clause is added to exclude non-leaf rows.
381 pub fn view_get_min_max(
382 &self,
383 view_id: &str,
384 column_name: &str,
385 config: &ViewConfig,
386 ) -> GenericSQLResult<String> {
387 let has_grouping_id =
388 !config.group_by.is_empty() && config.group_rollup_mode != GroupRollupMode::Flat;
389 let where_clause = if has_grouping_id {
390 " WHERE __GROUPING_ID__ = 0"
391 } else {
392 ""
393 };
394
395 Ok(format!(
396 "SELECT MIN(\"{}\"), MAX(\"{}\") FROM {}{}",
397 column_name, column_name, view_id, where_clause
398 ))
399 }
400
401 fn filter_term_to_sql(term: &FilterTerm, backslash_escaped: bool) -> Option<String> {
402 match term {
403 FilterTerm::Scalar(scalar) => Self::scalar_to_sql(scalar, backslash_escaped),
404 FilterTerm::Array(scalars) => {
405 let values: Vec<String> = scalars
406 .iter()
407 .filter_map(|x| Self::scalar_to_sql(x, backslash_escaped))
408 .collect();
409 if values.is_empty() {
410 None
411 } else {
412 Some(format!("({})", values.join(", ")))
413 }
414 },
415 }
416 }
417
418 fn scalar_to_sql(scalar: &Scalar, backslash_escaped: bool) -> Option<String> {
419 match scalar {
420 Scalar::Null => None,
421 Scalar::Bool(b) => Some(if *b { "TRUE" } else { "FALSE" }.to_string()),
422 Scalar::Float(f) => Some(f.to_string()),
423 Scalar::String(s) => Some(table_make_view::string_literal(s, backslash_escaped)),
424 }
425 }
426}