rhei_core/traits/router.rs
1//! [`QueryRouter`] trait — SQL routing to OLTP or OLAP.
2//!
3//! The production implementation (`SqlParserRouter` in `rhei-sync`) uses an
4//! AST-based approach via `sqlparser-rs`. A keyword-heuristic fallback fires
5//! when the parser cannot classify the statement.
6
7use crate::types::QueryTarget;
8
9/// Classifies SQL statements and routes them to the correct engine.
10///
11/// The default implementation (`SqlParserRouter` in `rhei-sync`) parses the
12/// SQL AST and applies the following heuristic:
13///
14/// - Writes (INSERT/UPDATE/DELETE), DDL, and simple point-lookups → OLTP
15/// - Aggregates, GROUP BY, JOINs, window functions, CTEs, subqueries → OLAP
16///
17/// # Contract for implementors
18///
19/// - `route` must be infallible and synchronous (no I/O).
20/// - When uncertain, prefer `QueryTarget::Oltp` (safety-first: writes must
21/// never accidentally go to the read-only OLAP engine).
22pub trait QueryRouter: Send + Sync {
23 /// Classify `sql` and return the engine that should execute it.
24 fn route(&self, sql: &str) -> QueryTarget;
25}