heliosdb-proxy 0.4.0

HeliosProxy - Intelligent connection router and failover manager for HeliosDB and PostgreSQL
Documentation
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! Query Rewriting Module
//!
//! Transparent query rewriting at the proxy layer for optimization,
//! compatibility, and security enforcement.
//!
//! # Features
//!
//! - **Pattern Matching**: Match queries by fingerprint, regex, AST, or table
//! - **Transformations**: Index hints, SELECT * expansion, LIMIT addition
//! - **Rule Engine**: Priority-based rule application
//! - **AI Safety**: Agent query limits and forbidden table enforcement
//!
//! # Architecture
//!
//! ```text
//!   Original Query → Parse → Match Rules → Apply Transformations → Rewritten Query
//!                      │         │                  │
//!                      │         │                  ├── Replace
//!                      │         │                  ├── AddIndexHint
//!                      │         ├── Fingerprint    ├── ExpandSelectStar
//!                      │         ├── Regex          ├── AddLimit
//!                      │         ├── AST            ├── AddWhereClause
//!                      │         └── Table          └── ReplaceTable
//!//!                   SQL AST
//! ```
//!
//! # Example
//!
//! ```rust,ignore
//! use heliosdb::proxy::rewriter::{QueryRewriter, RewriteRule, QueryPattern, Transformation};
//!
//! let mut rewriter = QueryRewriter::builder()
//!     .rule(RewriteRule::new("expand_star")
//!         .pattern(QueryPattern::table("users"))
//!         .transform(Transformation::ExpandSelectStar {
//!             columns: vec!["id", "name", "email"]
//!         }))
//!     .rule(RewriteRule::new("add_limit")
//!         .pattern(QueryPattern::all())
//!         .transform(Transformation::AddLimit(1000)))
//!     .build();
//!
//! let result = rewriter.rewrite("SELECT * FROM users")?;
//! // Result: SELECT id, name, email FROM users LIMIT 1000
//! ```

pub mod config;
pub mod rules;
pub mod matcher;
pub mod transformer;
pub mod parser;
pub mod metrics;

// Re-export main types
pub use config::{RewriterConfig, RewriterConfigBuilder};
pub use rules::{RewriteRule, RewriteRuleBuilder, QueryPattern, AstPattern, Transformation, Condition};
pub use matcher::{RuleMatcher, MatchResult};
pub use transformer::{TransformationEngine, TransformError};
pub use parser::{SqlParser, ParsedQuery, SqlStatement};
pub use metrics::{RewriteMetrics, RewriteStats, RuleStats};

use std::sync::Arc;
use parking_lot::RwLock;

/// Query rewriter
///
/// Main entry point for query rewriting operations.
pub struct QueryRewriter {
    /// Configuration
    config: RewriterConfig,

    /// SQL parser
    parser: SqlParser,

    /// Rewrite rules
    rules: Arc<RwLock<Vec<RewriteRule>>>,

    /// Rule matcher
    matcher: Arc<RwLock<RuleMatcher>>,

    /// Transformation engine
    transformer: TransformationEngine,

    /// Metrics
    metrics: Arc<RewriteMetrics>,
}

impl QueryRewriter {
    /// Create a new query rewriter
    pub fn new(config: RewriterConfig) -> Self {
        let rules = Arc::new(RwLock::new(config.rules.clone()));
        let matcher = Arc::new(RwLock::new(RuleMatcher::new(&config.rules)));
        let parser = SqlParser::new();
        let transformer = TransformationEngine::new();
        let metrics = Arc::new(RewriteMetrics::new());

        Self {
            config,
            parser,
            rules,
            matcher,
            transformer,
            metrics,
        }
    }

    /// Create a builder
    pub fn builder() -> QueryRewriterBuilder {
        QueryRewriterBuilder::new()
    }

    /// Check if rewriter is enabled
    pub fn is_enabled(&self) -> bool {
        self.config.enabled
    }

    /// Rewrite a query
    ///
    /// Returns the rewritten query and list of applied rules.
    pub fn rewrite(&self, query: &str) -> Result<RewriteResult, RewriteError> {
        if !self.config.enabled {
            return Ok(RewriteResult::unchanged(query));
        }

        let start = std::time::Instant::now();

        // Parse query to get fingerprint
        let parsed = self.parser.parse(query)?;
        let fingerprint = parsed.fingerprint();

        // Check cache for previously rewritten query
        // (In production, add caching by fingerprint)

        // Match rules
        let rules = self.rules.read();
        let matcher = self.matcher.read();
        let matched = matcher.match_query(&parsed, &rules);

        if matched.is_empty() {
            self.metrics.record_no_match(start.elapsed());
            return Ok(RewriteResult::unchanged(query));
        }

        // Apply transformations
        let mut current_query = query.to_string();
        let mut applied_rules = Vec::new();

        for rule in matched {
            if !rule.enabled {
                continue;
            }

            // Check condition
            if let Some(ref condition) = rule.condition {
                if !self.evaluate_condition(condition, &current_query) {
                    continue;
                }
            }

            // Apply transformation
            match self.transformer.apply(&current_query, &rule.transformation) {
                Ok(rewritten) => {
                    current_query = rewritten;
                    applied_rules.push(rule.id.clone());
                    self.metrics.record_rule_match(&rule.id);
                }
                Err(e) => {
                    if self.config.log_errors {
                        eprintln!("Rewrite error for rule {}: {}", rule.id, e);
                    }
                    // Continue with other rules
                }
            }
        }

        let duration = start.elapsed();
        self.metrics.record_rewrite(duration, !applied_rules.is_empty());

        if applied_rules.is_empty() {
            Ok(RewriteResult::unchanged(query))
        } else {
            if self.config.log_rewrites {
                println!("Rewritten query:");
                println!("  Original: {}", query);
                println!("  Rewritten: {}", current_query);
                println!("  Rules: {:?}", applied_rules);
            }

            Ok(RewriteResult {
                original: query.to_string(),
                rewritten: current_query,
                rules_applied: applied_rules,
                fingerprint,
                duration,
            })
        }
    }

    /// Test rewrite without metrics recording
    pub fn test_rewrite(&self, query: &str) -> Result<RewriteResult, RewriteError> {
        let parsed = self.parser.parse(query)?;
        let fingerprint = parsed.fingerprint();

        let rules = self.rules.read();
        let matcher = self.matcher.read();
        let matched = matcher.match_query(&parsed, &rules);

        let mut current_query = query.to_string();
        let mut applied_rules = Vec::new();

        for rule in matched {
            if !rule.enabled {
                continue;
            }

            if let Some(ref condition) = rule.condition {
                if !self.evaluate_condition(condition, &current_query) {
                    continue;
                }
            }

            if let Ok(rewritten) = self.transformer.apply(&current_query, &rule.transformation) {
                current_query = rewritten;
                applied_rules.push(rule.id.clone());
            }
        }

        Ok(RewriteResult {
            original: query.to_string(),
            rewritten: current_query,
            rules_applied: applied_rules,
            fingerprint,
            duration: std::time::Duration::ZERO,
        })
    }

    /// Add a new rule
    pub fn add_rule(&self, rule: impl Into<RewriteRule>) {
        let mut rules = self.rules.write();
        rules.push(rule.into());

        // Rebuild matcher
        let mut matcher = self.matcher.write();
        *matcher = RuleMatcher::new(&rules);
    }

    /// Remove a rule by ID
    pub fn remove_rule(&self, rule_id: &str) -> bool {
        let mut rules = self.rules.write();
        let initial_len = rules.len();
        rules.retain(|r| r.id != rule_id);

        if rules.len() != initial_len {
            let mut matcher = self.matcher.write();
            *matcher = RuleMatcher::new(&rules);
            true
        } else {
            false
        }
    }

    /// Update a rule
    pub fn update_rule(&self, rule_id: &str, update: impl FnOnce(&mut RewriteRule)) -> bool {
        let mut rules = self.rules.write();
        if let Some(rule) = rules.iter_mut().find(|r| r.id == rule_id) {
            update(rule);

            let mut matcher = self.matcher.write();
            *matcher = RuleMatcher::new(&rules);
            true
        } else {
            false
        }
    }

    /// Enable/disable a rule
    pub fn set_rule_enabled(&self, rule_id: &str, enabled: bool) -> bool {
        self.update_rule(rule_id, |r| r.enabled = enabled)
    }

    /// Get all rules
    pub fn get_rules(&self) -> Vec<RewriteRule> {
        self.rules.read().clone()
    }

    /// Get rule by ID
    pub fn get_rule(&self, rule_id: &str) -> Option<RewriteRule> {
        self.rules.read().iter().find(|r| r.id == rule_id).cloned()
    }

    /// Get statistics
    pub fn stats(&self) -> RewriteStats {
        self.metrics.stats()
    }

    /// Evaluate a condition
    fn evaluate_condition(&self, condition: &Condition, query: &str) -> bool {
        match condition {
            Condition::NoExistingLimit => {
                !query.to_uppercase().contains("LIMIT")
            }
            Condition::NoExistingOrderBy => {
                !query.to_uppercase().contains("ORDER BY")
            }
            Condition::HasSelectStar => {
                let upper = query.to_uppercase();
                upper.contains("SELECT *") || upper.contains("SELECT  *")
            }
            Condition::SessionVar { name: _, exists } => {
                // In production, check session variables
                // For now, always return true if exists is expected
                *exists
            }
            Condition::ClientType { client_type: _ } => {
                // In production, check client metadata
                true
            }
            Condition::TableExists { table: _ } => {
                // In production, check schema cache
                true
            }
            Condition::And(conditions) => {
                conditions.iter().all(|c| self.evaluate_condition(c, query))
            }
            Condition::Or(conditions) => {
                conditions.iter().any(|c| self.evaluate_condition(c, query))
            }
            Condition::Not(condition) => {
                !self.evaluate_condition(condition, query)
            }
        }
    }
}

/// Query rewriter builder
pub struct QueryRewriterBuilder {
    config: RewriterConfig,
}

impl QueryRewriterBuilder {
    /// Create a new builder
    pub fn new() -> Self {
        Self {
            config: RewriterConfig::default(),
        }
    }

    /// Enable the rewriter
    pub fn enabled(mut self, enabled: bool) -> Self {
        self.config.enabled = enabled;
        self
    }

    /// Log rewrites
    pub fn log_rewrites(mut self, log: bool) -> Self {
        self.config.log_rewrites = log;
        self
    }

    /// Log errors
    pub fn log_errors(mut self, log: bool) -> Self {
        self.config.log_errors = log;
        self
    }

    /// Add a rule
    pub fn rule(mut self, rule: impl Into<RewriteRule>) -> Self {
        self.config.rules.push(rule.into());
        self
    }

    /// Add multiple rules
    pub fn rules(mut self, rules: Vec<RewriteRule>) -> Self {
        self.config.rules.extend(rules);
        self
    }

    /// Enable SELECT * expansion
    pub fn expand_select_star(mut self, enabled: bool) -> Self {
        self.config.expand_select_star = enabled;
        self
    }

    /// Add default LIMIT to queries
    pub fn add_default_limit(mut self, enabled: bool) -> Self {
        self.config.add_default_limit = enabled;
        self
    }

    /// Set default LIMIT value
    pub fn default_limit(mut self, limit: u32) -> Self {
        self.config.default_limit = limit;
        self
    }

    /// Build the rewriter
    pub fn build(self) -> QueryRewriter {
        QueryRewriter::new(self.config)
    }
}

impl Default for QueryRewriterBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of a rewrite operation
#[derive(Debug, Clone)]
pub struct RewriteResult {
    /// Original query
    pub original: String,

    /// Rewritten query (same as original if no changes)
    pub rewritten: String,

    /// IDs of rules that were applied
    pub rules_applied: Vec<String>,

    /// Query fingerprint
    pub fingerprint: u64,

    /// Time taken to rewrite
    pub duration: std::time::Duration,
}

impl RewriteResult {
    /// Create an unchanged result
    pub fn unchanged(query: &str) -> Self {
        Self {
            original: query.to_string(),
            rewritten: query.to_string(),
            rules_applied: Vec::new(),
            fingerprint: 0,
            duration: std::time::Duration::ZERO,
        }
    }

    /// Check if query was modified
    pub fn was_rewritten(&self) -> bool {
        !self.rules_applied.is_empty()
    }

    /// Get the final query (rewritten or original)
    pub fn query(&self) -> &str {
        &self.rewritten
    }
}

/// Rewrite error
#[derive(Debug, Clone)]
pub enum RewriteError {
    /// Failed to parse query
    ParseError(String),

    /// Transformation failed
    TransformError(String),

    /// Rule not found
    RuleNotFound(String),

    /// Forbidden table access
    ForbiddenTable(String),

    /// Configuration error
    ConfigError(String),
}

impl std::fmt::Display for RewriteError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ParseError(msg) => write!(f, "Parse error: {}", msg),
            Self::TransformError(msg) => write!(f, "Transform error: {}", msg),
            Self::RuleNotFound(id) => write!(f, "Rule not found: {}", id),
            Self::ForbiddenTable(table) => write!(f, "Forbidden table: {}", table),
            Self::ConfigError(msg) => write!(f, "Config error: {}", msg),
        }
    }
}

impl std::error::Error for RewriteError {}

impl From<TransformError> for RewriteError {
    fn from(e: TransformError) -> Self {
        Self::TransformError(e.to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_rewriter_disabled() {
        let rewriter = QueryRewriter::builder()
            .enabled(false)
            .build();

        let result = rewriter.rewrite("SELECT * FROM users").unwrap();
        assert!(!result.was_rewritten());
        assert_eq!(result.query(), "SELECT * FROM users");
    }

    #[test]
    fn test_rewriter_add_limit() {
        let rewriter = QueryRewriter::builder()
            .enabled(true)
            .rule(RewriteRule::new("add_limit")
                .pattern(QueryPattern::All)
                .transform(Transformation::AddLimit(100))
                .condition(Condition::NoExistingLimit))
            .build();

        let result = rewriter.rewrite("SELECT * FROM users").unwrap();
        assert!(result.was_rewritten());
        assert!(result.rewritten.contains("LIMIT 100"));
    }

    #[test]
    fn test_rewriter_skip_existing_limit() {
        let rewriter = QueryRewriter::builder()
            .enabled(true)
            .rule(RewriteRule::new("add_limit")
                .pattern(QueryPattern::All)
                .transform(Transformation::AddLimit(100))
                .condition(Condition::NoExistingLimit))
            .build();

        let result = rewriter.rewrite("SELECT * FROM users LIMIT 50").unwrap();
        assert!(!result.was_rewritten());
    }

    #[test]
    fn test_rewriter_replace_query() {
        let rewriter = QueryRewriter::builder()
            .enabled(true)
            .rule(RewriteRule::new("replace")
                .pattern(QueryPattern::Fingerprint(12345))
                .transform(Transformation::Replace("SELECT 1".to_string())))
            .build();

        // This won't match because fingerprint doesn't match
        let result = rewriter.rewrite("SELECT * FROM users").unwrap();
        assert!(!result.was_rewritten());
    }

    #[test]
    fn test_add_remove_rule() {
        let rewriter = QueryRewriter::builder()
            .enabled(true)
            .build();

        assert!(rewriter.get_rules().is_empty());

        rewriter.add_rule(RewriteRule::new("test")
            .pattern(QueryPattern::All)
            .transform(Transformation::AddLimit(100)));

        assert_eq!(rewriter.get_rules().len(), 1);

        assert!(rewriter.remove_rule("test"));
        assert!(rewriter.get_rules().is_empty());
    }

    #[test]
    fn test_update_rule() {
        let rewriter = QueryRewriter::builder()
            .enabled(true)
            .rule(RewriteRule::new("test")
                .pattern(QueryPattern::All)
                .transform(Transformation::AddLimit(100)))
            .build();

        assert!(rewriter.get_rule("test").unwrap().enabled);

        rewriter.set_rule_enabled("test", false);

        assert!(!rewriter.get_rule("test").unwrap().enabled);
    }
}