rs3gw 0.2.1

High-Performance AI/HPC Object Storage Gateway powered by scirs2-io
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
//! Auto-generated module
//!
//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)

#[cfg(test)]
mod tests {
    use super::super::types::*;
    use crate::api::select::types::{AggregateFunction, ColumnRef, OrderByClause, SelectColumn};
    use crate::api::select::ParsedQuery;
    use std::collections::HashMap;
    use std::time::SystemTime;

    fn create_test_query() -> ParsedQuery {
        ParsedQuery {
            columns: vec![
                SelectColumn::Column(ColumnRef::Named("name".to_string())),
                SelectColumn::Column(ColumnRef::Named("age".to_string())),
            ],
            from_alias: Some("s3object".to_string()),
            where_clause: None,
            group_by: None,
            order_by: None,
            limit: None,
        }
    }
    fn create_test_data_stats() -> DataStatistics {
        DataStatistics {
            total_rows: 10000,
            total_bytes: 1024 * 1024,
            avg_row_size: 100.0,
            format: "CSV".to_string(),
            compression_ratio: None,
            column_cardinality: HashMap::new(),
            null_percentages: HashMap::new(),
            is_sorted: false,
            skew_factor: 0.1,
        }
    }
    #[tokio::test]
    async fn test_query_intelligence_creation() {
        let intelligence = QueryIntelligence::new();
        let summary = intelligence.get_summary().await;
        assert_eq!(summary.total_queries, 0);
    }
    #[tokio::test]
    async fn test_cost_prediction() {
        let intelligence = QueryIntelligence::new();
        let query = create_test_query();
        let stats = create_test_data_stats();
        let cost = intelligence.predict_cost(&query, &stats).await;
        assert!(cost.execution_time_ms > 0.0);
        assert!(cost.memory_bytes > 0);
        assert!(cost.confidence >= 0.0 && cost.confidence <= 1.0);
    }
    #[tokio::test]
    async fn test_execution_strategy() {
        let intelligence = QueryIntelligence::new();
        let query = create_test_query();
        let stats = create_test_data_stats();
        let strategy = intelligence.get_execution_strategy(&query, &stats).await;
        match strategy {
            ExecutionStrategy::Sequential | ExecutionStrategy::FullScan => {}
            _ => panic!("Expected Sequential or FullScan strategy for small dataset"),
        }
    }
    #[tokio::test]
    async fn test_execution_strategy_large_dataset() {
        let intelligence = QueryIntelligence::new();
        let query = create_test_query();
        let mut stats = create_test_data_stats();
        stats.total_bytes = 20 * 1024 * 1024;
        stats.total_rows = 2_000_000;
        stats.avg_row_size = 10.0;
        let strategy = intelligence.get_execution_strategy(&query, &stats).await;
        match strategy {
            ExecutionStrategy::Parallel { num_threads } => {
                assert!(num_threads > 1);
            }
            ExecutionStrategy::Streaming { chunk_size } => {
                assert!(chunk_size > 0);
            }
            ExecutionStrategy::FullScan => {}
            _ => {
                panic!("Expected parallel, streaming, or full scan strategy for large dataset")
            }
        }
    }
    #[tokio::test]
    async fn test_record_execution() {
        let intelligence = QueryIntelligence::new();
        let stats = QueryStats {
            sql: "SELECT * FROM s3object".to_string(),
            fingerprint: "test123".to_string(),
            execution_time_ms: 10.5,
            memory_bytes: 1024,
            rows_scanned: 100,
            rows_returned: 50,
            object_size_bytes: 10000,
            timestamp: SystemTime::now(),
            parallel_execution: false,
            cache_hit: false,
        };
        intelligence.record_execution(stats).await;
        let summary = intelligence.get_summary().await;
        assert_eq!(summary.total_queries, 1);
        assert_eq!(summary.avg_execution_time_ms, 10.5);
    }
    #[tokio::test]
    async fn test_find_similar_queries() {
        let intelligence = QueryIntelligence::new();
        let stats = QueryStats {
            sql: "SELECT name, age FROM s3object".to_string(),
            fingerprint: QueryIntelligence::compute_fingerprint(&create_test_query()),
            execution_time_ms: 10.0,
            memory_bytes: 1024,
            rows_scanned: 100,
            rows_returned: 50,
            object_size_bytes: 10000,
            timestamp: SystemTime::now(),
            parallel_execution: false,
            cache_hit: false,
        };
        intelligence.record_execution(stats).await;
        let query = create_test_query();
        let similar = intelligence.find_similar_queries(&query, 0.8).await;
        assert!(!similar.is_empty());
        assert_eq!(similar[0].similarity, 1.0);
    }
    #[tokio::test]
    async fn test_fingerprint_computation() {
        let query1 = create_test_query();
        let query2 = create_test_query();
        let fp1 = QueryIntelligence::compute_fingerprint(&query1);
        let fp2 = QueryIntelligence::compute_fingerprint(&query2);
        assert_eq!(fp1, fp2);
    }
    #[tokio::test]
    async fn test_query_normalization() {
        let query = create_test_query();
        let normalized = QueryIntelligence::normalize_query(&query);
        assert!(normalized.contains("s3object"));
    }
    #[tokio::test]
    async fn test_levenshtein_distance() {
        let dist1 = QueryIntelligence::levenshtein_distance("hello", "hello");
        assert_eq!(dist1, 0);
        let dist2 = QueryIntelligence::levenshtein_distance("hello", "hallo");
        assert_eq!(dist2, 1);
        let dist3 = QueryIntelligence::levenshtein_distance("", "hello");
        assert_eq!(dist3, 5);
    }
    #[tokio::test]
    async fn test_similarity_computation() {
        let sim1 = QueryIntelligence::compute_similarity("hello", "hello");
        assert_eq!(sim1, 1.0);
        let sim2 = QueryIntelligence::compute_similarity("hello", "hallo");
        assert!((0.8..1.0).contains(&sim2) || sim2 == 0.8);
        let sim3 = QueryIntelligence::compute_similarity("abc", "xyz");
        assert!(sim3 < 0.5);
    }
    #[tokio::test]
    async fn test_cost_breakdown() {
        let intelligence = QueryIntelligence::new();
        let mut query = create_test_query();
        query.columns.push(SelectColumn::Aggregate {
            function: crate::api::select::AggregateFunction::Count,
            column: None,
            alias: Some("count".to_string()),
        });
        query.group_by = Some(vec!["name".to_string()]);
        query.order_by = Some(vec![OrderByClause {
            column: "age".to_string(),
            ascending: true,
        }]);
        let stats = create_test_data_stats();
        let cost = intelligence.predict_cost(&query, &stats).await;
        assert!(cost.breakdown.scan_cost > 0.0);
        assert!(cost.breakdown.projection_cost > 0.0);
        assert!(cost.breakdown.aggregation_cost > 0.0);
        assert!(cost.breakdown.sort_cost > 0.0);
    }
    #[tokio::test]
    async fn test_cache_hit_tracking() {
        let intelligence = QueryIntelligence::new();
        let stats1 = QueryStats {
            sql: "SELECT * FROM s3object".to_string(),
            fingerprint: "test1".to_string(),
            execution_time_ms: 10.0,
            memory_bytes: 1024,
            rows_scanned: 100,
            rows_returned: 50,
            object_size_bytes: 10000,
            timestamp: SystemTime::now(),
            parallel_execution: false,
            cache_hit: true,
        };
        let stats2 = QueryStats {
            sql: "SELECT age FROM s3object".to_string(),
            fingerprint: "test2".to_string(),
            execution_time_ms: 20.0,
            memory_bytes: 2048,
            rows_scanned: 200,
            rows_returned: 100,
            object_size_bytes: 20000,
            timestamp: SystemTime::now(),
            parallel_execution: false,
            cache_hit: false,
        };
        intelligence.record_execution(stats1).await;
        intelligence.record_execution(stats2).await;
        let summary = intelligence.get_summary().await;
        assert_eq!(summary.total_queries, 2);
        assert_eq!(summary.cache_hit_rate, 0.5);
    }
    #[tokio::test]
    async fn test_index_recommendations_empty() {
        let intelligence = QueryIntelligence::new();
        let recommendations = intelligence.get_index_recommendations().await;
        assert!(recommendations.is_empty());
    }
    #[tokio::test]
    async fn test_index_recommendations_with_data() {
        let intelligence = QueryIntelligence::new();
        for i in 0..10 {
            let stats = QueryStats {
                sql: format!("SELECT * FROM s3object WHERE id = {}", i),
                fingerprint: format!("test{}", i),
                execution_time_ms: 10.0 + i as f64,
                memory_bytes: 1024,
                rows_scanned: 1000,
                rows_returned: 10,
                object_size_bytes: 10000,
                timestamp: SystemTime::now(),
                parallel_execution: false,
                cache_hit: false,
            };
            intelligence.record_execution(stats).await;
        }
        let recommendations = intelligence.get_index_recommendations().await;
        assert!(recommendations.is_empty() || recommendations.len() <= 10);
    }
    #[test]
    fn test_index_type_serialization() {
        let types = vec![
            IndexType::BTree,
            IndexType::Hash,
            IndexType::FullText,
            IndexType::Bitmap,
        ];
        for index_type in types {
            let json = serde_json::to_string(&index_type).expect("Failed to serialize");
            let deserialized: IndexType =
                serde_json::from_str(&json).expect("Failed to deserialize");
            assert_eq!(index_type, deserialized);
        }
    }
    #[test]
    fn test_index_reason_serialization() {
        let reasons = vec![
            IndexReason::FilterColumn,
            IndexReason::SortColumn,
            IndexReason::JoinColumn,
            IndexReason::GroupByColumn,
            IndexReason::HighScanCost,
        ];
        for reason in reasons {
            let json = serde_json::to_string(&reason).expect("Failed to serialize");
            let deserialized: IndexReason =
                serde_json::from_str(&json).expect("Failed to deserialize");
            assert_eq!(reason, deserialized);
        }
    }
    #[tokio::test]
    async fn test_complexity_trivial_query() {
        let intelligence = QueryIntelligence::new();
        let query = ParsedQuery {
            columns: vec![
                SelectColumn::Column(ColumnRef::Named("id".to_string())),
                SelectColumn::Column(ColumnRef::Named("name".to_string())),
            ],
            where_clause: None,
            from_alias: None,
            group_by: None,
            order_by: None,
            limit: Some(10),
        };
        let complexity = intelligence.calculate_complexity(&query, 500_000).await;
        assert_eq!(complexity.classification, ComplexityClass::Trivial);
        assert!(complexity.score < 10.0);
        assert!(complexity.resource_estimate.cacheable);
        assert_eq!(complexity.resource_estimate.cpu_intensity, 1);
    }
    #[tokio::test]
    async fn test_complexity_simple_query() {
        let intelligence = QueryIntelligence::new();
        let query = ParsedQuery {
            columns: vec![
                SelectColumn::Column(ColumnRef::Named("col1".to_string())),
                SelectColumn::Column(ColumnRef::Named("col2".to_string())),
                SelectColumn::Column(ColumnRef::Named("col3".to_string())),
                SelectColumn::Column(ColumnRef::Named("col4".to_string())),
            ],
            where_clause: None,
            from_alias: None,
            group_by: None,
            order_by: None,
            limit: None,
        };
        let complexity = intelligence.calculate_complexity(&query, 2_000_000).await;
        assert_eq!(complexity.classification, ComplexityClass::Trivial);
        assert!(complexity.score < 10.0);
    }
    #[tokio::test]
    async fn test_complexity_moderate_query() {
        let intelligence = QueryIntelligence::new();
        let query = ParsedQuery {
            columns: vec![
                SelectColumn::Column(ColumnRef::Named("category".to_string())),
                SelectColumn::Aggregate {
                    function: AggregateFunction::Count,
                    column: Some("*".to_string()),
                    alias: Some("count".to_string()),
                },
                SelectColumn::Aggregate {
                    function: AggregateFunction::Avg,
                    column: Some("price".to_string()),
                    alias: Some("avg_price".to_string()),
                },
            ],
            where_clause: None,
            from_alias: None,
            group_by: Some(vec!["category".to_string()]),
            order_by: None,
            limit: None,
        };
        let complexity = intelligence.calculate_complexity(&query, 50_000_000).await;
        assert_eq!(complexity.classification, ComplexityClass::Simple);
        assert!(complexity.score >= 10.0 && complexity.score < 25.0);
        assert!(complexity.components.aggregation_score > 0.0);
        assert_eq!(complexity.resource_estimate.memory_intensity, 3);
    }
    #[tokio::test]
    async fn test_complexity_with_sorting() {
        let intelligence = QueryIntelligence::new();
        let query = ParsedQuery {
            columns: vec![
                SelectColumn::Column(ColumnRef::Named("name".to_string())),
                SelectColumn::Column(ColumnRef::Named("age".to_string())),
            ],
            where_clause: None,
            from_alias: None,
            group_by: None,
            order_by: Some(vec![
                OrderByClause {
                    column: "age".to_string(),
                    ascending: false,
                },
                OrderByClause {
                    column: "name".to_string(),
                    ascending: true,
                },
            ]),
            limit: None,
        };
        let complexity = intelligence.calculate_complexity(&query, 10_000_000).await;
        assert!(complexity.components.sort_score > 0.0);
        assert_eq!(complexity.resource_estimate.memory_intensity, 4);
        assert_eq!(complexity.components.sort_score, 7.0);
    }
    #[tokio::test]
    async fn test_complexity_complex_query() {
        let intelligence = QueryIntelligence::new();
        let query = ParsedQuery {
            columns: vec![
                SelectColumn::Column(ColumnRef::Named("category".to_string())),
                SelectColumn::Aggregate {
                    function: AggregateFunction::Count,
                    column: Some("*".to_string()),
                    alias: Some("count".to_string()),
                },
                SelectColumn::Aggregate {
                    function: AggregateFunction::Sum,
                    column: Some("amount".to_string()),
                    alias: Some("total".to_string()),
                },
            ],
            where_clause: None,
            from_alias: None,
            group_by: Some(vec!["category".to_string()]),
            order_by: Some(vec![OrderByClause {
                column: "count".to_string(),
                ascending: false,
            }]),
            limit: None,
        };
        let complexity = intelligence.calculate_complexity(&query, 100_000_000).await;
        assert!(complexity.score >= 10.0);
        assert!(complexity.components.aggregation_score > 0.0);
        assert!(complexity.components.sort_score > 0.0);
        assert_eq!(complexity.resource_estimate.memory_intensity, 6);
    }
    #[tokio::test]
    async fn test_complexity_resource_estimation_small_object() {
        let intelligence = QueryIntelligence::new();
        let query = create_test_query();
        let complexity = intelligence.calculate_complexity(&query, 500_000).await;
        assert_eq!(complexity.resource_estimate.io_intensity, 1);
        assert!(complexity.resource_estimate.cacheable);
        assert_eq!(complexity.resource_estimate.recommended_parallelism, 1);
    }
    #[tokio::test]
    async fn test_complexity_resource_estimation_large_object() {
        let intelligence = QueryIntelligence::new();
        let query = create_test_query();
        let complexity = intelligence.calculate_complexity(&query, 500_000_000).await;
        assert!(complexity.resource_estimate.io_intensity >= 5);
        assert!(complexity.resource_estimate.recommended_parallelism > 1);
    }
    #[tokio::test]
    async fn test_complexity_with_limit() {
        let intelligence = QueryIntelligence::new();
        let mut query = create_test_query();
        query.limit = Some(100);
        let complexity = intelligence.calculate_complexity(&query, 10_000_000).await;
        assert!(complexity.components.feature_score < 0.0);
    }
    #[tokio::test]
    async fn test_complexity_execution_tiers() {
        let intelligence = QueryIntelligence::new();
        let trivial_query = ParsedQuery {
            columns: vec![SelectColumn::Column(ColumnRef::Named("id".to_string()))],
            where_clause: None,
            from_alias: None,
            group_by: None,
            order_by: None,
            limit: Some(1),
        };
        let complexity = intelligence
            .calculate_complexity(&trivial_query, 100_000)
            .await;
        assert_eq!(
            complexity.resource_estimate.execution_tier,
            ExecutionTier::VeryFast
        );
        let mut complex_query = create_test_query();
        complex_query.order_by = Some(vec![OrderByClause {
            column: "name".to_string(),
            ascending: true,
        }]);
        let complexity = intelligence
            .calculate_complexity(&complex_query, 2_000_000_000)
            .await;
        assert!(matches!(
            complexity.resource_estimate.execution_tier,
            ExecutionTier::Slow | ExecutionTier::VerySlow
        ));
    }
    #[tokio::test]
    async fn test_complexity_distribution() {
        let intelligence = QueryIntelligence::new();
        let stats = vec![
            QueryStats {
                sql: "SELECT * FROM t1".to_string(),
                fingerprint: "q1".to_string(),
                execution_time_ms: 5.0,
                memory_bytes: 1024,
                rows_scanned: 100,
                rows_returned: 100,
                object_size_bytes: 1000,
                timestamp: SystemTime::now(),
                parallel_execution: false,
                cache_hit: false,
            },
            QueryStats {
                sql: "SELECT * FROM t1".to_string(),
                fingerprint: "q2".to_string(),
                execution_time_ms: 50.0,
                memory_bytes: 2048,
                rows_scanned: 1000,
                rows_returned: 500,
                object_size_bytes: 10000,
                timestamp: SystemTime::now(),
                parallel_execution: false,
                cache_hit: false,
            },
            QueryStats {
                sql: "SELECT * FROM t1".to_string(),
                fingerprint: "q3".to_string(),
                execution_time_ms: 300.0,
                memory_bytes: 4096,
                rows_scanned: 10000,
                rows_returned: 5000,
                object_size_bytes: 100000,
                timestamp: SystemTime::now(),
                parallel_execution: false,
                cache_hit: false,
            },
        ];
        for stat in stats {
            intelligence.record_execution(stat).await;
        }
        let distribution = intelligence.get_complexity_distribution().await;
        assert_eq!(distribution["trivial"], 1);
        assert_eq!(distribution["simple"], 1);
        assert_eq!(distribution["moderate"], 1);
    }
    #[test]
    fn test_complexity_class_string_conversion() {
        assert_eq!(ComplexityClass::Trivial.as_str(), "trivial");
        assert_eq!(ComplexityClass::Simple.as_str(), "simple");
        assert_eq!(ComplexityClass::Moderate.as_str(), "moderate");
        assert_eq!(ComplexityClass::Complex.as_str(), "complex");
        assert_eq!(ComplexityClass::VeryComplex.as_str(), "very_complex");
    }
    #[test]
    fn test_complexity_class_color_codes() {
        assert_eq!(ComplexityClass::Trivial.color_code(), "#00ff00");
        assert_eq!(ComplexityClass::Simple.color_code(), "#90ee90");
        assert_eq!(ComplexityClass::Moderate.color_code(), "#ffff00");
        assert_eq!(ComplexityClass::Complex.color_code(), "#ffa500");
        assert_eq!(ComplexityClass::VeryComplex.color_code(), "#ff0000");
    }
    #[test]
    fn test_complexity_class_serialization() {
        let classes = vec![
            ComplexityClass::Trivial,
            ComplexityClass::Simple,
            ComplexityClass::Moderate,
            ComplexityClass::Complex,
            ComplexityClass::VeryComplex,
        ];
        for class in classes {
            let json = serde_json::to_string(&class).expect("Failed to serialize");
            let deserialized: ComplexityClass =
                serde_json::from_str(&json).expect("Failed to deserialize");
            assert_eq!(class, deserialized);
        }
    }
}