fraiseql-core 2.2.0

Core execution engine for FraiseQL v2 - Compiled GraphQL over SQL
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
582
583
//! Fact table versioning for aggregation query caching.
//!
//! This module provides multiple strategies for caching aggregation queries on fact tables.
//! Users can choose the strategy that best fits their data pipeline and freshness requirements.
//!
//! # Strategies
//!
//! | Strategy | Best For | Trade-off |
//! |----------|----------|-----------|
//! | `Disabled` | Real-time accuracy | No caching benefit |
//! | `VersionTable` | ETL/batch loads | Requires version bump discipline |
//! | `TimeBased` | Dashboards with acceptable lag | May serve stale data |
//! | `SchemaVersion` | Append-only/immutable facts | Only invalidates on deploy |
//!
//! # Version Table Schema
//!
//! When using `VersionTable` strategy, create the following table:
//!
//! ```sql
//! CREATE TABLE IF NOT EXISTS tf_versions (
//!     table_name TEXT PRIMARY KEY,
//!     version BIGINT NOT NULL DEFAULT 1,
//!     updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
//! );
//!
//! -- Helper function to bump version
//! CREATE OR REPLACE FUNCTION bump_tf_version(p_table_name TEXT)
//! RETURNS BIGINT AS $$
//! DECLARE
//!     new_version BIGINT;
//! BEGIN
//!     INSERT INTO tf_versions (table_name, version, updated_at)
//!     VALUES (p_table_name, 1, NOW())
//!     ON CONFLICT (table_name) DO UPDATE
//!     SET version = tf_versions.version + 1,
//!         updated_at = NOW()
//!     RETURNING version INTO new_version;
//!     RETURN new_version;
//! END;
//! $$ LANGUAGE plpgsql;
//!
//! -- Optional: Auto-bump trigger (adds write overhead)
//! CREATE OR REPLACE FUNCTION tf_auto_version_bump()
//! RETURNS TRIGGER AS $$
//! BEGIN
//!     PERFORM bump_tf_version(TG_TABLE_NAME);
//!     RETURN NULL;
//! END;
//! $$ LANGUAGE plpgsql;
//!
//! -- Apply to specific fact tables:
//! CREATE TRIGGER tf_sales_version_bump
//! AFTER INSERT OR UPDATE OR DELETE ON tf_sales
//! FOR EACH STATEMENT EXECUTE FUNCTION tf_auto_version_bump();
//! ```
//!
//! # Example Configuration
//!
//! ```rust
//! use fraiseql_core::cache::fact_table_version::{
//!     FactTableVersionStrategy, FactTableCacheConfig,
//! };
//! use std::collections::HashMap;
//!
//! let mut config = FactTableCacheConfig::default();
//!
//! // ETL-loaded sales data - use version table
//! config.set_strategy("tf_sales", FactTableVersionStrategy::VersionTable);
//!
//! // Real-time page views - cache for 5 minutes
//! config.set_strategy("tf_page_views", FactTableVersionStrategy::TimeBased {
//!     ttl_seconds: 300,
//! });
//!
//! // Historical exchange rates - never changes
//! config.set_strategy("tf_historical_rates", FactTableVersionStrategy::SchemaVersion);
//! ```

use std::{
    collections::HashMap,
    time::{Duration, Instant},
};

use serde::{Deserialize, Serialize};

/// Versioning strategy for fact table aggregation caching.
///
/// Different strategies offer different trade-offs between cache hit rate,
/// data freshness, and operational complexity.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
#[non_exhaustive]
pub enum FactTableVersionStrategy {
    /// No caching for aggregations (always query database).
    ///
    /// Use when: Real-time accuracy is required.
    /// Trade-off: No caching benefit, every query hits database.
    Disabled,

    /// Read version from `tf_versions` table.
    ///
    /// Cache key includes the version number, so when version bumps,
    /// old cache entries are automatically ignored.
    ///
    /// Use when: Data is loaded via ETL/batch processes that can bump versions.
    /// Trade-off: Requires discipline to bump version after data changes.
    ///
    /// # Version Bumping
    ///
    /// After loading data, call:
    /// ```sql
    /// SELECT bump_tf_version('tf_sales');
    /// ```
    ///
    /// Or use triggers for automatic bumping (adds write overhead).
    VersionTable,

    /// Time-based TTL caching.
    ///
    /// Cache entries expire after the specified duration regardless of
    /// whether the underlying data has changed.
    ///
    /// Use when: Some staleness is acceptable (e.g., dashboards).
    /// Trade-off: May serve stale data within TTL window.
    TimeBased {
        /// Cache TTL in seconds.
        ttl_seconds: u64,
    },

    /// Use schema version only (invalidate on deployment).
    ///
    /// Cache is only invalidated when the schema version changes,
    /// which typically happens during deployments.
    ///
    /// Use when: Fact table data is immutable or append-only and
    /// queries always filter to recent data.
    /// Trade-off: Stale data until next deployment.
    SchemaVersion,
}

impl Default for FactTableVersionStrategy {
    /// Default strategy is `Disabled` for safety (no stale data).
    fn default() -> Self {
        Self::Disabled
    }
}

impl FactTableVersionStrategy {
    /// Create a time-based strategy with the given TTL.
    #[must_use]
    pub const fn time_based(ttl_seconds: u64) -> Self {
        Self::TimeBased { ttl_seconds }
    }

    /// Check if caching is enabled for this strategy.
    #[must_use]
    pub const fn is_caching_enabled(&self) -> bool {
        !matches!(self, Self::Disabled)
    }

    /// Get TTL for time-based strategy, if applicable.
    #[must_use]
    pub const fn ttl_seconds(&self) -> Option<u64> {
        match self {
            Self::TimeBased { ttl_seconds } => Some(*ttl_seconds),
            _ => None,
        }
    }
}

/// Configuration for fact table aggregation caching.
///
/// Maps fact table names to their versioning strategies.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FactTableCacheConfig {
    /// Default strategy for tables not explicitly configured.
    #[serde(default)]
    pub default_strategy: FactTableVersionStrategy,

    /// Per-table strategy overrides.
    #[serde(default)]
    pub table_strategies: HashMap<String, FactTableVersionStrategy>,
}

impl FactTableCacheConfig {
    /// Create a new config with the given default strategy.
    #[must_use]
    pub fn with_default(strategy: FactTableVersionStrategy) -> Self {
        Self {
            default_strategy: strategy,
            table_strategies: HashMap::new(),
        }
    }

    /// Set strategy for a specific table.
    pub fn set_strategy(&mut self, table_name: &str, strategy: FactTableVersionStrategy) {
        self.table_strategies.insert(table_name.to_string(), strategy);
    }

    /// Get strategy for a table (falls back to default).
    #[must_use]
    pub fn get_strategy(&self, table_name: &str) -> &FactTableVersionStrategy {
        self.table_strategies.get(table_name).unwrap_or(&self.default_strategy)
    }

    /// Check if caching is enabled for a table.
    #[must_use]
    pub fn is_caching_enabled(&self, table_name: &str) -> bool {
        self.get_strategy(table_name).is_caching_enabled()
    }
}

/// Cached version information for a fact table.
#[derive(Debug, Clone)]
pub struct CachedVersion {
    /// The version number.
    pub version:    i64,
    /// When the version was fetched.
    pub fetched_at: Instant,
}

impl CachedVersion {
    /// Create a new cached version.
    #[must_use]
    pub fn new(version: i64) -> Self {
        Self {
            version,
            fetched_at: Instant::now(),
        }
    }

    /// Check if the cached version is still fresh.
    ///
    /// Versions are cached for a short time (default 1 second) to avoid
    /// hammering the `tf_versions` table on every query.
    #[must_use]
    pub fn is_fresh(&self, max_age: Duration) -> bool {
        self.fetched_at.elapsed() < max_age
    }
}

/// Version provider for fact tables.
///
/// Fetches and caches version numbers from the `tf_versions` table.
#[derive(Debug)]
pub struct FactTableVersionProvider {
    /// Cached versions (`table_name` -> version).
    versions:          std::sync::RwLock<HashMap<String, CachedVersion>>,
    /// How long to cache version lookups.
    version_cache_ttl: Duration,
}

impl Default for FactTableVersionProvider {
    fn default() -> Self {
        Self::new(Duration::from_secs(1))
    }
}

impl FactTableVersionProvider {
    /// Create a new version provider.
    ///
    /// # Arguments
    ///
    /// * `version_cache_ttl` - How long to cache version lookups (default 1 second)
    #[must_use]
    pub fn new(version_cache_ttl: Duration) -> Self {
        Self {
            versions: std::sync::RwLock::new(HashMap::new()),
            version_cache_ttl,
        }
    }

    /// Get cached version if still fresh, otherwise return None.
    #[must_use]
    pub fn get_cached_version(&self, table_name: &str) -> Option<i64> {
        let versions = self.versions.read().ok()?;
        let cached = versions.get(table_name)?;
        if cached.is_fresh(self.version_cache_ttl) {
            Some(cached.version)
        } else {
            None
        }
    }

    /// Update cached version for a table.
    pub fn set_cached_version(&self, table_name: &str, version: i64) {
        if let Ok(mut versions) = self.versions.write() {
            versions.insert(table_name.to_string(), CachedVersion::new(version));
        }
    }

    /// Clear cached version for a table.
    pub fn clear_cached_version(&self, table_name: &str) {
        if let Ok(mut versions) = self.versions.write() {
            versions.remove(table_name);
        }
    }

    /// Clear all cached versions.
    pub fn clear_all(&self) {
        if let Ok(mut versions) = self.versions.write() {
            versions.clear();
        }
    }
}

/// Generate cache key component for a fact table based on its versioning strategy.
///
/// This function returns the version component to include in cache keys.
///
/// # Returns
///
/// - `Some(version_string)` - Version to include in cache key
/// - `None` - Caching disabled, should not cache
#[must_use]
pub fn generate_version_key_component(
    _table_name: &str,
    strategy: &FactTableVersionStrategy,
    table_version: Option<i64>,
    schema_version: &str,
) -> Option<String> {
    match strategy {
        FactTableVersionStrategy::Disabled => None,

        FactTableVersionStrategy::VersionTable => {
            // Require a version from tf_versions table
            table_version.map(|v| format!("tv:{v}"))
        },

        FactTableVersionStrategy::TimeBased { ttl_seconds } => {
            // Use time bucket as version (floor to TTL boundary)
            let now = std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap_or_default()
                .as_secs();
            let bucket = now / ttl_seconds;
            Some(format!("tb:{bucket}"))
        },

        FactTableVersionStrategy::SchemaVersion => {
            // Use schema version only
            Some(format!("sv:{schema_version}"))
        },
    }
}

/// SQL to query version from `tf_versions` table.
pub const VERSION_TABLE_QUERY: &str = r"
    SELECT version FROM tf_versions WHERE table_name = $1
";

/// SQL to create the `tf_versions` table and helper functions.
pub const VERSION_TABLE_SCHEMA: &str = r"
-- Fact table version tracking for aggregation cache
CREATE TABLE IF NOT EXISTS tf_versions (
    table_name TEXT PRIMARY KEY,
    version BIGINT NOT NULL DEFAULT 1,
    updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- Index for fast lookups
CREATE INDEX IF NOT EXISTS idx_tf_versions_updated_at ON tf_versions (updated_at);

-- Helper function to bump version (call after data loads)
CREATE OR REPLACE FUNCTION bump_tf_version(p_table_name TEXT)
RETURNS BIGINT AS $$
DECLARE
    new_version BIGINT;
BEGIN
    INSERT INTO tf_versions (table_name, version, updated_at)
    VALUES (p_table_name, 1, NOW())
    ON CONFLICT (table_name) DO UPDATE
    SET version = tf_versions.version + 1,
        updated_at = NOW()
    RETURNING version INTO new_version;
    RETURN new_version;
END;
$$ LANGUAGE plpgsql;

-- Optional: Trigger function for automatic version bumping
-- Note: This adds overhead to every INSERT/UPDATE/DELETE
CREATE OR REPLACE FUNCTION tf_auto_version_bump()
RETURNS TRIGGER AS $$
BEGIN
    PERFORM bump_tf_version(TG_TABLE_NAME);
    RETURN NULL;
END;
$$ LANGUAGE plpgsql;

-- Example: Apply auto-bump trigger to a fact table
-- CREATE TRIGGER tf_sales_version_bump
-- AFTER INSERT OR UPDATE OR DELETE ON tf_sales
-- FOR EACH STATEMENT EXECUTE FUNCTION tf_auto_version_bump();
";

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)] // Reason: test code, panics are acceptable

    use super::*;

    #[test]
    fn test_strategy_default_is_disabled() {
        let strategy = FactTableVersionStrategy::default();
        assert_eq!(strategy, FactTableVersionStrategy::Disabled);
        assert!(!strategy.is_caching_enabled());
    }

    #[test]
    fn test_strategy_time_based() {
        let strategy = FactTableVersionStrategy::time_based(300);
        assert!(strategy.is_caching_enabled());
        assert_eq!(strategy.ttl_seconds(), Some(300));
    }

    #[test]
    fn test_strategy_version_table() {
        let strategy = FactTableVersionStrategy::VersionTable;
        assert!(strategy.is_caching_enabled());
        assert_eq!(strategy.ttl_seconds(), None);
    }

    #[test]
    fn test_strategy_schema_version() {
        let strategy = FactTableVersionStrategy::SchemaVersion;
        assert!(strategy.is_caching_enabled());
        assert_eq!(strategy.ttl_seconds(), None);
    }

    #[test]
    fn test_config_default_strategy() {
        let config = FactTableCacheConfig::default();
        assert_eq!(config.get_strategy("tf_sales"), &FactTableVersionStrategy::Disabled);
    }

    #[test]
    fn test_config_per_table_strategy() {
        let mut config = FactTableCacheConfig::default();
        config.set_strategy("tf_sales", FactTableVersionStrategy::VersionTable);
        config.set_strategy(
            "tf_page_views",
            FactTableVersionStrategy::TimeBased { ttl_seconds: 300 },
        );

        assert_eq!(config.get_strategy("tf_sales"), &FactTableVersionStrategy::VersionTable);
        assert_eq!(
            config.get_strategy("tf_page_views"),
            &FactTableVersionStrategy::TimeBased { ttl_seconds: 300 }
        );
        // Unconfigured table uses default
        assert_eq!(config.get_strategy("tf_other"), &FactTableVersionStrategy::Disabled);
    }

    #[test]
    fn test_config_with_default() {
        let config = FactTableCacheConfig::with_default(FactTableVersionStrategy::SchemaVersion);
        assert_eq!(config.get_strategy("tf_any"), &FactTableVersionStrategy::SchemaVersion);
    }

    #[test]
    fn test_generate_version_key_disabled() {
        let key = generate_version_key_component(
            "tf_sales",
            &FactTableVersionStrategy::Disabled,
            Some(42),
            "1.0.0",
        );
        assert!(key.is_none());
    }

    #[test]
    fn test_generate_version_key_version_table() {
        let key = generate_version_key_component(
            "tf_sales",
            &FactTableVersionStrategy::VersionTable,
            Some(42),
            "1.0.0",
        );
        assert_eq!(key, Some("tv:42".to_string()));

        // No version available - should return None
        let key = generate_version_key_component(
            "tf_sales",
            &FactTableVersionStrategy::VersionTable,
            None,
            "1.0.0",
        );
        assert!(key.is_none());
    }

    #[test]
    fn test_generate_version_key_time_based() {
        let key = generate_version_key_component(
            "tf_sales",
            &FactTableVersionStrategy::TimeBased { ttl_seconds: 300 },
            None,
            "1.0.0",
        );
        assert!(key.is_some());
        assert!(key.unwrap().starts_with("tb:"));
    }

    #[test]
    fn test_generate_version_key_schema_version() {
        let key = generate_version_key_component(
            "tf_sales",
            &FactTableVersionStrategy::SchemaVersion,
            None,
            "1.0.0",
        );
        assert_eq!(key, Some("sv:1.0.0".to_string()));
    }

    #[test]
    fn test_version_provider_caching() {
        let provider = FactTableVersionProvider::new(Duration::from_secs(10));

        // Initially no cached version
        assert!(provider.get_cached_version("tf_sales").is_none());

        // Set version
        provider.set_cached_version("tf_sales", 42);
        assert_eq!(provider.get_cached_version("tf_sales"), Some(42));

        // Clear version
        provider.clear_cached_version("tf_sales");
        assert!(provider.get_cached_version("tf_sales").is_none());
    }

    #[test]
    fn test_version_provider_clear_all() {
        let provider = FactTableVersionProvider::new(Duration::from_secs(10));

        provider.set_cached_version("tf_sales", 1);
        provider.set_cached_version("tf_orders", 2);

        provider.clear_all();

        assert!(provider.get_cached_version("tf_sales").is_none());
        assert!(provider.get_cached_version("tf_orders").is_none());
    }

    #[test]
    fn test_cached_version_freshness() {
        let cached = CachedVersion::new(42);

        // Should be fresh immediately
        assert!(cached.is_fresh(Duration::from_secs(1)));

        // Should be fresh for a longer duration
        assert!(cached.is_fresh(Duration::from_secs(60)));
    }

    #[test]
    fn test_strategy_serialization() {
        let strategies = vec![
            FactTableVersionStrategy::Disabled,
            FactTableVersionStrategy::VersionTable,
            FactTableVersionStrategy::TimeBased { ttl_seconds: 300 },
            FactTableVersionStrategy::SchemaVersion,
        ];

        for strategy in strategies {
            let json = serde_json::to_string(&strategy).unwrap();
            let deserialized: FactTableVersionStrategy = serde_json::from_str(&json).unwrap();
            assert_eq!(strategy, deserialized);
        }
    }

    #[test]
    fn test_config_serialization() {
        let mut config =
            FactTableCacheConfig::with_default(FactTableVersionStrategy::SchemaVersion);
        config.set_strategy("tf_sales", FactTableVersionStrategy::VersionTable);
        config.set_strategy("tf_events", FactTableVersionStrategy::TimeBased { ttl_seconds: 60 });

        let json = serde_json::to_string_pretty(&config).unwrap();
        let deserialized: FactTableCacheConfig = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.default_strategy, FactTableVersionStrategy::SchemaVersion);
        assert_eq!(deserialized.get_strategy("tf_sales"), &FactTableVersionStrategy::VersionTable);
    }
}